|
1 | | -import os |
2 | | - |
3 | 1 | import pytest |
4 | 2 | from PIL import Image |
5 | | - |
6 | 3 | from chizhik_api import ChizhikAPI |
7 | 4 |
|
8 | 5 |
|
9 | | -def _dump_debug(name: str, resp) -> None: |
10 | | - """Опциональный дамп ответа для отладки, если выставлен CHIZHIK_DEBUG=1.""" |
11 | | - if os.getenv("CHIZHIK_DEBUG") == "1": |
12 | | - try: |
13 | | - with open(f"debug_{name}.json", "w", encoding="utf-8") as f: |
14 | | - f.write(resp.text) |
15 | | - except Exception: |
16 | | - pass |
17 | | - |
18 | | - |
19 | | -@pytest.mark.asyncio |
20 | | -async def test_active_inout(schemashot): |
21 | | - async with ChizhikAPI() as api: |
22 | | - resp = await api.Advertising.active_inout() |
23 | | - schemashot.assert_json_match(resp.json(), api.Advertising.active_inout) |
24 | | - |
25 | | - |
26 | | -@pytest.mark.asyncio |
27 | | -async def test_cities_list(schemashot): |
28 | | - async with ChizhikAPI() as api: |
29 | | - resp = await api.Geolocation.cities_list(search_name="ар", page=1) |
30 | | - schemashot.assert_json_match(resp.json(), api.Geolocation.cities_list) |
31 | | - |
32 | | - |
33 | | -@pytest.mark.asyncio |
34 | | -async def test_tree(schemashot): |
35 | | - async with ChizhikAPI() as api: |
36 | | - resp = await api.Catalog.tree() |
37 | | - _dump_debug("tree", resp) |
38 | | - data = resp.json() |
39 | | - if not data: |
40 | | - pytest.skip("Пустое дерево категорий") |
41 | | - schemashot.assert_json_match(data, api.Catalog.tree) |
42 | | - |
43 | | - |
44 | | -@pytest.mark.asyncio |
45 | | -async def test_products_list(schemashot): |
46 | | - async with ChizhikAPI() as api: |
47 | | - # Получаем дерево категорий для извлечения first_category_id |
48 | | - tree_resp = await api.Catalog.tree() |
49 | | - _dump_debug("tree", tree_resp) |
50 | | - tree_data = tree_resp.json() |
51 | | - if not tree_data: |
52 | | - pytest.skip("Пустое дерево категорий") |
53 | | - first_category_id = tree_data[0]["id"] |
54 | | - |
55 | | - # Теперь получаем список продуктов |
56 | | - resp = await api.Catalog.products_list(category_id=first_category_id) |
57 | | - _dump_debug("products_list", resp) |
58 | | - data = resp.json() |
59 | | - items = data.get("items") or [] |
60 | | - if not items: |
61 | | - pytest.skip("В категории нет товаров") |
62 | | - schemashot.assert_json_match(data, api.Catalog.products_list) |
63 | | - |
64 | | - |
65 | | -@pytest.mark.asyncio |
66 | | -async def test_product_info(schemashot): |
67 | | - async with ChizhikAPI() as api: |
68 | | - # Получаем дерево категорий для извлечения first_category_id |
69 | | - tree_resp = await api.Catalog.tree() |
70 | | - _dump_debug("tree", tree_resp) |
71 | | - tree_data = tree_resp.json() |
72 | | - if not tree_data: |
73 | | - pytest.skip("Пустое дерево категорий") |
74 | | - first_category_id = tree_data[0]["id"] |
75 | | - |
76 | | - # Получаем список продуктов для извлечения first_product_id |
77 | | - products_resp = await api.Catalog.products_list(category_id=first_category_id) |
78 | | - _dump_debug("products_list", products_resp) |
79 | | - products_data = products_resp.json() |
80 | | - items = products_data.get("items") or [] |
81 | | - if not items: |
82 | | - pytest.skip("В категории нет товаров") |
83 | | - first_product_id = items[0]["id"] |
84 | | - |
85 | | - # Теперь получаем информацию о продукте |
86 | | - resp = await api.Catalog.Product.info(product_id=first_product_id) |
87 | | - schemashot.assert_json_match(resp.json(), api.Catalog.Product.info) |
88 | | - |
89 | | - |
90 | | -@pytest.mark.asyncio |
91 | | -async def test_download_image(): |
92 | | - async with ChizhikAPI() as api: |
93 | | - url = "https://chizhik.x5static.net/media/chizhik-assets/product_images/3060608.jpg" |
94 | | - resp = await api.General.download_image(url) |
95 | | - |
96 | | - # Определение формата через Pillow |
97 | | - with Image.open(resp) as img: |
98 | | - fmt = img.format.lower() |
99 | | - assert fmt in ("png", "jpeg", "webp") |
| 6 | +@pytest.fixture(scope="session") |
| 7 | +def anyio_backend(): |
| 8 | + """ |
| 9 | + Переопределяет фикстуру anyio_backend, чтобы использовать asyncio |
| 10 | + для всей сессии, устраняя ScopeMismatch с фикстурой 'api'. |
| 11 | + """ |
| 12 | + return 'asyncio' |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="session") |
| 16 | +async def api(): |
| 17 | + """Фикстура для инициализации API в рамках сессии""" |
| 18 | + # anyio автоматически управляет асинхронным контекстным менеджером |
| 19 | + async with ChizhikAPI() as api_instance: |
| 20 | + yield api_instance |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +async def category_data(api): |
| 25 | + """Фикстура для получения данных категории""" |
| 26 | + tree_resp = await api.Catalog.tree() |
| 27 | + tree_data = tree_resp.json() |
| 28 | + if not tree_data: |
| 29 | + pytest.skip("Пустое дерево категорий") |
| 30 | + return tree_data[0]["id"] |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +async def product_data(api, category_data): |
| 35 | + """Фикстура для получения данных продукта""" |
| 36 | + products_resp = await api.Catalog.products_list(category_id=category_data) |
| 37 | + products_data = products_resp.json() |
| 38 | + items = products_data.get("items") or [] |
| 39 | + if not items: |
| 40 | + pytest.skip("В категории нет товаров") |
| 41 | + return items[0]["id"] |
| 42 | + |
| 43 | + |
| 44 | +async def test_active_inout(api, schemashot): |
| 45 | + resp = await api.Advertising.active_inout() |
| 46 | + schemashot.assert_json_match(resp.json(), api.Advertising.active_inout) |
| 47 | + |
| 48 | + |
| 49 | +async def test_cities_list(api, schemashot): |
| 50 | + resp = await api.Geolocation.cities_list(search_name="ар", page=1) |
| 51 | + schemashot.assert_json_match(resp.json(), api.Geolocation.cities_list) |
| 52 | + |
| 53 | + |
| 54 | +async def test_tree(api, schemashot): |
| 55 | + resp = await api.Catalog.tree() |
| 56 | + data = resp.json() |
| 57 | + if not data: |
| 58 | + pytest.skip("Пустое дерево категорий") |
| 59 | + schemashot.assert_json_match(data, api.Catalog.tree) |
| 60 | + |
| 61 | + |
| 62 | +async def test_products_list(api, category_data, schemashot): |
| 63 | + resp = await api.Catalog.products_list(category_id=category_data) |
| 64 | + data = resp.json() |
| 65 | + items = data.get("items") or [] |
| 66 | + if not items: |
| 67 | + pytest.skip("В категории нет товаров") |
| 68 | + schemashot.assert_json_match(data, api.Catalog.products_list) |
| 69 | + |
| 70 | + |
| 71 | +async def test_product_info(api, product_data, schemashot): |
| 72 | + resp = await api.Catalog.Product.info(product_id=product_data) |
| 73 | + schemashot.assert_json_match(resp.json(), api.Catalog.Product.info) |
| 74 | + |
| 75 | + |
| 76 | +async def test_download_image(api): |
| 77 | + url = "https://chizhik.x5static.net/media/chizhik-assets/product_images/3060608.jpg" |
| 78 | + resp = await api.General.download_image(url) |
| 79 | + |
| 80 | + with Image.open(resp) as img: |
| 81 | + fmt = img.format.lower() |
| 82 | + assert fmt in ("png", "jpeg", "webp") |
0 commit comments