【发布时间】:2022-01-17 11:25:01
【问题描述】:
我有一个使用 3 个 dynamodb 表的函数 create。如何模拟三个 Dynamo 数据库表?
def create():
//This function uses a dynamodb table "x"
// Then it calls my_table() function
def my_table():
// This function basically uses two dynamodb table "y" and "z"
// This function returns a value which is used in create() function.
我的测试文件有以下代码 -
@patch.dict(os.environ, {"DYNAMODB_TABLE": "x",
'second_TABLE': "y",
'Third_TABLE': "z"
})
def test_create():
dynamodb_test()
event = { // my event values}
result = create(event)
assert result == 200
def dynamodb_test():
with mock_dynamodb2():
dynamodb = boto3.client('dynamodb', region_name='us-east-1')
dynamodb.create_table(
TableName=os.environ["DYNAMODB_TABLE"],
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)
yield dynamodb
每当我使用 pytest 测试 test_create() 函数时,我都会得到
botocore.exceptions.ClientError: 发生错误 (ExpiredTokenException) 时 调用扫描操作:请求中包含的安全令牌已过期
我认为它试图访问实际的 aws dynamo db,但我希望它使用模拟 dynamodb。我怎样才能做到这一点?
【问题讨论】:
标签: amazon-web-services amazon-dynamodb moto