@@ -59,7 +59,25 @@ def stored_blob_data(prefix_dir: str, filenames: list[str]):
5959 mock_blob_contents
6060 )
6161 yield
62+
63+ @contextmanager
64+ def mocked_blob_storage_contents (prefix_dir : str , filename : str , contents : str ):
65+ with mocked_blob_storage () as mock_blob_storage :
66+ mock_container_client = (
67+ mock_blob_storage .return_value .find_or_create_container .return_value
68+ )
69+ mock_blobs = []
70+ mock_blob_contents = []
71+ mock_blob = Mock (spec = BlobProperties )
72+ mock_blob .name = f"{ prefix_dir } /{ filename } "
73+ mock_blobs .append (mock_blob )
74+ mock_blob_contents = contents
6275
76+ mock_container_client .list_blobs .return_value = mock_blobs
77+ mock_container_client .get_blob_client ().download_blob ().readall .side_effect = (
78+ mock_blob_contents
79+ )
80+ yield
6381
6482@pytest .mark .django_db
6583class TestCreateAppointments :
@@ -391,3 +409,55 @@ def test_errors_with_wrong_format_data(self):
391409 Command ().handle (** {"date_str" : today_dirname })
392410
393411 assert Extract .objects .count () == 0
412+
413+ def test_extract_id_not_sequential_previous (self , mock_insights_logger ):
414+ """ Test when an extract is not sequential to the previous extract, a warning is logged """
415+ today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
416+
417+ first_filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
418+
419+ raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
420+
421+ previous_raw_data = '"NBSSAPPT_HDR"|"00000012"|"20250128"|"170922"|"000001"'
422+
423+ # add previous extract so we can test the non sequential extract
424+ Command ().create_extract (first_filename , raw_data )
425+
426+ with mocked_blob_storage_contents (today_dirname , "ANOTHER_FILE_NAME.dat" , previous_raw_data ):
427+ with pytest .raises (CommandError ) as error :
428+ Command ().handle (** {"date_str" : today_dirname })
429+ assert str (error .value ) == "Extract ID 12 is not sequential to last extract ID 13."
430+
431+ assert Extract .objects .count () == 1
432+
433+
434+ def test_extract_same_bso_and_extract_id (self , mock_insights_logger ):
435+ """ Test when an extract has the same extract id and bso code, a warning is logged """
436+ today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
437+
438+ filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
439+ raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
440+ Command ().create_extract (filename , raw_data )
441+
442+ same_bso_and_extract_id_raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
443+
444+ with mocked_blob_storage_contents (today_dirname , "ANOTHER_FILE_NAME.dat" , same_bso_and_extract_id_raw_data ):
445+ with pytest .raises (CommandError ) as error :
446+ Command ().handle (** {"date_str" : today_dirname })
447+ assert str (error .value ) == "Extract ID 13 is not sequential to last extract ID 13."
448+
449+ def test_extract_not_sequential_skipped_extract (self , mock_insights_logger ):
450+ """ Test when an extract is not the next extract in order (i.e. skipped an extract), a warning is logged """
451+ today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
452+ filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
453+
454+ raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
455+
456+ Command ().create_extract (filename , raw_data )
457+
458+ skip_extract_raw_data = '"NBSSAPPT_HDR"|"00000025"|"20250128"|"170922"|"000001"'
459+
460+ with mocked_blob_storage_contents (today_dirname , "ANOTHER_FILE_NAME.dat" , skip_extract_raw_data ):
461+ with pytest .raises (CommandError ) as error :
462+ Command ().handle (** {"date_str" : today_dirname })
463+ assert str (error .value ) == "Extract ID 25 is not sequential to last extract ID 13."
0 commit comments