Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.06 KB | None | 0 0
  1. import pytest
  2.  
  3. from extra import utils, configs
  4.  
  5. TIME_FORMAT = '%d.%m.%Y %X'
  6.  
  7.  
  8. @pytest.allure.feature('Тесты ресурса /category')
  9. @pytest.allure.story('Валидация XML-ответа ресурса /category')
  10. def test_category_validate_xml(newsapi):
  11.     """ Валидация XML-ответа /category """
  12.  
  13.     with pytest.allure.step("Отправка запроса /category?id=vse"):
  14.         r = newsapi.get('/category?id=vse')
  15.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  16.         r.raise_for_status()
  17.  
  18.     utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  19.  
  20.  
  21. @pytest.allure.feature('Тесты ресурса /category')
  22. @pytest.allure.story("Проверка наличия кластеров по всем темам")
  23. @pytest.mark.parametrize(
  24.     "theme",
  25.     configs.THEMES
  26. )
  27. def test_category_cluster_exist_for_themes(newsapi, theme):
  28.     """ Проверка наличия кластеров по всем темам """
  29.  
  30.     with pytest.allure.step("Отправка запроса с указанием темы: {}".format(theme)):
  31.         r = newsapi.get('/category?id={}'.format(theme))
  32.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  33.         r.raise_for_status()
  34.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  35.  
  36.     with pytest.allure.step("Получение списка кластеров"):
  37.         clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
  38.  
  39.     with pytest.allure.step("Проверка наличия кластеров для темы: {}".format(theme)):
  40.         assert len(clusters) != 0
  41.  
  42.  
  43. @pytest.allure.feature('Тесты ресурса /category')
  44. @pytest.allure.story("Проверка наличия кластеров по всем регионам")
  45. @pytest.mark.parametrize(
  46.     "region",
  47.     configs.REGIONS
  48. )
  49. def test_category_cluster_exist_for_regions(newsapi, region):
  50.     """ Проверка наличия кластеров по всем регионам """
  51.  
  52.     with pytest.allure.step("Отправка запроса с указанием региона: {}".format(region)):
  53.         r = newsapi.get('/category?id={}'.format(region))
  54.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  55.         r.raise_for_status()
  56.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category_regions.xsd')
  57.  
  58.     with pytest.allure.step("Получение списка кластеров"):
  59.         clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
  60.  
  61.     with pytest.allure.step("Проверка наличия кластеров для региона: {}".format(region)):
  62.         assert len(clusters) != 0
  63.  
  64.  
  65. @pytest.allure.feature('Тесты ресурса /category')
  66. @pytest.allure.story("Проверка наличия кластеров по всем подтемам")
  67. @pytest.mark.parametrize(
  68.     "sub_theme",
  69.     configs.SUB_THEMES
  70. )
  71. def test_category_cluster_exist_for_sub_themes(newsapi, sub_theme):
  72.     """ Проверка наличия кластеров по всем подтемам """
  73.  
  74.     with pytest.allure.step("Отправка запроса с указанием подтемы: {}".format(sub_theme)):
  75.         r = newsapi.get('/category?id={}'.format(sub_theme))
  76.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  77.         r.raise_for_status()
  78.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  79.  
  80.     with pytest.allure.step("Получение списка кластеров"):
  81.         clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
  82.  
  83.     with pytest.allure.step("Проверка наличия кластеров для подтемы: {}".format(sub_theme)):
  84.         assert len(clusters) != 0
  85.  
  86.  
  87. @pytest.allure.feature('Тесты ресурса /category')
  88. @pytest.allure.story("Проверка параметра limit (количество кластеров на странице)")
  89. @pytest.mark.parametrize(
  90.     "limit",
  91.     (1, 3, 5, 15, 21)
  92. )
  93. def test_category_clusters_limit_for_page(newsapi, limit):
  94.     """ Проверка параметра limit (количество кластеров на странице) """
  95.  
  96.     with pytest.allure.step("Отправка запроса /category c указанием лимита"):
  97.         r = newsapi.get('/category?id=vse&limit={}'.format(limit))
  98.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  99.         r.raise_for_status()
  100.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  101.  
  102.     with pytest.allure.step("Получение списка кластеров"):
  103.         clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
  104.  
  105.     with pytest.allure.step("Проверка количества кластеров на странице: limit = {}".format(limit)):
  106.         assert len(clusters) == limit
  107.  
  108.  
  109. @pytest.allure.feature('Тесты ресурса /category')
  110. @pytest.allure.story("Проверка смещения по страницам. Параметр offset")
  111. def test_category_clusters_offset(newsapi):
  112.     """ Проверка смещения по страницам. Параметр offset """
  113.  
  114.     with pytest.allure.step("Отправка запроса /category?id=vse с лимитом limit=4"):
  115.         r = newsapi.get('/category?id=vse&limit=4')
  116.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  117.         r.raise_for_status()
  118.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  119.  
  120.     with pytest.allure.step("Отправка запроса /category?id=vse с limit=2 и offset=0. Получение первой страницы"):
  121.         r_first_page = newsapi.get('/category?id=vse&limit=2&offset=0')
  122.         pytest.allure.attach('first page xml', utils.xml_text_to_pratty_format(r_first_page.text))
  123.         r_first_page.raise_for_status()
  124.         utils.validate_xml(r_first_page.content, 'extra/schemas/newsservice/category.xsd')
  125.  
  126.     with pytest.allure.step("Отправка запроса /category?id=vse с limit=2 и offset=1. Получение второй страницы"):
  127.         r_second_page = newsapi.get('/category?id=vse&limit=2&offset=1')
  128.         pytest.allure.attach('second page xml', utils.xml_text_to_pratty_format(r_second_page.text))
  129.         r_second_page.raise_for_status()
  130.         utils.validate_xml(r_second_page.content, 'extra/schemas/newsservice/category.xsd')
  131.  
  132.     with pytest.allure.step("Получение списка кластеров"):
  133.         clusters = set([x.attrib['id'] for x in (utils.get_all_tags_attrib_data(r.content, './cluster'))])
  134.         if len(clusters) < 4:
  135.             pytest.skip("Количество кластеров в выдаче < 4")
  136.         clusters_first_page = set([x.attrib['id'] for x in
  137.                                    (utils.get_all_tags_attrib_data(r_first_page.content, './cluster'))])
  138.         clusters_second_page = set([x.attrib['id'] for x in
  139.                                     (utils.get_all_tags_attrib_data(r_second_page.content, './cluster'))])
  140.  
  141.     with pytest.allure.step("Проверка вхождения кластеров с первой страницы в первичную выборку"):
  142.         assert clusters_first_page.issubset(clusters) is True
  143.  
  144.     with pytest.allure.step("Проверка вхождения кластеров со второй страницы в первичную выборку"):
  145.         assert clusters_second_page.issubset(clusters) is True
  146.  
  147.     with pytest.allure.step("Проверка кластеров первой и второй страницы на разность"):
  148.         assert not clusters_first_page.intersection(clusters_second_page)
  149.  
  150.  
  151. @pytest.allure.feature('Тесты ресурса /category')
  152. @pytest.allure.story("Проверка фильтрации выдачи /category по идентификатору кластера. Параметр cid")
  153. def test_category_clusters_filter(newsapi):
  154.     """ Проверка фильтрации выдачи /category по идентификатору кластера. Параметр cid """
  155.  
  156.     with pytest.allure.step("Отправка запроса /category?id=vse с лимитом limit=5"):
  157.         r = newsapi.get('/category?id=vse&limit=5')
  158.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  159.         r.raise_for_status()
  160.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  161.  
  162.     with pytest.allure.step("Получение списка кластеров"):
  163.         clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
  164.  
  165.     for cluster in clusters:
  166.         with pytest.allure.step("Проверка фильтрации по id кластера. id = {}".format(cluster.attrib['id'])):
  167.             with pytest.allure.step("Отправка запроса /category?id=vse&cid={}".format(cluster.attrib['id'])):
  168.                 r = newsapi.get('/category?id=vse&cid={}'.format(cluster.attrib['id']))
  169.                 pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  170.                 r.raise_for_status()
  171.                 utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  172.  
  173.             with pytest.allure.step("Получение списка кластеров после фильтрации"):
  174.                 filtered_clusters = [x.attrib['id'] for x in (utils.get_all_tags_attrib_data(r.content, './cluster'))]
  175.  
  176.             with pytest.allure.step("Проверка отфильтрованного результата"):
  177.                 assert cluster.attrib['id'] not in filtered_clusters
  178.  
  179.  
  180. @pytest.allure.feature('Тесты ресурса /category')
  181. @pytest.allure.story("Проверка выдачи с картинками. Параметр image=1")
  182. def test_category_clusters_with_images(newsapi):
  183.     """ Проверка выдачи с картинками. Параметр image=1 """
  184.  
  185.     with pytest.allure.step("Отправка запроса с параметром image=1"):
  186.         r = newsapi.get('/category?id=vse&image=1')
  187.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  188.         r.raise_for_status()
  189.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  190.  
  191.     with pytest.allure.step("Получение списка картинок"):
  192.         images = utils.get_all_tags_attrib_data(r.content, './cluster/image')
  193.  
  194.     for image in images:
  195.         with pytest.allure.step("Проверка наличия ключа картинки: {}".format(image.attrib['image_url'])):
  196.             assert image.attrib['key'] != ''
  197.  
  198.  
  199. @pytest.allure.feature('Тесты ресурса /category')
  200. @pytest.allure.story("Проверка выдачи без картинок. Параметр image=0")
  201. def test_category_clusters_without_images(newsapi):
  202.     """ Проверка выдачи без картинок. Параметр image=0 """
  203.  
  204.     with pytest.allure.step("Отправка запроса с параметром image=0"):
  205.         r = newsapi.get('/category?id=vse&image=0')
  206.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  207.         r.raise_for_status()
  208.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  209.  
  210.     with pytest.allure.step("Проверка наличия картинок в выдаче"):
  211.         images = utils.get_all_tags_attrib_data(r.content, './cluster/image')
  212.         for image in images:
  213.             assert len(image.attrib) == 0
  214.  
  215.  
  216. @pytest.allure.feature('Тесты ресурса /category')
  217. @pytest.allure.story("Проверка совпадения значений атрибутов unixtime и date")
  218. def test_category_unixtime_equal_date(newsapi):
  219.     """ Проверка совпадения значений атрибутов unixtime и date """
  220.  
  221.     with pytest.allure.step("Отправка запроса /category?id=vse"):
  222.         r = newsapi.get('/category?id=vse')
  223.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  224.         r.raise_for_status()
  225.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  226.  
  227.     with pytest.allure.step("Получение данных по кластерам"):
  228.         clusters = utils.get_all_tags_attrib_data(r.content, './cluster')
  229.  
  230.     for cluster in clusters:
  231.         with pytest.allure.step("Сравнение содержимого атрибутов unixtime и date для кластера "
  232.                                 "'{}'".format(cluster.attrib['id'])):
  233.             date = utils.get_datetime_from_str(cluster.attrib['date'], TIME_FORMAT)
  234.             unixtime = utils.get_datetime_from_unixtime(cluster.attrib['unixtime'])
  235.             assert date.strftime(TIME_FORMAT) == unixtime.strftime(TIME_FORMAT)
  236.  
  237.  
  238. @pytest.mark.skip('Необходимо доделать сервис (информация о смене позиции редактором)')
  239. @pytest.allure.feature('Тесты ресурса /category')
  240. @pytest.allure.story("Проверка сортировки выдачи по весу (weight)")
  241. def test_category_response_sorted_by_weight(newsapi):
  242.     """ Проверка сортировки выдачи по весу (weight) """
  243.  
  244.     with pytest.allure.step("Отправка запроса /category?id=vse"):
  245.         r = newsapi.get('/category?id=vse')
  246.         pytest.allure.attach('xml', utils.xml_text_to_pratty_format(r.text))
  247.         r.raise_for_status()
  248.         utils.validate_xml(r.content, 'extra/schemas/newsservice/category.xsd')
  249.  
  250.     with pytest.allure.step("Получение значения атрибута weight для кластеров"):
  251.         clusters_weight = [x.attrib['weight'] for x in (utils.get_all_tags_attrib_data(r.content, './cluster'))]
  252.  
  253.     with pytest.allure.step("Проверка сортировки кластеров выдачи по weight"):
  254.         assert clusters_weight == sorted(clusters_weight, reverse=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement