-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
320 lines (263 loc) · 9.06 KB
/
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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
def print_graph(g):
print(f"People: {len(g.people)}")
for pid, p in g.people.items():
print(f" * {p.name} ({p.id})")
for s in p.skills:
print(f" - {s.name} ({s.id})")
print(f"Skills: {len(g.skills)}")
for sid, s in g.skills.items():
print(f" * {s.name} ({s.id})")
for p in s.people:
print(f" - {p.name} ({p.id})")
print(f"People skills: {len(g.people_skills)}")
for person, skill in g.people_skills:
print(f"{person.name} <-> {skill.name}")
#############################
# Model tests
#############################
import model
p1 = model.Person(1, "Alice")
p2 = model.Person(2, "Bob")
p3 = model.Person(3, "Charles")
s1 = model.Skill(11, "Dancing")
s2 = model.Skill(22, "Fishing")
g = model.Graph()
g.add_person(p1)
g.add_person(p2)
g.add_person(p3)
g.add_skill(s1)
g.add_skill(s2)
g.link_person_to_skill(1, 11)
g.link_person_to_skill(1, 22)
g.link_person_to_skill(2, 11)
g.link_person_to_skill(3, 22)
assert g.people[p1.id] == p1
assert g.people[p2.id] == p2
assert g.people[p3.id] == p3
assert g.skills[s1.id] == s1
assert g.skills[s2.id] == s2
assert p1 in s1.people
assert p1 in s2.people
assert p2 in s1.people
assert p3 in s2.people
assert s1 in p1.skills
assert s2 in p1.skills
assert s1 in p2.skills
assert s2 in p3.skills
assert len(g.people_skills) == 4
for s in [s1, s2]:
for p in s.people:
assert (p, s) in g.people_skills
for p in [p1, p2, p3]:
for s in p.skills:
assert (p, s) in g.people_skills
print_graph(g)
#############################
# Repository tests
#############################
import os
import repository
TEST_DB_FILE = 'test-db.sqlite'
if os.path.exists(TEST_DB_FILE):
os.remove(TEST_DB_FILE)
# Create and initialize new DB
tmp_repo = repository.SqliteRepository(TEST_DB_FILE)
# Open exsisting DB
repo = repository.SqliteRepository(TEST_DB_FILE)
# Test all tables are empty
assert repo.get_skills_count() == 0
assert repo.get_people_count() == 0
assert repo.get_people_skills_count() == 0
assert repo.get_skills()[:] == []
assert repo.get_people()[:] == []
assert repo.get_people_skills()[:] == []
assert repo.find_people_by_id([1, 2, 3, 4]) == []
assert repo.find_skills_by_id([1, 2, 3, 4]) == []
assert repo.find_people_skills_of_people([1, 2, 3, 4]) == []
assert repo.find_people_skills_of_skills([1, 2, 3, 4]) == []
assert repo.find_skills_by_name("") == []
# Add 3 skills
repo.add_skill(111, "Hunting")
repo.add_skill(222, "Fishing")
repo.add_skill(333, "Farming")
# Test skills
assert repo.get_skills_count() == 3
assert repo.skill_exists(111)
assert repo.skill_exists(222)
assert repo.skill_exists(333)
assert not repo.skill_exists(444)
assert repo.find_skill("Hunting") == 111
assert repo.find_skill("Fishing") == 222
assert repo.find_skill("Farming") == 333
assert repo.find_skill("Dancing") == None
assert len(repo.find_skills_by_id([111, 222, 333, 555])) == 3
assert len(repo.find_skills_by_name("")) == 3
assert len(repo.find_skills_by_name("ing")) == 3
assert len(repo.find_skills_by_name("FISH")) == 1
skills = repo.get_skills()
assert len(skills) == 3
for s in skills:
if s.id == 111:
assert s.name == "Hunting"
elif s.id == 222:
assert s.name == "Fishing"
elif s.id == 333:
assert s.name == "Farming"
else:
assert False
# Test overwrite skill name (no effect, and no duplicates)
repo.add_skill(222, "Phishing")
assert repo.get_skills_count() == 3
skills = repo.get_skills()
assert len(skills) == 3
for s in skills:
if s.id == 111:
assert s.name == "Hunting"
elif s.id == 222:
assert s.name == "Fishing"
elif s.id == 333:
assert s.name == "Farming"
else:
assert False
# Add 2 people
repo.add_person(1, "Joe")
repo.add_person(2, "Sammy")
# Test people
assert repo.get_people_count() == 2
people = repo.get_people()
assert len(people) == 2
assert [p for p in people if p.id == 1][0].name == "Joe"
assert [p for p in people if p.id == 2][0].name == "Sammy"
assert len(repo.find_people_by_id([1, 2, 3, 4])) == 2
# Test overwrite person name (no effect, and no duplicates)
repo.add_person(1, "Joe Reloaded")
assert repo.get_people_count() == 2
people = repo.get_people()
assert len(people) == 2
assert [p for p in people if p.id == 1][0].name == "Joe"
assert [p for p in people if p.id == 2][0].name == "Sammy"
# Add 4 people-skills relationships
repo.add_person_skill(1, 111)
repo.add_person_skill(2, 111)
repo.add_person_skill(1, 222)
repo.add_person_skill(2, 333)
# Test people skills
assert repo.get_people_skills_count() == 4
people_skills = repo.get_people_skills()
assert len(people_skills) == 4
assert (1, 111) in people_skills
assert (2, 111) in people_skills
assert (1, 222) in people_skills
assert (2, 333) in people_skills
assert len(repo.find_people_skills_of_people([1, 2])) == 4
assert len(repo.find_people_skills_of_people([1, 999])) == 2
assert len(repo.find_people_skills_of_people([2])) == 2
assert len(repo.find_people_skills_of_skills([111])) == 2
assert len(repo.find_people_skills_of_skills([111, 999])) == 2
assert len(repo.find_people_skills_of_skills([111, 222, 333])) == 4
# Remove one person skill
repo.remove_person_skill(1, 222)
# Test people skills
assert repo.get_people_skills_count() == 3
people_skills = repo.get_people_skills()
assert len(people_skills) == 3
assert (1, 111) in people_skills
assert (2, 111) in people_skills
assert not (1, 222) in people_skills
assert (2, 333) in people_skills
assert len(repo.find_people_skills_of_people([1, 2])) == 3
assert len(repo.find_people_skills_of_people([1, 999])) == 1
assert len(repo.find_people_skills_of_people([2])) == 2
assert len(repo.find_people_skills_of_skills([111])) == 2
assert len(repo.find_people_skills_of_skills([111, 999])) == 2
assert len(repo.find_people_skills_of_skills([111, 222, 333])) == 3
# Remove non-existent person skill
repo.remove_person_skill(1, 222)
# Test people skills
people_skills = repo.get_people_skills()
assert len(people_skills) == 3
assert (1, 111) in people_skills
assert (2, 111) in people_skills
assert (2, 333) in people_skills
# Add duplicate people-skills relationships
repo.add_person_skill(1, 111)
repo.add_person_skill(1, 111)
repo.add_person_skill(1, 111)
# Test people skills
people_skills = repo.get_people_skills()
assert len(people_skills) == 3
assert (1, 111) in people_skills
assert (2, 111) in people_skills
assert (2, 333) in people_skills
#############################
# Repository + model test
#############################
g = repo.get_graph_snapshot()
print_graph(g)
assert len(g.people) == 2
assert len(g.skills) == 3
assert len(g.people_skills) == 3
# Subgraph filtered by people
people_subgraph = repo.get_people_subgraph_snapshot([2, 9999])
print_graph(people_subgraph)
assert len(people_subgraph.people) == 1
assert len(people_subgraph.skills) == 2
assert len(people_subgraph.people_skills) == 2
# Subgraph filtered by skills
skill_ids = set()
for skill_name in ['fish', 'farm']:
for skill in repo.find_skills_by_name(skill_name):
skill_ids.add(skill.id)
skills_subgraph = repo.get_skills_subgraph_snapshot(skill_ids)
print_graph(skills_subgraph)
assert len(skills_subgraph.people) == 1
assert len(skills_subgraph.skills) == 2
assert len(skills_subgraph.people_skills) == 1
#############################
# Model rendering test
#############################
alice = model.Person(1, "Alice")
bob = model.Person(2, "Bob")
charles = model.Person(3, "Charles")
diana = model.Person(4, "Diana")
cooking = model.Skill(11, "Cooking")
gardening = model.Skill(22, "Gardening")
fishing = model.Skill(33, "Fishing")
hunting = model.Skill(44, "Hunting")
woodworking = model.Skill(55, "Woodworking")
g = model.Graph()
g.add_person(alice)
g.add_person(bob)
g.add_person(charles)
g.add_person(diana)
g.add_skill(cooking)
g.add_skill(gardening)
g.add_skill(fishing)
g.add_skill(hunting)
g.add_skill(woodworking)
g.link_person_to_skill(alice.id, cooking.id)
g.link_person_to_skill(alice.id, fishing.id)
g.link_person_to_skill(alice.id, hunting.id)
g.link_person_to_skill(bob.id, woodworking.id)
g.link_person_to_skill(charles.id, cooking.id)
g.link_person_to_skill(charles.id, fishing.id)
g.link_person_to_skill(diana.id, cooking.id)
g.link_person_to_skill(diana.id, gardening.id)
import os
from render import *
dot_full_graph = FullGraphDotRenderer(g).render()
png_file = ImageFileRenderer(dot_full_graph).render(path_prefix="test-full.dot")
assert png_file == "test-full.dot.jpg"
assert os.path.exists(png_file)
dot_word_cloud_graph = WordCloudDotRenderer(g).render()
png_file = ImageFileRenderer(dot_word_cloud_graph).render(path_prefix="test-word-cloud.dot")
assert png_file == "test-word-cloud.dot.jpg"
assert os.path.exists(png_file)
dot_graph_sub_people = SubGraphDotRenderer(people_subgraph).render()
png_file = ImageFileRenderer(dot_graph_sub_people).render(path_prefix="test-sub-people.dot")
assert png_file == "test-sub-people.dot.jpg"
assert os.path.exists(png_file)
dot_graph_sub_skills = SubGraphDotRenderer(skills_subgraph).render()
png_file = ImageFileRenderer(dot_graph_sub_skills).render(path_prefix="test-sub-skills.dot")
assert png_file == "test-sub-skills.dot.jpg"
assert os.path.exists(png_file)