renamem orders

This commit is contained in:
2025-10-13 14:31:29 -03:00
parent 8c750e00d0
commit 466ff824dd
60 changed files with 165 additions and 59 deletions

View File

View File

@@ -0,0 +1,76 @@
import os
from dataclasses import dataclass
import jsonlines
import pytest
PYTEST_TABLE_NAME = 'pytest'
PK = 'id'
SK = '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['USER_TABLE'] = PYTEST_TABLE_NAME
os.environ['COURSE_TABLE'] = PYTEST_TABLE_NAME
os.environ['ENROLLMENT_TABLE'] = PYTEST_TABLE_NAME
os.environ['ORDER_TABLE'] = PYTEST_TABLE_NAME
os.environ['LOG_LEVEL'] = 'DEBUG'
os.environ['BUCKET_NAME'] = 'saladeaula.digital'
@dataclass
class LambdaContext:
function_name: str = 'test'
memory_limit_in_mb: int = 128
invoked_function_arn: str = 'arn:aws:lambda:eu-west-1:809313241:function:test'
aws_request_id: str = '52fdfc07-2182-154f-163f-5f0f9a621d72'
@pytest.fixture
def lambda_context() -> LambdaContext:
return LambdaContext()
@pytest.fixture
def dynamodb_client():
from boto3clients import dynamodb_client as client
client.create_table(
AttributeDefinitions=[
{'AttributeName': PK, 'AttributeType': 'S'},
{'AttributeName': SK, 'AttributeType': 'S'},
],
TableName=PYTEST_TABLE_NAME,
KeySchema=[
{'AttributeName': PK, 'KeyType': 'HASH'},
{'AttributeName': SK, 'KeyType': 'RANGE'},
],
ProvisionedThroughput={
'ReadCapacityUnits': 123,
'WriteCapacityUnits': 123,
},
)
yield client
client.delete_table(TableName=PYTEST_TABLE_NAME)
@pytest.fixture()
def dynamodb_persistence_layer(dynamodb_client):
from layercake.dynamodb import DynamoDBPersistenceLayer
return DynamoDBPersistenceLayer(PYTEST_TABLE_NAME, dynamodb_client)
@pytest.fixture()
def dynamodb_seeds(dynamodb_persistence_layer):
with open('tests/seeds.jsonl', 'rb') as fp:
reader = jsonlines.Reader(fp)
for line in reader.iter(type=dict, skip_invalid=True):
dynamodb_persistence_layer.put_item(item=line)

View File

View File

@@ -0,0 +1,43 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
import events.billing.append_enrollment as app
def test_append_enrollment(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
enrollment_id = '945e8672-1d72-45c6-b76c-ac06aa8b52ab'
event = {
'detail': {
'new_image': {
'id': enrollment_id,
'sk': 'METADATA#SUBSCRIPTION_COVERED',
'billing_day': 6,
'created_at': '2025-07-23T18:09:22.785678-03:00',
'org_id': 'cJtK9SsnJhKPyxESe7g3DG',
}
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
r = dynamodb_persistence_layer.collection.query(
KeyPair('BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG', 'START#2025-05-06#END#2025-06-05')
)
items = r['items']
assert items[0]['sk'] == 'START#2025-05-06#END#2025-06-05#SCHEDULE#AUTO_CLOSE'
assert (
items[1]['sk']
== 'START#2025-05-06#END#2025-06-05#ENROLLMENT#945e8672-1d72-45c6-b76c-ac06aa8b52ab'
)
assert items[2]['sk'] == 'START#2025-05-06#END#2025-06-05'
print(
dynamodb_persistence_layer.collection.get_item(
KeyPair(enrollment_id, 'METADATA#SUBSCRIPTION_COVERED')
)
)

View File

@@ -0,0 +1,69 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
import events.billing.cancel_enrollment as app
from utils import get_billing_period
enrollment_id = '77055ad7-03e1-4b07-98dc-a2f1a90913ba'
event = {
'detail': {
'new_image': {
'id': enrollment_id,
'sk': '0',
'user': {
'id': '5OxmMjL-ujoR5IMGegQz',
'name': 'Sérgio R Siqueira',
},
'course': {
'id': '123',
'name': 'pytest',
},
}
}
}
def test_cancel_enrollment(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
now_ = now()
start_date, end_date = get_billing_period(
billing_day=6,
date_=now_,
)
pk = 'BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG'
sk = 'START#{start}#END#{end}#ENROLLMENT#{enrollment_id}'.format(
start=start_date.isoformat(),
end=end_date.isoformat(),
enrollment_id=enrollment_id,
)
# Add up-to-date enrollment item to billing
dynamodb_persistence_layer.put_item(
item={
'id': pk,
'sk': sk,
'unit_price': 100,
'course': {'id': '123', 'name': 'pytest'},
'user': {'id': '5OxmMjL-ujoR5IMGegQz', 'name': 'Sérgio R Siqueira'},
'enrolled_at': now_,
}
)
event['detail']['new_image']['created_at'] = now_.isoformat()
assert app.lambda_handler(event, lambda_context) # type: ignore
r = dynamodb_persistence_layer.collection.query(KeyPair(pk, sk))
assert len(r['items']) == 2
assert sum(x['unit_price'] for x in r['items']) == 0
def test_cancel_old_enrollment(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event['detail']['new_image']['created_at'] = '2025-06-05T12:13:54.371416+00:00'
assert not app.lambda_handler(event, lambda_context) # type: ignore

View File

@@ -0,0 +1,38 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import (
DynamoDBPersistenceLayer,
SortKey,
TransactKey,
)
import events.billing.close_window as app
def test_close_window(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'keys': {
'id': 'BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG',
'sk': 'START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE',
},
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
r = dynamodb_persistence_layer.collection.get_items(
TransactKey('BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG')
+ SortKey('START#2025-07-01#END#2025-07-31')
+ SortKey('START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE#EXECUTED'),
flatten_top=False,
)
assert 's3_uri' in r['START#2025-07-01#END#2025-07-31']
assert 'created_at' in r['START#2025-07-01#END#2025-07-31']
assert 'updated_at' in r['START#2025-07-01#END#2025-07-31']
assert r['START#2025-07-01#END#2025-07-31']['status'] == 'CLOSED'
assert 'START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE#EXECUTED' in r

View File

@@ -0,0 +1,25 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer
import events.billing.send_email_on_closing as app
def test_send_email_on_closing(
monkeypatch,
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'new_image': {
'id': 'BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG',
'sk': 'START#2025-07-01#END#2025-07-31',
'status': 'CLOSED',
's3_uri': 's3://saladeaula.digital/billing/sample.pdf',
},
}
}
monkeypatch.setattr(app.sesv2_client, 'send_email', lambda *args, **kwargs: ...)
assert app.lambda_handler(event, lambda_context) # type: ignore

View File

@@ -0,0 +1,30 @@
from layercake.dynamodb import PartitionKey
import events.stopgap.remove_slots as app
from ...conftest import LambdaContext
def test_remove_slots(
dynamodb_seeds,
dynamodb_persistence_layer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'new_image': {
'id': '9omWNKymwU5U4aeun6mWzZ',
'sk': 'generated_items',
'create_date': '2024-07-23T20:43:37.303418-03:00',
'status': 'SUCCESS',
'scope': 'MILTI_USER',
}
},
}
assert app.lambda_handler(event, lambda_context) # type: ignore
result = dynamodb_persistence_layer.collection.query(
PartitionKey('vacancies#cJtK9SsnJhKPyxESe7g3DG')
)
assert len(result['items']) == 0

View File

@@ -0,0 +1,24 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
import events.stopgap.set_as_paid as app
def test_set_as_paid(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'new_image': {
'id': '9omWNKymwU5U4aeun6mWzZ',
}
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
doc = dynamodb_persistence_layer.get_item(
key=KeyPair('9omWNKymwU5U4aeun6mWzZ', '0'),
)
assert doc['status'] == 'PAID'

View File

@@ -0,0 +1,27 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, PartitionKey
import events.append_org_id as app
def test_append_org_id(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'new_image': {
'id': '9omWNKymwU5U4aeun6mWzZ',
'cnpj': '15608435000190',
'email': 'sergio@somosbeta.com.br',
}
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
r = dynamodb_persistence_layer.collection.query(
PartitionKey('9omWNKymwU5U4aeun6mWzZ')
)
assert 2 == len(r['items'])

View File

@@ -0,0 +1,27 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
import events.append_user_id as app
def test_append_user_id(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'new_image': {
'id': '9omWNKymwU5U4aeun6mWzZ',
'cpf': '07879819908',
'email': 'sergio@somosbeta.com.br',
}
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
r = dynamodb_persistence_layer.collection.get_item(
KeyPair('9omWNKymwU5U4aeun6mWzZ', '0')
)
assert 'user_id' in r

View File

@@ -0,0 +1,28 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, PartitionKey
import events.remove_slots_if_canceled as app
def test_remove_slots_if_canceled(
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
event = {
'detail': {
'new_image': {
'id': '9omWNKymwU5U4aeun6mWzZ',
'status': 'CANCELED',
'tenant_id': 'cJtK9SsnJhKPyxESe7g3DG',
}
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
r = dynamodb_persistence_layer.collection.query(
PartitionKey('vacancies#cJtK9SsnJhKPyxESe7g3DG')
)
assert len(r['items']) == 0

View File

@@ -0,0 +1,40 @@
// Org
{"id": "cJtK9SsnJhKPyxESe7g3DG", "sk": "metadata#payment_policy", "due_days": 90}
{"id": "cJtK9SsnJhKPyxESe7g3DG", "sk": "metadata#billing_policy", "billing_day": 1, "payment_method": "PIX"}
// Org admins
{"id": "cJtK9SsnJhKPyxESe7g3DG", "sk": "admins#1234", "create_date": "2025-03-12T16:51:52.632897-03:00", "email": "sergio@somosbeta.com.br", "name": "Sérgio R Siqueira"}
// Orders
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "0", "total": 398, "status": "PENDING", "payment_method": "MANUAL", "tenant_id": "cJtK9SsnJhKPyxESe7g3DG"}
{"id": "18f934d8-035a-4ebc-9f8b-6c84782b8c73", "sk": "0", "payment_method": "PAID"}
{"id": "6a60d026-d383-4707-b093-b6eddea1a24e", "sk": "items", "items": [{"id": "a810dd22-56c0-4d9b-8cd2-7e2ee9c45839", "name": "pytest", "quantity": 1, "unit_price": 109}]}
{"id": "a810dd22-56c0-4d9b-8cd2-7e2ee9c45839", "sk": "metadata#betaeducacao", "course_id": "dc1a0428-47bf-4db1-a5da-24be49c9fda6", "create_date": "2025-06-05T12:13:54.371416+00:00"}
// User data
{"id": "5OxmMjL-ujoR5IMGegQz", "sk": "0", "name": "Sérgio R Siqueira"}
{"id": "cnpj", "sk": "15608435000190", "user_id": "cJtK9SsnJhKPyxESe7g3DG"}
{"id": "cpf", "sk": "07879819908", "user_id": "5OxmMjL-ujoR5IMGegQz"}
{"id": "email", "sk": "sergio@somosbeta.com.br", "user_id": "5OxmMjL-ujoR5IMGegQz"}
// Slots
{"id": "vacancies#cJtK9SsnJhKPyxESe7g3DG", "sk": "9omWNKymwU5U4aeun6mWzZ#1"}
{"id": "vacancies#cJtK9SsnJhKPyxESe7g3DG", "sk": "9omWNKymwU5U4aeun6mWzZ#2"}
{"id": "vacancies#cJtK9SsnJhKPyxESe7g3DG", "sk": "9omWNKymwU5U4aeun6mWzZ#3"}
// Enrollments
{"id": "945e8672-1d72-45c6-b76c-ac06aa8b52ab", "sk": "0", "course": {"id": "123", "name": "pytest"}, "user": {"id": "5OxmMjL-ujoR5IMGegQz", "name": "Sérgio R Siqueira"}, "created_at": "2025-06-05T12:13:54.371416+00:00"}
{"id": "945e8672-1d72-45c6-b76c-ac06aa8b52ab", "sk": "author", "name": "Carolina Brand", "user_id": "SMEXYk5MQkKCzknJpxqr8n"}
{"id": "945e8672-1d72-45c6-b76c-ac06aa8b52ab", "sk": "METADATA#SUBSCRIPTION_COVERED", "billing_day": 6, "org_id": "cJtK9SsnJhKPyxESe7g3DG", "created_at": "2025-07-23T18:09:22.785678-03:00"}
{"id": "77055ad7-03e1-4b07-98dc-a2f1a90913ba", "sk": "0", "course": {"id": "123", "name": "pytest"}, "user": {"id": "5OxmMjL-ujoR5IMGegQz", "name": "Sérgio R Siqueira"}, "created_at": "2025-06-05T12:13:54.371416+00:00"}
{"id": "77055ad7-03e1-4b07-98dc-a2f1a90913ba", "sk": "METADATA#SUBSCRIPTION_COVERED", "billing_day": 6, "org_id": "cJtK9SsnJhKPyxESe7g3DG"}
{"id": "77055ad7-03e1-4b07-98dc-a2f1a90913ba", "sk": "canceled", "canceled_at": "2025-08-18T15:41:49.927856-03:00", "author": {"id": "123", "name": "Dexter Holland"}}
// Course
{"id": "123", "sk": "0", "access_period": "360", "cert": {"exp_interval": 360}, "created_at": "2024-12-30T00:33:33.088916-03:00", "metadata__konviva_class_id": "194", "metadata__unit_price": 99, "name": "Direção Defensiva (08 horas)", "tenant_id": "*", "updated_at": "2025-07-24T00:00:24.639003-03:00"}
{"id": "CUSTOM_PRICING#ORG#cJtK9SsnJhKPyxESe7g3DG", "sk": "COURSE#123", "created_at": "2025-07-24T16:10:09.304073-03:00", "unit_price": "79.2"}
// Billing
{"id": "BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG", "sk": "START#2025-07-01#END#2025-07-31", "created_at": "2025-07-24T15:20:52.464244-03:00", "status": "PENDING"}
{"id": "BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG", "sk": "START#2025-07-01#END#2025-07-31#ENROLLMENT#a08c94a2-7ee4-45fd-bfe7-73568c738b8b", "author": {"id": "SMEXYk5MQkKCzknJpxqr8n", "name": "Carolina Brand"}, "course": {"id": "7f7905aa-ec6d-4189-b884-50fa9b1bd0b8", "name": "NR-10 Reciclagem: 08 horas"}, "created_at": "2025-07-24T16:38:33.095216-03:00", "enrolled_at": "2025-07-24T11:26:56.975207-03:00", "unit_price": 169, "user": {"id": "iPWidwn4HsYtikiZD33smV", "name": "William da Silva Nascimento"}}
{"id": "BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG", "sk": "START#2025-07-01#END#2025-07-31#ENROLLMENT#ac09e8da-6cb2-4e31-84e7-238df2647a7a", "author": {"id": "SMEXYk5MQkKCzknJpxqr8n", "name": "Carolina Brand"}, "course": {"id": "7f7905aa-ec6d-4189-b884-50fa9b1bd0b8", "name": "NR-10 Reciclagem: 08 horas"}, "created_at": "2025-07-21T16:38:58.694031-03:00", "enrolled_at": "2025-07-21T11:26:56.913746-03:00", "unit_price": 169, "user": {"id": "ca8c9fca-b508-4842-8a48-fd5cc5632ac0", "name": "Geovane Soares De Lima"}}

View File

@@ -0,0 +1,64 @@
from datetime import date
from utils import get_billing_period
def test_get_billing_period():
assert get_billing_period(
billing_day=15,
date_=date(2023, 3, 20),
) == (date(2023, 3, 15), date(2023, 4, 14))
assert get_billing_period(
billing_day=15,
date_=date(2023, 3, 10),
) == (date(2023, 2, 15), date(2023, 3, 14))
# Leap year
assert get_billing_period(
billing_day=30,
date_=date(year=2028, month=2, day=1),
) == (
date(2028, 1, 30),
date(2028, 2, 28),
)
# Leap year
assert get_billing_period(
billing_day=29,
date_=date(year=2028, month=3, day=1),
) == (
date(2028, 2, 29),
date(2028, 3, 28),
)
assert get_billing_period(
billing_day=28,
date_=date(year=2025, month=2, day=28),
) == (
date(2025, 2, 28),
date(2025, 3, 27),
)
assert get_billing_period(
billing_day=5,
date_=date(year=2025, month=12, day=1),
) == (
date(2025, 11, 5),
date(2025, 12, 4),
)
assert get_billing_period(billing_day=25, date_=date(2025, 12, 14)) == (
date(2025, 11, 25),
date(2025, 12, 24),
)
assert get_billing_period(billing_day=25, date_=date(2025, 1, 14)) == (
date(2024, 12, 25),
date(2025, 1, 24),
)
assert get_billing_period(10, date(2024, 1, 5)) == (
date(2023, 12, 10),
date(2024, 1, 9),
)