feat: initial commit

This commit is contained in:
cătălin 2023-12-08 17:00:22 +01:00
commit e8291f4bef
Signed by: catalin
GPG key ID: 0178DF42F43E5FD2
33 changed files with 10300 additions and 0 deletions

0
tests/unit/__init__.py Normal file
View file

74
tests/unit/conftest.py Normal file
View file

@ -0,0 +1,74 @@
import pytest
from polyfactory.factories.pydantic_factory import ModelFactory
from polyfactory.pytest_plugin import register_fixture
from secretsanta.domain import Group, IRepo
from secretsanta.service import (
CreateGroupService,
GetGroupParticipantsService,
GetPairService,
)
from secretsanta.settings import get_config
class TestRepo(IRepo):
def __init__(self):
self.data = {}
async def get(self, group_uuid: str) -> Group | None:
return self.data.get(group_uuid)
async def create(self, group: Group) -> Group:
self.data[group.uuid] = group
return group
@register_fixture
class GroupFactory(ModelFactory[Group]):
__model__ = Group
@classmethod
def participants(cls) -> list[str]:
number_of_participants = cls.__random__.randint(2, 10)
return [cls.__faker__.name() for _ in range(number_of_participants)]
pairs = None
@pytest.fixture()
def group(group_factory):
return group_factory.build()
@pytest.fixture
def repo():
return TestRepo()
@pytest.fixture()
def repo_with_data(repo, group):
group.pairs = group.pair(group.participants)
repo.data[group.uuid] = group
return repo
@pytest.fixture()
def config():
return get_config(
s3_bucket_name="foo", s3_secret_access_key="foo", s3_access_key_id="foo"
)
@pytest.fixture()
def create_group_service(repo, config):
return CreateGroupService(repo=repo, config=config)
@pytest.fixture()
def get_participants_service(repo, config):
return GetGroupParticipantsService(repo=repo, config=config)
@pytest.fixture()
def get_pair_service(repo, config):
return GetPairService(repo=repo, config=config)

13
tests/unit/test_pair.py Normal file
View file

@ -0,0 +1,13 @@
from secretsanta.domain import Group
from tests.unit.conftest import GroupFactory
def test_pair(group):
group.pairs = Group.pair(group.participants)
assert len(group.pairs) == len(
group.participants
), "Number of pairs does not match participants"
for k, v in group.pairs.items():
assert k != v, "Self-pairings are not allowed"

View file

@ -0,0 +1,54 @@
import pytest
from secretsanta.service import participants2url
pytestmark = pytest.mark.anyio
async def test_create_group_service(create_group_service, group, config):
participants = await create_group_service.run(group)
assert (
participants2url(
participants=group.participants,
group_uuid=group.uuid,
server_url=config.server_url,
)
== participants
)
@pytest.mark.usefixtures("repo_with_data")
async def test_create_group_service_returns_existing_group(create_group_service, group, config, group_factory):
new_group = group_factory.build()
new_group.uuid = group.uuid
assert new_group != group
participants = await create_group_service.run(new_group)
assert (
participants2url(
participants=group.participants,
group_uuid=group.uuid,
server_url=config.server_url,
)
== participants
)
@pytest.mark.usefixtures("repo_with_data")
async def test_get_participants(get_participants_service, group, config):
participants = await get_participants_service.run(group.uuid)
assert (
participants2url(
participants=group.participants,
group_uuid=group.uuid,
server_url=config.server_url,
)
== participants
)
@pytest.mark.usefixtures("repo_with_data")
async def test_get_pair(get_pair_service, group):
assert group.pairs
for gifter, giftee in group.pairs.items():
pair = await get_pair_service.run(group.uuid, gifter)
assert pair == giftee