improve get items
This commit is contained in:
@@ -4,13 +4,18 @@ import boto3
|
||||
import jsonlines
|
||||
import pytest
|
||||
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
PYTEST_TABLE_NAME = 'pytest'
|
||||
DYNAMODB_ENDPOINT_URL = 'http://localhost:8000'
|
||||
PK = 'id'
|
||||
SK = 'sk'
|
||||
|
||||
PYTEST_TABLE_NAME = os.getenv('PYTEST_TABLE_NAME', 'pytest')
|
||||
# Check `pytest.ini` for more details.
|
||||
DYNAMODB_ENDPOINT_URL = os.getenv('DYNAMODB_ENDPOINT_URL')
|
||||
PK = os.getenv('DYNAMODB_PARTITION_KEY', 'pk')
|
||||
SK = os.getenv('DYNAMODB_SORT_KEY', 'sk')
|
||||
|
||||
# https://docs.pytest.org/en/7.1.x/reference/reference.html#pytest.hookspec.pytest_configure
|
||||
def pytest_configure():
|
||||
os.environ['TZ'] = 'America/Sao_Paulo'
|
||||
os.environ['DYNAMODB_PARTITION_KEY'] = PK
|
||||
os.environ['DYNAMODB_SORT_KEY'] = SK
|
||||
os.environ['PYTEST_TABLE_NAME'] = PYTEST_TABLE_NAME
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -38,7 +43,9 @@ def dynamodb_client():
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def dynamodb_persistence_layer(dynamodb_client) -> DynamoDBPersistenceLayer:
|
||||
def dynamodb_persistence_layer(dynamodb_client):
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
|
||||
return DynamoDBPersistenceLayer(PYTEST_TABLE_NAME, dynamodb_client)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
{"id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "0"}, "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"}, "email": {"S": "sergio@somosbeta.com.br"}, "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"}]}}
|
||||
{"id": {"S": "5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "emails#sergio@somosbeta.com.br"}, "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"}, "email_primary": {"BOOL": true}, "mx_record_exists": {"BOOL": true}, "update_date": {"S": "2023-11-09T12:13:04.308986-03:00"}}
|
||||
{"id": {"S": "cpf"}, "sk": {"S": "07879819908"}, "user_id": {"S": "5OxmMjL-ujoR5IMGegQz"}}
|
||||
{"id": {"S": "email"}, "sk": {"S": "osergiosiqueira@gmail.com"}, "user_id": {"S": "5OxmMjL-ujoR5IMGegQz"}}
|
||||
{"id": {"S": "logs#5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "2024-02-08T16:42:33.776409-03:00"}, "action": {"S": "OPEN_EMAIL"}}
|
||||
{"id": {"S": "logs#5OxmMjL-ujoR5IMGegQz"}, "sk": {"S": "2019-03-25T00:00:00-03:00"}, "action": {"S": "CLICK_EMAIL"}}
|
||||
{"id": {"S": "cJtK9SsnJhKPyxESe7g3DG"}, "sk": {"S": "0"}, "name": {"S": "EDUSEG"}, "cnpj": {"S": "15608435000190"}, "email": {"S": "org+15608435000190@users.noreply.betaeducacao.com.br"}}
|
||||
|
||||
@@ -119,7 +119,7 @@ def test_collection_get_item(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
collect = DynamoDBCollection(dynamodb_persistence_layer)
|
||||
collect = dynamodb_persistence_layer.collect
|
||||
data_notfound = collect.get_item(
|
||||
KeyPair(
|
||||
pk='5OxmMjL-ujoR5IMGegQz',
|
||||
@@ -157,6 +157,24 @@ def test_collection_get_item(
|
||||
)
|
||||
|
||||
|
||||
def test_collection_get_item_path_spec(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
collect = dynamodb_persistence_layer.collect
|
||||
|
||||
# This data was added from seeds
|
||||
data = collect.get_item(
|
||||
KeyPair(
|
||||
pk='5OxmMjL-ujoR5IMGegQz',
|
||||
sk=ComposeKey('sergio@somosbeta.com.br', prefix='emails'),
|
||||
),
|
||||
'mx_record_exists',
|
||||
default={},
|
||||
)
|
||||
assert data
|
||||
|
||||
|
||||
def test_collection_put_item(
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
@@ -181,7 +199,6 @@ def test_collection_put_item(
|
||||
assert data['sk'] == 'orgs#6d1044d5-18c5-437c-9219-fc2ace7e5ebc'
|
||||
assert 'name' in data
|
||||
assert 'ttl' in data
|
||||
assert 'ttl_date' in data
|
||||
|
||||
|
||||
def test_collection_delete_item(
|
||||
@@ -291,3 +308,66 @@ def test_collection_get_items_unflatten(
|
||||
},
|
||||
'payment_policy': {'due_days': Decimal('90')},
|
||||
}
|
||||
|
||||
|
||||
def test_collection_get_items_pair(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
collect = DynamoDBCollection(dynamodb_persistence_layer)
|
||||
doc = collect.get_items(
|
||||
KeyPair('5OxmMjL-ujoR5IMGegQz', '0')
|
||||
+ KeyPair('cpf', '07879819908')
|
||||
+ KeyPair('email', 'osergiosiqueira@gmail.com')
|
||||
)
|
||||
|
||||
assert doc == {
|
||||
'tenant:org_id': [
|
||||
'cJtK9SsnJhKPyxESe7g3DG',
|
||||
'edp8njvgQuzNkLx2ySNfAD',
|
||||
'8TVSi5oACLxTiT8ycKPmaQ',
|
||||
],
|
||||
'email_verified': True,
|
||||
'last_login': '2024-02-08T20:53:45.818126-03:00',
|
||||
'sk': '0',
|
||||
'cpf': {'user_id': '5OxmMjL-ujoR5IMGegQz'},
|
||||
'name': 'Sérgio Rafael de Siqueira',
|
||||
'id': '5OxmMjL-ujoR5IMGegQz',
|
||||
'create_date': '2019-03-25T00:00:00-03:00',
|
||||
'cognito:sub': '58efed8d-d276-41a8-8502-4ab8b5a6415e',
|
||||
'update_date': '2024-02-08T16:42:33.776409-03:00',
|
||||
'email': {'user_id': '5OxmMjL-ujoR5IMGegQz'},
|
||||
}
|
||||
|
||||
|
||||
def test_collection_get_items_pair_unflatten(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
collect = DynamoDBCollection(dynamodb_persistence_layer)
|
||||
doc = collect.get_items(
|
||||
KeyPair('5OxmMjL-ujoR5IMGegQz', '0')
|
||||
+ KeyPair('cpf', '07879819908')
|
||||
+ KeyPair('email', 'osergiosiqueira@gmail.com'),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
assert doc == {
|
||||
'5OxmMjL-ujoR5IMGegQz': {
|
||||
'tenant:org_id': [
|
||||
'cJtK9SsnJhKPyxESe7g3DG',
|
||||
'edp8njvgQuzNkLx2ySNfAD',
|
||||
'8TVSi5oACLxTiT8ycKPmaQ',
|
||||
],
|
||||
'email_verified': True,
|
||||
'last_login': '2024-02-08T20:53:45.818126-03:00',
|
||||
'cpf': '07879819908',
|
||||
'name': 'Sérgio Rafael de Siqueira',
|
||||
'create_date': '2019-03-25T00:00:00-03:00',
|
||||
'cognito:sub': '58efed8d-d276-41a8-8502-4ab8b5a6415e',
|
||||
'update_date': '2024-02-08T16:42:33.776409-03:00',
|
||||
'email': 'sergio@somosbeta.com.br',
|
||||
},
|
||||
'cpf': {'user_id': '5OxmMjL-ujoR5IMGegQz'},
|
||||
'email': {'user_id': '5OxmMjL-ujoR5IMGegQz'},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user