-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrest_api_test.py
163 lines (150 loc) · 6.15 KB
/
rest_api_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/rest-api/canonical-data.json
# File last updated on 2023-07-19
import json
import unittest
from rest_api import (
RestAPI,
)
class RestApiTest(unittest.TestCase):
def test_no_users(self):
database = {"users": []}
api = RestAPI(database)
response = api.get("/users")
expected = {"users": []}
self.assertDictEqual(json.loads(response), expected)
def test_add_user(self):
database = {"users": []}
api = RestAPI(database)
payload = json.dumps({"user": "Adam"})
response = api.post("/add", payload)
expected = {"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0}
self.assertDictEqual(json.loads(response), expected)
def test_get_single_user(self):
database = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0},
]
}
api = RestAPI(database)
payload = json.dumps({"users": ["Bob"]})
response = api.get("/users", payload)
expected = {
"users": [{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0}]
}
self.assertDictEqual(json.loads(response), expected)
def test_both_users_have_0_balance(self):
database = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0},
]
}
api = RestAPI(database)
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 3.0})
response = api.post("/iou", payload)
expected = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
{"name": "Bob", "owes": {"Adam": 3.0}, "owed_by": {}, "balance": -3.0},
]
}
self.assertDictEqual(json.loads(response), expected)
def test_borrower_has_negative_balance(self):
database = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
{"name": "Bob", "owes": {"Chuck": 3.0}, "owed_by": {}, "balance": -3.0},
{"name": "Chuck", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
]
}
api = RestAPI(database)
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 3.0})
response = api.post("/iou", payload)
expected = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
{
"name": "Bob",
"owes": {"Adam": 3.0, "Chuck": 3.0},
"owed_by": {},
"balance": -6.0,
},
]
}
self.assertDictEqual(json.loads(response), expected)
def test_lender_has_negative_balance(self):
database = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
{"name": "Bob", "owes": {"Chuck": 3.0}, "owed_by": {}, "balance": -3.0},
{"name": "Chuck", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
]
}
api = RestAPI(database)
payload = json.dumps({"lender": "Bob", "borrower": "Adam", "amount": 3.0})
response = api.post("/iou", payload)
expected = {
"users": [
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
{
"name": "Bob",
"owes": {"Chuck": 3.0},
"owed_by": {"Adam": 3.0},
"balance": 0.0,
},
]
}
self.assertDictEqual(json.loads(response), expected)
def test_lender_owes_borrower(self):
database = {
"users": [
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 3.0}, "balance": 3.0},
]
}
api = RestAPI(database)
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 2.0})
response = api.post("/iou", payload)
expected = {
"users": [
{"name": "Adam", "owes": {"Bob": 1.0}, "owed_by": {}, "balance": -1.0},
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 1.0}, "balance": 1.0},
]
}
self.assertDictEqual(json.loads(response), expected)
def test_lender_owes_borrower_less_than_new_loan(self):
database = {
"users": [
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 3.0}, "balance": 3.0},
]
}
api = RestAPI(database)
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 4.0})
response = api.post("/iou", payload)
expected = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {"Bob": 1.0}, "balance": 1.0},
{"name": "Bob", "owes": {"Adam": 1.0}, "owed_by": {}, "balance": -1.0},
]
}
self.assertDictEqual(json.loads(response), expected)
def test_lender_owes_borrower_same_as_new_loan(self):
database = {
"users": [
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 3.0}, "balance": 3.0},
]
}
api = RestAPI(database)
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 3.0})
response = api.post("/iou", payload)
expected = {
"users": [
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0},
]
}
self.assertDictEqual(json.loads(response), expected)