moto/tests/test_dynamodb/__init__.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
2.1 KiB
Python
Raw Normal View History

import os
from functools import wraps
from uuid import uuid4
import boto3
2024-01-07 12:03:33 +00:00
from moto import mock_aws
2023-11-04 10:37:32 +00:00
def dynamodb_aws_verified(create_table: bool = True):
"""
Function that is verified to work against AWS.
Can be run against AWS at any time by setting:
MOTO_TEST_ALLOW_AWS_REQUEST=true
2024-01-07 12:03:33 +00:00
If this environment variable is not set, the function runs in a `mock_aws` context.
This decorator will:
- Create a table
- Run the test and pass the table_name as an argument
- Delete the table
"""
2023-11-04 10:37:32 +00:00
def inner(func):
@wraps(func)
def pagination_wrapper():
table_name = "t" + str(uuid4())[0:6]
allow_aws_request = (
os.environ.get("MOTO_TEST_ALLOW_AWS_REQUEST", "false").lower() == "true"
)
if allow_aws_request:
if create_table:
2024-02-11 14:47:34 +00:00
print(f"Test {func} will create DDB Table {table_name}") # noqa
2024-01-07 12:03:33 +00:00
return create_table_and_test(table_name)
2023-11-04 10:37:32 +00:00
else:
return func()
else:
2024-01-07 12:03:33 +00:00
with mock_aws():
2023-11-04 10:37:32 +00:00
if create_table:
2024-01-07 12:03:33 +00:00
return create_table_and_test(table_name)
2023-11-04 10:37:32 +00:00
else:
return func()
2024-01-07 12:03:33 +00:00
def create_table_and_test(table_name):
client = boto3.client("dynamodb", region_name="us-east-1")
2023-11-04 10:37:32 +00:00
client.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 5},
Tags=[{"Key": "environment", "Value": "moto_tests"}],
)
waiter = client.get_waiter("table_exists")
waiter.wait(TableName=table_name)
try:
resp = func(table_name)
finally:
### CLEANUP ###
client.delete_table(TableName=table_name)
return resp
return pagination_wrapper
return inner