|
7 | 7 |
|
8 | 8 |
|
9 | 9 | # Add helper class stubs below |
10 | | -class WhichDiagnosticTest: |
| 10 | +class DiagnosticTestType: |
11 | 11 | """ |
12 | | - Test stub that maps criteria values to normalized diagnostic test selection keys. |
13 | | - Used by _add_join_to_diagnostic_tests for test harness evaluation. |
| 12 | + Mock mapping of diagnostic test type names to valid value IDs. |
14 | 13 | """ |
15 | 14 |
|
16 | | - ANY_TEST_IN_ANY_EPISODE = "any_test_in_any_episode" |
17 | | - ANY_TEST_IN_LATEST_EPISODE = "any_test_in_latest_episode" |
18 | | - ONLY_TEST_IN_LATEST_EPISODE = "only_test_in_latest_episode" |
19 | | - ONLY_NOT_VOID_TEST_IN_LATEST_EPISODE = "only_not_void_test_in_latest_episode" |
20 | | - LATEST_TEST_IN_LATEST_EPISODE = "latest_test_in_latest_episode" |
21 | | - LATEST_NOT_VOID_TEST_IN_LATEST_EPISODE = "latest_not_void_test_in_latest_episode" |
22 | | - EARLIEST_NOT_VOID_TEST_IN_LATEST_EPISODE = ( |
23 | | - "earliest_not_void_test_in_latest_episode" |
24 | | - ) |
25 | | - EARLIER_TEST_IN_LATEST_EPISODE = "earlier_test_in_latest_episode" |
26 | | - LATER_TEST_IN_LATEST_EPISODE = "later_test_in_latest_episode" |
27 | | - |
28 | 15 | _mapping = { |
29 | | - "any_test_in_any_episode": ANY_TEST_IN_ANY_EPISODE, |
30 | | - "any_test_in_latest_episode": ANY_TEST_IN_LATEST_EPISODE, |
31 | | - "only_test_in_latest_episode": ONLY_TEST_IN_LATEST_EPISODE, |
32 | | - "only_not_void_test_in_latest_episode": ONLY_NOT_VOID_TEST_IN_LATEST_EPISODE, |
33 | | - "latest_test_in_latest_episode": LATEST_TEST_IN_LATEST_EPISODE, |
34 | | - "latest_not_void_test_in_latest_episode": LATEST_NOT_VOID_TEST_IN_LATEST_EPISODE, |
35 | | - "earliest_not_void_test_in_latest_episode": EARLIEST_NOT_VOID_TEST_IN_LATEST_EPISODE, |
36 | | - "earlier_test_in_latest_episode": EARLIER_TEST_IN_LATEST_EPISODE, |
37 | | - "later_test_in_latest_episode": LATER_TEST_IN_LATEST_EPISODE, |
| 16 | + "pcr": 3001, |
| 17 | + "antigen": 3002, |
| 18 | + "lateral flow": 3003, |
38 | 19 | } |
39 | 20 |
|
40 | 21 | @classmethod |
41 | | - def from_description(cls, description: str) -> str: |
| 22 | + def get_valid_value_id(cls, description: str) -> int: |
42 | 23 | key = description.strip().lower() |
43 | 24 | if key not in cls._mapping: |
44 | 25 | raise ValueError(f"Unknown diagnostic test type: {description}") |
@@ -92,79 +73,35 @@ def _add_join_to_latest_episode(self) -> None: |
92 | 73 | # Replace this with the one you want to test, |
93 | 74 | # then use utils/oracle/test_subject_criteria_dev.py to run your scenarios |
94 | 75 |
|
95 | | - def _add_join_to_diagnostic_tests(self) -> None: |
| 76 | + def _add_criteria_diagnostic_test_type(self, proposed_or_confirmed: str) -> None: |
| 77 | + """ |
| 78 | + Filters diagnostic tests by type—proposed or confirmed. |
| 79 | + Requires prior join to external_tests_t (xt aliasing assumed). |
| 80 | + """ |
96 | 81 | try: |
97 | | - which = WhichDiagnosticTest.from_description(self.criteria_value) |
98 | 82 | idx = getattr(self, "criteria_index", 0) |
99 | 83 | xt = f"xt{idx}" |
100 | | - xtp = f"xt{idx - 1}" |
101 | | - |
102 | | - self.sql_from.append( |
103 | | - f"INNER JOIN external_tests_t {xt} ON {xt}.screening_subject_id = ss.screening_subject_id" |
104 | | - ) |
105 | | - |
106 | | - if which == WhichDiagnosticTest.ANY_TEST_IN_ANY_EPISODE: |
107 | | - return |
108 | | - |
109 | | - self._add_join_to_latest_episode() |
110 | | - |
111 | | - handlers = { |
112 | | - WhichDiagnosticTest.ANY_TEST_IN_LATEST_EPISODE: self._handle_any_test_in_latest_episode, |
113 | | - WhichDiagnosticTest.ONLY_TEST_IN_LATEST_EPISODE: self._handle_only_test_in_latest_episode, |
114 | | - WhichDiagnosticTest.ONLY_NOT_VOID_TEST_IN_LATEST_EPISODE: self._handle_only_test_in_latest_episode, |
115 | | - WhichDiagnosticTest.LATEST_TEST_IN_LATEST_EPISODE: self._handle_latest_test_in_latest_episode, |
116 | | - WhichDiagnosticTest.LATEST_NOT_VOID_TEST_IN_LATEST_EPISODE: self._handle_latest_test_in_latest_episode, |
117 | | - WhichDiagnosticTest.EARLIEST_NOT_VOID_TEST_IN_LATEST_EPISODE: self._handle_earliest_test_in_latest_episode, |
118 | | - WhichDiagnosticTest.EARLIER_TEST_IN_LATEST_EPISODE: self._handle_earlier_or_later_test, |
119 | | - WhichDiagnosticTest.LATER_TEST_IN_LATEST_EPISODE: self._handle_earlier_or_later_test, |
120 | | - } |
121 | | - |
122 | | - if which in handlers: |
123 | | - handlers[which](which, xt, xtp) |
| 84 | + |
| 85 | + if proposed_or_confirmed == "proposed": |
| 86 | + column = f"{xt}.proposed_type_id" |
| 87 | + elif proposed_or_confirmed == "confirmed": |
| 88 | + column = f"{xt}.confirmed_type_id" |
124 | 89 | else: |
125 | | - raise ValueError(f"Unsupported diagnostic test type: {which}") |
| 90 | + raise SelectionBuilderException( |
| 91 | + self.criteria_key_name, self.criteria_value |
| 92 | + ) |
126 | 93 |
|
127 | | - except Exception: |
128 | | - raise SelectionBuilderException(self.criteria_key_name, self.criteria_value) |
| 94 | + self.sql_where.append(f"AND {column} ") |
| 95 | + |
| 96 | + value = self.criteria_value.strip().lower() |
| 97 | + if value == "null": |
| 98 | + self.sql_where.append("IS NULL") |
| 99 | + elif value == "not null": |
| 100 | + self.sql_where.append("IS NOT NULL") |
| 101 | + else: |
| 102 | + comparator = self.criteria_comparator |
| 103 | + type_id = DiagnosticTestType.get_valid_value_id(self.criteria_value) |
| 104 | + self.sql_where.append(f"{comparator} {type_id}") |
129 | 105 |
|
130 | | - def _handle_any_test_in_latest_episode(self, which, xt, _): |
131 | | - self.sql_from.append(f"AND {xt}.subject_epis_id = ep.subject_epis_id") |
132 | | - |
133 | | - def _handle_only_test_in_latest_episode(self, which, xt, _): |
134 | | - self.sql_from.append(f"AND {xt}.subject_epis_id = ep.subject_epis_id") |
135 | | - if which == WhichDiagnosticTest.ONLY_NOT_VOID_TEST_IN_LATEST_EPISODE: |
136 | | - self.sql_from.append(f"AND {xt}.void = 'N'") |
137 | | - self.sql_from.append( |
138 | | - f"""AND NOT EXISTS ( |
139 | | - SELECT 'xto' FROM external_tests_t xto |
140 | | - WHERE xto.screening_subject_id = ss.screening_subject_id |
141 | | - {'AND xto.void = \'N\'' if which == WhichDiagnosticTest.ONLY_NOT_VOID_TEST_IN_LATEST_EPISODE else ''} |
142 | | - AND xto.subject_epis_id = ep.subject_epis_id |
143 | | - AND xto.ext_test_id != {xt}.ext_test_id )""" |
144 | | - ) |
145 | | - |
146 | | - def _handle_latest_test_in_latest_episode(self, which, xt, _): |
147 | | - self.sql_from.append( |
148 | | - f"""AND {xt}.ext_test_id = ( |
149 | | - SELECT MAX(xtx.ext_test_id) FROM external_tests_t xtx |
150 | | - WHERE xtx.screening_subject_id = ss.screening_subject_id |
151 | | - {'AND xtx.void = \'N\'' if which == WhichDiagnosticTest.LATEST_NOT_VOID_TEST_IN_LATEST_EPISODE else ''} |
152 | | - AND xtx.subject_epis_id = ep.subject_epis_id )""" |
153 | | - ) |
154 | | - |
155 | | - def _handle_earliest_test_in_latest_episode(self, which, xt, _): |
156 | | - self.sql_from.append( |
157 | | - f"""AND {xt}.ext_test_id = ( |
158 | | - SELECT MIN(xtn.ext_test_id) FROM external_tests_t xtn |
159 | | - WHERE xtn.screening_subject_id = ss.screening_subject_id |
160 | | - AND xtn.void = 'N' |
161 | | - AND xtn.subject_epis_id = ep.subject_epis_id )""" |
162 | | - ) |
163 | | - |
164 | | - def _handle_earlier_or_later_test(self, which, xt, xtp): |
165 | | - if getattr(self, "criteria_index", 0) == 0: |
| 106 | + except Exception: |
166 | 107 | raise SelectionBuilderException(self.criteria_key_name, self.criteria_value) |
167 | | - comparator = ( |
168 | | - "<" if which == WhichDiagnosticTest.EARLIER_TEST_IN_LATEST_EPISODE else ">" |
169 | | - ) |
170 | | - self.sql_from.append(f"AND {xt}.ext_test_id {comparator} {xtp}.ext_test_id") |
0 commit comments