-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_furniture.py
252 lines (214 loc) · 8.03 KB
/
test_furniture.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import unittest
import furniture
import json
import sqlite3
from unittest.mock import patch
import db
class TestFurniture(unittest.TestCase):
@patch('sqlite3.connect')
def test_post_furniture_happy_path(self, mock):
params = None
has_commit = False
class MockConn:
@staticmethod
def execute(a, b):
nonlocal params
params = b
@staticmethod
def commit():
nonlocal has_commit
has_commit = True
@staticmethod
def close():
pass
mock.return_value = MockConn
ret, code = furniture.create_furniture({
"title": "mocktitle",
"labels": "mocklabel",
"image_url": "mockurl",
"description": "mockdesc"}, "mockuser")
self.assertEqual(code, 201)
self.assertEqual(ret, json.dumps({"error": ""}))
self.assertEqual(
params,
["mockuser", "mocktitle", "mocklabel", "mockurl", "mockdesc"]
)
self.assertTrue(has_commit)
def test_post_furniture_input_invalid(self):
tests = [
{"invalid_field": "invalid"},
{
"title": 255,
"labels": "invalid type",
"image_url": "xxx",
"description": "sss"
},
{"title": "missing some fields"}
]
for test in tests:
ret, code = furniture.create_furniture(test, "mockuser")
self.assertEqual(ret, json.dumps({"error": "input invalid."}))
self.assertEqual(code, 400)
@patch('sqlite3.connect')
def test_post_furniture_sql_error(self, mock):
mock.side_effect = sqlite3.Error("mockerr")
ret, code = furniture.create_furniture({
"title": "mockt",
"labels": "mocklabel",
"image_url": "xxx",
"description": "sss"}, "mockuser")
self.assertEqual(code, 500)
self.assertEqual(ret, json.dumps({"error": "db error: mockerr"}))
@patch('sqlite3.connect')
def test_rate_buyer_happy_path(self, mock):
class MockCursor:
@staticmethod
def fetchone():
return ["[email protected]", "[email protected]", "completed"]
class MockConn:
def __init__(self):
self.exec_cnt = 0
self.params = []
self.has_commit = None
def execute(self, a, b):
self.exec_cnt += 1
self.params.append(b)
if self.exec_cnt == 1:
return MockCursor
def commit(self):
self.has_commit = True
def close(self):
pass
mock.return_value = MockConn()
ret, code = furniture.rate_owner("mockfid", "[email protected]", 5)
self.assertEqual(ret, json.dumps({"error": ""}))
self.assertEqual(code, 200)
self.assertEqual(mock.return_value.exec_cnt, 3)
self.assertEqual(mock.return_value.params,
[["mockfid"], [5, "[email protected]"], ["mockfid"]])
self.assertEqual(mock.return_value.has_commit, True)
@patch('sqlite3.connect')
def test_rate_buyer_client_error(self, mock):
class MockCursor:
def __init__(self, rec):
self.rec = rec
def fetchone(self):
return self.rec
class MockConn:
def __init__(self, rec):
self.rec = rec
self.has_commit = False
def execute(self, a, b):
return MockCursor(self.rec)
def close(self):
pass
for fid, buyer_email, rating, dbrec, exp_msg in [
(
"", "", 10000, None,
"rating score should be in the range of [0, 5]"
),
(
"", "", -1, None,
"rating score should be in the range of [0, 5]"
),
(
"mockfid", "[email protected]", 5, None,
"fid not existed"
),
(
"mockfid", "[email protected]", 5,
("[email protected]", "", "pending"),
"please rate after the transaction is completed"
),
(
"mockfid", "[email protected]", 5,
("[email protected]", "", "completed"),
"rating can only be triggered by the buyer"
),
(
"mockfid", "[email protected]", 5,
("[email protected]", "", "rated"),
"this transaction has been rated"
),
]:
mock.return_value = MockConn(dbrec)
ret, code = furniture.rate_owner(fid, buyer_email, rating)
ret = json.loads(ret)
self.assertEqual(code, 400)
self.assertEqual(ret["error"], exp_msg)
self.assertFalse(mock.return_value.has_commit)
@patch('sqlite3.connect')
def test_rate_buyer_sql_error(self, mock):
mock.side_effect = sqlite3.Error("mockerr")
ret, code = furniture.rate_owner("fid", "buyer", 5)
self.assertEqual(code, 500)
self.assertEqual(ret, json.dumps({"error": "db error: mockerr"}))
def test_buy_furniture_happy_path(self):
db.clear()
db.init_db()
db.insert_mock_user()
db.insert_mock_furniture()
resp, code = furniture.buy_furniture(1, "[email protected]")
self.assertEqual(code, 200)
self.assertEqual(resp, json.dumps({"error": ""}))
def test_buy_furniture_no_such_furniture(self):
db.clear()
db.init_db()
db.insert_mock_user()
resp, code = furniture.buy_furniture(1, "[email protected]")
self.assertEqual(resp, json.dumps({"error": "furniture not existed"}))
self.assertEqual(code, 400)
def test_buy_furniture_already_pending(self):
db.clear()
db.init_db()
db.insert_mock_user()
db.insert_mock_furniture()
_, _ = furniture.buy_furniture(1, "[email protected]")
resp, code = furniture.buy_furniture(1, "[email protected]")
self.assertEqual(resp, json.dumps(
{"error": "The item is already sold or in progress"}))
self.assertEqual(code, 400)
def test_owner_confirm_happy_path(self):
db.clear()
db.init_db()
db.insert_mock_user()
db.insert_mock_furniture()
db.insert_mock_buyer()
db.mock_buy('pending')
resp, code = furniture.owner_confirm(1, "[email protected]", "True")
self.assertEqual(resp, json.dumps({"error": ""}))
self.assertEqual(code, 200)
def test_owner_confirm_no_such_fid(self):
db.clear()
db.init_db()
db.insert_mock_user()
db.insert_mock_furniture()
db.insert_mock_buyer()
db.mock_buy('pending')
resp, code = furniture.owner_confirm(10, "[email protected]", "True")
self.assertEqual(resp, json.dumps({"error": "fid not existed"}))
self.assertEqual(code, 400)
def test_owner_confirm_not_owner(self):
db.clear()
db.init_db()
db.insert_mock_user()
db.insert_mock_furniture()
db.insert_mock_buyer()
db.mock_buy('pending')
resp, code = furniture.owner_confirm(
1, "[email protected]", "True")
self.assertEqual(resp, json.dumps(
{"error": "Only owner can confirm the transaction."}))
self.assertEqual(code, 400)
def test_owner_confirm_not_pending(self):
db.clear()
db.init_db()
db.insert_mock_user()
db.insert_mock_furniture()
db.insert_mock_buyer()
db.mock_buy('init')
resp, code = furniture.owner_confirm(1, "[email protected]", "True")
self.assertEqual(resp, json.dumps(
{"error": "the owner can only confirm "
"the pending transaction"}))
self.assertEqual(code, 400)