forked from tough-dev-school/python-testing-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
add tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TurkinDV
wants to merge
2
commits into
master
Choose a base branch
from
add_tests
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
add tests #1
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Empty file.
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,43 @@ | ||
| import json | ||
| from typing import Iterator | ||
| from urllib.parse import urljoin | ||
|
|
||
| import factory | ||
| import httpretty | ||
| import pytest | ||
|
|
||
| from server.apps.identity.intrastructure.services.placeholder import ( | ||
| UserResponse, | ||
| ) | ||
| from server.common.django.types import Settings | ||
|
|
||
|
|
||
| class UserResponseFactory(factory.Factory): | ||
| """User response factory for create lead.""" | ||
|
|
||
| id = factory.Sequence(int) | ||
|
|
||
| class Meta(object): | ||
| model = UserResponse | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def create_lead_api_user_response() -> UserResponseFactory: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Фикстуру если она просто что-то возвращает лучше назвать без глагола, то есть lead_api_user_response |
||
| """Generate random user response data.""" | ||
| return UserResponseFactory.create() | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def mock_api_create_lead( | ||
| create_lead_api_user_response: UserResponse, | ||
| settings: Settings, | ||
| ) -> Iterator[UserResponse]: | ||
| """Mock external `/users` calls.""" | ||
| with httpretty.httprettized(): | ||
| httpretty.register_uri( | ||
| method=httpretty.POST, | ||
| body=json.dumps(create_lead_api_user_response.model_dump()), | ||
| uri=urljoin(settings.PLACEHOLDER_API_URL, 'users'), | ||
| ) | ||
| yield create_lead_api_user_response | ||
| assert httpretty.has_request() | ||
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,47 @@ | ||
| from typing import Any, Callable, TypeAlias | ||
|
|
||
| import factory | ||
| import pytest | ||
|
|
||
| from server.apps.identity.models import User | ||
|
|
||
|
|
||
| class UserFactory(factory.django.DjangoModelFactory): | ||
| """Factory model User.""" | ||
|
|
||
| class Meta(object): | ||
| """Indication Django model.""" | ||
|
|
||
| model = User | ||
|
|
||
| class Params(object): # noqa: WPS110 | ||
| superuser = factory.Trait( | ||
| is_superuser=True, | ||
| is_staff=True, | ||
| ) | ||
| enabled = True | ||
|
|
||
| email = factory.Faker('email') | ||
| first_name = factory.Faker('first_name') | ||
| last_name = factory.Faker('last_name') | ||
| date_of_birth = factory.Faker('date_object') | ||
| address = factory.Faker('address') | ||
| job_title = factory.Faker('job') | ||
| phone = factory.Faker('phone_number') | ||
|
|
||
|
|
||
| UserFactoryType: TypeAlias = Callable[[], Callable[[], UserFactory]] | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def user_model_factory() -> UserFactoryType: | ||
| """Returns factory for fake random data for user model.""" | ||
| def factory(**fields: dict[str, Any]) -> Callable[[], UserFactory]: | ||
| return UserFactory.create(**fields) | ||
| return factory | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def user(user_model_factory: UserFactory) -> UserFactory: | ||
| """Model User.""" | ||
| return user_model_factory() |
Empty file.
26 changes: 26 additions & 0 deletions
26
tests/test_apps/test_identity/test_logic/test_usecases/test_user_create_new.py
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,26 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| from server.apps.identity.container import container | ||
| from server.apps.identity.logic.usecases.user_create_new import UserCreateNew | ||
|
|
||
| if TYPE_CHECKING: | ||
| from server.apps.identity.intrastructure.services.placeholder import ( | ||
| UserResponse, | ||
| ) | ||
| from tests.plugins.identity.user_factory import UserFactory | ||
|
|
||
| pytestmark = pytest.mark.django_db | ||
|
|
||
|
|
||
| def test_user_create_new( | ||
| user: 'UserFactory', | ||
| mock_api_create_lead: 'UserResponse', | ||
| ) -> None: | ||
| """Test create user in external service.""" | ||
| user_create_new = container.instantiate(UserCreateNew) | ||
| user_create_new(user) | ||
|
|
||
| user.refresh_from_db() | ||
| assert user.lead_id is not None |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Плагин pytest-django предоставляет фикстуру settings, которая возвращает настройки, поэтому по идее такая фикстура не нужна