2525@pytest .fixture
2626def mock_document_reference ():
2727 """Create a mock document reference"""
28- doc_ref = Mock ( spec = DocumentReference )
28+ doc_ref = DocumentReference . model_construct ( )
2929 doc_ref .id = "test-doc-id"
3030 doc_ref .nhs_number = "9000000001"
3131 doc_ref .s3_file_key = "original/test-key"
3232 doc_ref .s3_bucket_name = "original-bucket"
3333 doc_ref .file_location = "original-location"
34- doc_ref .virus_scanner_result = None
34+ doc_ref .virus_scanner_result = "CLEAN"
3535 doc_ref .file_size = 1234567890
36- doc_ref .doc_status = "uploading "
36+ doc_ref .doc_status = "preliminary "
3737 doc_ref .version = "1"
3838 doc_ref ._build_s3_location = Mock (
3939 return_value = "s3://test-lg-bucket/9000000001/test-doc-id"
@@ -481,6 +481,8 @@ def test_finalize_and_supersede_with_transaction_with_existing_finals(
481481 new_doc .id = "new-doc-id"
482482 new_doc .nhs_number = "9000000001"
483483 new_doc .doc_status = "final"
484+ new_doc .version = "2"
485+ new_doc .s3_version_id = "test-version-id"
484486
485487 existing_final_doc = Mock (spec = DocumentReference )
486488 existing_final_doc .id = "old-doc-id"
@@ -491,22 +493,12 @@ def test_finalize_and_supersede_with_transaction_with_existing_finals(
491493 existing_final_doc
492494 ]
493495
494- new_doc .model_dump = Mock (
495- return_value = {
496- "VirusScannerResult" : "Clean" ,
497- "DocStatus" : "final" ,
498- "FileLocation" : "s3://bucket/key" ,
499- "FileSize" : 1234 ,
500- "Uploaded" : True ,
501- "Uploading" : False ,
502- }
503- )
504-
505496 mock_build_update = Mock (return_value = {"Update" : "transaction1" })
506497 service .dynamo_service .build_update_transaction_item = mock_build_update
507498
508499 service ._finalize_and_supersede_with_transaction (new_doc )
509500
501+ # Assert fetch was called with the correct parameters
510502 service .document_service .fetch_documents_from_table .assert_called_once_with (
511503 table_name = MOCK_LG_TABLE_NAME ,
512504 index_name = "S3FileKeyIndex" ,
@@ -515,10 +507,31 @@ def test_finalize_and_supersede_with_transaction_with_existing_finals(
515507 query_filter = FinalOrPreliminaryAndNotSuperseded ,
516508 )
517509
510+ # Assert build_update_transaction_item was called twice (once for new doc, once for existing doc)
518511 assert service .dynamo_service .build_update_transaction_item .call_count == 2
512+
513+ # Assert the first call is for the new document with correct fields
514+ first_call = service .dynamo_service .build_update_transaction_item .call_args_list [0 ]
515+ assert first_call [1 ]["table_name" ] == MOCK_LG_TABLE_NAME
516+ assert first_call [1 ]["document_key" ] == {"ID" : new_doc .id }
517+ update_fields = first_call [1 ]["update_fields" ]
518+ assert "S3VersionID" in update_fields
519+ assert update_fields ["S3VersionID" ] == "test-version-id"
520+ assert "DocStatus" in update_fields
521+ assert "FileLocation" in update_fields
522+ assert first_call [1 ]["condition_fields" ] == {"DocStatus" : "preliminary" }
523+
524+ # Assert the second call is for superseding the existing final document
525+ second_call = service .dynamo_service .build_update_transaction_item .call_args_list [1 ]
526+ assert second_call [1 ]["table_name" ] == MOCK_LG_TABLE_NAME
527+ assert second_call [1 ]["document_key" ] == {"ID" : existing_final_doc .id }
528+ assert second_call [1 ]["update_fields" ] == {"Status" : "superseded" , "DocStatus" : "deprecated" }
529+ assert second_call [1 ]["condition_fields" ] == {"DocStatus" : "final" , "Version" : "1" }
530+
531+ # Assert transact_write_items was called with 2 transaction items
519532 service .dynamo_service .transact_write_items .assert_called_once ()
520533 call_args = service .dynamo_service .transact_write_items .call_args [0 ][0 ]
521- assert len (call_args ) == 2 # Two transaction items
534+ assert len (call_args ) == 2
522535
523536
524537def test_finalize_and_supersede_with_transaction_no_existing_docs (
@@ -532,17 +545,6 @@ def test_finalize_and_supersede_with_transaction_no_existing_docs(
532545
533546 service .document_service .fetch_documents_from_table .return_value = []
534547
535- new_doc .model_dump = Mock (
536- return_value = {
537- "VirusScannerResult" : "Clean" ,
538- "DocStatus" : "final" ,
539- "FileLocation" : "s3://bucket/key" ,
540- "FileSize" : 1234 ,
541- "Uploaded" : True ,
542- "Uploading" : False ,
543- }
544- )
545-
546548 mock_build_update = Mock (return_value = {"Update" : "transaction1" })
547549 service .dynamo_service .build_update_transaction_item = mock_build_update
548550
@@ -580,17 +582,6 @@ def test_finalize_and_supersede_with_transaction_multiple_existing(
580582 existing_doc2 ,
581583 ]
582584
583- new_doc .model_dump = Mock (
584- return_value = {
585- "VirusScannerResult" : "Clean" ,
586- "DocStatus" : "final" ,
587- "FileLocation" : "s3://bucket/key" ,
588- "FileSize" : 1234 ,
589- "Uploaded" : True ,
590- "Uploading" : False ,
591- }
592- )
593-
594585 mock_build_update = Mock (return_value = {"Update" : "transaction" })
595586 service .dynamo_service .build_update_transaction_item = mock_build_update
596587
@@ -618,17 +609,6 @@ def test_finalize_and_supersede_with_transaction_skips_same_id(
618609
619610 service .document_service .fetch_documents_from_table .return_value = [existing_doc ]
620611
621- new_doc .model_dump = Mock (
622- return_value = {
623- "VirusScannerResult" : "Clean" ,
624- "DocStatus" : "final" ,
625- "FileLocation" : "s3://bucket/key" ,
626- "FileSize" : 1234 ,
627- "Uploaded" : True ,
628- "Uploading" : False ,
629- }
630- )
631-
632612 mock_build_update = Mock (return_value = {"Update" : "transaction" })
633613 service .dynamo_service .build_update_transaction_item = mock_build_update
634614
@@ -657,17 +637,6 @@ def test_finalize_and_supersede_with_transaction_handles_transaction_cancelled(
657637
658638 service .document_service .fetch_documents_from_table .return_value = []
659639
660- # Mock model_dump
661- new_doc .model_dump = Mock (
662- return_value = {
663- "VirusScannerResult" : "Clean" ,
664- "DocStatus" : "final" ,
665- "FileLocation" : "s3://bucket/key" ,
666- "FileSize" : 1234 ,
667- "Uploaded" : True ,
668- "Uploading" : False ,
669- }
670- )
671640
672641 mock_build_update = Mock (return_value = {"Update" : "transaction" })
673642 service .dynamo_service .build_update_transaction_item = mock_build_update
0 commit comments