1+ from http import HTTPStatus
12import json
23from faker import Faker
34
89CUSTOMER_URL = "/api/customers/"
910
1011
11- def test__customer_azure_endpoint__creates_a_customer__when_customer_has_all_necesary_attributes (
12+ def test__create_customer_azure_endpoint__creates_a_customer__when_customer_has_all_necesary_attributes (
1213 customer_factory
1314):
1415 customer_body = customer_factory ().__dict__
@@ -24,11 +25,11 @@ def test__customer_azure_endpoint__creates_a_customer__when_customer_has_all_nec
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
31- def test__customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_necessary_attributes ():
32+ def test__create_customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_necessary_attributes ():
3233 customer_to_insert = {
3334 "id" : None ,
3435 "name" : Faker ().user_name (),
@@ -45,5 +46,169 @@ def test__customer_azure_endpoint__returns_a_status_400__when_dont_recieve_all_n
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'
51+
52+
53+ def test__delete_customer_azure_endpoint__returns_a_customer_with_true_deleted__when_its_id_is_found (
54+ test_db , customer_factory , insert_customer
55+ ):
56+ customer_preinsert = customer_factory ()
57+ inserted_customer = insert_customer (customer_preinsert , test_db ).__dict__
58+
59+ req = func .HttpRequest (
60+ method = 'DELETE' ,
61+ body = None ,
62+ url = CUSTOMER_URL ,
63+ route_params = {"id" : inserted_customer ["id" ]},
64+ )
65+
66+ response = azure_customers ._delete_customer .delete_customer (req )
67+ customer_json_data = json .loads (response .get_body ().decode ("utf-8" ))
68+
69+ assert response .status_code == HTTPStatus .OK
70+ assert customer_json_data ['deleted' ] is True
71+
72+
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+
87+ def test__update_customer_azure_endpoint__returns_an_updated_customer__when_customer_has_all_necesary_attributes (
88+ test_db , customer_factory , insert_customer
89+ ):
90+ existent_customer = customer_factory ()
91+ inserted_customer = insert_customer (existent_customer , test_db ).__dict__
92+
93+ inserted_customer ["description" ] = Faker ().sentence ()
94+
95+ body = json .dumps (inserted_customer ).encode ("utf-8" )
96+ req = func .HttpRequest (
97+ method = 'PUT' ,
98+ body = body ,
99+ url = CUSTOMER_URL ,
100+ route_params = {"id" : inserted_customer ["id" ]},
101+ )
102+
103+ response = azure_customers ._update_customer .update_customer (req )
104+ customer_json_data = json .loads (response .get_body ())
105+
106+ assert response .status_code == HTTPStatus .OK
107+ assert customer_json_data == inserted_customer
108+
109+
110+ def test__update_customer_azure_endpoint__returns_update_a_customer__when_customer_has_all_necesary_attributes (
111+ customer_factory
112+ ):
113+ existent_customer = customer_factory ().__dict__
114+
115+ body = json .dumps (existent_customer ).encode ("utf-8" )
116+ req = func .HttpRequest (
117+ method = 'PUT' ,
118+ body = body ,
119+ url = CUSTOMER_URL ,
120+ route_params = {"id" : Faker ().pyint ()},
121+ )
122+
123+ response = azure_customers ._update_customer .update_customer (req )
124+
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'
149+
150+
151+ def test__delete_customers_azure_endpoint__returns_a_status_code_400__when_customer_recive_invalid_id (
152+ ):
153+ req = func .HttpRequest (
154+ method = "DELETE" ,
155+ body = None ,
156+ url = CUSTOMER_URL ,
157+ route_params = {"id" : "invalid id" },
158+ )
159+
160+ response = azure_customers ._delete_customer .delete_customer (req )
161+
162+ assert response .status_code == HTTPStatus .BAD_REQUEST
163+ assert response .get_body () == b'Invalid Format ID'
164+
165+
166+ def test__customers_azure_endpoint__returns_all_customers (
167+ test_db , customer_factory , insert_customer
168+ ):
169+ customer_to_insert = customer_factory ()
170+
171+ inserted_customer = insert_customer (customer_to_insert , test_db ).__dict__
172+
173+ req = func .HttpRequest (method = 'GET' , body = None , url = CUSTOMER_URL )
174+ response = azure_customers ._get_customers .get_customers (req )
175+ customers_json_data = response .get_body ().decode ("utf-8" )
176+ customer_list = json .loads (customers_json_data )
177+
178+ assert response .status_code == HTTPStatus .OK
179+ assert customers_json_data <= json .dumps (inserted_customer )
180+ assert customer_list .pop () == inserted_customer
181+
182+
183+ def test__customer_azure_endpoint__returns_a_customer__when_customer_matches_its_id (
184+ test_db , customer_factory , insert_customer
185+ ):
186+ existent_customer = customer_factory ()
187+ inserted_customer = insert_customer (existent_customer , test_db ).__dict__
188+
189+ req = func .HttpRequest (
190+ method = 'GET' ,
191+ body = None ,
192+ url = CUSTOMER_URL ,
193+ route_params = {"id" : inserted_customer ["id" ]},
194+ )
195+
196+ response = azure_customers ._get_customers .get_customers (req )
197+ customer_json_data = response .get_body ().decode ("utf-8" )
198+
199+ assert response .status_code == HTTPStatus .OK
200+ 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'
0 commit comments