Skip to content

Commit e4ad705

Browse files
authored
Merge pull request #18 from xpoes123/issue15
add min date for query
2 parents 1df197a + ba9cb9c commit e4ad705

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

qbreader/asynchronous.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ async def query(
8080
maxReturnLength: Optional[int] = 25,
8181
tossupPagination: Optional[int] = 1,
8282
bonusPagination: Optional[int] = 1,
83+
min_year: int = Year.MIN_YEAR,
84+
max_year: int = Year.CURRENT_YEAR,
8385
) -> QueryResponse:
8486
"""Query the qbreader database for questions.
8587
@@ -127,7 +129,10 @@ class type.
127129
The page of tossups to return.
128130
bonusPagination : int, default = 1
129131
The page of bonuses to return.
130-
132+
min_year : int, default = Year.MIN_YEAR
133+
The earliest year to search.
134+
max_year : int, default = Year.CURRENT_YEAR
135+
The latest year to search.
131136
Returns
132137
-------
133138
QueryResponse
@@ -184,6 +189,15 @@ class type.
184189
elif param < 1:
185190
raise ValueError(f"{name} must be at least 1.")
186191

192+
for name, year in {
193+
"minYear": min_year,
194+
"maxYear": max_year,
195+
}.items():
196+
if not isinstance(year, int):
197+
raise TypeError(
198+
f"{name} must be an integer, not {type(param).__name__}."
199+
)
200+
187201
url = BASE_URL + "/query"
188202

189203
(
@@ -209,6 +223,8 @@ class type.
209223
"maxReturnLength": maxReturnLength,
210224
"tossupPagination": tossupPagination,
211225
"bonusPagination": bonusPagination,
226+
"minYear": min_year,
227+
"maxYear": max_year,
212228
}
213229
data = api_utils.prune_none(data)
214230

qbreader/synchronous.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def query(
4545
maxReturnLength: Optional[int] = 25,
4646
tossupPagination: Optional[int] = 1,
4747
bonusPagination: Optional[int] = 1,
48+
min_year: int = Year.MIN_YEAR,
49+
max_year: int = Year.CURRENT_YEAR,
4850
) -> QueryResponse:
4951
"""Query the qbreader database for questions.
5052
@@ -92,7 +94,10 @@ class type.
9294
The page of tossups to return.
9395
bonusPagination : int, default = 1
9496
The page of bonuses to return.
95-
97+
min_year : int, default = Year.MIN_YEAR
98+
The earliest year to search.
99+
max_year : int, default = Year.CURRENT_YEAR
100+
The latest year to search.
96101
Returns
97102
-------
98103
QueryResponse
@@ -149,6 +154,15 @@ class type.
149154
elif param < 1:
150155
raise ValueError(f"{name} must be at least 1.")
151156

157+
for name, year in {
158+
"minYear": min_year,
159+
"maxYear": max_year,
160+
}.items():
161+
if not isinstance(year, int):
162+
raise TypeError(
163+
f"{name} must be an integer, not {type(param).__name__}."
164+
)
165+
152166
url = BASE_URL + "/query"
153167

154168
(
@@ -174,6 +188,8 @@ class type.
174188
"maxReturnLength": maxReturnLength,
175189
"tossupPagination": tossupPagination,
176190
"bonusPagination": bonusPagination,
191+
"minYear": min_year,
192+
"maxYear": max_year,
177193
}
178194
data = api_utils.prune_none(data)
179195

tests/test_async.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ async def test_query(self, qbr, params: dict[str, Any], expected_answer: str):
116116
)
117117
assert judgement.correct()
118118

119+
@pytest.mark.asyncio
120+
async def test_query_min_max_year_range(self, qbr):
121+
min_year = 2010
122+
max_year = 2015
123+
124+
query = await qbr.query(
125+
questionType="tossup",
126+
searchType="question",
127+
min_year=min_year,
128+
max_year=max_year,
129+
maxReturnLength=10,
130+
)
131+
132+
for tossup in query.tossups:
133+
assert min_year <= tossup.set.year <= max_year
134+
119135
@pytest.mark.asyncio
120136
@pytest.mark.parametrize(
121137
"params, exception",

tests/test_sync.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ def test_query(self, params: dict[str, Any], expected_answer: str):
8888
elif params["questionType"] == "bonus":
8989
assert query.bonuses[0].check_answer_sync(0, expected_answer).correct()
9090

91+
def test_query_min_max_year_range(self):
92+
min_year = 2010
93+
max_year = 2015
94+
95+
query = qbr.query(
96+
questionType="tossup",
97+
searchType="question",
98+
min_year=min_year,
99+
max_year=max_year,
100+
maxReturnLength=10,
101+
)
102+
103+
for tossup in query.tossups:
104+
assert min_year <= tossup.set.year <= max_year
105+
91106
@pytest.mark.parametrize(
92107
"params, exception",
93108
[
@@ -133,6 +148,18 @@ def test_query(self, params: dict[str, Any], expected_answer: str):
133148
},
134149
ValueError,
135150
),
151+
(
152+
{
153+
"min_year": "not an int",
154+
},
155+
TypeError,
156+
),
157+
(
158+
{
159+
"max_year": "not an int",
160+
},
161+
TypeError,
162+
),
136163
],
137164
)
138165
def test_query_exception(self, params: dict[str, Any], exception: Exception):

0 commit comments

Comments
 (0)