|
| 1 | +# mock_selection_builder.py — Development-only testing harness for criteria logic |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) |
| 6 | +print("PYTHONPATH set to:", sys.path[0]) |
| 7 | +from classes.subject_selection_criteria_key import ( |
| 8 | + SubjectSelectionCriteriaKey, |
| 9 | +) |
| 10 | +from classes.episode_type import EpisodeType |
| 11 | +from classes.subject_has_episode import SubjectHasEpisode |
| 12 | +from classes.selection_builder_exception import SelectionBuilderException |
| 13 | + |
| 14 | + |
| 15 | +class MockSelectionBuilder: |
| 16 | + """ |
| 17 | + Lightweight test harness that mimics SubjectSelectionQueryBuilder behavior. |
| 18 | +
|
| 19 | + This class is meant for local testing of SQL fragment builders without the full |
| 20 | + application context. Developers can copy/paste individual _add_criteria_* methods |
| 21 | + from the real builder and test inputs/outputs directly. |
| 22 | +
|
| 23 | + Usage: |
| 24 | + - Create an instance with a criteria key and value |
| 25 | + - Call the appropriate criteria builder method |
| 26 | + - Use dump_sql() to inspect the resulting SQL |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, criteria_key, criteria_value): |
| 30 | + self.criteria_key = criteria_key |
| 31 | + self.criteria_key_name = criteria_key.description |
| 32 | + self.criteria_value = criteria_value |
| 33 | + self.sql_where = [] |
| 34 | + |
| 35 | + def _add_criteria_subject_has_episodes(self, episode_type=None): |
| 36 | + try: |
| 37 | + value = SubjectHasEpisode.by_description(self.criteria_value.lower()) |
| 38 | + if value == SubjectHasEpisode.YES: |
| 39 | + self.sql_where.append(" AND EXISTS ( SELECT 'ep' ") |
| 40 | + elif value == SubjectHasEpisode.NO: |
| 41 | + self.sql_where.append(" AND NOT EXISTS ( SELECT 'ep' ") |
| 42 | + else: |
| 43 | + raise SelectionBuilderException( |
| 44 | + self.criteria_key_name, self.criteria_value |
| 45 | + ) |
| 46 | + except Exception: |
| 47 | + raise SelectionBuilderException(self.criteria_key_name, self.criteria_value) |
| 48 | + |
| 49 | + self.sql_where.append(" FROM ep_subject_episode_t ep ") |
| 50 | + self.sql_where.append( |
| 51 | + " WHERE ep.screening_subject_id = ss.screening_subject_id " |
| 52 | + ) |
| 53 | + |
| 54 | + if episode_type is not None: |
| 55 | + self.sql_where.append( |
| 56 | + f" AND ep.episode_type_id = {episode_type.valid_value_id} " |
| 57 | + ) |
| 58 | + |
| 59 | + if self.criteria_key == SubjectSelectionCriteriaKey.SUBJECT_HAS_AN_OPEN_EPISODE: |
| 60 | + self.sql_where.append(" AND ep.episode_end_date IS NULL ") |
| 61 | + |
| 62 | + self.sql_where.append(" )") |
| 63 | + |
| 64 | + def dump_sql(self): |
| 65 | + return "\n".join(self.sql_where) |
0 commit comments