|
| 1 | +from lstore.db import Database |
| 2 | +from lstore.query import Query |
| 3 | + |
| 4 | +from random import choice, randint, sample, seed |
| 5 | + |
| 6 | +score = 0 |
| 7 | +def speed_tester1(): |
| 8 | + print("Checking exam M1 normal tester"); |
| 9 | + global score |
| 10 | + db = Database() |
| 11 | + # Create a table with 5 columns |
| 12 | + # Student Id and 4 grades |
| 13 | + # The first argument is name of the table |
| 14 | + # The second argument is the number of columns |
| 15 | + # The third argument is determining the which columns will be primay key |
| 16 | + # Here the first column would be student id and primary key |
| 17 | + grades_table = db.create_table('Grades', 5, 0) |
| 18 | + |
| 19 | + # create a query class for the grades table |
| 20 | + query = Query(grades_table) |
| 21 | + |
| 22 | + # dictionary for records to test the database: test directory |
| 23 | + records = {} |
| 24 | + |
| 25 | + number_of_records = 1000 |
| 26 | + number_of_aggregates = 100 |
| 27 | + seed(3562901) |
| 28 | + |
| 29 | + for i in range(0, number_of_records): |
| 30 | + key = 92106429 + randint(0, number_of_records) |
| 31 | + |
| 32 | + #skip duplicate keys |
| 33 | + while key in records: |
| 34 | + key = 92106429 + randint(0, number_of_records) |
| 35 | + |
| 36 | + records[key] = [key, randint(0, 20), randint(0, 20), randint(0, 20), randint(0, 20)] |
| 37 | + query.insert(*records[key]) |
| 38 | + print("Insert finished") |
| 39 | + |
| 40 | + # Check inserted records using select query |
| 41 | + for key in records: |
| 42 | + # select function will return array of records |
| 43 | + # here we are sure that there is only one record in that array |
| 44 | + record = query.select(key, 0, [1, 1, 1, 1, 1])[0] |
| 45 | + error = False |
| 46 | + for i, column in enumerate(record.columns): |
| 47 | + if column != records[key][i]: |
| 48 | + error = True |
| 49 | + if error: |
| 50 | + raise Exception('select error on', key, ':', record.columns, ', correct:', records[key]) |
| 51 | + else: |
| 52 | + pass |
| 53 | + |
| 54 | + |
| 55 | + updated_records = {} |
| 56 | + for key in records: |
| 57 | + updated_columns = [None, None, None, None, None] |
| 58 | + updated_records[key] = records[key].copy() |
| 59 | + for i in range(2, grades_table.num_columns): |
| 60 | + # updated value |
| 61 | + value = randint(0, 20) |
| 62 | + updated_columns[i] = value |
| 63 | + # update our test directory |
| 64 | + updated_records[key][i] = value |
| 65 | + query.update(key, *updated_columns) |
| 66 | + |
| 67 | + #check updated result for record |
| 68 | + record = query.select(key, 0, [1, 1, 1, 1, 1])[0] |
| 69 | + error = False |
| 70 | + for j, column in enumerate(record.columns): |
| 71 | + if column != updated_records[key][j]: |
| 72 | + error = True |
| 73 | + if error: |
| 74 | + print('update error on', records[key], 'and', updated_columns, ':', record.columns, ', correct:', records[key]) |
| 75 | + else: |
| 76 | + pass |
| 77 | + score = score + 15 |
| 78 | + |
| 79 | + keys = sorted(list(records.keys())) |
| 80 | + # aggregate on every column |
| 81 | + for c in range(0, grades_table.num_columns): |
| 82 | + for i in range(0, number_of_aggregates): |
| 83 | + r = sorted(sample(range(0, len(keys)), 2)) |
| 84 | + # calculate the sum form test directory |
| 85 | + # version 0 sum |
| 86 | + updated_column_sum = sum(map(lambda key: updated_records[key][c], keys[r[0]: r[1] + 1])) |
| 87 | + updated_result = query.sum(keys[r[0]], keys[r[1]], c) |
| 88 | + if updated_column_sum != updated_result: |
| 89 | + raise Exception('sum error on column', c, '[', keys[r[0]], ',', keys[r[1]], ']: ', updated_result, ', correct: ', updated_column_sum) |
| 90 | + else: |
| 91 | + pass |
| 92 | + score = score + 15 |
| 93 | + |
| 94 | + |
| 95 | +def speed_tester2(): |
| 96 | + print("\n\nChecking exam M1 extended tester"); |
| 97 | + global score |
| 98 | + db = Database() |
| 99 | + # Create a table with 5 columns |
| 100 | + # Student Id and 4 grades |
| 101 | + # The first argument is name of the table |
| 102 | + # The second argument is the number of columns |
| 103 | + # The third argument is determining the which columns will be primary key |
| 104 | + # Here the first column would be student id and primary key |
| 105 | + grades_table = db.create_table('Grades', 5, 0) |
| 106 | + |
| 107 | + # create a query class for the grades table |
| 108 | + query = Query(grades_table) |
| 109 | + |
| 110 | + # dictionary for records to test the database: test directory |
| 111 | + records = {} |
| 112 | + |
| 113 | + number_of_records = 1000 |
| 114 | + number_of_aggregates = 100 |
| 115 | + number_of_updates = 5 |
| 116 | + seed(3562901) |
| 117 | + |
| 118 | + for i in range(0, number_of_records): |
| 119 | + key = 92106429 + randint(0, number_of_records) |
| 120 | + |
| 121 | + #skip duplicate keys |
| 122 | + while key in records: |
| 123 | + key = 92106429 + randint(0, number_of_records) |
| 124 | + |
| 125 | + records[key] = [key, randint(0, 20), randint(0, 20), randint(0, 20), randint(0, 20)] |
| 126 | + query.insert(*records[key]) |
| 127 | + # print('inserted', records[key]) |
| 128 | + print("Insert finished") |
| 129 | + |
| 130 | + # Check inserted records using select query |
| 131 | + for key in records: |
| 132 | + # select function will return array of records |
| 133 | + # here we are sure that there is only one record in that array |
| 134 | + record = query.select(key, 0, [1, 1, 1, 1, 1])[0] |
| 135 | + error = False |
| 136 | + for i, column in enumerate(record.columns): |
| 137 | + if column != records[key][i]: |
| 138 | + error = True |
| 139 | + if error: |
| 140 | + raise Exception('select error on', key, ':', record.columns, ', correct:', records[key]) |
| 141 | + else: |
| 142 | + pass |
| 143 | + # print('select on', key, ':', record) |
| 144 | + |
| 145 | + |
| 146 | + all_updates = [] |
| 147 | + keys = sorted(list(records.keys())) |
| 148 | + for i in range(number_of_updates): |
| 149 | + all_updates.append({}) |
| 150 | + for key in records: |
| 151 | + updated_columns = [None, None, None, None, None] |
| 152 | + all_updates[i][key] = records[key].copy() |
| 153 | + for j in range(2, grades_table.num_columns): |
| 154 | + # updated value |
| 155 | + value = randint(0, 20) |
| 156 | + updated_columns[j] = value |
| 157 | + # update our test directory |
| 158 | + all_updates[i][key][j] = value |
| 159 | + query.update(key, *updated_columns) |
| 160 | + |
| 161 | + try: |
| 162 | + # Check records that were persisted in part 1 |
| 163 | + version = 0 |
| 164 | + expected_update = records if version <= -number_of_updates else all_updates[version + number_of_updates - 1] |
| 165 | + for key in keys: |
| 166 | + record = query.select(key, 0, [1, 1, 1, 1, 1])[0] |
| 167 | + error = False |
| 168 | + for k, column in enumerate(record.columns): |
| 169 | + if column != expected_update[key][k]: |
| 170 | + error = True |
| 171 | + if error: |
| 172 | + raise Exception('select error on', key, ':', record.columns, ', correct:', expected_update[key]) |
| 173 | + break |
| 174 | + print("Select version ", version, "finished") |
| 175 | + score = score + 20 |
| 176 | + except Exception as e: |
| 177 | + score = score + 5 |
| 178 | + print("Something went wrong during select") |
| 179 | + print(e) |
| 180 | + |
| 181 | + try: |
| 182 | + version = 0 |
| 183 | + expected_update = records if version <= -number_of_updates else all_updates[version + number_of_updates - 1] |
| 184 | + for j in range(0, number_of_aggregates): |
| 185 | + r = sorted(sample(range(0, len(keys)), 2)) |
| 186 | + column_sum = sum(map(lambda x: expected_update[x][0] if x in expected_update else 0, keys[r[0]: r[1] + 1])) |
| 187 | + result = query.sum(keys[r[0]], keys[r[1]], 0) |
| 188 | + if column_sum != result: |
| 189 | + raise Exception('sum error on [', keys[r[0]], ',', keys[r[1]], ']: ', result, ', correct: ', column_sum) |
| 190 | + print("Aggregate version ", version, "finished") |
| 191 | + score = score + 20 |
| 192 | + except Exception as e: |
| 193 | + score = score + 5 |
| 194 | + print("Something went wrong during sum") |
| 195 | + print(e) |
| 196 | + db.close() |
| 197 | + |
| 198 | +def correctness_tester(): |
| 199 | + global score |
| 200 | + db = Database() |
| 201 | + grades_table = db.create_table('Grades', 5, 0) |
| 202 | + |
| 203 | + # create a query class for the grades table |
| 204 | + query = Query(grades_table) |
| 205 | + |
| 206 | + # dictionary for records to test the database: test directory |
| 207 | + records = {} |
| 208 | + records[1] = [1, 1, 1, 1, 1] |
| 209 | + records[2] = [1, 2, 2, 2, 2] |
| 210 | + records[3] = [2, 3, 3, 3, 3] |
| 211 | + records[4] = [1, 2, 2, 2, 2] |
| 212 | + query.insert(*records[1]) |
| 213 | + # Test if correct columns are returned |
| 214 | + result = query.select(1, 0, [1,0,1,0,0]) |
| 215 | + print(len(result)) |
| 216 | + print(result[0].columns) |
| 217 | + if len(result) == 1 and len(result[0].columns) == 2 and result[0].columns[1] == records[1][2]: |
| 218 | + score += 5 |
| 219 | + print("[0] pass") |
| 220 | + elif len(result) == 1 and result[0].columns[0] == 1 and result[0].columns[2] == 1 and\ |
| 221 | + result[0].columns[3] == None and result[0].columns[4] == None and result[0].columns[1] == None: |
| 222 | + score += 5 |
| 223 | + print("[0] pass") |
| 224 | + # Test if insertion with existing primary_key is not allowed |
| 225 | + query.insert(*records[2]) |
| 226 | + result = query.select(1, 0, [1,1,1,1,1]) |
| 227 | + print(len(result)) |
| 228 | + print(0, result[0].columns, records[1]) |
| 229 | + if len(result) == 1 and len(result[0].columns) == 5 and result[0].columns[1] == records[1][1]\ |
| 230 | + and result[0].columns[2] == records[1][2] and result[0].columns[3] == records[1][3]\ |
| 231 | + and result[0].columns[4] == records[1][4]: |
| 232 | + score += 5 |
| 233 | + print("[1] pass") |
| 234 | + result = query.sum(1, 1, 1) |
| 235 | + if result == 1: |
| 236 | + score += 5 |
| 237 | + print("[2] pass") |
| 238 | + # Test if updated record with existing primary_key is not allowed |
| 239 | + query.insert(*records[3]) |
| 240 | + query.update(2, *records[4]) |
| 241 | + try: |
| 242 | + result = query.select(1, 0, [1,0,1,0,0]) |
| 243 | + print(1, result[0].columns, records[1]) |
| 244 | + if len(result) == 1 and len(result[0].columns) == 2 and result[0].columns[1] == records[1][2]: |
| 245 | + score += 5 |
| 246 | + print("[3] pass") |
| 247 | + elif len(result) == 1 and result[0].columns[0] == 1 and result[0].columns[2] == 1 and\ |
| 248 | + result[0].columns[3] == None and result[0].columns[4] == None and result[0].columns[1] == None: |
| 249 | + score += 5 |
| 250 | + print("[3] pass") |
| 251 | + except Exception as e: |
| 252 | + print("Something went wrong during update") |
| 253 | + print(e) |
| 254 | + result = query.select(2, 0, [1,1,1,1,1]) |
| 255 | + print(len(result)) |
| 256 | + if len(result) != 0: |
| 257 | + print(2, result[0].columns, records[3]) |
| 258 | + if len(result) == 1 and len(result[0].columns) == 5 and result[0].columns[1] == records[3][1]\ |
| 259 | + and result[0].columns[2] == records[3][2] and result[0].columns[3] == records[3][3]\ |
| 260 | + and result[0].columns[4] == records[3][4]: |
| 261 | + score += 5 |
| 262 | + print("[4] pass") |
| 263 | + |
| 264 | +from timeit import default_timer as timer |
| 265 | +from decimal import Decimal |
| 266 | + |
| 267 | +import os |
| 268 | +import glob |
| 269 | +import traceback |
| 270 | +import shutil |
| 271 | + |
| 272 | +def run_test(): |
| 273 | + start = timer() |
| 274 | + try: |
| 275 | + speed_tester1() |
| 276 | + except Exception as e: |
| 277 | + print("Something went wrong") |
| 278 | + print(e) |
| 279 | + traceback.print_exc() |
| 280 | + |
| 281 | + try: |
| 282 | + speed_tester2() |
| 283 | + except Exception as e: |
| 284 | + print("Something went wrong") |
| 285 | + print(e) |
| 286 | + traceback.print_exc() |
| 287 | + |
| 288 | + end = timer() |
| 289 | + print("\n------------------------------------") |
| 290 | + print("Time taken: ", Decimal(end - start).quantize(Decimal('0.01')), "seconds") |
| 291 | + print("Total score for speed testers: ", score) |
| 292 | + print("--------------------------------------\n") |
| 293 | + |
| 294 | + try: |
| 295 | + correctness_tester() |
| 296 | + except Exception as e: |
| 297 | + print("Something went wrong") |
| 298 | + print(e) |
| 299 | + traceback.print_exc() |
| 300 | + print("Total score: ", score + 5) |
| 301 | + |
| 302 | +run_test() |
0 commit comments