add dynamodb collection
This commit is contained in:
@@ -1,7 +1,44 @@
|
||||
import boto3
|
||||
import pytest
|
||||
|
||||
import layercake.jsonl as jsonl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
|
||||
table_name = 'pytest'
|
||||
dynamodb_endpoint_url = 'http://127.0.0.1:8000'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dynamodb_client():
|
||||
client = boto3.client('dynamodb', endpoint_url=dynamodb_endpoint_url)
|
||||
client.create_table(
|
||||
AttributeDefinitions=[
|
||||
{'AttributeName': 'id', 'AttributeType': 'S'},
|
||||
{'AttributeName': 'sk', 'AttributeType': 'S'},
|
||||
],
|
||||
TableName=table_name,
|
||||
KeySchema=[
|
||||
{'AttributeName': 'id', 'KeyType': 'HASH'},
|
||||
{'AttributeName': 'sk', 'KeyType': 'RANGE'},
|
||||
],
|
||||
ProvisionedThroughput={
|
||||
'ReadCapacityUnits': 123,
|
||||
'WriteCapacityUnits': 123,
|
||||
},
|
||||
)
|
||||
|
||||
yield client
|
||||
|
||||
client.delete_table(TableName=table_name)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def dynamodb_client():
|
||||
return boto3.client('dynamodb', endpoint_url='http://127.0.0.1:8000')
|
||||
def dynamodb_persistence_layer(dynamodb_client) -> DynamoDBPersistenceLayer:
|
||||
return DynamoDBPersistenceLayer(table_name, dynamodb_client)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def dynamodb_seeds(dynamodb_client):
|
||||
with jsonl.readlines('tests/seeds.jsonl') as lines:
|
||||
for line in lines:
|
||||
dynamodb_client.put_item(TableName=table_name, Item=line)
|
||||
|
||||
2
layercake/tests/seeds.jsonl
Normal file
2
layercake/tests/seeds.jsonl
Normal file
@@ -0,0 +1,2 @@
|
||||
{"update_date": {"S": "2024-02-08T16:42:33.776409-03:00"}, "create_date": {"S": "2019-03-25T00:00:00-03:00"}, "email_verified": {"BOOL": true}, "cognito:sub": {"S": "58efed8d-d276-41a8-8502-4ab8b5a6415e"}, "cpf": {"S": "07879819908"}, "sk": {"S": "0"}, "email": {"S": "sergio@somosbeta.com.br"}, "id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "name": {"S": "S\u00e9rgio Rafael de Siqueira"}, "last_login": {"S": "2024-02-08T20:53:45.818126-03:00"}, "tenant:org_id": {"L": [{"S": "cJtK9SsnJhKPyxESe7g3DG"}, {"S": "edp8njvgQuzNkLx2ySNfAD"}, {"S": "8TVSi5oACLxTiT8ycKPmaQ"}]}}
|
||||
{"email_verified": {"BOOL": true}, "update_date": {"S": "2024-02-08T16:42:33.776409-03:00"}, "create_date": {"S": "2019-03-25T00:00:00-03:00"}, "sk": {"S": "emails#sergio@somosbeta.com.br"}, "id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "email_primary": {"BOOL": true}, "mx_record_exists": {"BOOL": true}, "update_date": {"S": "2023-11-09T12:13:04.308986-03:00"}}
|
||||
@@ -5,6 +5,7 @@ import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBCollection,
|
||||
DynamoDBPersistenceLayer,
|
||||
Key,
|
||||
KeyPair,
|
||||
@@ -37,35 +38,57 @@ def test_keypair():
|
||||
assert KeyPair('123', 'abc') == {'id': '123', 'sk': 'abc'}
|
||||
|
||||
|
||||
def test_transact_write_items(dynamodb_client):
|
||||
user_layer = DynamoDBPersistenceLayer('pytest', dynamodb_client)
|
||||
transact = TransactItems(user_layer.table_name)
|
||||
def test_transact_write_items(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
transact = TransactItems(dynamodb_persistence_layer.table_name)
|
||||
transact.put(item=KeyPair('5OxmMjL-ujoR5IMGegQz', '0'))
|
||||
transact.put(item=KeyPair('cpf', '07879819908'))
|
||||
transact.put(
|
||||
item={
|
||||
'id': '5OxmMjL-ujoR5IMGegQz',
|
||||
'sk': '0',
|
||||
}
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': 'cpf',
|
||||
'sk': '07879819908',
|
||||
}
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': 'email',
|
||||
'sk': 'sergio@somosbeta.com.br',
|
||||
},
|
||||
item=KeyPair('email', 'sergio@somosbeta.com.br'),
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': '5OxmMjL-ujoR5IMGegQz',
|
||||
'sk': 'email:sergio@somosbeta.com.br',
|
||||
},
|
||||
item=KeyPair('5OxmMjL-ujoR5IMGegQz', 'emails#sergio@somosbeta.com.br'),
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
|
||||
with pytest.raises(ClientError):
|
||||
user_layer.transact_write_items(transact)
|
||||
dynamodb_persistence_layer.transact_write_items(transact)
|
||||
|
||||
|
||||
def test_collection(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
collect = DynamoDBCollection(dynamodb_persistence_layer)
|
||||
tenant_item = collect.get_item(
|
||||
key=KeyPair(
|
||||
pk='5OxmMjL-ujoR5IMGegQz',
|
||||
sk=Key('tenant'),
|
||||
),
|
||||
raise_if_missing=False,
|
||||
default={},
|
||||
)
|
||||
assert tenant_item == {}
|
||||
|
||||
email_item = collect.get_item(
|
||||
key=KeyPair(
|
||||
pk='5OxmMjL-ujoR5IMGegQz',
|
||||
sk=Key('sergio@somosbeta.com.br', prefix='emails'),
|
||||
),
|
||||
default={},
|
||||
)
|
||||
assert email_item == {
|
||||
'email_verified': True,
|
||||
'mx_record_exists': True,
|
||||
'sk': 'emails#sergio@somosbeta.com.br',
|
||||
'email_primary': True,
|
||||
'id': '5OxmMjL-ujoR5IMGegQz',
|
||||
'create_date': '2019-03-25T00:00:00-03:00',
|
||||
'update_date': '2023-11-09T12:13:04.308986-03:00',
|
||||
}
|
||||
|
||||
with pytest.raises(DynamoDBCollection.MissingError):
|
||||
collect.get_item(key=KeyPair('5OxmMjL-ujoR5IMGegQz', 'notfound'))
|
||||
|
||||
19
layercake/tests/test_jsonl.py
Normal file
19
layercake/tests/test_jsonl.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import layercake.jsonl as jsonl
|
||||
|
||||
|
||||
def test_readlines():
|
||||
with tempfile.NamedTemporaryFile() as fp:
|
||||
fp.writelines([b'{}\n' for _ in range(4)])
|
||||
fp.seek(0)
|
||||
|
||||
with jsonl.readlines(fp.name) as lines:
|
||||
assert sum(1 for _ in lines) == 4
|
||||
|
||||
with jsonl.readlines(Path('notfound.jsonl')) as lines:
|
||||
assert sum(1 for _ in lines) == 0
|
||||
|
||||
with jsonl.readlines(Path('notfound.jsonl')) as lines:
|
||||
assert list(lines) == []
|
||||
Reference in New Issue
Block a user