Skip to content

Instantly share code, notes, and snippets.

@subzero112233
Created September 5, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subzero112233/ef0da6ab5fbc98bf573ee0c7d28657c5 to your computer and use it in GitHub Desktop.
Save subzero112233/ef0da6ab5fbc98bf573ee0c7d28657c5 to your computer and use it in GitHub Desktop.
import pytest
import boto3
from botocore.exceptions import ClientError, ParamValidationError
from moto import mock_dynamodb2
from dynamodb_get_item import get_from_dynamodb
@mock_dynamodb2
def test_get_from_dynamodb():
client = boto3.client("dynamodb", region_name="us-east-1")
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
table_name = 'SanAntonioSpurs-test'
# Create the DynamoDB table.
client.create_table(
TableName=table_name,
AttributeDefinitions=[
{"AttributeName": "PlayerName", "AttributeType": "S"},
{"AttributeName": "JerseyNumber", "AttributeType": "N"},
],
KeySchema=[
{"AttributeName": "PlayerName", "KeyType": "HASH"},
{"AttributeName": "JerseyNumber", "KeyType": "RANGE"},
],
ProvisionedThroughput={"ReadCapacityUnits": 123, "WriteCapacityUnits": 123},
)
client.put_item(
TableName=table_name,
Item={"PlayerName": {"S": "Tony Parker"}, "JerseyNumber": {"N": "9"}, "Position": {"S": "Point Guard"}}
)
# Test that the code raises the relevant exceptions
with pytest.raises(ClientError):
get_from_dynamodb("SanAntonioSpurs-non-existing-table", {"PlayerName": "Bruce Bowen", "Position": "Shooting Guard", "JerseyNumber": 12})
with pytest.raises(ParamValidationError):
get_from_dynamodb(table_name, "intended type mismatch")
# Test that the code works properly
response = get_from_dynamodb(table_name, {"PlayerName": "Tony Parker", "JerseyNumber": 9})
assert response['PlayerName'] == "Tony Parker"
assert response['Position'] == "Point Guard"
assert response['JerseyNumber'] == 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment