Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
feta: add fixtures for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Smirnov committed Sep 16, 2023
1 parent a7bb432 commit 98b438d
Showing 1 changed file with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from typing import Unpack, Any

import pytest
from mimesis import Field, Schema
from mimesis.locales import Locale

from protocols import (
RegistrationDataFactory,
RegistrationData,
UserAssertion,
UserData,
)
from server.apps.identity.models import User


@pytest.fixture()
def registration_data_factory() -> RegistrationDataFactory:
def factory(**fields: Unpack[RegistrationData]) -> RegistrationData:
field = Field(locale=Locale.RU, seed=fields.pop("seed"))
password = field("password")
schema = Schema(schema=lambda: {
"email": field("person.email"),
"first_name": field("person.first_name"),
"last_name": field("person.last_name"),
"date_of_birth": field("datetime.date"),
"address": field("address.city"),
"job_title": field("person.occupation"),
"phone": field("person.telephone"),
})
return {
**schema.create()[0],
**{"password_first": password, "password_second": password},
**fields,
}

return factory


@pytest.fixture(scope="session")
def assert_correct_user() -> UserAssertion:
def factory(email: str, expected: UserData) -> None:
user = User.objects.get(email=email)
assert user.id
assert user.is_active
assert not user.is_superuser
assert not user.is_staff
for field, value in expected.items():
assert getattr(user, field) == value

return factory


@pytest.fixture()
def reg_data(registration_data_factory) -> RegistrationData:
yield registration_data_factory(seed=1)


@pytest.fixture()
def expected_user_data(reg_data: RegistrationData) -> dict[str, Any]:
yield {
key: value for key, value in reg_data.items() if not key.startswith("password")
}


@pytest.fixture()
def expected_serialized_user(reg_data: RegistrationData) -> dict[str, Any]:
yield {
"name": reg_data["first_name"],
"last_name": reg_data["last_name"],
"birthday": reg_data["date_of_birth"].strftime('%d.%m.%Y'),
"city_of_birth": reg_data["address"],
"position": reg_data["job_title"],
"email": reg_data["email"],
"phone": reg_data["phone"],
}


@pytest.fixture()
def user(
expected_user_data: RegistrationData,
) -> User:
yield User.objects.create(**expected_user_data)

0 comments on commit 98b438d

Please sign in to comment.