Skip to content

Commit 9ace2c2

Browse files
Fixed _add_criteria_has_temporary_address() method and added to tests
1 parent a4d8692 commit 9ace2c2

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

utils/oracle/query_builder_test_harness/test_subject_selection_query_builder_util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from classes.user import User
44
from classes.subject import Subject
55
import logging
6+
import pytest
67

78

9+
@pytest.mark.wip3
810
def test_subject_selection_query_builder():
911
"""
1012
This function demonstrates how to use the builder to create a query
@@ -46,3 +48,17 @@ def test_subject_selection_query_builder():
4648
assert (
4749
df.iloc[0]["subject_nhs_number"] == "9163626810"
4850
), "NHS number should match the input"
51+
52+
criteria = {
53+
"subject has temporary address": "no",
54+
}
55+
user = User()
56+
subject = Subject()
57+
query, bind_vars = builder.build_subject_selection_query(
58+
criteria=criteria, user=user, subject=subject, subjects_to_retrieve=1
59+
)
60+
61+
df = OracleDB().execute_query(query, bind_vars)
62+
logging.info(f"DataFrame: {df}")
63+
assert df is not None, "DataFrame should not be None"
64+
assert df.shape[0] == 1, "DataFrame should contain exactly one row"

utils/oracle/subject_selection_query_builder.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,17 +2340,23 @@ def _add_criteria_has_temporary_address(self) -> None:
23402340
try:
23412341
answer = YesNoType.from_description(self.criteria_value)
23422342

2343-
# INNER JOIN on sd_address_t with address type 13043 (temporary)
2344-
self.sql_from.append(
2345-
"INNER JOIN sd_address_t adds ON adds.contact_id = c.contact_id "
2346-
"AND adds.ADDRESS_TYPE = 13043"
2347-
)
2348-
2349-
# Apply logic for EFFECTIVE_FROM based on yes/no
23502343
if answer == YesNoType.YES:
2351-
self.sql_from.append("AND adds.EFFECTIVE_FROM IS NOT NULL")
2344+
self.sql_from.append(
2345+
" INNER JOIN sd_address_t adds ON adds.contact_id = c.contact_id "
2346+
" AND adds.ADDRESS_TYPE = 13043 "
2347+
" AND adds.EFFECTIVE_FROM IS NOT NULL "
2348+
)
23522349
elif answer == YesNoType.NO:
2353-
self.sql_where.append("AND adds.EFFECTIVE_FROM IS NULL")
2350+
self.sql_from.append(
2351+
" LEFT JOIN sd_address_t adds ON adds.contact_id = c.contact_id "
2352+
)
2353+
self.sql_where.append(
2354+
" AND NOT EXISTS ("
2355+
" SELECT 1 "
2356+
" FROM sd_address_t x "
2357+
" WHERE x.contact_id = c.contact_id "
2358+
" AND x.address_type = 13043) "
2359+
)
23542360
else:
23552361
raise ValueError()
23562362
except Exception:

0 commit comments

Comments
 (0)