|
import pytest |
|
import os |
|
import boto3 |
|
|
|
from botocore.exceptions import ClientError |
|
from moto import mock_dynamodb2 |
|
from service import dynamo as test_module |
|
from service.dynamo import keyField, purchase |
|
|
|
table_name = 'purchases-table' |
|
OK = 200 |
|
|
|
|
|
class TestDynamo(object): |
|
def _setup(self): |
|
os.environ['REGION'] = 'us-east-1' |
|
return self._create_table() |
|
|
|
@staticmethod |
|
@mock_dynamodb2 |
|
def _create_table(): |
|
dynamodb = boto3.resource('dynamodb', region_name=os.environ['REGION']) |
|
|
|
table = dynamodb.create_table( |
|
TableName=table_name, |
|
KeySchema=[ |
|
{ |
|
'AttributeName': keyField, |
|
'KeyType': 'HASH' |
|
} |
|
], |
|
AttributeDefinitions=[ |
|
{ |
|
'AttributeName': keyField, |
|
'AttributeType': 'N' |
|
} |
|
], |
|
ProvisionedThroughput={ |
|
'ReadCapacityUnits': 10, |
|
'WriteCapacityUnits': 10 |
|
} |
|
) |
|
|
|
print("Table status:", table.table_status) |
|
return dynamodb, table |
|
|
|
def test_harness(self): |
|
assert 1 == 1 |
|
|
|
@mock_dynamodb2 |
|
def test_get_table(self): |
|
dynamodb, table = self._setup() |
|
|
|
table = test_module.get_table(dynamodb, table_name) |
|
|
|
assert table.table_status == 'ACTIVE' |
|
assert table.table_name == table_name |
|
|
|
@mock_dynamodb2 |
|
def test_get_table_should_fail_if_given_wrong_table(self): |
|
dynamodb, table = self._setup() |
|
bad = "Tabley McTableson" |
|
|
|
table = test_module.get_table(dynamodb, bad) |
|
|
|
with pytest.raises(ClientError): |
|
assert table.table_status == 'ACTIVE' |
|
|
|
@mock_dynamodb2 |
|
def test_get_item(self): |
|
dynamodb, table = self._setup() |
|
table.put_item(Item=test_module.sample_item()) |
|
|
|
resp = test_module.get_item(table, {keyField: 1}) |
|
assert resp == test_module.sample_item() |
|
|
|
@mock_dynamodb2 |
|
def test_put_item(self): |
|
dynamodb, table = self._setup() |
|
|
|
res = test_module.put_item(table, test_module.sample_item()) |
|
assert res == OK |