Skip to content

Commit 44aad50

Browse files
committed
Added DiagnosticTestHasOutcomeOfResult mapping class and filtering logic for test outcome-of-result criteria
1 parent 51fcccd commit 44aad50

File tree

4 files changed

+87
-27
lines changed

4 files changed

+87
-27
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class DiagnosticTestHasOutcomeOfResult:
2+
"""
3+
Maps outcome-of-result descriptions to logical flags or valid value IDs.
4+
"""
5+
6+
YES = "yes"
7+
NO = "no"
8+
9+
_label_to_id = {
10+
"referred": 9101,
11+
"treated": 9102,
12+
"not required": 9103,
13+
# Extend as needed
14+
}
15+
16+
_valid_flags = {YES, NO}
17+
18+
@classmethod
19+
def from_description(cls, description: str):
20+
key = description.strip().lower()
21+
if key in cls._valid_flags:
22+
return key
23+
if key in cls._label_to_id:
24+
return cls._label_to_id[key]
25+
raise ValueError(f"Unknown outcome-of-result description: '{description}'")
26+
27+
@classmethod
28+
def get_id(cls, description: str) -> int:
29+
key = description.strip().lower()
30+
if key not in cls._label_to_id:
31+
raise ValueError(f"No ID available for outcome: '{description}'")
32+
return cls._label_to_id[key]

utils/oracle/mock_selection_builder.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
# Add helper class stubs below
10-
class DiagnosticTestHasResult:
10+
class DiagnosticTestHasOutcomeOfResult:
1111
"""
12-
Resolves whether a diagnostic test has a result, or maps to a specific result ID.
12+
Maps outcome-of-result criteria values to either flags or valid value IDs.
1313
"""
1414

1515
YES = "yes"
@@ -18,24 +18,24 @@ class DiagnosticTestHasResult:
1818
_mapping = {
1919
"yes": YES,
2020
"no": NO,
21-
"positive": 9001,
22-
"negative": 9002,
23-
"indeterminate": 9003,
21+
"referred": 9101,
22+
"treated": 9102,
23+
"not required": 9103,
2424
}
2525

2626
@classmethod
2727
def from_description(cls, description: str):
2828
key = description.strip().lower()
2929
if key not in cls._mapping:
30-
raise ValueError(f"Unknown test result description: '{description}'")
30+
raise ValueError(f"Unknown outcome description: '{description}'")
3131
return cls._mapping[key]
3232

3333
@classmethod
3434
def get_id(cls, description: str) -> int:
3535
val = cls.from_description(description)
3636
if isinstance(val, int):
3737
return val
38-
raise ValueError(f"No ID associated with: '{description}'")
38+
raise ValueError(f"No ID associated with outcome: '{description}'")
3939

4040

4141
class MockSelectionBuilder:
@@ -85,25 +85,25 @@ def _add_join_to_latest_episode(self) -> None:
8585
# Replace this with the one you want to test,
8686
# then use utils/oracle/test_subject_criteria_dev.py to run your scenarios
8787

88-
def _add_criteria_diagnostic_test_has_result(self) -> None:
88+
def _add_criteria_diagnostic_test_has_outcome_of_result(self) -> None:
8989
"""
90-
Adds WHERE clause to check whether a diagnostic test has a result (IS NULL / NOT NULL / = result_id).
90+
Adds WHERE clause filtering on whether the diagnostic test has an outcome-of-result.
9191
"""
9292
try:
9393
idx = getattr(self, "criteria_index", 0)
9494
xt = f"xt{idx}"
9595
value = self.criteria_value.strip().lower()
96-
result = DiagnosticTestHasResult.from_description(value)
96+
outcome = DiagnosticTestHasOutcomeOfResult.from_description(value)
9797

98-
self.sql_where.append(f"AND {xt}.result_id ")
98+
self.sql_where.append(f"AND {xt}.outcome_of_result_id ")
9999

100-
if result == DiagnosticTestHasResult.YES:
100+
if outcome == DiagnosticTestHasOutcomeOfResult.YES:
101101
self.sql_where.append("IS NOT NULL")
102-
elif result == DiagnosticTestHasResult.NO:
102+
elif outcome == DiagnosticTestHasOutcomeOfResult.NO:
103103
self.sql_where.append("IS NULL")
104104
else:
105-
result_id = DiagnosticTestHasResult.get_id(value)
106-
self.sql_where.append(f"= {result_id}")
105+
outcome_id = DiagnosticTestHasOutcomeOfResult.get_id(value)
106+
self.sql_where.append(f"= {outcome_id}")
107107

108108
except Exception:
109109
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)

utils/oracle/subject_selection_query_builder.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
from classes.diagnostic_test_type import DiagnosticTestType
3232
from classes.diagnostic_test_is_void import DiagnosticTestIsVoid
3333
from classes.diagnostic_test_has_result import DiagnosticTestHasResult
34+
from classes.diagnostic_test_has_outcome_of_result import (
35+
DiagnosticTestHasOutcomeOfResult,
36+
)
3437

3538

3639
class SubjectSelectionQueryBuilder:
@@ -1771,6 +1774,29 @@ def _add_criteria_diagnostic_test_has_result(self) -> None:
17711774
except Exception:
17721775
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
17731776

1777+
def _add_criteria_diagnostic_test_has_outcome_of_result(self) -> None:
1778+
"""
1779+
Adds WHERE clause filtering on whether the diagnostic test has an outcome-of-result.
1780+
"""
1781+
try:
1782+
idx = getattr(self, "criteria_index", 0)
1783+
xt = f"xt{idx}"
1784+
value = self.criteria_value.strip().lower()
1785+
outcome = DiagnosticTestHasOutcomeOfResult.from_description(value)
1786+
1787+
self.sql_where.append(f"AND {xt}.outcome_of_result_id ")
1788+
1789+
if outcome == DiagnosticTestHasOutcomeOfResult.YES:
1790+
self.sql_where.append("IS NOT NULL")
1791+
elif outcome == DiagnosticTestHasOutcomeOfResult.NO:
1792+
self.sql_where.append("IS NULL")
1793+
else:
1794+
outcome_id = DiagnosticTestHasOutcomeOfResult.get_id(value)
1795+
self.sql_where.append(f"= {outcome_id}")
1796+
1797+
except Exception:
1798+
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
1799+
17741800
# ------------------------------------------------------------------------
17751801
# 🧬 CADS Clinical Dataset Filters
17761802
# ------------------------------------------------------------------------

utils/oracle/test_subject_criteria_dev.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,22 @@ def make_builder(key, value, index=0):
3737
return b
3838

3939

40-
# === Test: DIAGNOSTIC_TEST_HAS_RESULT (yes) ===
41-
b = make_builder(SubjectSelectionCriteriaKey.DIAGNOSTIC_TEST_HAS_RESULT, "yes")
42-
b._add_criteria_diagnostic_test_has_result()
43-
print("=== DIAGNOSTIC_TEST_HAS_RESULT (yes) ===")
40+
# === Test: DIAGNOSTIC_TEST_HAS_OUTCOME (yes) ===
41+
b = make_builder(SubjectSelectionCriteriaKey.DIAGNOSTIC_TEST_HAS_OUTCOME, "yes")
42+
b._add_criteria_diagnostic_test_has_outcome_of_result()
43+
print("=== DIAGNOSTIC_TEST_HAS_OUTCOME (yes) ===")
4444
print(b.dump_sql(), end="\n\n")
4545

46-
# === Test: DIAGNOSTIC_TEST_HAS_RESULT (no) ===
47-
b = make_builder(SubjectSelectionCriteriaKey.DIAGNOSTIC_TEST_HAS_RESULT, "no")
48-
b._add_criteria_diagnostic_test_has_result()
49-
print("=== DIAGNOSTIC_TEST_HAS_RESULT (no) ===")
46+
# === Test: DIAGNOSTIC_TEST_HAS_OUTCOME (no) ===
47+
b = make_builder(SubjectSelectionCriteriaKey.DIAGNOSTIC_TEST_HAS_OUTCOME, "no", index=1)
48+
b._add_criteria_diagnostic_test_has_outcome_of_result()
49+
print("=== DIAGNOSTIC_TEST_HAS_OUTCOME (no) ===")
5050
print(b.dump_sql(), end="\n\n")
5151

52-
# === Test: DIAGNOSTIC_TEST_HAS_RESULT (positive) ===
53-
b = make_builder(SubjectSelectionCriteriaKey.DIAGNOSTIC_TEST_HAS_RESULT, "positive")
54-
b._add_criteria_diagnostic_test_has_result()
55-
print("=== DIAGNOSTIC_TEST_HAS_RESULT (positive) ===")
52+
# === Test: DIAGNOSTIC_TEST_HAS_OUTCOME (treated) ===
53+
b = make_builder(
54+
SubjectSelectionCriteriaKey.DIAGNOSTIC_TEST_HAS_OUTCOME, "treated", index=2
55+
)
56+
b._add_criteria_diagnostic_test_has_outcome_of_result()
57+
print("=== DIAGNOSTIC_TEST_HAS_OUTCOME (treated) ===")
5658
print(b.dump_sql(), end="\n\n")

0 commit comments

Comments
 (0)