forked from benevolentprof/inf1340_2015_asst2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_exercise3.py
284 lines (205 loc) · 7.04 KB
/
test_exercise3.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
#!/usr/bin/env python
""" Assignment 2, Exercise 3, INF1340, Fall, 2015. DBMS
Test module for exercise3.py
"""
__author__ = 'Susan Sim'
__email__ = "ses@drsusansim.org"
__copyright__ = "2015 Susan Sim"
__license__ = "MIT License"
from exercise3 import union, intersection, difference, MismatchedAttributesException
###########
# TABLES ##
###########
GRADUATES = [["Number", "Surname", "Age"],
[7274, "Robinson", 37],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
MANAGERS = [["Number", "Surname", "Age"],
[9297, "O'Malley", 56],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
HEADER_ONLY = [["Number", "Surname", "Age"]]
NO_HEADER = [[]]
EMPTY_LIST = []
STAFFS = [["Number", "First Name", "Surname", "Date of Birth"],
[9297, "Andy", "O'Malley", "56"],
[7432, "Barbara", "O'Malley", "39"],
[9824, "Carl", "Darkes", "38"]]
EMPLOYEES = [["Number", "Surname", "Age"],
[1000, "Ann", 20],
[1001, "Mary", 22],
[1002, "Ming", 60]]
#####################
# HELPER FUNCTIONS ##
#####################
def is_equal(t1, t2):
return sorted(t1) == sorted(t2)
###################
# TEST FUNCTIONS ##
###################
def test_union():
"""
Test union operation.
"""
result = [["Number", "Surname", "Age"],
[7274, "Robinson", 37],
[9297, "O'Malley", 56],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
assert is_equal(result, union(GRADUATES, MANAGERS))
def test_intersection():
"""
Test intersection operation.
"""
result = [["Number", "Surname", "Age"],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
assert is_equal(result, intersection(GRADUATES, MANAGERS))
def test_difference():
"""
Test difference operation.
"""
result = [["Number", "Surname", "Age"],
[7274, "Robinson", 37]]
assert is_equal(result, difference(GRADUATES, MANAGERS))
# ==================================== Test cases added, schema are the same ==========================================
def test_union_graduates_employees():
"""
Test union operation. Table1 is the GRADUATES table; Table2 is the EMPLOYEES table.
"""
result = [["Number", "Surname", "Age"],
[7274, "Robinson", 37],
[7432, "O'Malley", 39],
[9824, "Darkes", 38],
[1000, "Ann", 20],
[1001, "Mary", 22],
[1002, "Ming", 60]]
assert is_equal(result, union(GRADUATES, EMPLOYEES))
def test_intersection_graduates_employees():
"""
Test intersection operation.Table1 is the GRADUATES table; Table2 is the EMPLOYEES table.
"""
assert intersection(GRADUATES, EMPLOYEES) is None
def test_difference_graduates_employees():
"""
Test difference operation.Table1 is the GRADUATES table; Table2 is the EMPLOYEES table.
"""
result = [["Number", "Surname", "Age"],
[7274, "Robinson", 37],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
assert is_equal(result, difference(GRADUATES, EMPLOYEES))
def test_difference_employees_graduates():
"""
Test difference operation.Table1 is the EMPLOYEES table; Table2 is the GRADUATES table.
"""
result = [["Number", "Surname", "Age"],
[1000, "Ann", 20],
[1001, "Mary", 22],
[1002, "Ming", 60]]
assert is_equal(result, difference(EMPLOYEES, GRADUATES))
def test_union_graduates_header():
"""
Test union operation. Table1 is the GRADUATES table; Table2 only has header, does not contain any record.
"""
result = [["Number", "Surname", "Age"],
[7274, "Robinson", 37],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
assert is_equal(result, union(GRADUATES, HEADER_ONLY))
def test_intersection_graduates_header():
"""
Test intersection operation.Table1 is the GRADUATES table; Table2 only has header, does not contain any record.
"""
assert intersection(GRADUATES, HEADER_ONLY) is None
def test_difference_graduates_header():
"""
Test difference operation.Table1 is the GRADUATES table; Table2 only has header, does not contain any record.
"""
result = [["Number", "Surname", "Age"],
[7274, "Robinson", 37],
[7432, "O'Malley", 39],
[9824, "Darkes", 38]]
assert is_equal(result, difference(GRADUATES, HEADER_ONLY))
def test_difference_header_graduates():
"""
Test difference operation.Table1 only does not contain any record; Table2 is the GRADUATES table.
"""
assert difference(HEADER_ONLY, GRADUATES) is None
# ===================================== Schema are different =========================================================
def test_union_different_schema_1():
"""
Test union operation.Scheme are not the same
"""
try:
union(MANAGERS, STAFFS)
except MismatchedAttributesException:
assert True
else:
assert False
def test_intersection_different_schema_1():
"""
Test intersection operation. Scheme are not the same.
"""
try:
intersection(MANAGERS, STAFFS)
except MismatchedAttributesException:
assert True
else:
assert False
def test_difference_different_schema_1():
"""
Test difference operation. Scheme are not the same.
"""
try:
difference(MANAGERS, STAFFS)
except MismatchedAttributesException:
assert True
else:
assert False
def test_union_different_schema_2():
"""
Test union operation.Scheme are not the same. Table2 only has a header.
"""
try:
union(MANAGERS, NO_HEADER)
except MismatchedAttributesException:
assert True
else:
assert False
def test_intersection_different_schema_2():
"""
Test intersection operation. Scheme are not the same.Table2 is empty.
"""
try:
intersection(MANAGERS, NO_HEADER)
except MismatchedAttributesException:
assert True
else:
assert False
def test_difference_different_schema_2():
"""
Test difference operation. Scheme are not the same.Table2 is empty.
"""
try:
difference(MANAGERS, NO_HEADER)
except MismatchedAttributesException:
assert True
else:
assert False
# =================================== table1 or table2 is an empty list ===============================================
def test_union_graduates_empty():
"""
Test union operation. Table1 is the GRADUATES table; Table2 is an empty list.
"""
assert union(GRADUATES, EMPTY_LIST) is None
def test_intersection_graduates_empty():
"""
Test intersection operation.Table1 is the GRADUATES table; Table2 is an empty list.
"""
assert intersection(GRADUATES, EMPTY_LIST) is None
def test_difference_graduates_empty():
"""
Test difference operation.Table1 is the GRADUATES table; Table2 is an empty list.
"""
assert difference(GRADUATES, EMPTY_LIST) is None