|
1 | | -from pathlib import Path |
2 | | -from typing import Any, Callable, Generator |
| 1 | +from unittest.mock import AsyncMock, MagicMock |
3 | 2 |
|
4 | 3 | import pytest |
5 | | - |
6 | | -from vinted_api_kit import VintedApi |
7 | | -from vinted_api_kit.client.vinted_http_client import VintedHttpClient |
8 | | -from vinted_api_kit.services.item_service import ItemService |
9 | | - |
10 | | - |
11 | | -@pytest.fixture |
12 | | -def vinted_http_client(tmp_path: Path) -> Generator[VintedHttpClient, Any, None]: |
13 | | - client = VintedHttpClient( |
14 | | - locale="fr", |
15 | | - proxies={"http": "http://127.0.0.1:8888"}, |
16 | | - client_ip="123.123.123.123", |
17 | | - cookies_dir=tmp_path, |
18 | | - persist_cookies=False, |
19 | | - ) |
20 | | - yield client |
21 | | - import asyncio |
22 | | - |
23 | | - asyncio.run(client.close()) |
24 | | - |
25 | | - |
26 | | -@pytest.fixture |
27 | | -def item_service(vinted_http_client: VintedHttpClient): |
28 | | - return ItemService(client=vinted_http_client) |
29 | | - |
30 | | - |
31 | | -@pytest.fixture |
32 | | -async def vinted_api(): |
33 | | - async with VintedApi() as api: |
34 | | - yield api |
| 4 | +from curl_cffi.requests import Response |
35 | 5 |
|
36 | 6 |
|
37 | 7 | @pytest.fixture |
38 | | -def sample_catalog_item_data() -> Callable[[int], dict[str, Any]]: |
39 | | - def factory(ts: int = 1710000000) -> dict[str, Any]: |
40 | | - return { |
41 | | - "id": 77, |
42 | | - "title": "Vintage Jeans", |
43 | | - "brand_title": "Levis", |
44 | | - "size_title": "32", |
45 | | - "price": {"currency_code": "EUR", "amount": 45}, |
46 | | - "photo": { |
47 | | - "url": "https://cdn.vinted.net/catalog.jpg", |
48 | | - "high_resolution": {"timestamp": ts}, |
49 | | - }, |
50 | | - "photos": [{"high_resolution": {"timestamp": ts}}], |
51 | | - "url": "https://www.vinted.it/items/77-vintage-jeans", |
52 | | - } |
53 | | - |
54 | | - return factory |
| 8 | +def sample_catalog_item_data(): |
| 9 | + return { |
| 10 | + "id": 123, |
| 11 | + "title": "Nike Air Max", |
| 12 | + "brand_title": "Nike", |
| 13 | + "size_title": "42", |
| 14 | + "price": {"amount": 50.0, "currency_code": "EUR"}, |
| 15 | + "photo": { |
| 16 | + "url": "https://example.com/photo.jpg", |
| 17 | + "high_resolution": {"timestamp": 1734796339}, |
| 18 | + }, |
| 19 | + "url": "https://vinted.com/items/123-nike-air-max", |
| 20 | + } |
55 | 21 |
|
56 | 22 |
|
57 | 23 | @pytest.fixture |
58 | | -def sample_detailed_item_data() -> dict[str, Any]: |
| 24 | +def sample_detailed_item_data(): |
59 | 25 | return { |
60 | | - "id": 42, |
61 | | - "title": "Test Product", |
62 | | - "description": "Very rare Vinted item", |
63 | | - "brand_dto": {"title": "Nike", "slug": "nike"}, |
64 | | - "price": {"currency_code": "EUR", "amount": 19.99}, |
65 | | - "total_item_price": {"amount": 22.49}, |
| 26 | + "id": 456, |
| 27 | + "title": "Adidas Sneakers", |
| 28 | + "description": "Great condition", |
| 29 | + "brand_dto": {"title": "Adidas", "slug": "adidas"}, |
| 30 | + "price": {"amount": 75.0, "currency_code": "USD"}, |
| 31 | + "total_item_price": {"amount": 80.0, "currency_code": "USD"}, |
66 | 32 | "photos": [ |
67 | | - { |
68 | | - "url": "https://cdn.vinted.net/images.jpg", |
69 | | - "high_resolution": {"timestamp": 1710000000}, |
70 | | - } |
| 33 | + {"url": "https://example.com/photo1.jpg", "high_resolution": {"timestamp": 1734796339}} |
71 | 34 | ], |
72 | | - "url": "https://www.vinted.it/items/42-test-product", |
| 35 | + "url": "https://vinted.com/items/456", |
73 | 36 | "plugins": [ |
74 | 37 | { |
75 | 38 | "name": "attributes", |
76 | | - "data": {"attributes": [{"code": "size", "data": {"value": "M"}}]}, |
| 39 | + "data": {"attributes": [{"code": "size", "data": {"value": "44"}}]}, |
77 | 40 | } |
78 | 41 | ], |
79 | 42 | } |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def mock_async_session(): |
| 47 | + session = AsyncMock() |
| 48 | + session.cookies = MagicMock() |
| 49 | + session.cookies.get = MagicMock(return_value=None) |
| 50 | + session.headers = {} |
| 51 | + return session |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def mock_http_response(): |
| 56 | + response = MagicMock(spec=Response) |
| 57 | + response.status_code = 200 |
| 58 | + response.json.return_value = { |
| 59 | + "items": [ |
| 60 | + { |
| 61 | + "id": 1, |
| 62 | + "title": "Item 1", |
| 63 | + "price": {"amount": 10, "currency_code": "EUR"}, |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + return response |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture |
| 71 | +def temp_cookies_dir(tmp_path): |
| 72 | + cookies_dir = tmp_path / "cookies" |
| 73 | + cookies_dir.mkdir() |
| 74 | + return cookies_dir |
0 commit comments