1
- from json import dumps
1
+ import random
2
+ from json import dumps , loads
2
3
from uuid import uuid4
3
4
5
+ import pytest
4
6
import requests
5
7
from assertpy .assertpy import assert_that
8
+ from jsonpath_ng import parse
6
9
7
10
from config import BASE_URI
11
+ from utils .file_reader import read_file
8
12
9
13
10
14
def test_read_all_has_kent ():
@@ -22,7 +26,7 @@ def test_read_all_has_kent():
22
26
23
27
24
28
def test_new_person_can_be_added ():
25
- unique_last_name = create_new_person ()
29
+ unique_last_name = create_person_with_unique_last_name ()
26
30
27
31
# After user is created, we read all the users and then use filter expression to find if the
28
32
# created user is present in the response list
@@ -32,7 +36,7 @@ def test_new_person_can_be_added():
32
36
33
37
34
38
def test_created_person_can_be_deleted ():
35
- persons_last_name = create_new_person ()
39
+ persons_last_name = create_person_with_unique_last_name ()
36
40
37
41
peoples = requests .get (BASE_URI ).json ()
38
42
newly_created_user = search_created_user_in (peoples , persons_last_name )[0 ]
@@ -42,14 +46,45 @@ def test_created_person_can_be_deleted():
42
46
assert_that (response .status_code ).is_equal_to (requests .codes .ok )
43
47
44
48
45
- def create_new_person ():
46
- # Ensure a user with a unique last name is created everytime the test runs
47
- # Note: json.dumps() is used to convert python dict to json string
48
- unique_last_name = f'User { str (uuid4 ())} '
49
- payload = dumps ({
50
- 'fname' : 'New' ,
51
- 'lname' : unique_last_name
52
- })
49
+ @pytest .fixture
50
+ def create_data ():
51
+ payload = read_file ('create_person.json' )
52
+
53
+ random_no = random .randint (0 , 1000 )
54
+ last_name = f'Olabini{ random_no } '
55
+
56
+ payload ['lname' ] = last_name
57
+ yield payload
58
+
59
+
60
+ def test_person_can_be_added_with_a_json_template (create_data ):
61
+ create_person_with_unique_last_name (create_data )
62
+
63
+ response = requests .get (BASE_URI )
64
+ peoples = loads (response .text )
65
+
66
+ # Get all last names for any object in the root array
67
+ # Here $ = root, [*] represents any element in the array
68
+ # Read full syntax: https://pypi.org/project/jsonpath-ng/
69
+ jsonpath_expr = parse ("$.[*].lname" )
70
+ result = [match .value for match in jsonpath_expr .find (peoples )]
71
+
72
+ expected_last_name = create_data ['lname' ]
73
+ assert_that (result ).contains (expected_last_name )
74
+
75
+
76
+ def create_person_with_unique_last_name (body = None ):
77
+ if body is None :
78
+ # Ensure a user with a unique last name is created everytime the test runs
79
+ # Note: json.dumps() is used to convert python dict to json string
80
+ unique_last_name = f'User { str (uuid4 ())} '
81
+ payload = dumps ({
82
+ 'fname' : 'New' ,
83
+ 'lname' : unique_last_name
84
+ })
85
+ else :
86
+ unique_last_name = body ['lname' ]
87
+ payload = dumps (body )
53
88
54
89
# Setting default headers to show that the client accepts json
55
90
# And will send json in the headers
0 commit comments