Skip to content

Commit 972bec2

Browse files
author
Ubuntu
committed
Upgrating to python 3.13
1 parent f4eabae commit 972bec2

File tree

5 files changed

+64
-40
lines changed

5 files changed

+64
-40
lines changed

python-test-samples/apigw-lambda-dynamodb/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![python: 3.9](https://img.shields.io/badge/Python-3.9-green)](https://img.shields.io/badge/Python-3.9-green)
1+
[![python: 3.13](https://img.shields.io/badge/Python-3.13-green)](https://img.shields.io/badge/Python-3.13-green)
22
[![AWS: DynamoDB](https://img.shields.io/badge/AWS-DynamoDB-blueviolet)](https://img.shields.io/badge/AWS-DynamoDB-blueviolet)
33
[![test: unit](https://img.shields.io/badge/Test-Unit-blue)](https://img.shields.io/badge/Test-Unit-blue)
44
[![test: integration](https://img.shields.io/badge/Test-Integration-yellow)](https://img.shields.io/badge/Test-Integration-yellow)
@@ -86,11 +86,15 @@ The [unit test tear-down](tests/unit/mock_test.py#L61-66) removes the mocked Dyn
8686
To run the unit test, execute the following
8787
```shell
8888
# Run from the project directory serverless-test-samples/python-test-samples/apigw-lambda-dynamodb
89+
# Verify Python version (Should show Python 3.13.x)
90+
91+
python3 --version
92+
pip3 --version
93+
8994
# Create and Activate a Python Virtual Environment
9095
# One-time setup
91-
9296
pip3 install virtualenv
93-
python3 -m venv venv
97+
python3 -m virtualenv venv
9498
source ./venv/bin/activate
9599

96100
# install dependencies

python-test-samples/apigw-lambda-dynamodb/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Resources:
5656
TableName: !Ref DynamoDBTable
5757
CodeUri: src/
5858
Handler: app.lambda_handler
59-
Runtime: python3.9
59+
Runtime: python3.13
6060
Architectures:
6161
- x86_64
6262
Environment:

python-test-samples/apigw-lambda-dynamodb/tests/integration/test_api_gateway.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def setUp(self) -> None:
3939
"""
4040
stack_name = TestApiGateway.get_stack_name()
4141

42-
client = boto3.client("cloudformation")
42+
client = boto3.client("cloudformation", region_name=self.aws_region)
4343

4444
try:
4545
response = client.describe_stacks(StackName=stack_name)
@@ -63,41 +63,64 @@ def setUp(self) -> None:
6363
# Using unique id's per unit test will isolate test data
6464
self.id_postfix = "_" + str(uuid4())
6565

66-
6766
# 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
7477

7578
def tearDown(self) -> None:
7679
"""
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.
7982
"""
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")
90101

91102
def test_api_gateway_200(self):
92103
"""
93104
Call the API Gateway endpoint and check the response for a 200
94105
"""
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
97114

98115
def test_api_gateway_404(self):
99116
"""
100117
Call the API Gateway endpoint and check the response for a 404 (id not found)
101118
"""
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
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
pytest
1+
pytest>=7.0.0
22
pytest-mock
3-
boto3
4-
moto
5-
aws_lambda_powertools
3+
moto>=4.0.0
4+
boto3>=1.26.0
5+
aws-lambda-powertools>=2.0.0
66
aws-xray-sdk
77
fastjsonschema
8-
pyyaml
8+
requests>=2.28.0
9+
pyyaml>=6.0
910
uuid

python-test-samples/apigw-lambda-dynamodb/tests/unit/mock_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
import yaml
1111
import boto3
1212
from boto3.dynamodb.conditions import Key
13-
import moto
13+
from moto import mock_aws # Changed from import moto
1414

1515

1616
# Import the handler under test
1717
from src import app
1818

1919
# Mock the DynamoDB Service during the test
20-
@moto.mock_dynamodb
21-
20+
@mock_aws # Changed from @moto.mock_dynamodb
2221
class TestSampleLambdaWithDynamoDB(TestCase):
2322
"""
2423
Unit Test class for src/app.py
@@ -136,7 +135,4 @@ def test_lambda_handler_notfound_path(self):
136135
# Check the log entry item
137136
for item in id_items["Items"]:
138137
self.assertEqual(item["data"], "NOTFOUND: Name Not Found for ID TEST002" + self.id_postfix)
139-
self.assertEqual(item["SK"][0:11], "DT#" + datetime.now().strftime("%Y%m%d"))
140-
141-
142-
138+
self.assertEqual(item["SK"][0:11], "DT#" + datetime.now().strftime("%Y%m%d"))

0 commit comments

Comments
 (0)