Skip to content

Commit 3a3586f

Browse files
Added logic to check and correct for duplicate records. (#55)
Adjusted existing compartment tests to use this logic <!-- markdownlint-disable-next-line first-line-heading --> ## Description <!-- Describe your changes in detail. --> Made C5 only select distinct NHS Numbers when obtaining test data ## Context <!-- Why is this change required? What problem does it solve? --> Fixed an issue in compartment 5 where it wold fail due to obtaining duplicate results. ## Type of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply. --> - [x] Refactoring (non-breaking change) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I am familiar with the [contributing guidelines](https://github.com/nhs-england-tools/playwright-python-blueprint/blob/main/CONTRIBUTING.md) - [x] I have followed the code style of the project - [x] I have added tests to cover my changes (where appropriate) - [ ] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.
1 parent 8044806 commit 3a3586f

File tree

2 files changed

+61
-31
lines changed

2 files changed

+61
-31
lines changed

utils/oracle/oracle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def connect_to_db(self) -> oracledb.Connection:
2929
return conn
3030
except Exception as queryExecutionError:
3131
logging.error(
32-
f"Failed to to extract subject ID with error: {queryExecutionError}"
32+
f"Failed to extract subject ID with error: {queryExecutionError}"
3333
)
3434

3535
def disconnect_from_db(self, conn: oracledb.Connection) -> None:
@@ -147,15 +147,15 @@ def delete_all_users_from_approved_users_table(
147147
self.disconnect_from_db(conn)
148148

149149
def execute_query(
150-
self, query: str, parameters: list | None = None
150+
self, query: str, parameters: dict | None = None
151151
) -> pd.DataFrame: # To use when "select xxxx" (stored procedures)
152152
"""
153153
This is used to execute any sql queries.
154154
A query is provided and then the result is returned as a pandas dataframe
155155
156156
Args:
157157
query (str): The SQL query you wish to run
158-
parameters (list | None): Optional - Any parameters you want to pass on in a list
158+
parameters (dict | None): Optional - Any parameters you want to pass on in a dictionary
159159
160160
Returns:
161161
df (pd.DataFrame): A pandas dataframe of the result of the query

utils/oracle/oracle_specific_functions.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,30 @@ def get_kit_id_from_db(
2929
kit_id_df (pd.DataFrame): A pandas DataFrame containing the result of the query
3030
"""
3131
logging.info("Retrieving useable test kit ids")
32-
kit_id_df = OracleDB().execute_query(
33-
f"""select tk.kitid, tk.screening_subject_id, sst.subject_nhs_number
32+
query = """select tk.kitid, tk.screening_subject_id, sst.subject_nhs_number
3433
from tk_items_t tk
3534
inner join ep_subject_episode_t se on se.screening_subject_id = tk.screening_subject_id
3635
inner join screening_subject_t sst on (sst.screening_subject_id = tk.screening_subject_id)
3736
inner join sd_contact_t sdc on (sdc.nhs_number = sst.subject_nhs_number)
38-
where tk.tk_type_id = {tk_type_id}
37+
where tk.tk_type_id = :tk_type_id
3938
and tk.logged_in_flag = 'N'
40-
and sdc.hub_id = {hub_id}
39+
and sdc.hub_id = :hub_id
4140
and device_id is null
4241
and tk.invalidated_date is null
43-
and se.latest_event_status_id in ({SqlQueryValues.S10_EVENT_STATUS}, {SqlQueryValues.S19_EVENT_STATUS})
42+
and se.latest_event_status_id in (:s10_event_status, :s19_event_status)
4443
order by tk.kitid DESC
45-
fetch first {no_of_kits_to_retrieve} rows only"""
46-
)
44+
fetch first :subjects_to_retrieve rows only"""
45+
46+
params = {
47+
"s10_event_status": SqlQueryValues.S10_EVENT_STATUS,
48+
"s19_event_status": SqlQueryValues.S19_EVENT_STATUS,
49+
"tk_type_id": tk_type_id,
50+
"hub_id": hub_id,
51+
"subjects_to_retrieve": no_of_kits_to_retrieve,
52+
}
53+
54+
kit_id_df = OracleDB().execute_query(query, params)
55+
4756
return kit_id_df
4857

4958

@@ -83,22 +92,29 @@ def get_kit_id_logged_from_db(smokescreen_properties: dict) -> pd.DataFrame:
8392
Returns:
8493
return kit_id_df (pd.DataFrame): A pandas DataFrame containing the result of the query
8594
"""
86-
kit_id_df = OracleDB().execute_query(
87-
f"""SELECT tk.kitid,tk.device_id,tk.screening_subject_id
95+
query = """SELECT tk.kitid,tk.device_id,tk.screening_subject_id
8896
FROM tk_items_t tk
8997
INNER JOIN kit_queue kq ON kq.device_id = tk.device_id
9098
INNER JOIN ep_subject_episode_t se ON se.screening_subject_id = tk.screening_subject_id
9199
WHERE tk.logged_in_flag = 'Y'
92100
AND kq.test_kit_status IN ('LOGGED', 'POSTED')
93-
AND se.episode_status_id = {SqlQueryValues.OPEN_EPISODE_STATUS_ID}
101+
AND se.episode_status_id = :open_episode_status_id
94102
AND tk.tk_type_id = 2
95-
AND se.latest_event_status_id = {SqlQueryValues.S43_EVENT_STATUS}
96-
AND tk.logged_in_at = {smokescreen_properties["c3_fit_kit_results_test_org_id"]}
103+
AND se.latest_event_status_id = :s43_event_status
104+
AND tk.logged_in_at = :logged_in_at
97105
AND tk.reading_flag = 'N'
98106
AND tk.test_results IS NULL
99-
fetch first {smokescreen_properties["c3_total_fit_kits_to_retrieve"]} rows only
107+
fetch first :subjects_to_retrieve rows only
100108
"""
101-
)
109+
110+
params = {
111+
"s43_event_status": SqlQueryValues.S43_EVENT_STATUS,
112+
"logged_in_at": smokescreen_properties["c3_fit_kit_results_test_org_id"],
113+
"open_episode_status_id": SqlQueryValues.OPEN_EPISODE_STATUS_ID,
114+
"subjects_to_retrieve": smokescreen_properties["c3_total_fit_kits_to_retrieve"],
115+
}
116+
117+
kit_id_df = OracleDB().execute_query(query, params)
102118

103119
return kit_id_df
104120

@@ -263,25 +279,31 @@ def get_subjects_for_appointments(subjects_to_retrieve: int) -> pd.DataFrame:
263279
Returns:
264280
subjects_df (pd.DataFrame): A pandas DataFrame containing the result of the query
265281
"""
266-
subjects_df = OracleDB().execute_query(
267-
f"""
268-
select tk.kitid, ss.subject_nhs_number, se.screening_subject_id
282+
283+
query = """select tk.kitid, ss.subject_nhs_number, se.screening_subject_id
269284
from tk_items_t tk
270285
inner join ep_subject_episode_t se on se.screening_subject_id = tk.screening_subject_id
271286
inner join screening_subject_t ss on ss.screening_subject_id = se.screening_subject_id
272287
inner join sd_contact_t c on c.nhs_number = ss.subject_nhs_number
273-
where se.latest_event_status_id = {SqlQueryValues.A8_EVENT_STATUS}
288+
where se.latest_event_status_id = :a8_event_status
274289
and tk.logged_in_flag = 'Y'
275-
and se.episode_status_id = {SqlQueryValues.OPEN_EPISODE_STATUS_ID}
290+
and se.episode_status_id = :open_episode_status_id
276291
and ss.screening_status_id != 4008
277292
and tk.logged_in_at = 23159
278293
and c.hub_id = 23159
279294
and tk.tk_type_id = 2
280295
and tk.datestamp > add_months(sysdate,-24)
281296
order by ss.subject_nhs_number desc
282-
fetch first {subjects_to_retrieve} rows only
297+
fetch first :subjects_to_retrieve rows only
283298
"""
284-
)
299+
params = {
300+
"a8_event_status": SqlQueryValues.A8_EVENT_STATUS,
301+
"open_episode_status_id": SqlQueryValues.OPEN_EPISODE_STATUS_ID,
302+
"subjects_to_retrieve": subjects_to_retrieve,
303+
}
304+
305+
subjects_df = OracleDB().execute_query(query, params)
306+
285307
return subjects_df
286308

287309

@@ -297,9 +319,8 @@ def get_subjects_with_booked_appointments(subjects_to_retrieve: int) -> pd.DataF
297319
Returns:
298320
subjects_df (pd.DataFrame): A pandas DataFrame containing the result of the query
299321
"""
300-
subjects_df = OracleDB().execute_query(
301-
f"""
302-
select a.appointment_date, s.subject_nhs_number, c.person_family_name, c.person_given_name
322+
323+
query = """select distinct(s.subject_nhs_number), a.appointment_date, c.person_family_name, c.person_given_name
303324
from
304325
(select count(*), ds.screening_subject_id
305326
from
@@ -318,10 +339,11 @@ def get_subjects_with_booked_appointments(subjects_to_retrieve: int) -> pd.DataF
318339
inner join screening_subject_t s on s.screening_subject_id = se.screening_subject_id
319340
inner join sd_contact_t c on c.nhs_number = s.subject_nhs_number
320341
inner join appointment_t a on se.subject_epis_id = a.subject_epis_id
321-
where se.latest_event_status_id = {SqlQueryValues.POSITIVE_APPOINTMENT_BOOKED}
342+
where se.latest_event_status_id = :positive_appointment_booked
322343
and tk.logged_in_flag = 'Y'
323-
and se.episode_status_id = {SqlQueryValues.OPEN_EPISODE_STATUS_ID}
344+
and se.episode_status_id = :open_episode_status_id
324345
and tk.logged_in_at = 23159
346+
and tk.algorithm_sc_id = 23162
325347
--and a.appointment_date > sysdate-27
326348
and a.cancel_date is null
327349
and a.attend_info_id is null and a.attend_date is null
@@ -330,7 +352,15 @@ def get_subjects_with_booked_appointments(subjects_to_retrieve: int) -> pd.DataF
330352
and tk.tk_type_id = 2
331353
--and tk.datestamp > add_months(sysdate,-24)
332354
order by a.appointment_date desc
333-
fetch first {subjects_to_retrieve} rows only
355+
fetch first :subjects_to_retrieve rows only
334356
"""
335-
)
357+
358+
params = {
359+
"positive_appointment_booked": SqlQueryValues.POSITIVE_APPOINTMENT_BOOKED,
360+
"open_episode_status_id": SqlQueryValues.OPEN_EPISODE_STATUS_ID,
361+
"subjects_to_retrieve": subjects_to_retrieve,
362+
}
363+
364+
subjects_df = OracleDB().execute_query(query, params)
365+
336366
return subjects_df

0 commit comments

Comments
 (0)