-
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 all commits
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
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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# flake8: noqa | ||
from ._customers import create_customer |
2 changes: 2 additions & 0 deletions
2
V2/time_tracker/customers/_application/_customers/__init__.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,2 @@ | ||
# flake8: noqa | ||
from ._create_customer import create_customer |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
import dataclasses | ||
import json | ||
import typing | ||
|
||
import azure.functions as func | ||
|
||
from ... import _domain | ||
from ... import _infrastructure | ||
from time_tracker._infrastructure import DB | ||
|
||
|
||
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( | ||
id=None, | ||
deleted=None, | ||
status=None, | ||
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.type != typing.Optional[field.type])]: | ||
return False | ||
return True | ||
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,7 @@ | ||
# flake8: noqa | ||
from ._entities import Customer | ||
from ._persistence_contracts import CustomersDao | ||
from ._services import CustomerService | ||
from ._use_cases import ( | ||
CreateCustomerUseCase, | ||
) |
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,2 @@ | ||
# flake8: noqa | ||
from ._customer import 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,11 @@ | ||
from dataclasses import dataclass | ||
import typing | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Customer: | ||
id: typing.Optional[int] | ||
name: str | ||
description: str | ||
deleted: typing.Optional[bool] | ||
status: typing.Optional[int] |
2 changes: 2 additions & 0 deletions
2
V2/time_tracker/customers/_domain/_persistence_contracts/__init__.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,2 @@ | ||
# flake8: noqa | ||
from ._customers_dao import CustomersDao |
9 changes: 9 additions & 0 deletions
9
V2/time_tracker/customers/_domain/_persistence_contracts/_customers_dao.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,9 @@ | ||
import abc | ||
|
||
from time_tracker.customers._domain import Customer | ||
|
||
|
||
class CustomersDao(abc.ABC): | ||
@abc.abstractmethod | ||
def create(self, data: Customer) -> Customer: | ||
pass |
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,2 @@ | ||
# flake8: noqa | ||
from ._customer import CustomerService |
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,10 @@ | ||
from time_tracker.customers._domain import Customer, CustomersDao | ||
|
||
|
||
class CustomerService: | ||
|
||
def __init__(self, customer_dao: CustomersDao): | ||
self.customer_dao = customer_dao | ||
|
||
def create(self, data: Customer) -> Customer: | ||
return self.customer_dao.create(data) |
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,2 @@ | ||
# flake8: noqa | ||
from ._create_customer_use_case import CreateCustomerUseCase |
10 changes: 10 additions & 0 deletions
10
V2/time_tracker/customers/_domain/_use_cases/_create_customer_use_case.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,10 @@ | ||
from time_tracker.customers._domain import Customer, CustomerService | ||
|
||
|
||
class CreateCustomerUseCase: | ||
|
||
def __init__(self, customer_service: CustomerService): | ||
self.customer_service = customer_service | ||
|
||
def create_customer(self, data: Customer) -> Customer: | ||
return self.customer_service.create(data) |
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,2 @@ | ||
# flake8: noqa | ||
from ._data_persistence import CustomersSQLDao |
2 changes: 2 additions & 0 deletions
2
V2/time_tracker/customers/_infrastructure/_data_persistence/__init__.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,2 @@ | ||
# flake8: noqa | ||
from ._customer_dao import CustomersSQLDao |
41 changes: 41 additions & 0 deletions
41
V2/time_tracker/customers/_infrastructure/_data_persistence/_customer_dao.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,41 @@ | ||
import dataclasses | ||
|
||
import sqlalchemy as sq | ||
mandres2015 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import time_tracker.customers._domain as domain | ||
from time_tracker._infrastructure import _db | ||
|
||
|
||
class CustomersSQLDao(domain.CustomersDao): | ||
|
||
def __init__(self, database: _db.DB): | ||
self.customer_key = [field.name for field in dataclasses.fields(domain.Customer)] | ||
self.db = database | ||
self.customer = sq.Table( | ||
'customer', | ||
self.db.metadata, | ||
sq.Column('id', sq.Integer, primary_key=True, autoincrement=True), | ||
sq.Column('name', sq.String, unique=True, nullable=False), | ||
sq.Column('description', sq.String), | ||
sq.Column('deleted', sq.Boolean), | ||
sq.Column('status', sq.Integer), | ||
extend_existing=True, | ||
) | ||
|
||
def create(self, data: domain.Customer) -> domain.Customer: | ||
try: | ||
new_customer = data.__dict__ | ||
new_customer.pop('id', None) | ||
new_customer['deleted'] = False | ||
new_customer['status'] = 1 | ||
|
||
query = self.customer.insert().values(new_customer).return_defaults() | ||
customer = self.db.get_session().execute(query) | ||
new_customer.update({"id": customer.inserted_primary_key[0]}) | ||
return self.__create_customer_dto(new_customer) | ||
except sq.exc.IntegrityError: | ||
return None | ||
|
||
def __create_customer_dto(self, customer: dict) -> domain.Customer: | ||
customer = {key: customer.get(key) for key in self.customer_key} | ||
return domain.Customer(**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,2 @@ | ||
# flake8: noqa | ||
from ._application import create_customer |
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.