-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.test.py
211 lines (178 loc) · 6.65 KB
/
lib.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
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
import sqlite3
import unittest
from datetime import datetime, timedelta
from typing import List, Tuple
from lib import respond
from params import max_recs_per_day
from translations import translations
nb_bridges_per_pool = 3
con = sqlite3.connect(":memory:")
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("CREATE TABLE bridges (value TEXT, pool INT)")
cur.execute("CREATE TABLE users (username TEXT, bridge TEXT, trust FLOAT)")
cur.execute(
"CREATE TABLE recommendations (src TEXT, dst TEXT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
)
def fill_db(
bridges: List[Tuple] = [],
users: List[Tuple] = [],
recommendations: List[Tuple] = [],
) -> None:
cur = con.cursor()
cur.execute("DELETE FROM bridges")
cur.executemany("INSERT INTO bridges (value, pool) VALUES (?, ?)", bridges)
cur.execute("DELETE FROM users")
cur.executemany(
"INSERT INTO users (username, bridge, trust) VALUES (?, ?, ?)", users
)
cur.execute("DELETE FROM recommendations")
cur.executemany(
"INSERT INTO recommendations (src, dst, ts) VALUES (?, ?, ?)", recommendations
)
class TestRespond(unittest.TestCase):
def assert_db_equals(
self,
bridges: List[Tuple] = [],
users: List[Tuple] = [],
recommendations: List[Tuple] = [],
) -> None:
cur = con.cursor()
self.assertEqual(
[
(list(row)[0], list(row)[1])
for row in cur.execute("SELECT * FROM bridges").fetchall()
],
bridges,
)
self.assertEqual(
[
(list(row)[0], list(row)[1], list(row)[2])
for row in cur.execute("SELECT * FROM users").fetchall()
],
users,
)
self.assertEqual(
[
(list(row)[0], list(row)[1]) # do not check time stamp
for row in cur.execute("SELECT * FROM recommendations").fetchall()
],
recommendations,
)
## help
def test_help(self):
fill_db()
response = respond(con, "help", "bobby")
self.assertEqual(response, translations["en"]["help_text"])
## get_bridge
# No bridges available: should fail gracefully
def test_get_bridge_0(self):
fill_db()
response = respond(con, "get_bridge", "bobby")
self.assertEqual(response, "No bridges available")
# User does not exist: should fail gracefully
def test_get_bridge_1(self):
fill_db(
bridges=[("b0", 0), ("b1", 0), ("b2", 0)], users=[("not-bobby", "b0", 0)]
)
response = respond(con, "get_bridge", "bobby")
self.assertEqual(response, "Failed: you are unknown")
# User exist but is not trusted: should fail gracefully
def test_get_bridge_2(self):
fill_db(bridges=[("b0", 0), ("b1", 0), ("b2", 0)], users=[("bobby", "b0", 0)])
response = respond(con, "get_bridge", "bobby")
self.assertEqual(response, "Failed: you are not trusted enough")
# User already exists and is trusted: should send the same bridge when asked several times
def test_get_bridge_3(self):
fill_db(bridges=[("b0", 0), ("b1", 0), ("b2", 0)], users=[("bobby", "b0", 0.8)])
first_response = respond(con, "get_bridge", "bobby")
self.assertTrue(first_response in ["b0", "b1", "b2"])
for i in range(10):
subsequent_response = respond(con, "get_bridge", "bobby")
self.assertEqual(first_response, subsequent_response)
## recommend
# No specified username: should respond with help text
def test_recommend_0(self):
fill_db()
response = respond(con, "recommend", "bobby")
self.assertEqual(response, translations["en"]["help_text"])
# Unknown recommender
def test_recommend_1(self):
fill_db()
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(response, "Failed: you are unknown")
# Untrusted recommender
def test_recommend_2(self):
fill_db(users=[("bobby", None, 0)])
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(response, "Failed: you are not trusted yet")
# Too many recommendations
def test_recommend_3(self):
fill_db(
users=[("bobby", None, 0.8)],
recommendations=[
("bobby", f"friend of bobby #{i}", datetime.now())
for i in range(2 * max_recs_per_day)
],
)
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(response, "Can't recomment more than 5 users per day")
# Already recommended
def test_recommend_4(self):
fill_db(
users=[("bobby", None, 0.8), ("charlie", None, 0.4)],
recommendations=[("bobby", "charlie", datetime.now() - timedelta(days=2))],
)
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(response, "You already recommended charlie")
# Recomendee does not exist yet
def test_recommend_5(self):
fill_db(
users=[("bobby", None, 0.8)],
)
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(response, "Successfully recommended charlie")
self.assert_db_equals(
users=[("bobby", None, 0.8), ("charlie", None, 0.4)],
recommendations=[("bobby", "charlie")],
)
# Recomendee exists and is more trustworthy
def test_recommend_6(self):
users = [("bobby", None, 0.5), ("charlie", None, 0.8)]
fill_db(
users=users,
)
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(
response,
"charlie is already more trusted than you. You can recommend someone else.",
)
self.assert_db_equals(
users=users,
recommendations=[],
)
# Recomendee exists and is less trustworthy
def test_recommend_7(self):
fill_db(
users=[("bobby", None, 0.8), ("charlie", None, 0.2)],
)
response = respond(con, "recommend charlie", "bobby")
self.assertEqual(
response,
"Successfully improved trust of charlie",
)
self.assert_db_equals(
users=[("bobby", None, 0.8), ("charlie", None, 0.5)],
recommendations=[("bobby", "charlie")],
)
## default
def test_default(self):
fill_db()
response = respond(
con,
"not_a_known_command",
"bobby",
)
self.assertEqual(response, translations["en"]["help_text"])
if __name__ == "__main__":
unittest.main()