Skip to content

Commit df37956

Browse files
committed
Added subject filter for presence of existing surveillance review case
1 parent 6b959ee commit df37956

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class DoesSubjectHaveSurveillanceReviewCase:
2+
"""
3+
Maps binary criteria for presence of a surveillance review case.
4+
"""
5+
6+
YES = "yes"
7+
NO = "no"
8+
9+
_valid_values = {YES, NO}
10+
11+
@classmethod
12+
def from_description(cls, description: str) -> str:
13+
key = description.strip().lower()
14+
if key not in cls._valid_values:
15+
raise ValueError(
16+
f"Unknown surveillance review case presence: '{description}'"
17+
)
18+
return key

utils/oracle/mock_selection_builder.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@
99

1010

1111
# Add helper class stubs below
12-
class SurveillanceReviewStatusType:
13-
"""
14-
Maps descriptive surveillance review statuses to valid value IDs.
15-
"""
12+
class DoesSubjectHaveSurveillanceReviewCase:
13+
YES = "yes"
14+
NO = "no"
1615

17-
_label_to_id = {
18-
"awaiting review": 9301,
19-
"in progress": 9302,
20-
"completed": 9303,
21-
# Extend as needed
16+
_mapping = {
17+
"yes": YES,
18+
"no": NO,
2219
}
2320

2421
@classmethod
25-
def get_id(cls, description: str) -> int:
22+
def from_description(cls, description: str) -> str:
2623
key = description.strip().lower()
27-
if key not in cls._label_to_id:
28-
raise ValueError(f"Unknown review status: '{description}'")
29-
return cls._label_to_id[key]
24+
if key not in cls._mapping:
25+
raise ValueError(
26+
f"Unknown surveillance review case presence: '{description}'"
27+
)
28+
return cls._mapping[key]
3029

3130

3231
class MockSelectionBuilder:
@@ -95,16 +94,18 @@ def _add_join_to_surveillance_review(self):
9594
# Replace this with the one you want to test,
9695
# then use utils/oracle/test_subject_criteria_dev.py to run your scenarios
9796

98-
def _add_criteria_surveillance_review_status(self) -> None:
97+
def _add_criteria_does_subject_have_surveillance_review_case(self) -> None:
9998
"""
100-
Filters subjects based on the review_status_id in their surveillance review dataset.
99+
Filters subjects based on presence or absence of a surveillance review case.
101100
"""
102101
try:
103-
self._add_join_to_surveillance_review()
104-
status_id = SurveillanceReviewStatusType.get_id(self.criteria_value)
102+
value = DoesSubjectHaveSurveillanceReviewCase.from_description(self.criteria_value)
103+
104+
clause = "AND EXISTS" if value == "yes" else "AND NOT EXISTS"
105105

106106
self.sql_where.append(
107-
f"AND sr.review_status_id {self.criteria_comparator} {status_id}"
107+
f"{clause} (SELECT 'sr' FROM surveillance_review sr "
108+
"WHERE sr.subject_id = ss.screening_subject_id)"
108109
)
109110

110111
except Exception:

utils/oracle/subject_selection_query_builder.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
from classes.latest_episode_latest_investigation_dataset import (
4040
LatestEpisodeLatestInvestigationDataset,
4141
)
42-
from classes.surveillance_review_status_type import SurveillanceReviewStatusType
42+
from classes.surveillance_review_status_type import SurveillanceReviewStatusType
43+
from classes.does_subject_have_surveillance_review_case import (
44+
DoesSubjectHaveSurveillanceReviewCase,
45+
)
4346

4447

4548
class SubjectSelectionQueryBuilder:
@@ -1999,13 +2002,31 @@ def _add_criteria_surveillance_review_status(self) -> None:
19992002

20002003
except Exception:
20012004
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
2002-
2005+
20032006
def _add_join_to_surveillance_review(self) -> None:
20042007
"""
20052008
Internal helper. Adds the necessary join to the surveillance review dataset for filtering.
20062009
"""
20072010
self.sql_from.append("-- JOIN to surveillance review placeholder")
20082011

2012+
def _add_criteria_does_subject_have_surveillance_review_case(self) -> None:
2013+
"""
2014+
Filters subjects based on presence or absence of a surveillance review case.
2015+
"""
2016+
try:
2017+
value = DoesSubjectHaveSurveillanceReviewCase.from_description(
2018+
self.criteria_value
2019+
)
2020+
2021+
clause = "AND EXISTS" if value == "yes" else "AND NOT EXISTS"
2022+
2023+
self.sql_where.append(
2024+
f"{clause} (SELECT 'sr' FROM surveillance_review sr "
2025+
"WHERE sr.subject_id = ss.screening_subject_id)"
2026+
)
2027+
2028+
except Exception:
2029+
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
20092030

20102031
# ------------------------------------------------------------------------
20112032
# 🧬 CADS Clinical Dataset Filters

utils/oracle/test_subject_criteria_dev.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ def make_builder(key, value, index=0, comparator="="):
4141
return b
4242

4343

44-
# === Test: SURVEILLANCE_REVIEW_STATUS (completed) ===
44+
# === Test: HAS_EXISTING_SURVEILLANCE_REVIEW_CASE (yes) ===
4545
b = make_builder(
46-
SubjectSelectionCriteriaKey.SURVEILLANCE_REVIEW_STATUS, "completed", comparator=">="
46+
SubjectSelectionCriteriaKey.HAS_EXISTING_SURVEILLANCE_REVIEW_CASE, "yes"
4747
)
48-
b._add_criteria_surveillance_review_status()
49-
print("=== SURVEILLANCE_REVIEW_STATUS (completed) ===")
48+
b._add_criteria_does_subject_have_surveillance_review_case()
49+
print("=== HAS_EXISTING_SURVEILLANCE_REVIEW_CASE (yes) ===")
50+
print(b.dump_sql(), end="\n\n")
51+
52+
# === Test: HAS_EXISTING_SURVEILLANCE_REVIEW_CASE (no) ===
53+
b = make_builder(
54+
SubjectSelectionCriteriaKey.HAS_EXISTING_SURVEILLANCE_REVIEW_CASE, "no", index=1
55+
)
56+
b._add_criteria_does_subject_have_surveillance_review_case()
57+
print("=== HAS_EXISTING_SURVEILLANCE_REVIEW_CASE (no) ===")
5058
print(b.dump_sql(), end="\n\n")

0 commit comments

Comments
 (0)