99
1010from manage_breast_screening .notifications .management .commands .create_appointments import (
1111 Command ,
12+ ExtractValidationError ,
1213)
1314from manage_breast_screening .notifications .models import (
1415 ZONE_INFO ,
@@ -60,7 +61,6 @@ def stored_blob_data(prefix_dir: str, filenames: list[str]):
6061 )
6162 yield
6263
63-
6464@pytest .mark .django_db
6565class TestCreateAppointments :
6666 def test_handle_creates_records (self ):
@@ -391,3 +391,54 @@ def test_errors_with_wrong_format_data(self):
391391 Command ().handle (** {"date_str" : today_dirname })
392392
393393 assert Extract .objects .count () == 0
394+
395+ def test_extract_id_not_sequential_previous (self , mock_insights_logger ):
396+ """ Test when an extract is not sequential to the previous extract, a warning is logged """
397+ today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
398+
399+ filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
400+
401+ raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
402+
403+ previous_raw_data = '"NBSSAPPT_HDR"|"00000012"|"20250128"|"170922"|"000001"'
404+
405+ Command ().create_extract (filename , raw_data )
406+
407+ with pytest .raises (ExtractValidationError ) as error :
408+ Command ().validate_extract (filename , previous_raw_data )
409+ assert str (error .value ) == "Extract ID 12 is not sequential to last extract ID 13."
410+
411+ assert Extract .objects .count () == 1
412+
413+
414+ def test_extract_same_bso_and_extract_id (self , mock_insights_logger ):
415+ """ Test when an extract has the same extract id and bso code, a warning is logged """
416+ today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
417+
418+ filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
419+ raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
420+ Command ().create_extract (filename , raw_data )
421+
422+ same_bso_and_extract_id_raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
423+
424+ with pytest .raises (ExtractValidationError ) as error :
425+ Command ().validate_extract (filename , same_bso_and_extract_id_raw_data )
426+ assert str (error .value ) == "Extract ID 13 is not sequential to last extract ID 13."
427+
428+ def test_extract_not_sequential_skipped_extract (self , mock_insights_logger ):
429+ """ Test when an extract is not the next extract in order (i.e. skipped an extract), a warning is logged """
430+ today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
431+ filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
432+
433+ raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
434+
435+ Command ().create_extract (filename , raw_data )
436+
437+ skip_extract_raw_data = '"NBSSAPPT_HDR"|"00000025"|"20250128"|"170922"|"000001"'
438+
439+ with pytest .raises (ExtractValidationError ) as error :
440+ Command ().validate_extract (filename , skip_extract_raw_data )
441+ assert str (error .value ) == "Extract ID 25 is not sequential to last extract ID 13."
442+
443+ assert Extract .objects .count () == 1
444+
0 commit comments