moto/tests/test_dynamodb2/test_dynamodb.py

141 lines
4.7 KiB
Python
Raw Normal View History

2017-05-11 01:58:42 +00:00
import boto3
2021-10-18 19:44:29 +00:00
import sure # noqa # pylint: disable=unused-import
import pytest
2019-10-31 15:44:26 +00:00
2022-03-09 17:57:25 +00:00
from boto3.dynamodb.conditions import Key
from moto import mock_dynamodb2
2013-12-05 11:16:56 +00:00
2019-12-20 19:16:17 +00:00
2022-03-09 17:57:25 +00:00
def test_deprecation_warning():
with pytest.warns(None) as record:
mock_dynamodb2()
str(record[0].message).should.contain(
"Module mock_dynamodb2 has been deprecated, and will be removed in a later release"
2019-12-20 19:16:17 +00:00
)
2019-12-24 18:23:46 +00:00
2022-03-09 17:57:25 +00:00
"""
Copy some basics test from DynamoDB
Verify that the behaviour still works using the 'mock_dynamodb2' decorator
"""
2019-12-24 18:23:46 +00:00
2017-09-22 01:12:11 +00:00
@mock_dynamodb2
def test_basic_projection_expression_using_get_item():
2019-10-31 15:44:26 +00:00
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
# Create the DynamoDB table.
2022-03-09 17:57:25 +00:00
dynamodb.create_table(
2019-10-31 15:44:26 +00:00
TableName="users",
KeySchema=[
2019-10-31 15:44:26 +00:00
{"AttributeName": "forum_name", "KeyType": "HASH"},
{"AttributeName": "subject", "KeyType": "RANGE"},
],
AttributeDefinitions=[
2019-10-31 15:44:26 +00:00
{"AttributeName": "forum_name", "AttributeType": "S"},
{"AttributeName": "subject", "AttributeType": "S"},
],
2019-10-31 15:44:26 +00:00
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
)
2019-10-31 15:44:26 +00:00
table = dynamodb.Table("users")
2019-10-31 15:44:26 +00:00
table.put_item(
Item={"forum_name": "the-key", "subject": "123", "body": "some test message"}
)
2019-10-31 15:44:26 +00:00
table.put_item(
Item={
"forum_name": "not-the-key",
"subject": "123",
"body": "some other test message",
}
)
result = table.get_item(
2019-10-31 15:44:26 +00:00
Key={"forum_name": "the-key", "subject": "123"},
ProjectionExpression="body, subject",
)
2019-10-31 15:44:26 +00:00
result["Item"].should.be.equal({"subject": "123", "body": "some test message"})
# The projection expression should not remove data from storage
2019-10-31 15:44:26 +00:00
result = table.get_item(Key={"forum_name": "the-key", "subject": "123"})
2019-10-31 15:44:26 +00:00
result["Item"].should.be.equal(
{"forum_name": "the-key", "subject": "123", "body": "some test message"}
)
@mock_dynamodb2
2022-03-09 17:57:25 +00:00
def test_condition_expression_with_dot_in_attr_name():
dynamodb = boto3.resource("dynamodb", region_name="us-east-2")
table_name = "Test"
dynamodb.create_table(
2022-03-09 17:57:25 +00:00
TableName=table_name,
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
2022-03-09 17:57:25 +00:00
table = dynamodb.Table(table_name)
2022-03-09 17:57:25 +00:00
email_like_str = "test@foo.com"
2022-03-10 14:39:59 +00:00
record = {"id": "key-0", "first": {email_like_str: {"third": {"VALUE"}}}}
2022-03-09 17:57:25 +00:00
table.put_item(Item=record)
2022-03-09 17:57:25 +00:00
table.update_item(
Key={"id": "key-0"},
UpdateExpression="REMOVE #first.#second, #other",
ExpressionAttributeNames={
"#first": "first",
"#second": email_like_str,
"#third": "third",
"#other": "other",
},
ExpressionAttributeValues={":value": "VALUE", ":one": 1},
ConditionExpression="size(#first.#second.#third) = :one AND contains(#first.#second.#third, :value)",
ReturnValues="ALL_NEW",
2019-11-03 15:33:27 +00:00
)
2022-03-09 17:57:25 +00:00
item = table.get_item(Key={"id": "key-0"})["Item"]
2022-03-10 14:39:59 +00:00
item.should.equal({"id": "key-0", "first": {}})
@mock_dynamodb2
2022-03-09 17:57:25 +00:00
def test_query_filter_boto3():
table_schema = {
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"},
2019-11-03 15:33:27 +00:00
],
2022-03-09 17:57:25 +00:00
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"},
2019-11-03 15:33:27 +00:00
],
2022-03-09 17:57:25 +00:00
}
2022-03-09 17:57:25 +00:00
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
table = dynamodb.create_table(
TableName="test-table", BillingMode="PAY_PER_REQUEST", **table_schema
2019-11-03 15:33:27 +00:00
)
2022-03-09 17:57:25 +00:00
for i in range(0, 3):
2022-04-27 11:58:59 +00:00
table.put_item(Item={"pk": "pk", "sk": "sk-{}".format(i)})
2022-03-09 17:57:25 +00:00
res = table.query(KeyConditionExpression=Key("pk").eq("pk"))
res["Items"].should.have.length_of(3)
2022-03-09 17:57:25 +00:00
res = table.query(KeyConditionExpression=Key("pk").eq("pk") & Key("sk").lt("sk-1"))
res["Items"].should.have.length_of(1)
res["Items"].should.equal([{"pk": "pk", "sk": "sk-0"}])
2019-11-03 15:33:27 +00:00
2022-03-09 17:57:25 +00:00
res = table.query(KeyConditionExpression=Key("pk").eq("pk") & Key("sk").lte("sk-1"))
res["Items"].should.have.length_of(2)
res["Items"].should.equal([{"pk": "pk", "sk": "sk-0"}, {"pk": "pk", "sk": "sk-1"}])
2022-03-09 17:57:25 +00:00
res = table.query(KeyConditionExpression=Key("pk").eq("pk") & Key("sk").gt("sk-1"))
res["Items"].should.have.length_of(1)
res["Items"].should.equal([{"pk": "pk", "sk": "sk-2"}])
2022-03-09 17:57:25 +00:00
res = table.query(KeyConditionExpression=Key("pk").eq("pk") & Key("sk").gte("sk-1"))
res["Items"].should.have.length_of(2)
res["Items"].should.equal([{"pk": "pk", "sk": "sk-1"}, {"pk": "pk", "sk": "sk-2"}])