Skip to content

Commit c8fcfbf

Browse files
authored
Bug Fix: Historical Entity Filtering Bug (#304)
* bug fix: updated period filtering logic * updated test cases * unit tests moved to the integration tests
1 parent 0d157e9 commit c8fcfbf

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

application/data_access/entity_queries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def _apply_period_option_filter(query, params):
331331
)
332332
elif PeriodOption.historical in options:
333333
return query.filter(
334-
or_(EntityOrm.end_date.is_not(None), EntityOrm.end_date < func.now())
334+
and_(EntityOrm.end_date.is_not(None), EntityOrm.end_date < func.now())
335335
)
336336

337337

tests/integration/test_entity_queries.py

+81
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from datetime import datetime
2+
import pytest
13
from application.data_access.entity_queries import lookup_entity_link
4+
from application.data_access.entity_queries import _apply_period_option_filter
25
from application.db.models import EntityOrm
36

47

@@ -39,3 +42,81 @@ def test__lookup_entity_link_returns_the_looked_up_entity_when_the_link_exists(
3942
assert linked_entity["typology"] == lookup_entity["typology"]
4043
assert linked_entity["name"] == lookup_entity["name"]
4144
assert linked_entity["reference"] == lookup_entity["reference"]
45+
46+
47+
@pytest.mark.parametrize("period", [["current"], ["historical"], ["all"]])
48+
def test_apply_period_option_filter(db_session, period):
49+
entities = [
50+
{
51+
"entry_date": "2024-10-07",
52+
"start_date": "2020-01-13",
53+
"end_date": "2023-01-12",
54+
"entity": 2300104,
55+
"name": "Cooling Towers at the former Willington Power Station",
56+
"dataset": "certificate-of-immunity",
57+
"typology": "geography",
58+
"reference": "1456996",
59+
"prefix": "certificate-of-immunity",
60+
"organisation_entity": "16",
61+
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
62+
"point": "POINT (-0.33737897872924805 53.74541799747043)",
63+
},
64+
{
65+
"entry_date": "2024-10-07",
66+
"start_date": "2020-01-13",
67+
"end_date": "2000-01-12",
68+
"entity": 2300103,
69+
"name": "Cooling Towers at the former Willington Power Station",
70+
"dataset": "certificate-of-immunity",
71+
"typology": "geography",
72+
"reference": "1456995",
73+
"prefix": "certificate-of-immunity",
74+
"organisation_entity": "16",
75+
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
76+
"point": "POINT (-0.33737897872924805 53.74541799747043)",
77+
},
78+
{
79+
"entry_date": "2024-10-07",
80+
"start_date": "2020-01-13",
81+
"end_date": "2030-12-12",
82+
"entity": 2300106,
83+
"name": "Cooling Towers at the former Willington Power Station",
84+
"dataset": "certificate-of-immunity",
85+
"typology": "geography",
86+
"reference": "1456997",
87+
"prefix": "certificate-of-immunity",
88+
"organisation_entity": "16",
89+
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
90+
"point": "POINT (-0.33737897872924805 53.74541799747043)",
91+
},
92+
{
93+
"entry_date": "2024-10-07",
94+
"start_date": "2020-01-13",
95+
"end_date": "2050-01-12",
96+
"entity": 2300105,
97+
"name": "Cooling Towers at the former Willington Power Station",
98+
"dataset": "certificate-of-immunity",
99+
"typology": "geography",
100+
"reference": "1456999",
101+
"prefix": "certificate-of-immunity",
102+
"organisation_entity": "16",
103+
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
104+
"point": "POINT (-0.33737897872924805 53.74541799747043)",
105+
},
106+
]
107+
for entity in entities:
108+
db_session.add(EntityOrm(**entity))
109+
110+
db_session.flush()
111+
query = db_session.query(EntityOrm)
112+
result = _apply_period_option_filter(query, {"period": period}).all()
113+
114+
if period == ["current"]:
115+
assert all(
116+
entity.end_date is None or entity.end_date > datetime.now().date()
117+
for entity in result
118+
)
119+
if period == ["historical"]:
120+
assert all(entity.end_date < datetime.now().date() for entity in result)
121+
if period == ["all"]:
122+
assert len(result) == 4

0 commit comments

Comments
 (0)