forked from benevolentprof/inf1340_2015_asst3
/
test_exercise1.py
152 lines (111 loc) · 3.6 KB
/
test_exercise1.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
#!/usr/bin/env python
""" Assignment 3, Exercise 1, INF1340, Fall, 2015. DBMS
Test module for exercise3.py
"""
__author__ = 'Farid Gassanov, Dusan Miletic, Tessie Riggs'
__email__ = "farid.gassanov@mail.utoronto.ca, dusan.miletic@utoronto.ca, tessie.riggs@gmail.com"
__copyright__ = "2015 Farid Gassanov, Dusan Miletic, Tessie Riggs"
from exercise1 import selection, project, cross_product
###########
# TABLES ##
###########
STUDENTS = [["FirstName", "Surname", "IQ", "GPA"],
["Hoban", "Washburne", 94, 3.2],
["Zoe", "Washburne", 110, 3.5],
["Malcolm", "Reynolds", 81, 2.6],
["Jayne", "Cobb", 55, 1.1],
["Inara", "Serra", 158, 4.0]]
EMPTY_LIST = [["FirstName", "Surname", "IQ", "GPA"]]
COMPLETELY_EMPTY1 = []
COMPLETELY_EMPTY2 = []
R1 = [["Student", "Class"],
["Zoe", "Programming"],
["Jayne", "Web Design"],
["Inara", "Programming"]]
R2 = [["Class", "Professor"],
["Programming", "Sim"],
["Web Design", "Yu"]]
#####################
# HELPER FUNCTIONS ##
#####################
def is_equal(t1, t2):
t1.sort()
t2.sort()
return t1 == t2
#####################
# FILTER FUNCTIONS ##
#####################
def filter_students(row):
"""
Check if student represented by row has an IQ
of at least 90 and a GPA of more than 3.4
:param row: A List in the format:
[{First name}, {Surname}, {IQ}, {GPA}]
:return: True if the row satisfies the
condition.
"""
return row[-2] >= 90 and row[-1] > 3.4
###################
# TEST FUNCTIONS ##
###################
def test_selection():
"""
Test the selection operation.
"""
result = [["FirstName", "Surname", "IQ", "GPA"],
["Zoe", "Washburne", 110, 3.5],
["Inara", "Serra", 158, 4.0]]
assert is_equal(result, selection(STUDENTS, filter_students))
def test_empty_list_selection():
"""
Test selection operation when using a list that is empty.
"""
try:
selection(EMPTY_LIST, filter_students)
except AttributeError:
assert True
def test_project():
"""
Test the projection operation.
"""
result = [["FirstName", "Surname"],
["Hoban", "Washburne"],
["Zoe", "Washburne"],
["Malcolm", "Reynolds"],
["Jayne", "Cobb"],
["Inara", "Serra"]]
assert is_equal(result, project(STUDENTS, ["FirstName", "Surname"]))
def test_empty_list_projection():
"""
Test projection operation when using a list that is empty.
"""
try:
project(COMPLETELY_EMPTY1, ["Surname"])
except IndexError:
assert True
def test_wrong_column_projection():
"""
Test the projection operation when faced with a column head that did not exist, i.e. an
Unknown Attribute.
"""
try:
project(EMPTY_LIST, ["Does Not Exist"])
except AttributeError:
assert True
def test_cross_product():
"""
Test cross product operation.
"""
result = [["Student", "Class", "Class", "Professor"],
["Zoe", "Programming", "Programming", "Sim"],
["Zoe", "Programming", "Web Design", "Yu"],
["Jayne", "Web Design", "Web Design", "Yu"],
["Jayne", "Web Design", "Programming", "Sim"],
["Inara", "Programming", "Programming", "Sim"],
["Inara", "Programming", "Web Design", "Yu"]]
assert is_equal(result, cross_product(R1, R2))
def test_empty_cross_product():
"""
Test cross product operation if uses on empty lists.
"""
assert cross_product(EMPTY_LIST, COMPLETELY_EMPTY1) is None