1
1
from json import dumps
2
2
from uuid import uuid4
3
-
4
3
import requests
5
4
from assertpy .assertpy import assert_that
6
-
7
5
from config import BASE_URI
8
6
from utils .print_helpers import pretty_print
9
7
@@ -12,11 +10,10 @@ def test_read_all_has_kent():
12
10
# We use requests.get() with url to make a get request
13
11
response = requests .get (BASE_URI )
14
12
# response from requests has many useful properties
15
- # we can assert on the response status code
16
13
print (response .status_code )
17
14
print (requests .codes .ok )
18
15
assert_that (response .status_code ).is_equal_to (requests .codes .ok )
19
- # We can get python dict as response by using . json() method
16
+ # Note : To Convert the JSON data into the Python Dictionary use - json() method.
20
17
peoples = response .json ()
21
18
# pretty_print() is a module from python that indents the json response in a more readable way.
22
19
pretty_print (peoples )
@@ -36,34 +33,28 @@ def test_new_person_can_be_added():
36
33
37
34
def test_created_person_can_be_deleted ():
38
35
persons_last_name = create_new_person ()
39
-
40
36
peoples = requests .get (BASE_URI ).json ()
41
37
newly_created_user = search_created_user_in (peoples , persons_last_name )[0 ]
42
-
43
38
delete_url = f'{ BASE_URI } /{ newly_created_user ["person_id" ]} '
44
39
response = requests .delete (delete_url )
45
40
assert_that (response .status_code ).is_equal_to (requests .codes .ok )
46
41
47
42
48
43
def create_new_person ():
49
44
# Ensure a user with a unique last name is created everytime the test runs
50
- # Note: json.dumps() is used to convert python dict to json string
51
45
unique_last_name = f'User { str (uuid4 ())} '
52
46
print (str (uuid4 ()))
53
- # dumps() is a method that takes the python dictionary and converts it into json string
47
+ # Note: json. dumps() or dumps() method - is used to convert the Python dictionary to JSON String
54
48
payload = dumps ({
55
49
'fname' : 'New' ,
56
50
'lname' : unique_last_name
57
51
})
58
-
59
- # Setting default headers to show that the client accepts json
60
- # And will send json in the headers
52
+ # Setting default headers to show that the client accepts json and will send the json in the headers
61
53
headers = {
62
54
'Content-Type' : 'application/json' ,
63
55
'Accept' : 'application/json'
64
56
}
65
-
66
- # We use requests.post method with keyword params to make the request more readable
57
+ # We use requests.post() method with keyword params to make the request more readable
67
58
response = requests .post (url = BASE_URI , data = payload , headers = headers )
68
59
print (response .status_code )
69
60
assert_that (response .status_code , description = 'Person not created' ).is_equal_to (requests .codes .no_content )
@@ -72,4 +63,4 @@ def create_new_person():
72
63
73
64
74
65
def search_created_user_in (peoples , last_name ):
75
- return [person for person in peoples if person ['lname' ] == last_name ]
66
+ return [person for person in peoples if person ['lname' ] == last_name ]
0 commit comments