5
5
from assertpy .assertpy import assert_that
6
6
7
7
from config import BASE_URI
8
+ from utils .print_helpers import pretty_print
8
9
9
10
10
11
def test_read_all_has_kent ():
11
12
# We use requests.get() with url to make a get request
12
13
response = requests .get (BASE_URI )
13
14
# response from requests has many useful properties
14
15
# we can assert on the response status code
16
+ print (response .status_code )
17
+ print (requests .codes .ok )
15
18
assert_that (response .status_code ).is_equal_to (requests .codes .ok )
16
19
# We can get python dict as response by using .json() method
17
- response_text = response .json ()
18
- first_names = [people ['fname' ] for people in response_text ]
20
+ peoples = response .json ()
21
+ # pretty_print() is a module from python that indents the json response in a more readable way.
22
+ pretty_print (peoples )
23
+ first_names = [people ['fname' ] for people in peoples ]
24
+ pretty_print (first_names )
19
25
assert_that (first_names ).contains ('Kent' )
20
26
21
27
22
28
def test_new_person_can_be_added ():
23
29
unique_last_name = create_new_person ()
24
-
25
30
# After user is created, we read all the users and then use filter expression to find if the
26
31
# created user is present in the response list
27
32
peoples = requests .get (BASE_URI ).json ()
@@ -44,6 +49,8 @@ def create_new_person():
44
49
# Ensure a user with a unique last name is created everytime the test runs
45
50
# Note: json.dumps() is used to convert python dict to json string
46
51
unique_last_name = f'User { str (uuid4 ())} '
52
+ print (str (uuid4 ()))
53
+ # dumps() is a method that takes the python dictionary and converts it into json string
47
54
payload = dumps ({
48
55
'fname' : 'New' ,
49
56
'lname' : unique_last_name
@@ -58,9 +65,11 @@ def create_new_person():
58
65
59
66
# We use requests.post method with keyword params to make the request more readable
60
67
response = requests .post (url = BASE_URI , data = payload , headers = headers )
68
+ print (response .status_code )
61
69
assert_that (response .status_code , description = 'Person not created' ).is_equal_to (requests .codes .no_content )
70
+ print (unique_last_name )
62
71
return unique_last_name
63
72
64
73
65
74
def search_created_user_in (peoples , last_name ):
66
- return [person for person in peoples if person ['lname' ] == last_name ]
75
+ return [person for person in peoples if person ['lname' ] == last_name ]
0 commit comments