11import unittest
2- from unittest .mock import patch
2+ from unittest .mock import call , patch
33
44import audit_table
55from common .models .errors import UnhandledAuditTableError
@@ -37,82 +37,29 @@ def test_change_audit_table_status_to_processed_raises(self):
3737 self .assertIn ("fail!" , str (ctx .exception ))
3838 self .mock_logger .error .assert_called_once ()
3939
40- def test_get_record_count_by_message_id_returns_the_record_count (self ):
41- """Test that get_record_count_by_message_id retrieves the integer value of the total record count"""
40+ def test_get_record_count_and_failures_by_message_id_returns_the_record_count_and_failures (self ):
41+ """Test that get_record_count_by_message_id retrieves the integer values of the total record count and
42+ failures"""
4243 test_message_id = "1234"
43-
4444 self .mock_dynamodb_client .get_item .return_value = {
45- "Item" : {"message_id" : {"S" : test_message_id }, "record_count" : {"N" : "1000" }}
45+ "Item" : {"message_id" : {"S" : test_message_id }, "record_count" : {"N" : "1000" }, "records_failed" : { "N" : "5" } }
4646 }
4747
48- self .assertEqual (audit_table .get_record_count_by_message_id (test_message_id ), 1000 )
49-
50- def test_get_record_count_by_message_id_returns_none_if_record_count_not_set (self ):
51- """Test that if the record count has not yet been set on the audit item then None is returned"""
52- test_message_id = "1234"
53-
54- self .mock_dynamodb_client .get_item .return_value = {"Item" : {"message_id" : {"S" : test_message_id }}}
48+ record_count , failed_count = audit_table .get_record_count_and_failures_by_message_id (test_message_id )
5549
56- self .assertIsNone (audit_table .get_record_count_by_message_id (test_message_id ))
50+ self .assertEqual (record_count , 1000 )
51+ self .assertEqual (failed_count , 5 )
5752
58- def test_set_records_succeeded_count (self ):
53+ def test_get_record_count_and_failures_by_message_id_returns_zero_if_values_not_set (self ):
54+ """Test that if the record count has not yet been set on the audit item then zero is returned"""
5955 test_message_id = "1234"
60- self .mock_dynamodb_client .get_item .return_value = {
61- "Item" : {"message_id" : {"S" : test_message_id }, "record_count" : {"N" : "1000" }, "records_failed" : {"N" : "42" }}
62- }
63- audit_table .set_records_succeeded_count (test_message_id )
64- self .mock_dynamodb_client .get_item .assert_called_once ()
65- self .mock_dynamodb_client .update_item .assert_called_once_with (
66- TableName = AUDIT_TABLE_NAME ,
67- Key = {AuditTableKeys .MESSAGE_ID : {"S" : test_message_id }},
68- UpdateExpression = "SET #attribute = :value" ,
69- ExpressionAttributeNames = {"#attribute" : AuditTableKeys .RECORDS_SUCCEEDED },
70- ExpressionAttributeValues = {":value" : {"N" : "958" }},
71- ConditionExpression = "attribute_exists(message_id)" ,
72- ReturnValues = "UPDATED_NEW" ,
73- )
74- self .mock_logger .info .assert_called_once ()
7556
76- def test_set_records_succeeded_count_no_failures (self ):
77- test_message_id = "1234"
78- self .mock_dynamodb_client .get_item .return_value = {
79- "Item" : {"message_id" : {"S" : test_message_id }, "record_count" : {"N" : "1000" }}
80- }
81- audit_table .set_records_succeeded_count (test_message_id )
82- self .mock_dynamodb_client .get_item .assert_called_once ()
83- self .mock_dynamodb_client .update_item .assert_called_once_with (
84- TableName = AUDIT_TABLE_NAME ,
85- Key = {AuditTableKeys .MESSAGE_ID : {"S" : test_message_id }},
86- UpdateExpression = "SET #attribute = :value" ,
87- ExpressionAttributeNames = {"#attribute" : AuditTableKeys .RECORDS_SUCCEEDED },
88- ExpressionAttributeValues = {":value" : {"N" : "1000" }},
89- ConditionExpression = "attribute_exists(message_id)" ,
90- ReturnValues = "UPDATED_NEW" ,
91- )
92- self .mock_logger .info .assert_called_once ()
93-
94- def test_set_records_succeeded_count_no_records (self ):
95- test_message_id = "1234"
9657 self .mock_dynamodb_client .get_item .return_value = {"Item" : {"message_id" : {"S" : test_message_id }}}
97- audit_table .set_records_succeeded_count (test_message_id )
98- self .mock_dynamodb_client .get_item .assert_called_once ()
99- self .mock_dynamodb_client .update_item .assert_called_once_with (
100- TableName = AUDIT_TABLE_NAME ,
101- Key = {AuditTableKeys .MESSAGE_ID : {"S" : test_message_id }},
102- UpdateExpression = "SET #attribute = :value" ,
103- ExpressionAttributeNames = {"#attribute" : AuditTableKeys .RECORDS_SUCCEEDED },
104- ExpressionAttributeValues = {":value" : {"N" : "0" }},
105- ConditionExpression = "attribute_exists(message_id)" ,
106- ReturnValues = "UPDATED_NEW" ,
107- )
108- self .mock_logger .info .assert_called_once ()
10958
110- def test_set_records_succeeded_count_raises (self ):
111- self .mock_dynamodb_client .update_item .side_effect = Exception ("fail!" )
112- with self .assertRaises (UnhandledAuditTableError ) as ctx :
113- audit_table .set_records_succeeded_count ("msg1" )
114- self .assertIn ("fail!" , str (ctx .exception ))
115- self .mock_logger .error .assert_called_once ()
59+ record_count , failed_count = audit_table .get_record_count_and_failures_by_message_id (test_message_id )
60+
61+ self .assertEqual (record_count , 0 )
62+ self .assertEqual (failed_count , 0 )
11663
11764 def test_increment_records_failed_count (self ):
11865 """Checks audit table correctly increments the records_failed count"""
@@ -135,29 +82,61 @@ def test_increment_records_failed_count_raises(self):
13582 self .assertIn ("fail!" , str (ctx .exception ))
13683 self .mock_logger .error .assert_called_once ()
13784
138- def test_set_audit_table_ingestion_end_time (self ):
139- """Checks audit table correctly sets ingestion_end_time to the requested value"""
85+ def test_set_audit_record_success_count_and_end_time (self ):
86+ """Checks audit table correctly sets ingestion_end_time and success count to the requested value"""
14087 test_file_key = "RSV_Vaccinations_v5_X26_20210730T12000000.csv"
14188 test_message_id = "1234"
142- test_end_time = 1627647000
143- audit_table .set_audit_table_ingestion_end_time (test_file_key , test_message_id , test_end_time )
89+ test_end_time = "20251208T14430000"
90+ test_success_count = 5
91+
92+ audit_table .set_audit_record_success_count_and_end_time (
93+ test_file_key , test_message_id , test_success_count , test_end_time
94+ )
95+
14496 self .mock_dynamodb_client .update_item .assert_called_once_with (
14597 TableName = AUDIT_TABLE_NAME ,
14698 Key = {AuditTableKeys .MESSAGE_ID : {"S" : test_message_id }},
147- UpdateExpression = f"SET #{ AuditTableKeys .INGESTION_END_TIME } = :{ AuditTableKeys .INGESTION_END_TIME } " ,
148- ExpressionAttributeNames = {f"#{ AuditTableKeys .INGESTION_END_TIME } " : AuditTableKeys .INGESTION_END_TIME },
149- ExpressionAttributeValues = {f":{ AuditTableKeys .INGESTION_END_TIME } " : {"S" : "20210730T12100000" }},
99+ UpdateExpression = (
100+ f"SET #{ AuditTableKeys .INGESTION_END_TIME } = :{ AuditTableKeys .INGESTION_END_TIME } "
101+ f", #{ AuditTableKeys .RECORDS_SUCCEEDED } = :{ AuditTableKeys .RECORDS_SUCCEEDED } "
102+ ),
103+ ExpressionAttributeNames = {
104+ f"#{ AuditTableKeys .INGESTION_END_TIME } " : AuditTableKeys .INGESTION_END_TIME ,
105+ f"#{ AuditTableKeys .RECORDS_SUCCEEDED } " : AuditTableKeys .RECORDS_SUCCEEDED ,
106+ },
107+ ExpressionAttributeValues = {
108+ f":{ AuditTableKeys .INGESTION_END_TIME } " : {"S" : test_end_time },
109+ f":{ AuditTableKeys .RECORDS_SUCCEEDED } " : {"N" : str (test_success_count )},
110+ },
150111 ConditionExpression = "attribute_exists(message_id)" ,
151- ReturnValues = "UPDATED_NEW" ,
152112 )
153- self .mock_logger .info .assert_called_once ()
113+ self .mock_logger .info .assert_has_calls (
114+ [
115+ call (
116+ "ingestion_end_time for %s file, with message id %s, was successfully updated to %s in the audit table" ,
117+ "RSV_Vaccinations_v5_X26_20210730T12000000.csv" ,
118+ "1234" ,
119+ "20251208T14430000" ,
120+ ),
121+ call (
122+ "records_succeeded for %s file, with message id %s, was successfully updated to %s in the audit table" ,
123+ "RSV_Vaccinations_v5_X26_20210730T12000000.csv" ,
124+ "1234" ,
125+ "5" ,
126+ ),
127+ ]
128+ )
154129
155- def test_set_audit_table_ingestion_end_time_throws_exception_with_invalid_id (self ):
130+ def test_set_audit_record_success_count_and_end_time_throws_exception_with_invalid_id (self ):
156131 test_file_key = "RSV_Vaccinations_v5_X26_20210730T12000000.csv"
157132 test_message_id = "1234"
158- test_end_time = 1627647000
159- self .mock_dynamodb_client .update_item .side_effect = Exception ("fail!" )
133+ test_end_time = "20251208T14430000"
134+ test_success_count = 5
135+ self .mock_dynamodb_client .update_item .side_effect = Exception ("Unhandled error" )
136+
160137 with self .assertRaises (UnhandledAuditTableError ) as ctx :
161- audit_table .set_audit_table_ingestion_end_time (test_file_key , test_message_id , test_end_time )
162- self .assertIn ("fail!" , str (ctx .exception ))
138+ audit_table .set_audit_record_success_count_and_end_time (
139+ test_file_key , test_message_id , test_success_count , test_end_time
140+ )
141+ self .assertIn ("Unhandled error" , str (ctx .exception ))
163142 self .mock_logger .error .assert_called_once ()
0 commit comments