1
1
import requests
2
- import test_helper
3
-
4
2
5
3
BASE_URL = "http://localhost:3000/api/v2/items"
4
+ HEADERS = {"Content-Type" : "application/json" ,
5
+ 'APIKEY' : "4125a7b2-7ef8-4c4f-9ff9-3386c0dbcb5c" }
6
6
7
7
VALID_ITEM = {
8
- "Name" : "Test Name" ,
9
- "Description" : "Test Description"
10
- }
11
-
12
- INVALID_ITEM_1 = {}
13
- HEADERS = {"Content-Type" : "application/json" , 'APIKEY' : "4125a7b2-7ef8-4c4f-9ff9-3386c0dbcb5c" }
14
-
15
- UPDATED_ITEM = {
16
8
"code" : "XLE77785i" ,
17
9
"description" : "Focused human-resource implementation" ,
18
10
"short_description" : "offer" ,
30
22
"supplier_part_number" : "Ay-062669-VVl"
31
23
}
32
24
25
+ UPDATED_ITEM = {
26
+ "code" : "XLE77785i" ,
27
+ "description" : "Updated human-resource implementation" ,
28
+ "short_description" : "updated offer" ,
29
+ "upc_code" : "7946503676171" ,
30
+ "model_number" : "fM-605648-lbu" ,
31
+ "commodity_code" : "qB-2533" ,
32
+ "item_line" : 2 ,
33
+ "item_group" : 2 ,
34
+ "item_type" : 2 ,
35
+ "unit_purchase_quantity" : 30 ,
36
+ "unit_order_quantity" : 25 ,
37
+ "pack_order_quantity" : 18 ,
38
+ "supplier_id" : 50 ,
39
+ "supplier_code" : "SUP128" ,
40
+ "supplier_part_number" : "Ay-062669-VV2"
41
+ }
42
+
43
+ INVALID_ITEM = {}
44
+
45
+
46
+ def test_PostItem ():
47
+ post_response = requests .post (BASE_URL , headers = HEADERS , json = VALID_ITEM )
48
+ assert post_response .status_code == 201 , f"Expected 201 but got { post_response .status_code } "
49
+
50
+ try :
51
+ post_response_json = post_response .text
52
+ except ValueError :
53
+ raise ValueError (
54
+ f"POST response is not valid JSON: { post_response .text } " )
55
+
56
+ uid = post_response_json
57
+ assert uid , "UID not returned in POST response"
58
+
59
+ get_response = requests .get (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
60
+ assert get_response .status_code == 200 , f"Expected 200 but got { get_response .status_code } "
61
+
62
+ try :
63
+ get_response_json = get_response .json ()
64
+ except ValueError :
65
+ raise ValueError (
66
+ f"GET response is not valid JSON: { get_response .text } " )
67
+
68
+ for key , value in VALID_ITEM .items ():
69
+ assert get_response_json [key ] == value , f"Field '{ key } ' mismatch: { get_response_json [key ]} != { value } "
70
+
71
+ delete_response = requests .delete (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
72
+ assert delete_response .status_code == 200 , f"Expected 200 but got { delete_response .status_code } "
33
73
34
- def test_invalid_post ():
35
- # post an item TYPE with an empty body
36
- post_response = requests .post (
37
- BASE_URL , headers = HEADERS , json = INVALID_ITEM_1 )
38
- # the response should return a bad request
39
- assert post_response .status_code == 400
74
+ get_deleted_response = requests .get (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
75
+ assert get_deleted_response .status_code == 404 , f"Expected 404 for deleted item but got { get_deleted_response .status_code } "
40
76
41
77
42
- def test_put_Item_that_does_not_exist ():
78
+ def test_PostItemFail ():
79
+ post_response = requests .post (BASE_URL , headers = HEADERS , json = INVALID_ITEM )
80
+ assert post_response .status_code == 400 , f"Expected 400 but got { post_response .status_code } "
81
+
82
+
83
+ def test_PutItem ():
84
+ post_response = requests .post (BASE_URL , headers = HEADERS , json = VALID_ITEM )
85
+ assert post_response .status_code == 201 , f"Expected 201 but got { post_response .status_code } "
86
+
87
+ try :
88
+ uid = post_response .text
89
+ except ValueError :
90
+ raise ValueError (f"POST response is not valid: { post_response .text } " )
91
+
92
+ assert uid , "UID not returned in POST response"
93
+ id = int (uid [1 :])
94
+ UPDATED_ITEM ["id" ] = id
95
+
96
+ UPDATED_ITEM ["uid" ] = uid
97
+
98
+ put_response = requests .put (
99
+ f"{ BASE_URL } /{ uid } " , headers = HEADERS , json = UPDATED_ITEM )
100
+ assert put_response .status_code == 200 , f"Expected 200 but got { put_response .status_code } "
101
+
102
+ get_response = requests .get (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
103
+ assert get_response .status_code == 200 , f"Expected 200 but got { get_response .status_code } "
104
+
105
+ try :
106
+ get_response_json = get_response .json ()
107
+ except ValueError :
108
+ raise ValueError (
109
+ f"GET response is not valid JSON: { get_response .text } " )
110
+
111
+ for key , value in UPDATED_ITEM .items ():
112
+ assert get_response_json [key ] == value , f"Field '{ key } ' mismatch: { get_response_json [key ]} != { value } "
113
+
114
+ delete_response = requests .delete (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
115
+ assert delete_response .status_code == 200 , f"Expected 200 but got { delete_response .status_code } "
116
+
117
+ get_deleted_response = requests .get (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
118
+ assert get_deleted_response .status_code == 404 , f"Expected 404 for deleted item but got { get_deleted_response .status_code } "
119
+
120
+
121
+ def test_PutItemFail ():
122
+ uid = "P999999"
43
123
put_response = requests .put (
44
- BASE_URL + "999999999999" , json = VALID_ITEM )
45
- assert put_response .status_code == 404
124
+ f"{ BASE_URL } /{ uid } " , headers = HEADERS , json = VALID_ITEM )
125
+ assert put_response .status_code == 404 , f"Expected 404 but got { put_response .status_code } "
126
+
127
+
128
+ def test_DeleteItem ():
129
+ post_response = requests .post (BASE_URL , headers = HEADERS , json = VALID_ITEM )
130
+ assert post_response .status_code == 201 , f"Expected 201 but got { post_response .status_code } "
131
+
132
+ try :
133
+ uid = post_response .text
134
+ except ValueError :
135
+ raise ValueError (f"POST response is not valid: { post_response .text } " )
136
+
137
+ assert uid , "UID not returned in POST response"
138
+
139
+ delete_response = requests .delete (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
140
+ assert delete_response .status_code == 200 , f"Expected 200 but got { delete_response .status_code } "
141
+
142
+ get_deleted_response = requests .get (f"{ BASE_URL } /{ uid } " , headers = HEADERS )
143
+ assert get_deleted_response .status_code == 404 , f"Expected 404 for deleted item but got { get_deleted_response .status_code } "
0 commit comments