Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pytest
- from extra import utils, configs
- TIME_FORMAT = '%d.%m.%Y %X'
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story('Валидация XML-ответа ресурса /category')
- def test_category_validate_xml(newsapi):
- """ Валидация XML-ответа /category """
- with pytest.allure.step("Отправка запроса /category?id=vse"):
- r = newsapi.get('/category?id=vse')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка наличия кластеров по всем темам")
- @pytest.mark.parametrize(
- "theme",
- configs.THEMES
- )
- def test_category_cluster_exist_for_themes(newsapi, theme):
- """ Проверка наличия кластеров по всем темам """
- with pytest.allure.step("Отправка запроса с указанием темы: {}".format(theme)):
- r = newsapi.get('/category?id={}'.format(theme))
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка кластеров"):
- clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
- with pytest.allure.step("Проверка наличия кластеров для темы: {}".format(theme)):
- assert len(clusters) != 0
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка наличия кластеров по всем регионам")
- @pytest.mark.parametrize(
- "region",
- configs.REGIONS
- )
- def test_category_cluster_exist_for_regions(newsapi, region):
- """ Проверка наличия кластеров по всем регионам """
- with pytest.allure.step("Отправка запроса с указанием региона: {}".format(region)):
- r = newsapi.get('/category?id={}'.format(region))
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category_regions.xsd')
- with pytest.allure.step("Получение списка кластеров"):
- clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
- with pytest.allure.step("Проверка наличия кластеров для региона: {}".format(region)):
- assert len(clusters) != 0
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка наличия кластеров по всем подтемам")
- @pytest.mark.parametrize(
- "sub_theme",
- configs.SUB_THEMES
- )
- def test_category_cluster_exist_for_sub_themes(newsapi, sub_theme):
- """ Проверка наличия кластеров по всем подтемам """
- with pytest.allure.step("Отправка запроса с указанием подтемы: {}".format(sub_theme)):
- r = newsapi.get('/category?id={}'.format(sub_theme))
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка кластеров"):
- clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
- with pytest.allure.step("Проверка наличия кластеров для подтемы: {}".format(sub_theme)):
- assert len(clusters) != 0
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка параметра limit (количество кластеров на странице)")
- @pytest.mark.parametrize(
- "limit",
- (1, 3, 5, 15, 21)
- )
- def test_category_clusters_limit_for_page(newsapi, limit):
- """ Проверка параметра limit (количество кластеров на странице) """
- with pytest.allure.step("Отправка запроса /category c указанием лимита"):
- r = newsapi.get('/category?id=vse&limit={}'.format(limit))
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка кластеров"):
- clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
- with pytest.allure.step("Проверка количества кластеров на странице: limit = {}".format(limit)):
- assert len(clusters) == limit
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка смещения по страницам. Параметр offset")
- def test_category_clusters_offset(newsapi):
- """ Проверка смещения по страницам. Параметр offset """
- with pytest.allure.step("Отправка запроса /category?id=vse с лимитом limit=4"):
- r = newsapi.get('/category?id=vse&limit=4')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Отправка запроса /category?id=vse с limit=2 и offset=0. Получение первой страницы"):
- r_first_page = newsapi.get('/category?id=vse&limit=2&offset=0')
- pytest.allure.attach('first page xml', utils.xml_text_to_pratty_format(r_first_page.text))
- r_first_page.raise_for_status()
- utils.validate_xml(r_first_page.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Отправка запроса /category?id=vse с limit=2 и offset=1. Получение второй страницы"):
- r_second_page = newsapi.get('/category?id=vse&limit=2&offset=1')
- pytest.allure.attach('second page xml', utils.xml_text_to_pratty_format(r_second_page.text))
- r_second_page.raise_for_status()
- utils.validate_xml(r_second_page.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка кластеров"):
- clusters = set([x.attrib['id'] for x in (utils.get_all_tags_attrib_data(r.content, './cluster'))])
- if len(clusters) < 4:
- pytest.skip("Количество кластеров в выдаче < 4")
- clusters_first_page = set([x.attrib['id'] for x in
- (utils.get_all_tags_attrib_data(r_first_page.content, './cluster'))])
- clusters_second_page = set([x.attrib['id'] for x in
- (utils.get_all_tags_attrib_data(r_second_page.content, './cluster'))])
- with pytest.allure.step("Проверка вхождения кластеров с первой страницы в первичную выборку"):
- assert clusters_first_page.issubset(clusters) is True
- with pytest.allure.step("Проверка вхождения кластеров со второй страницы в первичную выборку"):
- assert clusters_second_page.issubset(clusters) is True
- with pytest.allure.step("Проверка кластеров первой и второй страницы на разность"):
- assert not clusters_first_page.intersection(clusters_second_page)
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка фильтрации выдачи /category по идентификатору кластера. Параметр cid")
- def test_category_clusters_filter(newsapi):
- """ Проверка фильтрации выдачи /category по идентификатору кластера. Параметр cid """
- with pytest.allure.step("Отправка запроса /category?id=vse с лимитом limit=5"):
- r = newsapi.get('/category?id=vse&limit=5')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка кластеров"):
- clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
- for cluster in clusters:
- with pytest.allure.step("Проверка фильтрации по id кластера. id = {}".format(cluster.attrib['id'])):
- with pytest.allure.step("Отправка запроса /category?id=vse&cid={}".format(cluster.attrib['id'])):
- r = newsapi.get('/category?id=vse&cid={}'.format(cluster.attrib['id']))
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка кластеров после фильтрации"):
- filtered_clusters = [x.attrib['id'] for x in (utils.get_all_tags_attrib_data(r.content, './cluster'))]
- with pytest.allure.step("Проверка отфильтрованного результата"):
- assert cluster.attrib['id'] not in filtered_clusters
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка выдачи с картинками. Параметр image=1")
- def test_category_clusters_with_images(newsapi):
- """ Проверка выдачи с картинками. Параметр image=1 """
- with pytest.allure.step("Отправка запроса с параметром image=1"):
- r = newsapi.get('/category?id=vse&image=1')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение списка картинок"):
- images = utils.get_all_tags_attrib_data(r.content, './cluster/image')
- for image in images:
- with pytest.allure.step("Проверка наличия ключа картинки: {}".format(image.attrib['image_url'])):
- assert image.attrib['key'] != ''
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка выдачи без картинок. Параметр image=0")
- def test_category_clusters_without_images(newsapi):
- """ Проверка выдачи без картинок. Параметр image=0 """
- with pytest.allure.step("Отправка запроса с параметром image=0"):
- r = newsapi.get('/category?id=vse&image=0')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Проверка наличия картинок в выдаче"):
- images = utils.get_all_tags_attrib_data(r.content, './cluster/image')
- for image in images:
- assert len(image.attrib) == 0
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка совпадения значений атрибутов unixtime и date")
- def test_category_unixtime_equal_date(newsapi):
- """ Проверка совпадения значений атрибутов unixtime и date """
- with pytest.allure.step("Отправка запроса /category?id=vse"):
- r = newsapi.get('/category?id=vse')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение данных по кластерам"):
- clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
- for cluster in clusters:
- with pytest.allure.step("Сравнение содержимого атрибутов unixtime и date для кластера "
- "'{}'".format(cluster.attrib['id'])):
- date = utils.get_datetime_from_str(cluster.attrib['date'], TIME_FORMAT)
- unixtime = utils.get_datetime_from_unixtime(cluster.attrib['unixtime'])
- assert date.strftime(TIME_FORMAT) == unixtime.strftime(TIME_FORMAT)
- @pytest.mark.skip('Необходимо доделать сервис (информация о смене позиции редактором)')
- @pytest.allure.feature('Тесты ресурса /category')
- @pytest.allure.story("Проверка сортировки выдачи по весу (weight)")
- def test_category_response_sorted_by_weight(newsapi):
- """ Проверка сортировки выдачи по весу (weight) """
- with pytest.allure.step("Отправка запроса /category?id=vse"):
- r = newsapi.get('/category?id=vse')
- pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
- r.raise_for_status()
- utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
- with pytest.allure.step("Получение значения атрибута weight для кластеров"):
- clusters_weight = [x.attrib['weight'] for x in (utils.get_all_tags_attrib_data(r.content, './cluster'))]
- with pytest.allure.step("Проверка сортировки кластеров выдачи по weight"):
- assert clusters_weight == sorted(clusters_weight, reverse=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement