Skip to content

Commit 6824013

Browse files
committed
fix: TT-418 renamed tests and problems solved
1 parent 2d0efbd commit 6824013

File tree

8 files changed

+94
-43
lines changed

8 files changed

+94
-43
lines changed

V2/serverless.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ service: azure-time-tracker
22

33
frameworkVersion: "2"
44

5-
#region Config
5+
#region start Config
66

77
provider:
88
name: azure
@@ -38,13 +38,13 @@ package:
3838
- "!.gitignore"
3939
- "!.git/**"
4040

41-
#endregion Fin Config
41+
#endregion end Config
4242

43-
#region Functions
43+
#region start Functions
4444

4545
functions:
4646

47-
#region Activities
47+
#region start Activities
4848

4949
get_activities:
5050
handler: time_tracker/activities/interface.get_activities
@@ -86,9 +86,9 @@ functions:
8686
route: activities/
8787
authLevel: anonymous
8888

89-
#endregion fin Activities
89+
#endregion end Activities
9090

91-
#region Time-Entries
91+
#region start Time-Entries
9292

9393
create_time_entry:
9494
handler: time_tracker/time_entries/interface.create_time_entry
@@ -110,9 +110,9 @@ functions:
110110
route: time-entries/{id}
111111
authLevel: anonymous
112112

113-
#endregion Fin TimeEntries
113+
#endregion end Time-Entries
114114

115-
#region Customer
115+
#region start Customer
116116

117117
create_customer:
118118
handler: time_tracker/customers/interface.create_customer
@@ -154,7 +154,6 @@ functions:
154154
route: customers/{id}
155155
authLevel: anonymous
156156

157-
#endregion Fin Customer
157+
#endregion end Customer
158158

159-
#endregion Fin Funciones
160-
159+
#endregion end Functions

V2/tests/api/azure/customer_azure_endpoints_test.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from http import HTTPStatus
12
import json
23
from faker import Faker
34

@@ -24,7 +25,7 @@ def test__create_customer_azure_endpoint__creates_a_customer__when_customer_has_
2425
customer_json_data = json.loads(response.get_body())
2526
customer_body['id'] = customer_json_data['id']
2627

27-
assert response.status_code == 201
28+
assert response.status_code == HTTPStatus.CREATED
2829
assert customer_json_data == customer_body
2930

3031

@@ -45,7 +46,7 @@ def test__create_customer_azure_endpoint__returns_a_status_400__when_dont_reciev
4546

4647
response = azure_customers._create_customer.create_customer(req)
4748

48-
assert response.status_code == 400
49+
assert response.status_code == HTTPStatus.BAD_REQUEST
4950
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'
5051

5152

@@ -65,10 +66,24 @@ def test__delete_customer_azure_endpoint__returns_a_customer_with_true_deleted__
6566
response = azure_customers._delete_customer.delete_customer(req)
6667
customer_json_data = json.loads(response.get_body().decode("utf-8"))
6768

68-
assert response.status_code == 200
69+
assert response.status_code == HTTPStatus.OK
6970
assert customer_json_data['deleted'] is True
7071

7172

73+
def test__delete_customer_azure_endpoint__returns_not_found__when_its_id_is_not_found():
74+
req = func.HttpRequest(
75+
method='DELETE',
76+
body=None,
77+
url=CUSTOMER_URL,
78+
route_params={"id": Faker().pyint()},
79+
)
80+
81+
response = azure_customers._delete_customer.delete_customer(req)
82+
83+
assert response.status_code == HTTPStatus.NOT_FOUND
84+
assert response.get_body() == b'Not found'
85+
86+
7287
def test__update_customer_azure_endpoint__returns_an_updated_customer__when_customer_has_all_necesary_attributes(
7388
test_db, customer_factory, insert_customer
7489
):
@@ -88,7 +103,7 @@ def test__update_customer_azure_endpoint__returns_an_updated_customer__when_cust
88103
response = azure_customers._update_customer.update_customer(req)
89104
customer_json_data = json.loads(response.get_body())
90105

91-
assert response.status_code == 200
106+
assert response.status_code == HTTPStatus.OK
92107
assert customer_json_data == inserted_customer
93108

94109

@@ -107,8 +122,30 @@ def test__update_customer_azure_endpoint__returns_update_a_customer__when_custom
107122

108123
response = azure_customers._update_customer.update_customer(req)
109124

110-
assert response.status_code == 409
111-
assert response.get_body() == b'This customer does not exist'
125+
assert response.status_code == HTTPStatus.CONFLICT
126+
assert response.get_body() == b'This customer does not exist or is duplicated'
127+
128+
129+
def test__update_customer_azure_endpoint__returns_invalid_format__when_customer_doesnt_have_all_necesary_attributes(
130+
customer_factory, insert_customer, test_db
131+
):
132+
existent_customer = customer_factory()
133+
inserted_customer = insert_customer(existent_customer, test_db).__dict__
134+
135+
inserted_customer.pop("name")
136+
137+
body = json.dumps(inserted_customer).encode("utf-8")
138+
req = func.HttpRequest(
139+
method='PUT',
140+
body=body,
141+
url=CUSTOMER_URL,
142+
route_params={"id": inserted_customer["id"]},
143+
)
144+
145+
response = azure_customers._update_customer.update_customer(req)
146+
147+
assert response.status_code == HTTPStatus.BAD_REQUEST
148+
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'
112149

113150

114151
def test__delete_customers_azure_endpoint__returns_a_status_code_400__when_customer_recive_invalid_id(
@@ -122,7 +159,7 @@ def test__delete_customers_azure_endpoint__returns_a_status_code_400__when_custo
122159

123160
response = azure_customers._delete_customer.delete_customer(req)
124161

125-
assert response.status_code == 400
162+
assert response.status_code == HTTPStatus.BAD_REQUEST
126163
assert response.get_body() == b'Invalid Format ID'
127164

128165

@@ -138,7 +175,7 @@ def test__customers_azure_endpoint__returns_all_customers(
138175
customers_json_data = response.get_body().decode("utf-8")
139176
customer_list = json.loads(customers_json_data)
140177

141-
assert response.status_code == 200
178+
assert response.status_code == HTTPStatus.OK
142179
assert customers_json_data <= json.dumps(inserted_customer)
143180
assert customer_list.pop() == inserted_customer
144181

@@ -159,5 +196,19 @@ def test__customer_azure_endpoint__returns_a_customer__when_customer_matches_its
159196
response = azure_customers._get_customers.get_customers(req)
160197
customer_json_data = response.get_body().decode("utf-8")
161198

162-
assert response.status_code == 200
199+
assert response.status_code == HTTPStatus.OK
163200
assert customer_json_data == json.dumps(inserted_customer)
201+
202+
203+
def test__customer_azure_endpoint__returns_invalid_id__when_customer_not_matches_its_id():
204+
req = func.HttpRequest(
205+
method='GET',
206+
body=None,
207+
url=CUSTOMER_URL,
208+
route_params={"id": "Invalid ID"},
209+
)
210+
211+
response = azure_customers._get_customers.get_customers(req)
212+
213+
assert response.status_code == HTTPStatus.BAD_REQUEST
214+
assert response.get_body() == b'The id has an invalid format'

V2/tests/integration/daos/customers_dao_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import pytest
21
import typing
2+
3+
import pytest
34
from faker import Faker
45

56
import time_tracker.customers._domain as domain

V2/time_tracker/customers/_application/_customers/_create_customer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dataclasses
22
import json
33
import typing
4+
from http import HTTPStatus
45

56
import azure.functions as func
67

@@ -32,10 +33,10 @@ def create_customer(req: func.HttpRequest) -> func.HttpResponse:
3233

3334
if created_customer:
3435
body = json.dumps(created_customer.__dict__)
35-
status_code = 201
36+
status_code = HTTPStatus.CREATED
3637
else:
3738
body = b'This customer already exists'
38-
status_code = 409
39+
status_code = HTTPStatus.CONFLICT
3940

4041
return func.HttpResponse(
4142
body=body,
@@ -45,7 +46,7 @@ def create_customer(req: func.HttpRequest) -> func.HttpResponse:
4546
except ValueError:
4647
return func.HttpResponse(
4748
body=b'Invalid format or structure of the attributes of the customer',
48-
status_code=400,
49+
status_code=HTTPStatus.BAD_REQUEST,
4950
mimetype="application/json"
5051
)
5152

V2/time_tracker/customers/_application/_customers/_delete_customer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from http import HTTPStatus
23

34
import azure.functions as func
45

@@ -20,19 +21,19 @@ def delete_customer(req: func.HttpRequest) -> func.HttpResponse:
2021
if not deleted_customer:
2122
return func.HttpResponse(
2223
body="Not found",
23-
status_code=404,
24+
status_code=HTTPStatus.NOT_FOUND,
2425
mimetype=DATATYPE
2526
)
2627

2728
return func.HttpResponse(
2829
body=json.dumps(deleted_customer.__dict__, default=str),
29-
status_code=200,
30+
status_code=HTTPStatus.OK,
3031
mimetype=DATATYPE,
3132
)
3233

3334
except ValueError:
3435
return func.HttpResponse(
3536
body=b"Invalid Format ID",
36-
status_code=400,
37+
status_code=HTTPStatus.BAD_REQUEST,
3738
mimetype=DATATYPE
3839
)

V2/time_tracker/customers/_application/_customers/_get_customers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from http import HTTPStatus
12
import json
23

34
import azure.functions as func
@@ -9,13 +10,13 @@
910

1011
def get_customers(req: func.HttpRequest) -> func.HttpResponse:
1112
customer_id = req.route_params.get('id')
12-
status_code = 200
13+
status_code = HTTPStatus.OK
1314

1415
try:
1516
if customer_id:
1617
response = _get_by_id(int(customer_id))
1718
if response == b'This customer does not exist':
18-
status_code = 404
19+
status_code = HTTPStatus.NOT_FOUND
1920
else:
2021
response = _get_all()
2122

@@ -24,7 +25,7 @@ def get_customers(req: func.HttpRequest) -> func.HttpResponse:
2425
)
2526
except ValueError:
2627
return func.HttpResponse(
27-
body=b"The id has an invalid format", status_code=400, mimetype="application/json"
28+
body=b"The id has an invalid format", status_code=HTTPStatus.BAD_REQUEST, mimetype="application/json"
2829
)
2930

3031

V2/time_tracker/customers/_application/_customers/_update_customer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dataclasses
22
import json
33
import typing
4+
from http import HTTPStatus
45

56
import azure.functions as func
67

@@ -29,10 +30,10 @@ def update_customer(req: func.HttpRequest) -> func.HttpResponse:
2930

3031
if updated_customer:
3132
body = json.dumps(updated_customer.__dict__)
32-
status_code = 200
33+
status_code = HTTPStatus.OK
3334
else:
34-
body = b'This customer does not exist'
35-
status_code = 409
35+
body = b'This customer does not exist or is duplicated'
36+
status_code = HTTPStatus.CONFLICT
3637

3738
return func.HttpResponse(
3839
body=body,
@@ -42,7 +43,7 @@ def update_customer(req: func.HttpRequest) -> func.HttpResponse:
4243
except ValueError:
4344
return func.HttpResponse(
4445
body=b'Invalid format or structure of the attributes of the customer',
45-
status_code=400,
46+
status_code=HTTPStatus.BAD_REQUEST,
4647
mimetype="application/json"
4748
)
4849

V2/time_tracker/customers/_infrastructure/_data_persistence/_customer_dao.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import typing
33

44
import sqlalchemy as sq
5-
import sqlalchemy.sql as sql
65

76
import time_tracker.customers._domain as domain
87
from time_tracker._infrastructure import _db
@@ -25,8 +24,8 @@ def __init__(self, database: _db.DB):
2524
)
2625

2726
def get_by_id(self, id: int) -> domain.Customer:
28-
query = sql.select(self.customer).where(
29-
sql.and_(self.customer.c.id == id, self.customer.c.deleted.is_(False))
27+
query = sq.sql.select(self.customer).where(
28+
sq.sql.and_(self.customer.c.id == id, self.customer.c.deleted.is_(False))
3029
)
3130
customer = self.db.get_session().execute(query).one_or_none()
3231
return self.__create_customer_dto(dict(customer)) if customer else None
@@ -70,12 +69,9 @@ def delete(self, customer_id: int) -> domain.Customer:
7069

7170
def update(self, id: int, data: domain.Customer) -> domain.Customer:
7271
try:
73-
new_customer = {
74-
"name": data.name,
75-
"description": data.description,
76-
"status": data.status,
77-
"deleted": data.deleted
78-
}
72+
new_customer = data.__dict__
73+
new_customer.pop("id")
74+
7975
customer_validated = {key: value for (key, value) in new_customer.items() if value is not None}
8076
query = self.customer.update().where(self.customer.c.id == id).values(customer_validated)
8177
self.db.get_session().execute(query)

0 commit comments

Comments
 (0)