Skip to content

Commit cbcd6d3

Browse files
Updated API test cases and schema validation logic
1 parent 6f9630a commit cbcd6d3

13 files changed

+197
-7
lines changed

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pytest
2-
requests
2+
requests
3+
jsonschema
0 Bytes
Binary file not shown.
1.39 KB
Binary file not shown.
2.23 KB
Binary file not shown.

service/api_client.py

+13
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ def __init__(self):
1414
def get_booking_ids(self):
1515
return api_request.get_booking_ids(base_url=self.base_url, token= self.token)
1616

17+
def get_booking_by_id(self, id):
18+
return api_request.get_booking_by_id(self.base_url, self.token, id)
1719

20+
def create_booking(self, booking_data):
21+
return api_request.create_booking(self.base_url, self.token, booking_data)
22+
23+
def update_booking(self, id, updated_data):
24+
return api_request.update_booking(self.base_url, self.token, id, updated_data)
25+
26+
def partial_update_booking(self, id, partially_updated_data):
27+
return api_request.partial_update_booking(self.base_url, self.token, id, partially_updated_data)
28+
29+
def delete_booking(self, id):
30+
return api_request.delete_booking(self.base_url, self.token, id)

service/api_request.py

+60-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import requests
22
from dataclasses import dataclass
33

4+
from tornado.httputil import url_concat
5+
6+
47
@dataclass
58
class APIResponse:
69
status_code : str
@@ -27,14 +30,68 @@ def get_response(self, response):
2730
# GET
2831
def get_booking_ids(self, base_url, token):
2932
url = f"{base_url}/booking"
33+
headers = {"Content-Type": "application/json","Authorization": token}
34+
response = requests.request("GET", url=url, headers=headers)
35+
return self.get_response(response)
36+
37+
# GET
38+
def get_booking_by_id(self, base_url, token, id):
39+
url = f"{base_url}/booking/{id}"
40+
headers = {"Content-Type":"application/json", "Authorization":token}
41+
response = requests.get(url=url, headers=headers)
42+
return self.get_response(response)
43+
44+
# POST
45+
def create_booking(self, base_url, token, booking_data):
46+
url = f"{base_url}/booking"
47+
headers = {"Content-Type": "application/json", "Authorization": token}
48+
payload = booking_data
49+
response = requests.post(url, headers=headers, json=payload)
50+
return self.get_response(response)
51+
52+
# PUT
53+
def update_booking(self, base_url, token, id, updated_data):
54+
url = f"{base_url}/booking/{id}"
55+
headers = {
56+
"Content-Type" : "application/json",
57+
"Accept" : "application/json"
58+
}
59+
cookies = {
60+
"token": token
61+
}
62+
payload = updated_data
63+
response = requests.put(url, headers=headers, cookies=cookies, json=payload)
64+
return self.get_response(response)
65+
66+
#Patch
67+
def partial_update_booking(self, base_url, token, id, partially_updated_data):
68+
url = f"{base_url}/booking/{id}"
3069
headers = {
3170
"Content-Type": "application/json",
32-
"Authorization": token
33-
}
71+
"Accept": "application/json"
72+
}
73+
cookies = {
74+
"token": token
75+
}
76+
payload = partially_updated_data
77+
response = requests.patch(url, headers=headers, cookies=cookies, json=payload)
78+
return self.get_response(response)
3479

35-
response = requests.request("GET", url=url, headers=headers)
80+
# DELETE
81+
def delete_booking(self, base_url, token, id):
82+
url = f"{base_url}/booking/{id}"
83+
headers = {"Content-Type": "application/json"}
84+
cookies = {"token": token}
85+
response = requests.delete(url, headers=headers, cookies=cookies)
86+
87+
# print(response.status_code, response.json())
3688
return self.get_response(response)
3789

3890

3991

4092

93+
94+
95+
96+
97+

test_data/getBooking_schema.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"firstname": {"type": "string"},
6+
"lastname": {"type": "string"},
7+
"totalprice": {"type": "integer"},
8+
"depositpaid": {"type": "boolean"},
9+
"bookingdates": {
10+
"type": "object",
11+
"properties": {
12+
"checkin": {
13+
"type": "string",
14+
"format": "date"
15+
},
16+
"checkout": {
17+
"type": "string",
18+
"format": "date"
19+
}
20+
},
21+
"required": ["checkin", "checkout"]
22+
},
23+
"additionalneeds": {
24+
"type": "string"
25+
}
26+
},
27+
"required": ["firstname", "lastname", "totalprice", "depositpaid", "bookingdates"]
28+
}
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/conftest.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ def access_token():
1414

1515

1616

17+
18+
19+
20+

tests/test_api.py

+90-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,94 @@
1+
import json
2+
import pytest
13
from service.api_client import APIClient
24
api_client = APIClient()
5+
from jsonschema import validate
6+
import jsonschema.exceptions
7+
8+
class TestRestfulBookerAPI:
9+
def test_get_booking_ids(self):
10+
response = api_client.get_booking_ids()
11+
print(response.as_dict)
12+
assert response.status_code == 200
13+
14+
def test_get_booking_by_id(self):
15+
ids = api_client.get_booking_ids()
16+
id = ids.as_dict[0]["bookingid"]
17+
response = api_client.get_booking_by_id(id=id)
18+
print(response.as_dict)
19+
assert response.status_code == 200
20+
21+
def test_get_booking_by_id_json_schema(self):
22+
response = api_client.get_booking_by_id(id=1)
23+
assert response.status_code == 200
24+
with open("test_data/getBooking_schema.json", "r") as schema_file:
25+
schema = json.load(schema_file)
26+
try:
27+
validate(instance=response.as_dict, schema=schema)
28+
except jsonschema.exceptions.ValidationError as e:
29+
pytest.fail(f"Schema Mismatched{e}")
30+
31+
def test_successful_booking_creation(self):
32+
booking_data = {
33+
"firstname": "Prashant",
34+
"lastname": "Farakate",
35+
"totalprice": 100,
36+
"depositpaid": True,
37+
"bookingdates": {
38+
"checkin": "2024-01-01",
39+
"checkout": "2024-01-05"
40+
},
41+
"additionalneeds": "Dinner"
42+
}
43+
44+
response = api_client.create_booking(booking_data)
45+
assert response.status_code == 200
46+
assert response.as_dict["booking"]["firstname"] == "Prashant"
47+
assert response.as_dict["booking"]["lastname"] == "Farakate"
48+
assert response.as_dict["booking"]["additionalneeds"] == "Dinner"
49+
50+
def test_update_booking(self):
51+
ids = api_client.get_booking_ids()
52+
id = ids.as_dict[0]["bookingid"]
53+
54+
updated_data = {
55+
"firstname": "Sachin",
56+
"lastname": "Khot",
57+
"totalprice": 1200,
58+
"depositpaid": True,
59+
"bookingdates": {
60+
"checkin": "2024-01-01",
61+
"checkout": "2024-01-05"
62+
},
63+
"additionalneeds": "Lunch"
64+
}
65+
response = api_client.update_booking(id, updated_data)
66+
67+
assert response.status_code == 200
68+
assert response.as_dict["firstname"] == "Sachin"
69+
assert response.as_dict["lastname"] == "khot"
70+
assert response.as_dict["totalprice"] == 1200
71+
assert response.as_dict["depositpaid"] == True
72+
assert response.as_dict["additionalneeds"] == "Lunch"
73+
74+
def test_partial_update_booking(self):
75+
ids = api_client.get_booking_ids()
76+
id = ids.as_dict[0]["bookingid"]
77+
78+
partially_updated_data = {
79+
"totalprice": 2400,
80+
"additionalneeds": "Dinner"
81+
}
82+
response = api_client.partial_update_booking(id, partially_updated_data)
83+
84+
assert response.status_code == 200
85+
assert response.as_dict["totalprice"] == 2400
86+
assert response.as_dict["additionalneeds"] == "Dinner"
87+
88+
def test_delete_booking(self):
89+
ids = api_client.get_booking_ids()
90+
id = ids.as_dict[0]["bookingid"]
91+
response = api_client.delete_booking(id=id)
92+
assert response.status_code == 201
393

4-
def test_get_booking_ids():
5-
response = api_client.get_booking_ids()
6-
assert response.status_code == 200
794

Binary file not shown.

0 commit comments

Comments
 (0)