update conftest

This commit is contained in:
2025-03-21 12:29:05 -03:00
parent b22644a165
commit 36b97b1291
8 changed files with 76 additions and 43 deletions

View File

@@ -7,7 +7,12 @@ requires-python = ">=3.12"
dependencies = ["layercake"] dependencies = ["layercake"]
[dependency-groups] [dependency-groups]
dev = ["pytest>=8.3.4", "pytest-cov>=6.0.0", "ruff>=0.9.1"] dev = [
"pytest>=8.3.4",
"pytest-cov>=6.0.0",
"pytest-env>=1.1.5",
"ruff>=0.9.1",
]
[tool.pytest.ini_options] [tool.pytest.ini_options]
addopts = "--cov --cov-report html -v" addopts = "--cov --cov-report html -v"

5
http-api/pytest.ini Normal file
View File

@@ -0,0 +1,5 @@
[pytest]
env =
DYNAMODB_ENDPOINT_URL=http://127.0.0.1:8000
DYNAMODB_PARTITION_KEY=id
DYNAMODB_SORT_KEY=sk

View File

@@ -36,6 +36,7 @@ Globals:
COURSE_TABLE: !Ref CourseTable COURSE_TABLE: !Ref CourseTable
ELASTIC_CLOUD_ID: "{{resolve:ssm:/betaeducacao/elastic/cloud_id/str}}" ELASTIC_CLOUD_ID: "{{resolve:ssm:/betaeducacao/elastic/cloud_id/str}}"
ELASTIC_AUTH_PASS: "{{resolve:ssm:/betaeducacao/elastic/auth_pass/str}}" ELASTIC_AUTH_PASS: "{{resolve:ssm:/betaeducacao/elastic/auth_pass/str}}"
DYNAMODB_PARTITION_KEY: id
Resources: Resources:
HttpLog: HttpLog:

View File

@@ -1,15 +1,17 @@
import base64 import base64
import json import json
import os
from dataclasses import dataclass from dataclasses import dataclass
from http import HTTPMethod from http import HTTPMethod
from typing import Generator
import boto3 import boto3
import pytest import pytest
from layercake.dynamodb import DynamoDBPersistenceLayer from layercake.dynamodb import DynamoDBPersistenceLayer
table_name = 'pytest' PYTEST_TABLE_NAME = os.getenv('PYTEST_TABLE_NAME', 'pytest')
dynamodb_endpoint_url = 'http://127.0.0.1:8000' DYNAMODB_ENDPOINT_URL = os.getenv('DYNAMODB_ENDPOINT_URL')
PK = os.getenv('DYNAMODB_PARTITION_KEY', 'pk')
SK = os.getenv('DYNAMODB_SORT_KEY', 'sk')
@dataclass @dataclass
@@ -94,22 +96,16 @@ def http_api_proxy():
@pytest.fixture @pytest.fixture
def dynamodb_client(): def dynamodb_client():
return boto3.client('dynamodb', endpoint_url=dynamodb_endpoint_url) client = boto3.client('dynamodb', endpoint_url=DYNAMODB_ENDPOINT_URL)
client.create_table(
@pytest.fixture()
def dynamodb_persistence_layer(
dynamodb_client,
) -> Generator[DynamoDBPersistenceLayer, None, None]:
dynamodb_client.create_table(
AttributeDefinitions=[ AttributeDefinitions=[
{'AttributeName': 'id', 'AttributeType': 'S'}, {'AttributeName': PK, 'AttributeType': 'S'},
{'AttributeName': 'sk', 'AttributeType': 'S'}, {'AttributeName': SK, 'AttributeType': 'S'},
], ],
TableName=table_name, TableName=PYTEST_TABLE_NAME,
KeySchema=[ KeySchema=[
{'AttributeName': 'id', 'KeyType': 'HASH'}, {'AttributeName': PK, 'KeyType': 'HASH'},
{'AttributeName': 'sk', 'KeyType': 'RANGE'}, {'AttributeName': SK, 'KeyType': 'RANGE'},
], ],
ProvisionedThroughput={ ProvisionedThroughput={
'ReadCapacityUnits': 123, 'ReadCapacityUnits': 123,
@@ -117,9 +113,14 @@ def dynamodb_persistence_layer(
}, },
) )
yield DynamoDBPersistenceLayer(table_name, dynamodb_client) yield client
dynamodb_client.delete_table(TableName=table_name) client.delete_table(TableName=PYTEST_TABLE_NAME)
@pytest.fixture()
def dynamodb_persistence_layer(dynamodb_client) -> DynamoDBPersistenceLayer:
return DynamoDBPersistenceLayer(PYTEST_TABLE_NAME, dynamodb_client)
@pytest.fixture @pytest.fixture

15
http-api/uv.lock generated
View File

@@ -353,6 +353,7 @@ dependencies = [
dev = [ dev = [
{ name = "pytest" }, { name = "pytest" },
{ name = "pytest-cov" }, { name = "pytest-cov" },
{ name = "pytest-env" },
{ name = "ruff" }, { name = "ruff" },
] ]
@@ -363,6 +364,7 @@ requires-dist = [{ name = "layercake", directory = "../layercake" }]
dev = [ dev = [
{ name = "pytest", specifier = ">=8.3.4" }, { name = "pytest", specifier = ">=8.3.4" },
{ name = "pytest-cov", specifier = ">=6.0.0" }, { name = "pytest-cov", specifier = ">=6.0.0" },
{ name = "pytest-env", specifier = ">=1.1.5" },
{ name = "ruff", specifier = ">=0.9.1" }, { name = "ruff", specifier = ">=0.9.1" },
] ]
@@ -444,6 +446,7 @@ requires-dist = [
dev = [ dev = [
{ name = "pytest", specifier = ">=8.3.5" }, { name = "pytest", specifier = ">=8.3.5" },
{ name = "pytest-cov", specifier = ">=6.0.0" }, { name = "pytest-cov", specifier = ">=6.0.0" },
{ name = "pytest-env", specifier = ">=1.1.5" },
{ name = "ruff", specifier = ">=0.11.1" }, { name = "ruff", specifier = ">=0.11.1" },
] ]
@@ -638,6 +641,18 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/36/3b/48e79f2cd6a61dbbd4807b4ed46cb564b4fd50a76166b1c4ea5c1d9e2371/pytest_cov-6.0.0-py3-none-any.whl", hash = "sha256:eee6f1b9e61008bd34975a4d5bab25801eb31898b032dd55addc93e96fcaaa35", size = 22949 }, { url = "https://files.pythonhosted.org/packages/36/3b/48e79f2cd6a61dbbd4807b4ed46cb564b4fd50a76166b1c4ea5c1d9e2371/pytest_cov-6.0.0-py3-none-any.whl", hash = "sha256:eee6f1b9e61008bd34975a4d5bab25801eb31898b032dd55addc93e96fcaaa35", size = 22949 },
] ]
[[package]]
name = "pytest-env"
version = "1.1.5"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "pytest" },
]
sdist = { url = "https://files.pythonhosted.org/packages/1f/31/27f28431a16b83cab7a636dce59cf397517807d247caa38ee67d65e71ef8/pytest_env-1.1.5.tar.gz", hash = "sha256:91209840aa0e43385073ac464a554ad2947cc2fd663a9debf88d03b01e0cc1cf", size = 8911 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/de/b8/87cfb16045c9d4092cfcf526135d73b88101aac83bc1adcf82dfb5fd3833/pytest_env-1.1.5-py3-none-any.whl", hash = "sha256:ce90cf8772878515c24b31cd97c7fa1f4481cd68d588419fd45f10ecaee6bc30", size = 6141 },
]
[[package]] [[package]]
name = "python-dateutil" name = "python-dateutil"
version = "2.9.0.post0" version = "2.9.0.post0"

View File

@@ -11,10 +11,10 @@ from botocore.exceptions import ClientError
from .dateutils import now, timestamp from .dateutils import now, timestamp
TZ = os.getenv('TZ', 'UTC') TZ = os.getenv('TZ', 'UTC')
DELIMITER = os.getenv('DELIMITER', '#') PK = os.getenv('DYNAMODB_PARTITION_KEY', 'pk')
LIMIT = int(os.getenv('LIMIT', 25)) SK = os.getenv('DYNAMODB_SORT_KEY', 'sk')
PARTITION_KEY = os.getenv('PARTITION_KEY', 'pk') LIMIT = int(os.getenv('DYNAMODB_LIMIT', 25))
SORT_KEY = os.getenv('SORT_KEY', 'sk') DELIMITER = os.getenv('DYNAMODB_DELIMITER', '#')
logger = Logger(__name__) logger = Logger(__name__)
@@ -85,20 +85,20 @@ class PartitionKey(PrimaryKey):
"""Represents a partition key for DynamoDB queries""" """Represents a partition key for DynamoDB queries"""
def __init__(self, pk: str) -> None: def __init__(self, pk: str) -> None:
super().__init__(**{PARTITION_KEY: pk}) super().__init__(**{PK: pk})
def expr_attr_name(self) -> dict: def expr_attr_name(self) -> dict:
return {'#pk': PARTITION_KEY} return {'#pk': PK}
def expr_attr_values(self) -> dict: def expr_attr_values(self) -> dict:
return {':pk': self[PARTITION_KEY]} return {':pk': self[PK]}
class KeyPair(PrimaryKey): class KeyPair(PrimaryKey):
"""Represents a composite key (partition key and sort key) for DynamoDB queries""" """Represents a composite key (partition key and sort key) for DynamoDB queries"""
def __init__(self, pk: str, sk: str) -> None: def __init__(self, pk: str, sk: str) -> None:
super().__init__(**{PARTITION_KEY: pk, SORT_KEY: sk}) super().__init__(**{PK: pk, SK: sk})
def __repr__(self) -> str: def __repr__(self) -> str:
pk, sk = self.values() pk, sk = self.values()
@@ -106,14 +106,14 @@ class KeyPair(PrimaryKey):
def expr_attr_name(self) -> dict: def expr_attr_name(self) -> dict:
return { return {
'#pk': PARTITION_KEY, '#pk': PK,
'#sk': SORT_KEY, '#sk': SK,
} }
def expr_attr_values(self) -> dict: def expr_attr_values(self) -> dict:
return { return {
':pk': self[PARTITION_KEY], ':pk': self[PK],
':sk': self[SORT_KEY], ':sk': self[SK],
} }

View File

@@ -1,3 +1,5 @@
[pytest] [pytest]
env = env =
PARTITION_KEY=id DYNAMODB_ENDPOINT_URL=http://127.0.0.1:8000
DYNAMODB_PARTITION_KEY=id
DYNAMODB_SORT_KEY=sk

View File

@@ -1,25 +1,29 @@
import os
import boto3 import boto3
import pytest import pytest
import layercake.jsonl as jsonl import layercake.jsonl as jsonl
from layercake.dynamodb import DynamoDBPersistenceLayer from layercake.dynamodb import DynamoDBPersistenceLayer
table_name = 'pytest' PYTEST_TABLE_NAME = os.getenv('PYTEST_TABLE_NAME', 'pytest')
dynamodb_endpoint_url = 'http://127.0.0.1:8000' DYNAMODB_ENDPOINT_URL = os.getenv('DYNAMODB_ENDPOINT_URL')
PK = os.getenv('DYNAMODB_PARTITION_KEY', 'pk')
SK = os.getenv('DYNAMODB_SORT_KEY', 'sk')
@pytest.fixture @pytest.fixture
def dynamodb_client(): def dynamodb_client():
client = boto3.client('dynamodb', endpoint_url=dynamodb_endpoint_url) client = boto3.client('dynamodb', endpoint_url=DYNAMODB_ENDPOINT_URL)
client.create_table( client.create_table(
AttributeDefinitions=[ AttributeDefinitions=[
{'AttributeName': 'id', 'AttributeType': 'S'}, {'AttributeName': PK, 'AttributeType': 'S'},
{'AttributeName': 'sk', 'AttributeType': 'S'}, {'AttributeName': SK, 'AttributeType': 'S'},
], ],
TableName=table_name, TableName=PYTEST_TABLE_NAME,
KeySchema=[ KeySchema=[
{'AttributeName': 'id', 'KeyType': 'HASH'}, {'AttributeName': PK, 'KeyType': 'HASH'},
{'AttributeName': 'sk', 'KeyType': 'RANGE'}, {'AttributeName': SK, 'KeyType': 'RANGE'},
], ],
ProvisionedThroughput={ ProvisionedThroughput={
'ReadCapacityUnits': 123, 'ReadCapacityUnits': 123,
@@ -29,16 +33,16 @@ def dynamodb_client():
yield client yield client
client.delete_table(TableName=table_name) client.delete_table(TableName=PYTEST_TABLE_NAME)
@pytest.fixture() @pytest.fixture()
def dynamodb_persistence_layer(dynamodb_client) -> DynamoDBPersistenceLayer: def dynamodb_persistence_layer(dynamodb_client) -> DynamoDBPersistenceLayer:
return DynamoDBPersistenceLayer(table_name, dynamodb_client) return DynamoDBPersistenceLayer(PYTEST_TABLE_NAME, dynamodb_client)
@pytest.fixture() @pytest.fixture()
def dynamodb_seeds(dynamodb_client): def dynamodb_seeds(dynamodb_client):
with jsonl.readlines('tests/seeds.jsonl') as lines: with jsonl.readlines('tests/seeds.jsonl') as lines:
for line in lines: for line in lines:
dynamodb_client.put_item(TableName=table_name, Item=line) dynamodb_client.put_item(TableName=PYTEST_TABLE_NAME, Item=line)