forked from ashiqks/Flask-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_querybuilder.py
More file actions
200 lines (167 loc) · 7.07 KB
/
Copy pathtest_querybuilder.py
File metadata and controls
200 lines (167 loc) · 7.07 KB
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
import unittest2
import querybuilder as qb
class TestQueryBuilder(unittest2.TestCase):
def test_empty(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {"is_in": {}, "is_btwn": {}}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = "SELECT `school_name` FROM myTable"
self.assertEqual(actualSQL, expectedSQL)
def test_empty_multiple_select(self):
tablename = "myTable"
select_cols = ["school_name", "midpoint.act.score"]
filter_dict = {"is_in": {}, "is_btwn": {}}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = "SELECT `school_name`, `midpoint_act_score` FROM myTable"
self.assertEqual(actualSQL, expectedSQL)
def test_nested_empty_in(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {
"school.region_id": [],
"school.ownership": [],
"school.degrees_awarded.highest": [],
"school.institutional_characteristics.level": [],
"school.minority_serving.historically_black": [],
"singlesex.or.coed": []
},
"is_btwn": {}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = "SELECT `school_name` FROM myTable"
self.assertEqual(actualSQL, expectedSQL)
def test_nested_empty_in_with_nonempty_in(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {
"school.region_id": [],
"school.ownership": ["Private nonprofit"],
"school.degrees_awarded.highest": [],
"school.institutional_characteristics.level": [],
"school.minority_serving.historically_black": [],
"singlesex.or.coed": []
},
"is_btwn": {}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = 'SELECT `school_name` FROM myTable WHERE `school_ownership` IN ("Private nonprofit")'
self.assertEqual(actualSQL, expectedSQL)
def test_nested_empty_in_with_nonempty_in_and_nonempty_btwn(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {
"school.region_id": [],
"school.ownership": ["Private nonprofit"],
"school.degrees_awarded.highest": [],
"school.institutional_characteristics.level": [],
"school.minority_serving.historically_black": [],
"singlesex.or.coed": []
},
"is_btwn": {
"latest.student.size": {
"min": 0,
"max": 50000,
"inclusive": "true"
}
}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = 'SELECT `school_name` FROM myTable WHERE `latest_student_size` BETWEEN 0 AND 50000 AND `school_ownership` IN ("Private nonprofit")'
self.assertEqual(actualSQL, expectedSQL)
def test_btwn_one_condition(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {},
"is_btwn": {
"midpoint.act.score": {
"min": 28,
"max": 34,
"inclusive": True
}
}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = "SELECT `school_name` FROM myTable WHERE `midpoint_act_score` BETWEEN 28 AND 34"
self.assertEqual(actualSQL, expectedSQL)
def test_btwn_two_conditions(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {},
"is_btwn": {
"latest.admissions.act_scores.midpoint.cumulative": {
"min": 0,
"max": 36,
"inclusive": True
},
"latest.student.size": {
"min": 0,
"max": 50000,
"inclusive": True
}
}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = "SELECT `school_name` FROM myTable WHERE `latest_admissions_act_scores_midpoint_cumulative` BETWEEN 0 AND 36 AND `latest_student_size` BETWEEN 0 AND 50000"
self.assertEqual(actualSQL, expectedSQL)
def test_in(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {
"school.region_id": ["Mid East (DE, DC, MD, NJ, NY, PA)"]
},
"is_btwn": {}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = 'SELECT `school_name` FROM myTable WHERE `school_region_id` IN ("Mid East (DE, DC, MD, NJ, NY, PA)")'
self.assertEqual(actualSQL, expectedSQL)
def test_btwn_and_in(self):
tablename = "myTable"
select_cols = ["school_name"]
filter_dict = {
"is_in": {
"school.region_id": ["Mid East (DE, DC, MD, NJ, NY, PA)"]
},
"is_btwn": {
"midpoint.act.score": {
"min": 28,
"max": 34,
"inclusive": True
}
}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = 'SELECT `school_name` FROM myTable WHERE `midpoint_act_score` BETWEEN 28 AND 34 AND `school_region_id` IN ("Mid East (DE, DC, MD, NJ, NY, PA)")'
self.assertEqual(actualSQL, expectedSQL)
def test_multiple_btwn_and_in(self):
tablename = "codetran_collegedata.collegescorecard"
select_cols = ["school_name"]
filter_dict = {
"is_in": {
"school.institutional_characteristics.level": ["4-year"],
"singlesex.or.coed": ["Single-Sex: Women", "Co-Educational"]
},
"is_btwn": {
"latest.admissions.act_scores.midpoint.cumulative": {
"min": 14,
"max": 34,
"inclusive": True
},
"latest.student.size": {
"min": 0,
"max": 35000,
"inclusive": True
}
}
}
actualSQL = qb.build_query(tablename, select_cols, filter_dict)
expectedSQL = 'SELECT `school_name` FROM codetran_collegedata.collegescorecard WHERE `latest_admissions_act_scores_midpoint_cumulative` BETWEEN 14 AND 34 AND `latest_student_size` BETWEEN 0 AND 35000 AND `school_institutional_characteristics_level` IN ("4-year") AND `singlesex_or_coed` IN ("Single-Sex: Women", "Co-Educational")'
self.assertEqual(actualSQL, expectedSQL)
if __name__ == '__main__':
unittest2.main()