99
1010from manage_breast_screening .notifications .management .commands .create_appointments import (
1111 Command ,
12- ExtractValidationError ,
1312)
1413from manage_breast_screening .notifications .models import (
1514 ZONE_INFO ,
@@ -60,6 +59,25 @@ def stored_blob_data(prefix_dir: str, filenames: list[str]):
6059 mock_blob_contents
6160 )
6261 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
75+
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 :
@@ -396,18 +414,20 @@ def test_extract_id_not_sequential_previous(self, mock_insights_logger):
396414 """ Test when an extract is not sequential to the previous extract, a warning is logged """
397415 today_dirname = datetime .now ().strftime ("%Y-%m-%d" )
398416
399- filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
417+ first_filename = f"{ today_dirname } /{ VALID_DATA_FILE } "
400418
401419 raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
402420
403421 previous_raw_data = '"NBSSAPPT_HDR"|"00000012"|"20250128"|"170922"|"000001"'
404422
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-
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+
411431 assert Extract .objects .count () == 1
412432
413433
@@ -421,9 +441,10 @@ def test_extract_same_bso_and_extract_id(self, mock_insights_logger):
421441
422442 same_bso_and_extract_id_raw_data = '"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"'
423443
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."
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."
427448
428449 def test_extract_not_sequential_skipped_extract (self , mock_insights_logger ):
429450 """ Test when an extract is not the next extract in order (i.e. skipped an extract), a warning is logged """
@@ -436,9 +457,7 @@ def test_extract_not_sequential_skipped_extract(self, mock_insights_logger):
436457
437458 skip_extract_raw_data = '"NBSSAPPT_HDR"|"00000025"|"20250128"|"170922"|"000001"'
438459
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-
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