-
Notifications
You must be signed in to change notification settings - Fork 0
feat: TT-418 CRUD customer v2 #359
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 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
feat: TT-418 create tests added
- Loading branch information
commit a3b09deabf452586ac07b208a9980c544dc78452
There are no files selected for viewing
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,49 @@ | ||
| import json | ||
| from faker import Faker | ||
|
|
||
| import azure.functions as func | ||
|
|
||
| import time_tracker.customers._application._customers as azure_customers | ||
|
|
||
| CUSTOMER_URL = "/api/customers/" | ||
|
|
||
|
|
||
| def test__customer_azure_endpoint__creates_a_customer__when_customer_has_all_necesary_attributes( | ||
| customer_factory | ||
| ): | ||
| customer_body = customer_factory().__dict__ | ||
|
|
||
| body = json.dumps(customer_body).encode("utf-8") | ||
| req = func.HttpRequest( | ||
| method='POST', | ||
| body=body, | ||
| url=CUSTOMER_URL, | ||
| ) | ||
|
|
||
| response = azure_customers._create_customer.create_customer(req) | ||
| customer_json_data = json.loads(response.get_body()) | ||
| customer_body['id'] = customer_json_data['id'] | ||
|
|
||
| assert response.status_code == 201 | ||
| assert customer_json_data == customer_body | ||
|
|
||
|
|
||
| def test__customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_necessary_attributes(): | ||
| customer_to_insert = { | ||
| "id": None, | ||
| "name": Faker().user_name(), | ||
| "deleted": False, | ||
| "status": 1 | ||
| } | ||
|
|
||
| body = json.dumps(customer_to_insert).encode("utf-8") | ||
| req = func.HttpRequest( | ||
| method='POST', | ||
| body=body, | ||
| url=CUSTOMER_URL, | ||
| ) | ||
|
|
||
| response = azure_customers._create_customer.create_customer(req) | ||
|
|
||
| assert response.status_code == 400 | ||
| assert response.get_body() == b'Invalid format or structure of the attributes of the customer' |
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # flake8: noqa | ||
| from fixtures import _activity_factory, _test_db, _insert_activity | ||
| from fixtures import _time_entry_factory | ||
| from fixtures import _customer_factory |
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,35 @@ | ||
| import pytest | ||
|
|
||
| import time_tracker.customers._domain as domain | ||
| import time_tracker.customers._infrastructure as infrastructure | ||
| from time_tracker._infrastructure import DB | ||
|
|
||
|
|
||
| @pytest.fixture(name='create_fake_dao') | ||
| def _fake_dao() -> domain.CustomersDao: | ||
| def _create_fake_dao(db_fake: DB) -> domain.CustomersDao: | ||
| dao = infrastructure.CustomersSQLDao(db_fake) | ||
| return dao | ||
| return _create_fake_dao | ||
|
|
||
|
|
||
| @pytest.fixture(name='clean_database', autouse=True) | ||
| def _clean_database(): | ||
| yield | ||
| db_fake = DB() | ||
| dao = infrastructure.CustomersSQLDao(db_fake) | ||
| query = dao.customer.delete() | ||
| dao.db.get_session().execute(query) | ||
|
|
||
|
|
||
| def test__customer_dao__returns_a_customer_dto__when_saves_correctly_with_sql_database( | ||
| test_db, customer_factory, create_fake_dao | ||
| ): | ||
| dao = create_fake_dao(test_db) | ||
|
|
||
| customer_to_insert = customer_factory() | ||
|
|
||
| inserted_customer = dao.create(customer_to_insert) | ||
|
|
||
| assert isinstance(inserted_customer, domain.Customer) | ||
| assert inserted_customer == customer_to_insert |
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,14 @@ | ||
| from time_tracker.customers._domain import CustomerService | ||
|
|
||
|
|
||
| def test__create_customer__uses_the_customer_dao__to_create_a_customer(mocker, customer_factory): | ||
| expected_customer = mocker.Mock() | ||
| customer_dao = mocker.Mock( | ||
| create=mocker.Mock(return_value=expected_customer) | ||
| ) | ||
| customer_service = CustomerService(customer_dao) | ||
|
|
||
| new_customer = customer_service.create(customer_factory()) | ||
|
|
||
| assert customer_dao.create.called | ||
| assert expected_customer == new_customer |
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,18 @@ | ||
| from pytest_mock import MockFixture | ||
|
|
||
| from time_tracker.customers._domain import _use_cases | ||
|
|
||
|
|
||
| def test__create_customer_function__uses_the_customer_service__to_create_a_customer( | ||
| mocker: MockFixture, customer_factory | ||
| ): | ||
| expected_customer = mocker.Mock() | ||
| customer_service = mocker.Mock( | ||
| create=mocker.Mock(return_value=expected_customer) | ||
| ) | ||
|
|
||
| customer_use_case = _use_cases.CreateCustomerUseCase(customer_service) | ||
| new_customer = customer_use_case.create_customer(customer_factory()) | ||
|
|
||
| assert customer_service.create.called | ||
| assert expected_customer == new_customer |
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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| # flake8: noqa | ||
| from ._time_entries import create_customer | ||
| from ._customers import create_customer |
File renamed without changes.
56 changes: 56 additions & 0 deletions
56
V2/time_tracker/customers/_application/_customers/_create_customer.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,56 @@ | ||
| import dataclasses | ||
| import json | ||
|
|
||
| import azure.functions as func | ||
|
|
||
| from ... import _domain | ||
| from ... import _infrastructure | ||
| from time_tracker._infrastructure import DB | ||
|
|
||
| DEFAULT_FIELDS = ["id", "deleted", "status"] | ||
|
|
||
|
|
||
| def create_customer(req: func.HttpRequest) -> func.HttpResponse: | ||
| try: | ||
| database = DB() | ||
| customer_dao = _infrastructure.CustomersSQLDao(database) | ||
| customer_service = _domain.CustomerService(customer_dao) | ||
| use_case = _domain._use_cases.CreateCustomerUseCase(customer_service) | ||
| customer_data = req.get_json() | ||
|
|
||
| customer_is_valid = _validate_customer(customer_data) | ||
| if not customer_is_valid: | ||
| raise ValueError | ||
|
|
||
| customer_to_create = _domain.Customer( | ||
| **dict.fromkeys(DEFAULT_FIELDS), | ||
| name=str(customer_data["name"]).strip(), | ||
| description=str(customer_data["description"]), | ||
| ) | ||
| created_customer = use_case.create_customer(customer_to_create) | ||
|
|
||
| if created_customer: | ||
| body = json.dumps(created_customer.__dict__) | ||
| status_code = 201 | ||
| else: | ||
| body = b'This customer already exists' | ||
| status_code = 409 | ||
|
|
||
| return func.HttpResponse( | ||
| body=body, | ||
| status_code=status_code, | ||
| mimetype="application/json" | ||
| ) | ||
| except ValueError: | ||
| return func.HttpResponse( | ||
| body=b'Invalid format or structure of the attributes of the customer', | ||
| status_code=400, | ||
| mimetype="application/json" | ||
| ) | ||
|
|
||
|
|
||
| def _validate_customer(customer_data: dict) -> bool: | ||
| if [field.name for field in dataclasses.fields(_domain.Customer) | ||
| if (field.name not in customer_data) and (field.name not in DEFAULT_FIELDS)]: | ||
mandres2015 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return False | ||
| return True | ||
mandres2015 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
56 changes: 0 additions & 56 deletions
56
V2/time_tracker/customers/_application/_time_entries/_create_customer.py
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
V2/time_tracker/customers/_application/_time_entries/_delete_time_entry.py
This file was deleted.
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
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 |
|---|---|---|
| @@ -1,6 +1,2 @@ | ||
| # flake8: noqa | ||
| from ._create_customer_use_case import CreateCustomerUseCase | ||
| from ._update_customer_use_case import UpdateCustomerUseCase | ||
| from ._get_by_id_customer_use_case import GetByIdCustomerUseCase | ||
| from ._get_all_customer_use_case import GetAllCustomerUseCase | ||
| from ._delete_customer_use_case import DeleteCustomerUseCase | ||
| from ._create_customer_use_case import CreateCustomerUseCase |
10 changes: 0 additions & 10 deletions
10
V2/time_tracker/customers/_domain/_use_cases/_delete_customer_use_case.py
This file was deleted.
Oops, something went wrong.
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.