This repository was archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Feature/python testing homework #57
Open
Hyper-glitch
wants to merge
60
commits into
tough-dev-school:master
Choose a base branch
from
Hyper-glitch:feature/python_testing_homework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
91b3d52
feta: add mimesis to poetry
63829d4
feta: add tests for placeholder module
a7bb432
feta: add protocols module
98b438d
feta: add fixtures for tests
1b84666
refactor: move files
2d45717
feat: add mock settings
0693c44
refactor: move protocols
3ae8d67
refactor: remove extra fixture
941e2f4
refactor: del typehint to resole import error
f8fcbc4
feat: add tests for user_create_new.py
873ed29
refactor: rename new_ids to response, because its UserResponse
577859c
style: change double quotes to single
2c77850
style: fix pipeline comments
f2cfe57
wtf
21330d0
feat: add test for url_path method
75f0952
feat: add init for tests
0f446e9
feat: add type annotation
188743d
feat: move protocols to plugins
89faa6e
feat: move protocols to plugins
e815cd0
feat: add annotations
e970b74
feat: change import path
7f513e9
feat: unuse plugin
b5f96f1
feat: move to plugins
0337705
feat: add test for _UserManager
889f180
refactor: change type path
b20eb6a
refactor: move types to plugins
3f7952d
refactor: add docstrings and fix pipeline issues
764029b
refactor: try to fix pipeline issues
49ecefb
style: use isort
747684d
feat: add isort lib
f8f5bb9
fix: try to fix pipelin
e07740b
fix: try to fix pipeline
257a5a9
fix: try to fix pipeline
ff273a3
fix: try to fix pipeline
7946c12
fix: try to fix pipeline
fa37aab
fix: try to fix pipeline
d66bc12
fix: try to fix pipeline
abe1699
fix: try to fix pipeline
8e7449b
fix: try to fix pipeline
c72efe1
refactor: remove extra files
94e3ca8
feat: registrate a new plugin
d94f101
refactor: make assertion simple
8d1e7fc
refactor: move user fixture to plugin
c2f0b81
feat: add httpretty to poetry
42b901a
feat: add new fixtures for tests
a140df8
feat: add assert not lead user
613da1e
feat: change deprecated method
f522e09
feat: add fixtures for picture tests
dfd0357
feat: add new plugins picture
6143d15
feat: add pictures data factory, picture types and fixtures for testing
4c32e52
feat: add tests for pictures
5606ac5
feat: add failed_pydantic_fields fixture and import test_success_vali…
f842aa0
feat: make dit hierarchy more flatten
6c02234
refactor: mar django test for whole module
6e8e430
feat: add tests for user views
4f243a0
feat: add auth client fixture
9958c85
feat: reg new plugin for mark slow tests
1c25f00
feat: add pytest options
941971d
feat: add slow test plugin
949c222
feat: mark slow tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import datetime as dt | ||
| from typing import Any, Unpack, TYPE_CHECKING | ||
| from typing import Callable, Protocol, TypeAlias, TypedDict, final | ||
|
|
||
| import pytest | ||
| from mimesis import Field, Schema | ||
| from mimesis.locales import Locale | ||
|
|
||
| from server.apps.identity.intrastructure.services.placeholder import UserResponse | ||
| from server.apps.identity.models import User | ||
|
|
||
| if TYPE_CHECKING: | ||
| from django.test import Client | ||
|
|
||
|
|
||
| @final | ||
| class UserData(TypedDict, total=False): | ||
| """ | ||
| Represent the user data that is required to create a new user. | ||
| Does not include `password`, because it's special field in Django. | ||
| """ | ||
|
|
||
| email: str | ||
| first_name: str | ||
| last_name: str | ||
| date_of_birth: dt.datetime | ||
| address: str | ||
| job_title: str | ||
| phone: str | ||
| lead_id: int | ||
| is_staff: bool | ||
| is_active: bool | ||
|
|
||
|
|
||
| UserAssertion: TypeAlias = Callable[[str, UserData], None] | ||
| NotLeadUserAssertion: TypeAlias = Callable[[str], None] | ||
|
|
||
|
|
||
| @final | ||
| class RegistrationData(UserData, total=False): | ||
| """Represent the user data that is required to create a new user.""" | ||
|
|
||
| password_first: str | ||
| password_second: str | ||
|
|
||
|
|
||
| @final | ||
| class RegistrationDataFactory(Protocol): | ||
| """User data factory protocol.""" | ||
|
|
||
| def __call__(self, **fields: Unpack[RegistrationData]) -> RegistrationData: | ||
| """User data factory protocol.""" | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def registration_data_factory() -> RegistrationDataFactory: | ||
| """Fxture that generate fake registration data.""" | ||
|
|
||
| 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: | ||
| """Fixture that check created user attrs from database.""" | ||
|
|
||
| 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_name in expected.items(): | ||
| assert getattr(user, field) == value_name | ||
|
|
||
| return factory | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def reg_data(registration_data_factory) -> RegistrationData: | ||
| """Fixture that return user reg data.""" | ||
| return registration_data_factory(seed=1) | ||
Hyper-glitch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def expected_user_data(reg_data: RegistrationData) -> dict[str, Any]: | ||
| """Fixture that return exeected user data.""" | ||
| return { | ||
| key: value_name | ||
| for key, value_name in reg_data.items() | ||
| if not key.startswith('password') | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def expected_serialized_user(reg_data: RegistrationData) -> dict[str, Any]: | ||
| """Serialized user's key-values that expected in test.""" | ||
| return { | ||
| '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': | ||
| """Return created user in database.""" | ||
| return User.objects.create(**expected_user_data) | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def expected_user_response( | ||
| expected_user_data: RegistrationData, | ||
| ) -> 'UserResponse': | ||
| """Return user response obj.""" | ||
| return UserResponse(id=1) | ||
|
|
||
|
|
||
| @pytest.fixture(scope='session') | ||
| def assert_not_lead_user() -> NotLeadUserAssertion: | ||
| """Check that user is not lead.""" | ||
| def factory(email: str) -> None: | ||
| user = User.objects.get(email=email) | ||
| assert user.lead_id is None | ||
|
|
||
| return factory | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def authorized_client(client: 'Client', user: 'User') -> 'Client': | ||
| """Authorized User object.""" | ||
| client.force_login(user) | ||
| return client | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.