Add time filter tests

This commit is contained in:
spsaefan
2026-07-27 11:56:19 +02:00
parent bc59c3f563
commit a6a78bb556
@@ -14,6 +14,7 @@ from inse.dashboard.service import (
get_dashboard_dfg_topics,
get_dashboard_eu_project_statuses,
get_dashboard_patents,
get_dashboard_publications,
get_dashboard_statistics,
get_top_authors,
get_topic_heatmap,
@@ -43,7 +44,36 @@ async def test_dashboard_patents_uses_org_patent_total():
"organization_name": "Example Org",
"value": 42,
}
search.assert_awaited_once_with(ORG_INSTITUTION_ID, page=0, size=0)
search.assert_awaited_once_with(
ORG_INSTITUTION_ID,
page=0,
size=0,
from_year=None,
to_year=None,
)
@pytest.mark.anyio
async def test_dashboard_publications_filters_by_publication_year_period():
with patch(
"inse.dashboard.service.elasticsearch.search",
new=AsyncMock(return_value={"hits": {"total": {"value": 7}}}),
) as search:
result = await get_dashboard_publications(
ORG_INSTITUTION_ID,
"Example Org",
from_year=2020,
to_year=2024,
)
assert result.value == 7
query = search.await_args.kwargs["query"]
assert query["bool"]["must"][0]["nested"]["query"]["term"] == {
"authorships.institutions.id": ORG_INSTITUTION_ID
}
assert query["bool"]["filter"] == [
{"range": {"publication_year": {"gte": 2020, "lte": 2024}}}
]
@pytest.mark.anyio
@@ -104,6 +134,51 @@ async def test_dashboard_eu_project_statuses_deduplicates_projects():
)
@pytest.mark.anyio
async def test_dashboard_eu_project_statuses_restricts_project_set_by_period():
async def scan(**kwargs):
yield {"_source": {"projectID": "P2"}}
yield {"_source": {"projectID": "P1"}}
with patch(
"inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id",
new=AsyncMock(return_value=["Example University"]),
), patch("inse.dashboard.service.elasticsearch.scan", new=scan), patch(
"inse.dashboard.service.elasticsearch.search",
new=AsyncMock(
return_value={
"aggregations": {
"ongoing": {"projects": {"value": 1}},
"upcoming": {"projects": {"value": 0}},
"completed": {"projects": {"value": 1}},
}
}
),
) as search:
await get_dashboard_eu_project_statuses(
ORG_INSTITUTION_ID,
"Example Org",
from_year=2020,
to_year=2024,
)
assert search.await_args.kwargs["body"]["query"] == {
"bool": {
"filter": [
{"terms": {"id": ["P1", "P2"]}},
{
"bool": {
"filter": [
{"range": {"startDate": {"lte": "2024-12-31"}}},
{"range": {"endDate": {"gte": "2020-01-01"}}},
]
}
},
]
}
}
@pytest.mark.anyio
async def test_dashboard_bmbf_project_statuses_uses_funding_dates():
response = {
@@ -789,7 +864,7 @@ async def test_topic_heatmap_maps_field_year_counts_and_shares():
@pytest.mark.anyio
async def test_topic_heatmap_defaults_to_last_five_years():
async def test_topic_heatmap_defaults_to_all_time():
async def search(**kwargs):
if kwargs["index"] == config.settings.openalex_works_search_index:
return {
@@ -809,13 +884,9 @@ async def test_topic_heatmap_defaults_to_last_five_years():
organization_name="Example Org",
)
current_year = datetime.now().year
assert result.years == list(range(current_year - 4, current_year + 1))
assert result.years == []
body = mocked_search.await_args_list[0].kwargs["body"]
assert body["query"]["bool"]["filter"][0]["range"]["publication_year"] == {
"gte": current_year - 4,
"lte": current_year,
}
assert body["query"]["bool"]["filter"] == []
@pytest.mark.anyio