@@ -225,3 +225,58 @@ def get_matching_subject(
225225 except Exception :
226226 raise ValueError ("No subject found matching the given criteria" )
227227 return subject
228+
229+ def there_is_letter_batch_for_subject (
230+ self ,
231+ nhs_no : str ,
232+ letter_batch_code : str ,
233+ letter_batch_title : str ,
234+ assertion : bool = True ,
235+ ) -> None :
236+ """
237+ Checks if the subject under test has a specific letter batch assosiated with them.
238+ Args:
239+ nhs_no (str): The subject's NHS number.
240+ letter_batch_code (str): The letter batch code.
241+ letter_batch_title (str): The letter batch title.
242+ assertion (bool): If the subject should have this batch (True), or should not have this batch (False).
243+ """
244+ sql_query = """ SELECT lb.batch_id
245+ FROM lett_batch_records lbr
246+ INNER JOIN lett_batch lb
247+ ON lb.batch_id = lbr.batch_id
248+ INNER JOIN valid_values ld
249+ ON ld.valid_value_id = lb.description_id
250+ INNER JOIN valid_values lbs
251+ ON lbs.valid_value_id = lb.status_id
252+ WHERE lb.batch_state_id = 12018
253+ AND lbr.screening_subject_id = :subject_id
254+ AND lbs.allowed_value = :batch_code
255+ AND LOWER(ld.description) = LOWER(:batch_title)
256+ AND lbr.non_inclusion_id IS NULL
257+ AND lbr.key_id != 11539
258+ """
259+
260+ subject_id = self .oracle_db .get_subject_id_from_nhs_number (nhs_no )
261+
262+ params = {
263+ "subject_id" : subject_id ,
264+ "batch_code" : letter_batch_code ,
265+ "batch_title" : letter_batch_title ,
266+ }
267+
268+ batch_df = self .oracle_db .execute_query (sql_query , params )
269+ if assertion :
270+ assert (
271+ not batch_df .empty
272+ ), f"[DB ASSERTION FAILED] Subject { nhs_no } does not have a { letter_batch_code } - { letter_batch_title } batch when they are expected to"
273+ logging .info (
274+ f"[DB ASSERTION Passed] Subject { nhs_no } has a { letter_batch_code } - { letter_batch_title } batch"
275+ )
276+ else :
277+ assert (
278+ batch_df .empty
279+ ), f"[DB ASSERTION FAILED] Subject { nhs_no } has a { letter_batch_code } - { letter_batch_title } batch when they are expected not to"
280+ logging .info (
281+ f"[DB ASSERTION Passed] Subject { nhs_no } does not have a { letter_batch_code } - { letter_batch_title } batch"
282+ )
0 commit comments