@@ -30,38 +30,69 @@ def __init__(self, criteria_key, criteria_value, criteria_comparator=">="):
3030
3131 # Don't delete this method; it is used to inspect the SQL fragments
3232 def dump_sql (self ):
33- return "\n " .join (self .sql_where )
34-
33+ parts = []
34+
35+ if self .sql_from :
36+ parts .append ("-- FROM clause --" )
37+ parts .extend (self .sql_from )
38+
39+ if self .sql_where :
40+ parts .append ("-- WHERE clause --" )
41+ parts .extend (self .sql_where )
42+
43+ return "\n " .join (parts )
44+
3545 def _add_join_to_latest_episode (self ) -> None :
3646 """
3747 Mock stub for adding latest episode join. No-op for test harness.
3848 """
3949 self .sql_from .append ("-- JOIN to latest episode placeholder" )
4050
41-
4251 # === Example testable method below ===
4352 # Replace this with the one you want to test,
4453 # then use utils/oracle/test_subject_criteria_dev.py to run your scenarios
4554
46- def _add_criteria_kit_has_analyser_result_code (self ) -> None :
55+ def _add_join_to_appointments (self ) -> None :
4756 """
48- Filters kits based on whether they have an analyser error code .
49- Requires prior join to tk_items_t as alias 'tk' (via WHICH_TEST_KIT) .
57+ Adds join to appointment_t table based on appointment selection strategy .
58+ Requires prior join to latest episode (ep). Aliases the appointment table as 'ap' .
5059
5160 Accepts values:
52- - "yes" → analyser_error_code IS NOT NULL
53- - "no" → analyser_error_code IS NULL
61+ - "any_appointment_in_latest_episode"
62+ - "latest_appointment_in_latest_episode"
63+ - "earlier_appointment_in_latest_episode"
64+ - "later_appointment_in_latest_episode"
5465 """
5566 try :
5667 value = self .criteria_value .strip ().lower ()
68+ ap_alias = "ap"
69+ apr_alias = "ap_prev" # Simulated prior alias for test support
70+
71+ self ._add_join_to_latest_episode ()
72+ self .sql_from .append (
73+ f"INNER JOIN appointment_t { ap_alias } ON { ap_alias } .subject_epis_id = ep.subject_epis_id"
74+ )
5775
58- if value == "yes" :
59- self .sql_where .append ("AND tk.analyser_error_code IS NOT NULL" )
60- elif value == "no" :
61- self .sql_where .append ("AND tk.analyser_error_code IS NULL" )
76+ if value == "any_appointment_in_latest_episode" :
77+ return
78+ elif value == "latest_appointment_in_latest_episode" :
79+ self .sql_from .append (
80+ f"AND { ap_alias } .appointment_id = ("
81+ f" SELECT MAX(apx.appointment_id)"
82+ f" FROM appointment_t apx"
83+ f" WHERE apx.subject_epis_id = ep.subject_epis_id"
84+ f" AND apx.void = 'N')"
85+ )
86+ elif value == "earlier_appointment_in_latest_episode" :
87+ self .sql_from .append (
88+ f"AND { ap_alias } .appointment_id < { apr_alias } .appointment_id"
89+ )
90+ elif value == "later_appointment_in_latest_episode" :
91+ self .sql_from .append (
92+ f"AND { ap_alias } .appointment_id > { apr_alias } .appointment_id"
93+ )
6294 else :
63- raise ValueError (f"Invalid value for analyser result code presence : { value } " )
95+ raise ValueError (f"Invalid appointment selection value : { value } " )
6496
6597 except Exception :
6698 raise SelectionBuilderException (self .criteria_key_name , self .criteria_value )
67-
0 commit comments