add postgres
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from http import HTTPMethod
|
||||
|
||||
import jsonlines
|
||||
import pytest
|
||||
@@ -27,11 +30,83 @@ class LambdaContext:
|
||||
aws_request_id: str = '52fdfc07-2182-154f-163f-5f0f9a621d72'
|
||||
|
||||
|
||||
class HttpApiProxy:
|
||||
def __call__(
|
||||
self,
|
||||
raw_path: str,
|
||||
method: str = HTTPMethod.GET,
|
||||
body: dict = {},
|
||||
*,
|
||||
headers: dict = {},
|
||||
auth_flow_type: str = 'USER_AUTH',
|
||||
queryStringParameters: dict = {},
|
||||
**kwargs,
|
||||
) -> dict:
|
||||
return {
|
||||
'version': '2.0',
|
||||
'routeKey': '$default',
|
||||
'rawPath': raw_path,
|
||||
'rawQueryString': 'parameter1=value1¶meter1=value2¶meter2=value',
|
||||
'cookies': ['cookie1', 'cookie2'],
|
||||
'headers': headers,
|
||||
'queryStringParameters': queryStringParameters,
|
||||
'requestContext': {
|
||||
'accountId': '123456789012',
|
||||
'apiId': 'api-id',
|
||||
'authorizer': {
|
||||
'lambda': {
|
||||
'user': {
|
||||
'name': 'Sérgio R Siqueira',
|
||||
'email': 'sergio@somosbeta.com.br',
|
||||
'email_verified': 'true',
|
||||
'custom:user_id': '5OxmMjL-ujoR5IMGegQz',
|
||||
'sub': 'c4f30dbd-083e-4b84-aa50-c31afe9b9c01',
|
||||
},
|
||||
'auth_flow_type': auth_flow_type,
|
||||
},
|
||||
'jwt': {
|
||||
'claims': {'claim1': 'value1', 'claim2': 'value2'},
|
||||
'scopes': ['scope1', 'scope2'],
|
||||
},
|
||||
},
|
||||
'domainName': 'id.execute-api.us-east-1.amazonaws.com',
|
||||
'domainPrefix': 'id',
|
||||
'http': {
|
||||
'method': str(method),
|
||||
'path': raw_path,
|
||||
'protocol': 'HTTP/1.1',
|
||||
'sourceIp': '192.168.0.1/32',
|
||||
'userAgent': 'agent',
|
||||
},
|
||||
'requestId': 'id',
|
||||
'routeKey': '$default',
|
||||
'stage': '$default',
|
||||
'time': '12/Mar/2020:19:03:58 +0000',
|
||||
'timeEpoch': 1583348638390,
|
||||
},
|
||||
'body': _base64_dict(body),
|
||||
'pathParameters': {'parameter1': 'value1'},
|
||||
'isBase64Encoded': True,
|
||||
'stageVariables': {'stageVariable1': 'value1', 'stageVariable2': 'value2'},
|
||||
}
|
||||
|
||||
|
||||
def _base64_dict(obj: dict = {}) -> str | None:
|
||||
if not obj:
|
||||
return None
|
||||
return base64.b64encode(json.dumps(obj).encode()).decode()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def lambda_context() -> LambdaContext:
|
||||
return LambdaContext()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def http_api_proxy():
|
||||
return HttpApiProxy()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dynamodb_client():
|
||||
from boto3clients import dynamodb_client as client
|
||||
@@ -57,8 +132,29 @@ def dynamodb_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_client):
|
||||
with jsonlines.open('tests/seeds.jsonl') as lines:
|
||||
for line in lines:
|
||||
dynamodb_client.put_item(TableName=PYTEST_TABLE_NAME, Item=line)
|
||||
def seeds(dynamodb_client):
|
||||
from layercake.dynamodb import serialize
|
||||
|
||||
with open('tests/seeds.jsonl', 'rb') as fp:
|
||||
reader = jsonlines.Reader(fp)
|
||||
|
||||
for line in reader.iter(type=dict, skip_invalid=True):
|
||||
dynamodb_client.put_item(
|
||||
TableName=PYTEST_TABLE_NAME,
|
||||
Item=serialize(line),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
import app
|
||||
|
||||
return app
|
||||
|
||||
@@ -3,7 +3,10 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
import events.create_user as app
|
||||
|
||||
|
||||
def test_create_user(dynamodb_client, dynamodb_seeds, lambda_context: LambdaContext):
|
||||
def test_create_user(
|
||||
seeds,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
event = {
|
||||
'detail': {
|
||||
'new_image': {
|
||||
|
||||
@@ -3,7 +3,10 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
import events.enroll as app
|
||||
|
||||
|
||||
def test_enroll(dynamodb_client, dynamodb_seeds, lambda_context: LambdaContext):
|
||||
def test_enroll(
|
||||
seeds,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
event = {
|
||||
'detail': {
|
||||
'new_image': {
|
||||
|
||||
@@ -14,4 +14,4 @@ def test_update_user(lambda_context: LambdaContext):
|
||||
},
|
||||
}
|
||||
|
||||
assert app.lambda_handler(event, lambda_context)
|
||||
assert app.lambda_handler(event, lambda_context) # type: ignore
|
||||
|
||||
@@ -1,2 +1,32 @@
|
||||
{"id": {"S": "123"}, "sk": {"S": "0"}}
|
||||
{"id": {"S": "03ad39c6-055e-48d8-acc8-e11385ed8904"}, "sk": {"S": "konviva"}, "user_id": {"N": "275"}, "class_id": {"N": "26943"}}
|
||||
// Users
|
||||
{"id": "123", "sk": "0"}
|
||||
|
||||
// Enrollments
|
||||
{"id": "197991aa-52e2-4e0c-b2a4-a7c53bcfee02", "sk": "0", "progress": 10, "status": "IN_PROGRESS"}
|
||||
|
||||
{"id": "03ad39c6-055e-48d8-acc8-e11385ed8904", "sk": "0"}
|
||||
{"id": "03ad39c6-055e-48d8-acc8-e11385ed8904", "sk": "konviva", "user_id": 275, "class_id": 26943}
|
||||
|
||||
{"id": "d9da85f2-e09f-472d-9515-3d91d70f1e8a", "sk": "0", "progress": 0, "status": "PENDING"}
|
||||
{"id": "d9da85f2-e09f-472d-9515-3d91d70f1e8a", "sk": "SCHEDULE#REMINDER_NO_ACCESS_AFTER_3_DAYS"}
|
||||
|
||||
{"id": "6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8", "sk": "0", "progress": 80, "status": "IN_PROGRESS", "user": {"id": "123", "name": "Myles Kennedy"}, "course": {"id": "432", "name": "pytest"}, "created_at": "2022-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8", "sk": "METADATA#COURSE", "access_period": 360, "cert": {"exp_interval": 365}, "created_at": "2025-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8", "sk": "METADATA#DEDUPLICATION_WINDOW", "offset_days": 90, "created_at": "2025-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8", "sk": "SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS"}
|
||||
{"id": "6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8", "sk": "SCHEDULE#SET_AS_EXPIRED"}
|
||||
|
||||
{"id": "cc2c3bce-c34a-4e82-aa6c-1a19e70ec5ae", "sk": "0", "progress": 109, "status": "COMPLETED", "user": {"id": "321", "name": "Chester Bennington"}, "course": {"id": "432", "name": "pytest"}, "created_at": "2022-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "cc2c3bce-c34a-4e82-aa6c-1a19e70ec5ae", "sk": "METADATA#COURSE", "access_period": 360, "cert": {"exp_interval": 365}, "created_at": "2025-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "cc2c3bce-c34a-4e82-aa6c-1a19e70ec5ae", "sk": "METADATA#DEDUPLICATION_WINDOW", "offset_days": 90, "created_at": "2025-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "cc2c3bce-c34a-4e82-aa6c-1a19e70ec5ae", "sk": "SCHEDULE#SET_AS_ARCHIVED"}
|
||||
|
||||
{"id": "5db53b35-0bae-4907-afda-a213cb5bf651", "sk": "0", "progress": 0, "status": "PENDING", "user": {"id": "1234", "name": "Michael Jackinson"}, "course": {"id": "432", "name": "pytest"}, "created_at": "2022-04-06T11:07:32.762178-03:00"}
|
||||
{"id": "5db53b35-0bae-4907-afda-a213cb5bf651", "sk": "METADATA#COURSE", "access_period": 360, "cert": {"exp_interval": 365}, "created_at": "2025-04-06T11:07:32.762178-03:00"}
|
||||
|
||||
// To relate with the enrollment
|
||||
{"id": "konviva", "sk": "123", "enrollment_id": "d9da85f2-e09f-472d-9515-3d91d70f1e8a"}
|
||||
{"id": "konviva", "sk": "456", "enrollment_id": "197991aa-52e2-4e0c-b2a4-a7c53bcfee02"}
|
||||
{"id": "konviva", "sk": "567", "enrollment_id": "6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8"}
|
||||
{"id": "konviva", "sk": "899", "enrollment_id": "cc2c3bce-c34a-4e82-aa6c-1a19e70ec5ae"}
|
||||
{"id": "konviva", "sk": "221", "enrollment_id": "5db53b35-0bae-4907-afda-a213cb5bf651"}
|
||||
Reference in New Issue
Block a user