Skip to content

Commit afdb4bc

Browse files
adding python test (#216)
1 parent 496bfd3 commit afdb4bc

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import sys
2+
from pathlib import Path
3+
4+
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
5+
6+
import pytest
7+
import polars as pl
8+
import responses
9+
from pipelines.defs.extract.tcgdex.extract_sets import extract_sets_data
10+
11+
12+
@pytest.fixture
13+
def mock_api_response():
14+
"""Sample API responses matching tcgdex series format with sets"""
15+
return {
16+
"https://api.tcgdex.net/v2/en/series/me": {
17+
"id": "me",
18+
"name": "Mega Evolution",
19+
"sets": [
20+
{
21+
"id": "me01",
22+
"name": "Mega Evolution",
23+
"cardCount": {"official": 12, "total": 12},
24+
"logo": "https://example.com/me01.png",
25+
"symbol": "https://example.com/me01-symbol.png",
26+
},
27+
{
28+
"id": "me02",
29+
"name": "Phantasmal Flames",
30+
"cardCount": {"official": 25, "total": 25},
31+
"logo": "https://example.com/me02.png",
32+
"symbol": "https://example.com/me02-symbol.png",
33+
},
34+
],
35+
},
36+
"https://api.tcgdex.net/v2/en/series/sv": {
37+
"id": "sv",
38+
"name": "Scarlet & Violet",
39+
"sets": [
40+
{
41+
"id": "sv01",
42+
"name": "Scarlet & Violet",
43+
"cardCount": {"official": 198, "total": 258},
44+
"logo": "https://example.com/sv01.png",
45+
"symbol": "https://example.com/sv01-symbol.png",
46+
},
47+
{
48+
"id": "sv02",
49+
"name": "Paldea Evolved",
50+
"cardCount": {"official": 193, "total": 279},
51+
"logo": "https://example.com/sv02.png",
52+
"symbol": None,
53+
},
54+
],
55+
},
56+
"https://api.tcgdex.net/v2/en/series/swsh": {
57+
"id": "swsh",
58+
"name": "Sword & Shield",
59+
"sets": [
60+
{
61+
"id": "swsh1",
62+
"name": "Sword & Shield",
63+
"cardCount": {"official": 202, "total": 216},
64+
"logo": None,
65+
"symbol": "https://example.com/swsh1-symbol.png",
66+
},
67+
],
68+
},
69+
}
70+
71+
72+
@pytest.mark.benchmark
73+
@responses.activate
74+
def test_extract_sets_data_success(mock_series_responses):
75+
"""Test successful extraction of sets from multiple series"""
76+
# Mock all API calls
77+
for url, response_data in mock_series_responses.items():
78+
responses.add(
79+
responses.GET,
80+
url,
81+
json=response_data,
82+
status=200,
83+
)
84+
85+
result = extract_sets_data()
86+
87+
# Assertions
88+
assert isinstance(result, pl.DataFrame) # nosec
89+
assert len(result) == 5 # nosec (2 + 2 + 1 sets)
90+
assert set(result.columns) == { # nosec
91+
"series_id",
92+
"set_id",
93+
"set_name",
94+
"official_card_count",
95+
"total_card_count",
96+
"logo",
97+
"symbol",
98+
}
99+
assert set(result["series_id"].to_list()) == {"me", "sv", "swsh"} # nosec
100+
assert set(result["set_id"].to_list()) == {"me01", "me02", "sv01", "sv02", "swsh1"} # nosec
101+
102+
103+
@pytest.mark.benchmark
104+
@responses.activate
105+
def test_extract_sets_data_empty_sets(mock_series_responses):
106+
"""Test extraction when a series has no sets"""
107+
# Modify one response to have empty sets
108+
mock_series_responses["https://api.tcgdex.net/v2/en/series/me"]["sets"] = []
109+
110+
for url, response_data in mock_series_responses.items():
111+
responses.add(
112+
responses.GET,
113+
url,
114+
json=response_data,
115+
status=200,
116+
)
117+
118+
result = extract_sets_data()
119+
120+
assert isinstance(result, pl.DataFrame) # nosec
121+
assert len(result) == 3 # nosec (0 + 2 + 1 sets)
122+
assert "me" not in result["series_id"].to_list() # nosec
123+
124+
125+
@pytest.mark.benchmark
126+
@responses.activate
127+
def test_extract_sets_data_null_card_counts():
128+
"""Test extraction with null card counts"""
129+
mock_responses = {
130+
"https://api.tcgdex.net/v2/en/series/me": {
131+
"id": "me",
132+
"name": "Mega Evolution",
133+
"sets": [],
134+
},
135+
"https://api.tcgdex.net/v2/en/series/sv": {
136+
"id": "sv",
137+
"name": "Scarlet & Violet",
138+
"sets": [
139+
{
140+
"id": "sv01",
141+
"name": "Scarlet & Violet",
142+
"cardCount": {},
143+
"logo": None,
144+
"symbol": None,
145+
},
146+
],
147+
},
148+
"https://api.tcgdex.net/v2/en/series/swsh": {
149+
"id": "swsh",
150+
"name": "Sword & Shield",
151+
"sets": [],
152+
},
153+
}
154+
155+
for url, response_data in mock_responses.items():
156+
responses.add(
157+
responses.GET,
158+
url,
159+
json=response_data,
160+
status=200,
161+
)
162+
163+
result = extract_sets_data()
164+
165+
assert isinstance(result, pl.DataFrame) # nosec
166+
assert len(result) == 1 # nosec
167+
assert result["official_card_count"].to_list()[0] is None # nosec
168+
assert result["total_card_count"].to_list()[0] is None # nosec

0 commit comments

Comments
 (0)