-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtests.py
265 lines (249 loc) · 9.29 KB
/
tests.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
import os
import unittest
from defog_data.metadata import get_db, dbs
class TestDB(unittest.TestCase):
def test_load_all_in_diff_dir(self):
# get current directory
test_dir = os.getcwd()
# cd to /tmp and attempt to load a db
os.chdir("/tmp")
all_db_names = [
"academic",
"advising",
"atis",
"broker",
"car_dealership",
"derm_treatment",
"ewallet",
"geography",
"restaurants",
"scholar",
"yelp",
]
for db_name in all_db_names:
db = get_db(db_name)
db_schema = db["table_metadata"]
assert len(db_schema) > 0
assert "glossary" in db
os.chdir(test_dir)
def dbs_exist(self):
assert len(dbs) == 11
# check that all the tables exist in each db
def test_academic(self):
db_name = "academic"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"cite",
"author",
"domain",
"writes",
"journal",
"keyword",
"conference",
"publication",
"organization",
"domain_author",
"domain_journal",
"domain_keyword",
"domain_conference",
"domain_publication",
"publication_keyword",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 42)
def test_advising(self):
db_name = "advising"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"area",
"course",
"program",
"student",
"semester",
"instructor",
"program_course",
"student_record",
"course_offering",
"course_tags_count",
"comment_instructor",
"course_prerequisite",
"offering_instructor",
"program_requirement",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 109)
def test_atis(self):
db_name = "atis"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"city",
"days",
"fare",
"month",
"state",
"flight",
"airline",
"airport",
"aircraft",
"time_zone",
"fare_basis",
"flight_leg",
"flight_fare",
"flight_stop",
"restriction",
"dual_carrier",
"food_service",
"time_interval",
"ground_service",
"airport_service",
"class_of_service",
"code_description",
"compartment_class",
"equipment_sequence",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 127)
def test_broker(self):
db_name = "broker"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"sbCustomer",
"sbTicker",
"sbDailyPrice",
"sbTransaction",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
glossary = get_db(db_name)["glossary"]
expected_glossary = """- sbTicker can be joined to sbDailyPrice on sbTickerId
- sbCustomer can be joined to sbTransaction on sbCustId
- sbTicker can be joined to sbTransaction on sbTickerId
- ADV (Average Daily Volume) for a ticker = AVG(sbDpVolume) from sbDailyPrice table for that ticker
- ATH (All Time High) price for a ticker = MAX(sbDpHigh) from sbDailyPrice table for that ticker
- ATP (Average Transaction Price) for a customer = SUM(sbTxAmount)/SUM(sbTxShares) from sbTransaction table for that customer
- NCT (Net Commission Total) = SUM(sbTxCommission) from sbTransaction table"""
self.assertEqual(glossary, expected_glossary)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 43)
def test_car_dealership(self):
db_name = "car_dealership"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"cars",
"salespersons",
"customers",
"sales",
"inventory_snapshots",
"payments_received",
"payments_made",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 55)
def test_derm_treatment(self):
db_name = "derm_treatment"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"doctors",
"patients",
"drugs",
"diagnoses",
"treatments",
"outcomes",
"adverse_events",
"concomitant_meds",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 81)
def test_ewallet(self):
db_name = "ewallet"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"consumer_div.users",
"consumer_div.merchants",
"consumer_div.coupons",
"consumer_div.wallet_transactions_daily",
"consumer_div.wallet_user_balance_daily",
"consumer_div.wallet_merchant_balance_daily",
"consumer_div.notifications",
"consumer_div.user_sessions",
"consumer_div.user_setting_snapshot",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
glossary = get_db(db_name)["glossary"]
expected_glossary = """- sender_id and receiver_id can be joined with either users.uid or merchants.mid depending on the sender_type/receiver_type
- if a user applied a coupon to a purchase, there will be 2 rows in wallet_transactions_daily:
- 1st row where coupon_id is NULL, amount = purchase value
- 2nd row where coupon_id is NOT NULL, amount = coupon value applied
- the sender and receiver id will be the same for both rows, but they will have different txid's
- when using coupons.code, wallet_transactions_daily.gateway_name, filter case insensitively
- Total Transaction Volume (TTV) = SUM(wallet_transactions_daily.amount)
- Total Coupon Discount Redeemed (TCDR) = SUM(wallet_transactions_daily.amount) WHERE coupon_id IS NOT NULL
- Session Density = COUNT(user_sessions.user_id) / COUNT(DISTINCT user_sessions.user_id)
- Active Merchants Percentage (APM) = COUNT(DISTINCT CASE WHEN sender_type = 1 THEN wallet_transactions_daily.sender_id WHEN receiver_type = 1 THEN wallet_transactions_daily.receiver_id ELSE NULL END) / COUNT(DISTINCT merchants.mid)"""
print(glossary)
self.assertEqual(glossary, expected_glossary)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 96)
def test_geography(self):
db_name = "geography"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"city",
"lake",
"river",
"state",
"highlow",
"mountain",
"border_info",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 29)
def test_restaurants(self):
db_name = "restaurants"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = ["location", "geographic", "restaurant"]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 12)
def test_scholar(self):
db_name = "scholar"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"cite",
"field",
"paper",
"venue",
"author",
"writes",
"dataset",
"journal",
"keyphrase",
"paperfield",
"paperdataset",
"paperkeyphrase",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 28)
def test_yelp(self):
db_name = "yelp"
db_schema = get_db(db_name)["table_metadata"]
expected_tables = [
"tip",
"users",
"review",
"checkin",
"business",
"category",
"neighbourhood",
]
self.assertEqual(list(db_schema.keys()), expected_tables)
num_columns = sum([len(db_schema[table]) for table in db_schema])
self.assertEqual(num_columns, 36)
if __name__ == "__main__":
unittest.main()