@@ -39,7 +39,7 @@ def setUp(self) -> None:
39
39
"""
40
40
stack_name = TestApiGateway .get_stack_name ()
41
41
42
- client = boto3 .client ("cloudformation" )
42
+ client = boto3 .client ("cloudformation" , region_name = self . aws_region )
43
43
44
44
try :
45
45
response = client .describe_stacks (StackName = stack_name )
@@ -63,41 +63,64 @@ def setUp(self) -> None:
63
63
# Using unique id's per unit test will isolate test data
64
64
self .id_postfix = "_" + str (uuid4 ())
65
65
66
-
67
66
# Seed the DynamoDB Table with Test Data
68
- dynamodb_resource = boto3 .resource ("dynamodb" , region_name = self .aws_region )
69
- dynamodb_table = dynamodb_resource .Table (name = self .dynamodb_table_name )
70
- dynamodb_table .put_item (Item = {"PK" : "TEST001" + self .id_postfix ,
71
- "SK" : "NAME#" ,
72
- "data" : "Unit Test Name Data" })
73
-
67
+ try :
68
+ dynamodb_resource = boto3 .resource ("dynamodb" , region_name = self .aws_region )
69
+ dynamodb_table = dynamodb_resource .Table (name = self .dynamodb_table_name )
70
+ dynamodb_table .put_item (Item = {"PK" : "TEST001" + self .id_postfix ,
71
+ "SK" : "NAME#" ,
72
+ "data" : "Unit Test Name Data" })
73
+ print (f"Successfully seeded test data for TEST001{ self .id_postfix } " )
74
+ except Exception as e :
75
+ print (f"Warning: Could not seed test data: { e } " )
76
+ raise
74
77
75
78
def tearDown (self ) -> None :
76
79
"""
77
- # For tear-down, remove any data injected for the tests
78
- # Take particular care to ensure these values are unique and identifiable as TEST data.
80
+ For tear-down, remove any data injected for the tests
81
+ Take particular care to ensure these values are unique and identifiable as TEST data.
79
82
"""
80
- dynamodb_resource = boto3 .resource ("dynamodb" , region_name = self .aws_region )
81
- dynamodb_table = dynamodb_resource .Table (name = self .dynamodb_table_name )
82
-
83
- for id in ["TEST001" + self .id_postfix ,"TEST002" + self .id_postfix ]:
84
- id_items = dynamodb_table .query (
85
- KeyConditionExpression = Key ('PK' ).eq (id )
86
- )
87
- if "Items" in id_items :
88
- for item in id_items ["Items" ]:
89
- dynamodb_table .delete_item (Key = {"PK" :item ["PK" ],"SK" :item ["SK" ]})
83
+ try :
84
+ dynamodb_resource = boto3 .resource ("dynamodb" , region_name = self .aws_region )
85
+ dynamodb_table = dynamodb_resource .Table (name = self .dynamodb_table_name )
86
+
87
+ for id in ["TEST001" + self .id_postfix ,"TEST002" + self .id_postfix ]:
88
+ try :
89
+ id_items = dynamodb_table .query (
90
+ KeyConditionExpression = Key ('PK' ).eq (id )
91
+ )
92
+ if "Items" in id_items :
93
+ for item in id_items ["Items" ]:
94
+ dynamodb_table .delete_item (Key = {"PK" :item ["PK" ],"SK" :item ["SK" ]})
95
+ print (f"Successfully cleaned up test data for { id } " )
96
+ except Exception as item_error :
97
+ print (f"Could not clean up items for { id } : { item_error } " )
98
+ except Exception as e :
99
+ print (f"Warning: tearDown cleanup failed (this may be a credentials issue): { e } " )
100
+ print ("Test data may remain in DynamoDB - clean up manually if needed" )
90
101
91
102
def test_api_gateway_200 (self ):
92
103
"""
93
104
Call the API Gateway endpoint and check the response for a 200
94
105
"""
95
- response = requests .get (self .api_endpoint .replace ("{id}" ,"TEST001" + self .id_postfix ))
96
- self .assertEqual (response .status_code , requests .codes .ok )
106
+ try :
107
+ response = requests .get (self .api_endpoint .replace ("{id}" ,"TEST001" + self .id_postfix ))
108
+ print (f"API Response Status: { response .status_code } " )
109
+ print (f"API Response Body: { response .text } " )
110
+ self .assertEqual (response .status_code , requests .codes .ok )
111
+ except Exception as e :
112
+ print (f"Test failed with error: { e } " )
113
+ raise
97
114
98
115
def test_api_gateway_404 (self ):
99
116
"""
100
117
Call the API Gateway endpoint and check the response for a 404 (id not found)
101
118
"""
102
- response = requests .get (self .api_endpoint .replace ("{id}" ,"TEST002" + self .id_postfix ))
103
- self .assertEqual (response .status_code , requests .codes .not_found )
119
+ try :
120
+ response = requests .get (self .api_endpoint .replace ("{id}" ,"TEST002" + self .id_postfix ))
121
+ print (f"API Response Status: { response .status_code } " )
122
+ print (f"API Response Body: { response .text } " )
123
+ self .assertEqual (response .status_code , requests .codes .not_found )
124
+ except Exception as e :
125
+ print (f"Test failed with error: { e } " )
126
+ raise
0 commit comments