Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion qbreader/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ async def query(
maxReturnLength: Optional[int] = 25,
tossupPagination: Optional[int] = 1,
bonusPagination: Optional[int] = 1,
min_year: int = Year.MIN_YEAR,
max_year: int = Year.CURRENT_YEAR,
) -> QueryResponse:
"""Query the qbreader database for questions.

Expand Down Expand Up @@ -127,7 +129,10 @@ class type.
The page of tossups to return.
bonusPagination : int, default = 1
The page of bonuses to return.

min_year : int, default = Year.MIN_YEAR
The earliest year to search.
max_year : int, default = Year.CURRENT_YEAR
The latest year to search.
Returns
-------
QueryResponse
Expand Down Expand Up @@ -184,6 +189,15 @@ class type.
elif param < 1:
raise ValueError(f"{name} must be at least 1.")

for name, year in {
"minYear": min_year,
"maxYear": max_year,
}.items():
if not isinstance(year, int):
raise TypeError(
f"{name} must be an integer, not {type(param).__name__}."
)

url = BASE_URL + "/query"

(
Expand All @@ -209,6 +223,8 @@ class type.
"maxReturnLength": maxReturnLength,
"tossupPagination": tossupPagination,
"bonusPagination": bonusPagination,
"minYear": min_year,
"maxYear": max_year,
}
data = api_utils.prune_none(data)

Expand Down
18 changes: 17 additions & 1 deletion qbreader/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def query(
maxReturnLength: Optional[int] = 25,
tossupPagination: Optional[int] = 1,
bonusPagination: Optional[int] = 1,
min_year: int = Year.MIN_YEAR,
max_year: int = Year.CURRENT_YEAR,
) -> QueryResponse:
"""Query the qbreader database for questions.

Expand Down Expand Up @@ -92,7 +94,10 @@ class type.
The page of tossups to return.
bonusPagination : int, default = 1
The page of bonuses to return.

min_year : int, default = Year.MIN_YEAR
The earliest year to search.
max_year : int, default = Year.CURRENT_YEAR
The latest year to search.
Returns
-------
QueryResponse
Expand Down Expand Up @@ -149,6 +154,15 @@ class type.
elif param < 1:
raise ValueError(f"{name} must be at least 1.")

for name, year in {
"minYear": min_year,
"maxYear": max_year,
}.items():
if not isinstance(year, int):
raise TypeError(
f"{name} must be an integer, not {type(param).__name__}."
)

url = BASE_URL + "/query"

(
Expand All @@ -174,6 +188,8 @@ class type.
"maxReturnLength": maxReturnLength,
"tossupPagination": tossupPagination,
"bonusPagination": bonusPagination,
"minYear": min_year,
"maxYear": max_year,
}
data = api_utils.prune_none(data)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ async def test_query(self, qbr, params: dict[str, Any], expected_answer: str):
)
assert judgement.correct()

@pytest.mark.asyncio
async def test_query_min_max_year_range(self, qbr):
min_year = 2010
max_year = 2015

query = await qbr.query(
questionType="tossup",
searchType="question",
min_year=min_year,
max_year=max_year,
maxReturnLength=10,
)

for tossup in query.tossups:
assert min_year <= tossup.set.year <= max_year

@pytest.mark.asyncio
@pytest.mark.parametrize(
"params, exception",
Expand Down
27 changes: 27 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ def test_query(self, params: dict[str, Any], expected_answer: str):
elif params["questionType"] == "bonus":
assert query.bonuses[0].check_answer_sync(0, expected_answer).correct()

def test_query_min_max_year_range(self):
min_year = 2010
max_year = 2015

query = qbr.query(
questionType="tossup",
searchType="question",
min_year=min_year,
max_year=max_year,
maxReturnLength=10,
)

for tossup in query.tossups:
assert min_year <= tossup.set.year <= max_year

@pytest.mark.parametrize(
"params, exception",
[
Expand Down Expand Up @@ -133,6 +148,18 @@ def test_query(self, params: dict[str, Any], expected_answer: str):
},
ValueError,
),
(
{
"min_year": "not an int",
},
TypeError,
),
(
{
"max_year": "not an int",
},
TypeError,
),
],
)
def test_query_exception(self, params: dict[str, Any], exception: Exception):
Expand Down
Loading