Skip to content

Commit 312f923

Browse files
committed
Merge branch 'TT-418-CRUD-CUSTOMER-V2' of https://github.com/ioet/time-tracker-backend into TT-418-CRUD-CUSTOMER-V2
2 parents c4fbb0f + 0b228b7 commit 312f923

File tree

12 files changed

+187
-11
lines changed

12 files changed

+187
-11
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest
2+
import json
3+
from faker import Faker
4+
5+
import azure.functions as func
6+
7+
import time_tracker.customers._application._customers as azure_customers
8+
from time_tracker._infrastructure import DB
9+
from time_tracker.customers import _domain as domain_customers
10+
from time_tracker.customers import _infrastructure as infrastructure_customers
11+
12+
13+
CUSTOMER_URL = "/api/customers/"
14+
15+
16+
def test__customer_azure_endpoint__creates_a_customer__when_customer_has_all_necesary_attributes(
17+
customer_factory
18+
):
19+
customer_body = customer_factory().__dict__
20+
21+
body = json.dumps(customer_body).encode("utf-8")
22+
req = func.HttpRequest(
23+
method='POST',
24+
body=body,
25+
url=CUSTOMER_URL,
26+
)
27+
28+
response = azure_customers._create_customer.create_customer(req)
29+
customer_json_data = json.loads(response.get_body())
30+
customer_body['id'] = customer_json_data['id']
31+
32+
assert response.status_code == 201
33+
assert customer_json_data == customer_body
34+
35+
36+
def test__customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_necessary_attributes():
37+
customer_to_insert = {
38+
"id": None,
39+
"name": Faker().user_name(),
40+
"deleted": False,
41+
"status": 1
42+
}
43+
44+
body = json.dumps(customer_to_insert).encode("utf-8")
45+
req = func.HttpRequest(
46+
method='POST',
47+
body=body,
48+
url=CUSTOMER_URL,
49+
)
50+
51+
response = azure_customers._create_customer.create_customer(req)
52+
53+
assert response.status_code == 400
54+
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'

V2/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# flake8: noqa
22
from fixtures import _activity_factory, _test_db, _insert_activity
33
from fixtures import _time_entry_factory
4+
from fixtures import _customer_factory

V2/tests/fixtures.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from faker import Faker
33

44
import time_tracker.activities._domain as activities_domain
5-
import time_tracker.activities._infrastructure as activities_infrastructure
65
import time_tracker.time_entries._domain as time_entries_domain
6+
import time_tracker.customers._domain as customers_domain
7+
import time_tracker.activities._infrastructure as activities_infrastructure
78
from time_tracker._infrastructure import DB
89

910

@@ -73,3 +74,23 @@ def _new_activity(activity: activities_domain.Activity, database: DB):
7374
new_activity = dao.create(activity)
7475
return new_activity
7576
return _new_activity
77+
78+
79+
@pytest.fixture(name='customer_factory')
80+
def _customer_factory() -> customers_domain.Customer:
81+
def _make_customer(
82+
name: str = Faker().name(),
83+
description: str = Faker().sentence(),
84+
deleted: bool = False,
85+
status: int = 1,
86+
):
87+
customer = customers_domain.Customer(
88+
id=None,
89+
name=name,
90+
description=description,
91+
deleted=deleted,
92+
status=status
93+
)
94+
return customer
95+
96+
return _make_customer
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
import time_tracker.customers._domain as domain
4+
import time_tracker.customers._infrastructure as infrastructure
5+
from time_tracker._infrastructure import DB
6+
7+
8+
@pytest.fixture(name='create_fake_dao')
9+
def _fake_dao() -> domain.CustomersDao:
10+
def _create_fake_dao(db_fake: DB) -> domain.CustomersDao:
11+
dao = infrastructure.CustomersSQLDao(db_fake)
12+
return dao
13+
return _create_fake_dao
14+
15+
16+
@pytest.fixture(name='clean_database', autouse=True)
17+
def _clean_database():
18+
yield
19+
db_fake = DB()
20+
dao = infrastructure.CustomersSQLDao(db_fake)
21+
query = dao.customer.delete()
22+
dao.db.get_session().execute(query)
23+
24+
25+
def test__customer_dao__returns_a_customer_dto__when_saves_correctly_with_sql_database(
26+
test_db, customer_factory, create_fake_dao
27+
):
28+
dao = create_fake_dao(test_db)
29+
30+
customer_to_insert = customer_factory()
31+
32+
inserted_customer = dao.create(customer_to_insert)
33+
34+
assert isinstance(inserted_customer, domain.Customer)
35+
assert inserted_customer == customer_to_insert
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from time_tracker.customers._domain import CustomerService
2+
3+
4+
def test__create_customer__uses_the_customer_dao__to_create_a_customer(mocker, customer_factory):
5+
expected_customer = mocker.Mock()
6+
customer_dao = mocker.Mock(
7+
create=mocker.Mock(return_value=expected_customer)
8+
)
9+
customer_service = CustomerService(customer_dao)
10+
11+
new_customer = customer_service.create(customer_factory())
12+
13+
assert customer_dao.create.called
14+
assert expected_customer == new_customer
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pytest_mock import MockFixture
2+
3+
from time_tracker.customers._domain import _use_cases
4+
5+
6+
def test__create_customer_function__uses_the_customer_service__to_create_a_customer(
7+
mocker: MockFixture, customer_factory
8+
):
9+
expected_customer = mocker.Mock()
10+
customer_service = mocker.Mock(
11+
create=mocker.Mock(return_value=expected_customer)
12+
)
13+
14+
customer_use_case = _use_cases.CreateCustomerUseCase(customer_service)
15+
new_customer = customer_use_case.create_customer(customer_factory())
16+
17+
assert customer_service.create.called
18+
assert expected_customer == new_customer
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# flake8: noqa
22
from ._customers import create_customer
33
from ._customers import get_customers
4-
from ._customers import delete_customer
4+
from ._customers import delete_customer
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# flake8: noqa
2-
from ._create_customer import create_customer
3-
from ._get_customers import get_customers
4-
from ._delete_customer import delete_customer
2+
from ._create_customer import create_customer
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
3+
import azure.functions as func
4+
5+
from ... import _domain
6+
from ... import _infrastructure
7+
from time_tracker._infrastructure import DB
8+
9+
10+
def delete_time_entry(req: func.HttpRequest) -> func.HttpResponse:
11+
time_entry_dao = _infrastructure.TimeEntriesSQLDao(DB())
12+
time_entry_service = _domain.TimeEntryService(time_entry_dao)
13+
use_case = _domain._use_cases.DeleteTimeEntryUseCase(time_entry_service)
14+
15+
try:
16+
time_entry_id = int(req.route_params.get("id"))
17+
deleted_time_entry = use_case.delete_time_entry(time_entry_id)
18+
if not deleted_time_entry:
19+
return func.HttpResponse(
20+
body="Not found",
21+
status_code=404,
22+
mimetype="application/json"
23+
)
24+
25+
return func.HttpResponse(
26+
body=json.dumps(deleted_time_entry.__dict__, default=str),
27+
status_code=200,
28+
mimetype="application/json",
29+
)
30+
31+
except ValueError:
32+
return func.HttpResponse(
33+
body=b"Invalid Format ID",
34+
status_code=400,
35+
mimetype="application/json"
36+
)

V2/time_tracker/customers/_domain/_persistence_contracts/_customers_dao.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import abc
2-
import typing
2+
# import typing
33

44
from time_tracker.customers._domain import Customer
55

0 commit comments

Comments
 (0)