33from vax_suppliers import TestPair , OdsVax
44from constants import (
55 ActionFlag , BusRowResult , DestinationType , Operation ,
6- create_row ,
7- SOURCE_BUCKET ,
86 ACK_BUCKET ,
9- TEMP_ACK_PREFIX ,
107 RAVS_URI ,
118 OperationOutcome
129)
1310from utils import (
1411 poll_s3_file_pattern , fetch_pk_and_operation_from_dynamodb ,
1512 validate_fatal_error ,
16- delete_file_from_s3 ,
17- delete_filename_from_audit_table ,
18- delete_filename_from_events_table ,
19- get_file_content_from_s3
13+ get_file_content_from_s3 ,
14+ aws_cleanup ,
15+ create_row ,
2016)
2117from clients import logger
2218from errors import DynamoDBMismatchError
@@ -39,18 +35,19 @@ def __init__(self, scenario: dict):
3935 self .description : str = scenario .get ("description" , "" )
4036 self .ods_vax : OdsVax = scenario .get ("ods_vax" )
4137 self .actions : list [TestAction ] = scenario .get ("actions" , [])
42- ods_vax = self .ods_vax
43- self .ods = ods_vax .ods_code
44- self .vax = ods_vax .vax
38+ self .ods = self .ods_vax .ods_code
39+ self .vax = self .ods_vax .vax
4540 self .dose_amount : float = scenario .get ("dose_amount" , 0.5 )
46- self .inject_char = scenario .get ("test_encoding " , False )
41+ self .inject_cp1252 = scenario .get ("create_with_cp1252_encoded_character " , False )
4742 self .header = scenario .get ("header" , "NHS_NUMBER" )
4843 self .version = scenario .get ("version" , 5 )
4944 self .operation_outcome = scenario .get ("operation_outcome" , "" )
50- # initialise attribs to be set later
51- self .ack_keys = {DestinationType .INF : None , DestinationType .BUS : None }
52- self .key = None # TODO is identifier and key the same
5345 self .enabled = scenario .get ("enabled" , False )
46+ self .ack_keys = {DestinationType .INF : None , DestinationType .BUS : None }
47+ # initialise attribs to be set later
48+ self .key = None # S3 key of the uploaded file
49+ self .file_name = None # name of the generated CSV file
50+ self .identifier = None # unique identifier of subject in the CSV file rows
5451
5552 def get_poll_destinations (self , pending : bool ) -> bool :
5653 # loop through keys in test (inf and bus)
@@ -113,14 +110,14 @@ def check_bus_file_content(self):
113110 elif row_HEADER_RESPONSE_CODE == "Fatal Error" :
114111 validate_fatal_error (desc , row , i , operation_outcome )
115112
116- def generate_csv_file_good (self ):
113+ def generate_csv_file (self ):
117114
118115 self .file_name = self .get_file_name (self .vax , self .ods , self .version )
119116 logger .info (f"Test \" { self .name } \" File { self .file_name } " )
120117 data = []
121118 self .identifier = str (uuid .uuid4 ())
122119 for action in self .actions :
123- row = create_row (self .identifier , self .dose_amount , action .action , self .header , self .inject_char )
120+ row = create_row (self .identifier , self .dose_amount , action .action , self .header , self .inject_cp1252 )
124121 logger .info (f" > { action .action } - { self .vax } /{ self .ods } - { self .identifier } " )
125122 data .append (row )
126123 df = pd .DataFrame (data )
@@ -129,48 +126,35 @@ def generate_csv_file_good(self):
129126
130127 def get_file_name (self , vax_type , ods , version = "5" ):
131128 timestamp = datetime .now (timezone .utc ).strftime ("%Y%m%dT%H%M%S00" )
132- # timestamp = timestamp[:-3]
133129 return f"{ vax_type } _Vaccinations_v{ version } _{ ods } _{ timestamp } .csv"
134130
135131 def cleanup (self ):
136- if self .key :
137- archive_file = f"archive/{ self .key } "
138- if not delete_file_from_s3 (SOURCE_BUCKET , archive_file ):
139- logger .warning (f"S3 delete fail { SOURCE_BUCKET } : { archive_file } " )
140- delete_filename_from_audit_table (self .key )
141- delete_filename_from_events_table (self .identifier )
142- for ack_key in self .ack_keys .values ():
143- if ack_key :
144- if not delete_file_from_s3 (ACK_BUCKET , ack_key ):
145- logger .warning (f"s3 delete fail { ACK_BUCKET } : { ack_key } " )
146- # cleanup TEMP_ACK
147- delete_file_from_s3 (ACK_BUCKET , TEMP_ACK_PREFIX )
148-
149-
150- class TestCases :
151- def __init__ (self , test_cases : dict ):
152- self .test_cases = []
153- # scenarios = scenario.get(environment)
154- for s in test_cases :
155- self .test_cases .append (TestCase (s ))
156-
157- def enable_tests (self , names : list [str ]):
158- for name in names :
159- for test in self .test_cases :
160- if test .name == name :
161- test .enabled = True
162- break
163- else :
164- raise Exception (f"Test case with name '{ name } ' not found." )
165-
166- def generate_csv_files_good (self ) -> list [TestCase ]:
167- """Generate CSV files based on a list of Test Rules."""
168- ret = []
169- for seed_data in self .test_cases :
170- if seed_data .enabled :
171- seed_data .generate_csv_file_good ()
172- ret .append (seed_data )
173- return ret
132+ aws_cleanup (self .key , self .identifier , self .ack_keys )
133+
134+
135+ def create_test_cases (test_case_dict : dict ) -> list [TestCase ]:
136+ """Initialize test cases from a dictionary."""
137+ return [TestCase (name ) for name in test_case_dict ]
138+
139+
140+ def enable_tests (test_cases : list [TestCase ], names : list [str ]) -> None :
141+ """Enable only the test cases with the given names."""
142+ for name in names :
143+ for test in test_cases :
144+ if test .name == name :
145+ test .enabled = True
146+ break
147+ else :
148+ raise Exception (f"Test case with name '{ name } ' not found." )
149+
150+
151+ def generate_csv_files (test_cases : list [TestCase ]) -> list [TestCase ]:
152+ """Generate CSV files for all enabled test cases."""
153+ ret = []
154+ for test in test_cases :
155+ if test .enabled :
156+ test .generate_csv_file ()
157+ ret .append (test )
174158
175159
176160scenarios = {
@@ -220,7 +204,7 @@ def generate_csv_files_good(self) -> list[TestCase]:
220204 "ods_vax" : TestPair .YGA_MENACWY_CRUDS ,
221205 "operation_outcome" : ActionFlag .CREATE ,
222206 "actions" : [TestAction (ActionFlag .CREATE )],
223- "test_encoding " : True
207+ "create_with_cp1252_encoded_character " : True
224208 }
225209 ],
226210 "ref" : []
0 commit comments