def main(): # Set up the tester. test = asst3.start_test('orm custom types (Datetime and Coordinate) test', TOTAL_MARK) # Import student's code. import orm mark = 0 case_number = 1 output = orm.export("easydb", asst3_schema) with open(STUDENT_FILE, "wt") as f: f.write(output) # Start the server. server = asst3.Server(filename="student_export.txt") if not server.start("preload.txt"): print("ERROR: server could not start.\n" \ "HINT: Check the correctness of the exported schema file") os.remove(STUDENT_FILE) return # Set up student's database object and connect to the database. db = orm.setup("easydb", asst3_schema) asst3.try_connect(db, server) # ===== CASE 1: Create a row object with Coordinate field with no error. ===== print("CASE {}: Create a row object with Coordinate field with no error.". format(case_number)) # This should not raise any error. try: toronto = asst3_schema.City(db, name="Toronto", location=(12.3, 12.3)) if toronto.location == (12.3, 12.3): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 2: Create a row object with Coordinate field whose blank is set to True. ===== print( "CASE {}: Create a row object with Coordinate field whose blank is set to True." .format(case_number)) # This should not raise any error. try: toronto = asst3_schema.City(db, name="Toronto") if toronto.location == (0.0, 0.0): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the filed is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 3: Assign the Coordinate field with wrong type. ===== print("CASE {}: Assign the Coordinate field with wrong type.".format( case_number)) # This should raise an TypeError or ValueError error. try: toronto.location = 3 except TypeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except ValueError: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: {}".format(case_number, "expected TypeError or ValueError")) case_number = case_number + 1 # ===== CASE 4: Assign and save the row object with Coordinate field with invalid value. ===== print( "CASE {}: Assign and save the row object with Coordinate field with invalid value." .format(case_number)) # This should raise a ValueError error. try: toronto.location = (200.2, 500.3) toronto.save() except ValueError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected ValueError")) case_number = case_number + 1 # ===== CASE 5: Save and get the row object with Coordinate field with no error. ===== print( "CASE {}: Save and get the row object with Coordinate field with no error." .format(case_number)) # This should not raise any error. try: toronto.location = (43.74, -79.37) toronto.save() except Exception as e: print("CASE {} FAIL: {}".format(case_number)) else: try: city1 = asst3_schema.City.get(db, 1) if city1.location == (43.74, -79.37): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the saved and received field do not match". format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 6: Filter the row object with Coordinate field. ===== print("CASE {}: Filter the row object with Coordinate field.".format( case_number)) # This should not raise any error. try: not_toronto_A = asst3_schema.City(db, name="NotTorontoA", location=(43.74, 12.3)) not_toronto_B = asst3_schema.City(db, name="NotTorontoB", location=(12.3, -79.37)) not_toronto_A.save() not_toronto_B.save() results1 = asst3_schema.City.filter(db, location=(43.74, -79.37)) results2 = asst3_schema.City.filter(db, location__ne=(43.74, -79.37)) # equal case if results1[0].name == "Toronto" and len(results1) == 1: # not equal case if results2[0].name in ["NotTorontoA", "NotTorontoB" ] and results2[1].name in [ "NotTorontoA", "NotTorontoB" ] and len(results2) == 2: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the filter result (not equal case) is not correct" .format(case_number)) else: print( "CASE {} FAIL: the filter result (equal case) is not correct". format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 7: Create a row object with DateTime field with no error. ===== print("CASE {}: Create a row object with Datetime field with no error.". format(case_number)) # This should not raise any error. try: midterm = asst3_schema.Event(db, location=toronto, start=datetime(2019, 10, 29, 16, 0), end=datetime(2019, 10, 29, 17, 30)) if midterm.start == datetime(2019, 10, 29, 16, 0) and midterm.end == datetime( 2019, 10, 29, 17, 30): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 8: Create a row object with DateTime field whose blank is set to True is set properly. ===== print( "CASE {}: Create a row object with DateTime field whose blank is set to True." .format(case_number)) # This should not raise any error. try: due = asst3_schema.Event(db, location=toronto, start=datetime(2019, 11, 17, 23, 59)) if due.end == datetime.fromtimestamp(0): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 9: Create a row object with DateTime field whose default is set properly (calling a function). ===== print( "CASE {}: Create a row object with DateTime field whose default is set properly (calling a function)." .format(case_number)) # This should not raise any error. try: event1 = asst3_schema.Event(db, location=toronto, end=datetime(2019, 11, 17, 12)) if type(event1.start) is datetime: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 10: Assign the Datetime field with wrong type. ===== print("CASE {}: Assign the Datetime field with wrong type.".format( case_number)) # This should raise a TypeError error. try: event1.start = 3 except TypeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected TypeError")) case_number = case_number + 1 # ===== CASE 11: Assign the Datetime field with correct type. ===== print("CASE {}: Assign the Datetime field with correct type.".format( case_number)) # This should not raise any error. try: event1.start = datetime(2019, 9, 15, 12) if event1.start == datetime(2019, 9, 15, 12): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 12: Save and get the row object with Datetime field with no error. ===== print( "CASE {}: Save and get the row object with Datetime field with no error." .format(case_number)) # This should not raise any error. try: event1.save() except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: # This should not raise any error. try: event2 = asst3_schema.Event.get(db, 1) if event2.start == datetime(2019, 9, 15, 12): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the saved and received field do not match". format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 13: Filter the row object with Datetime field. ===== print("CASE {}: Filter the row object with Datetime field.".format( case_number)) # This should not raise any error. try: results = asst3_schema.Event.filter(db, start=datetime(2019, 9, 15, 12)) if (results[0].location.name == "Toronto") and (len(results) == 1): mark = mark + 1 print("CASE {} PASS".format(case_number)) else: print("CASE {} FAIL: filter result is not correct".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 14: Count the row object with Datetime field. ===== print("CASE {}: Count the row object with Datetime field.".format( case_number)) # This should not raise any error. try: results = asst3_schema.Event.filter(db, start__gt=datetime( 2019, 7, 15, 12)) if (results[0].location.name == "Toronto") and (len(results) == 1): mark = mark + 1 print("CASE {} PASS".format(case_number)) else: print("CASE {} FAIL: count result is not correct".format( case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) test.add_mark(mark) db.close() server.end() os.remove(STUDENT_FILE)
def main(): # Set up the tester. test = asst3.start_test('orm get command test', TOTAL_MARK) # Import student's code. import orm mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start("preload.txt") # Set up student's database object and connect to the database. db = orm.setup("easydb", asst3_schema) asst3.try_connect(db, server) # ===== CASE 1: Get id which does not exist. ===== print("CASE {}: Get id which does not exist.".format(case_number).format( case_number)) (mark, result) = asst3.run_test_case( asst3_schema.User.get, (db, 777), case_number, 1, mark, True, orm.exceptions.ObjectDoesNotExist ) # This should raise an ObjectDoesNotExist error. case_number = case_number + 1 # ===== CASE 2: Get a row object with no error. ===== id_number = random.randint(1, 3) if id_number == 1: age = 38 elif id_number == 2: age = 48 else: age = 21 print("CASE {}: Get a row object with no error.".format(case_number)) # This should not raise any error. try: user1 = asst3_schema.User.get(db, id_number) if user1.age == age: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the field value of the row object is not correct" .format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 3: Get a row object which has foreign key field with no error. ===== id_number = random.randint(1, 4) if id_number == 1: firstName = "James" else: firstName = "Alice" print( "CASE {}: Get a row object which has foreign key field with no error.". format(case_number)) # This should not raise any error. try: account1 = asst3_schema.Account.get(db, id_number) if (account1.type == "Normal") and (account1.user.firstName == firstName): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the field value of the row object is not correct" .format(case_number)) except Exception as e: print("CASE 2 FAIL: {}".format(case_number, str(e))) db.close() server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('easydb scan command test', TOTAL_MARK) # Import student's code. from orm import easydb, exceptions mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start(datafile="preload.txt") # database tables tb = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) # Set up student's database object and connect to the database. db = easydb.Database(tb) asst3.try_connect(db, server) # ===== CASE 1: Table name does not exist. ===== print("CASE {}: Table name does not exist.".format(case_number)) (mark, result) = asst3.run_test_case( db.scan, ("Bob", ("firstName", easydb.OP_EQ, "James")), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 2: Query is not a tuple. ===== print("CASE {}: Query is not a tuple.".format(case_number)) (mark, result) = asst3.run_test_case(db.scan, ("User", 3), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 3: Query has more than 3 elements. ===== print("CASE {}: Query has more than 3 elements.".format(case_number)) (mark, result) = asst3.run_test_case(db.scan, ("User", ("age", easydb.OP_EQ, 27, 90)), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 4: Query has less than 3 elements. ===== print("CASE {}: Query has less than 3 elements.".format(case_number)) (mark, result) = asst3.run_test_case(db.scan, ("User", ("age", easydb.OP_EQ)), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 5: Column name is not a string. ===== print("CASE {}: Column name is not a string.".format(case_number)) (mark, result) = asst3.run_test_case(db.scan, ("User", (3, easydb.OP_EQ, 27)), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 6: Column name does not exist. ===== print("CASE {}: Column name does not exist.".format(case_number)) (mark, result) = asst3.run_test_case( db.scan, ("User", ("bob", easydb.OP_EQ, 27)), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 7: Operator is not supported. ===== print("CASE {}: Operator is not supported.") (mark, result) = asst3.run_test_case( db.scan, ("User", ("age", 10, 27)), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 8: Type of operand does not match. ===== print("CASE {}: Type of operand does not match.".format(case_number)) (mark, result) = asst3.run_test_case( db.scan, ("User", ("age", easydb.OP_EQ, "27")), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 9: Operator of a foreign key operand is not OP_EQ or OP_NE. ===== print("CASE {}: Operator of a foreign key column is not OP_EQ or OP_NE.". format(case_number)) (mark, result) = asst3.run_test_case( db.scan, ("Account", ("user", easydb.OP_GT, 10)), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 10: Operand of a foreign key column is not an integer. ===== print("CASE {}: Operand of a foreign key column is not an integer.".format( case_number)) (mark, result) = asst3.run_test_case( db.scan, ("Account", ("user", easydb.OP_EQ, "Bob")), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 11: Everything is ok with equal operator. ===== print("CASE {}: Everything is ok with equal operator.".format(case_number)) # This should not raise any error. try: IDs = db.scan("User", ("firstName", easydb.OP_EQ, "James")) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: if [1] == IDs: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the scan result is not correct".format( case_number)) case_number = case_number + 1 # ===== CASE 12: Everything is ok with not equal operator. ===== print("CASE {}: Everything is ok with not equal operator.".format( case_number)) # This should not raise any error. try: IDs = db.scan("User", ("firstName", easydb.OP_NE, "James")) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: if IDs is not None and all(ID in IDs for ID in [2, 3]) and len(IDs) == 2: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the scan result is not correct".format( case_number)) case_number = case_number + 1 # ===== CASE 13: Everything is ok with less than operator. ===== print("CASE {}: Everything is ok with less than operator.".format( case_number)) # This should not raise any error. try: IDs = db.scan("User", ("age", easydb.OP_LT, 100)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: if IDs is not None and all(ID in IDs for ID in [1, 2, 3]) and len(IDs) == 3: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the scan result is not correct".format( case_number)) case_number = case_number + 1 # ===== CASE 14: Everything is ok with greater than operator. ===== print("CASE {}: Everything is ok with greater than operator.".format( case_number)) # This should not raise any error. try: IDs = db.scan("User", ("height", easydb.OP_GT, 190.0)) except Exception as e: db.close() print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() if IDs is not None and len(IDs) == 0: line = 'Request { table_id: 1, command: Query(3, 4, Float(190.0)) }' if server.expect(line) is not None: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: server did not receive the Scan command". format(case_number)) else: print("CASE {} FAIL: the scan result is not correct".format( case_number)) server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('easydb drop command test', TOTAL_MARK) # Import student's code. from orm import easydb, exceptions mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start(datafile="preload.txt") # database tables tb = ( ("User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ("Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), )) ) # Set up student's database object and connect to the database. db = easydb.Database(tb) asst3.try_connect(db, server) # ===== CASE 1: Drop non-existing row. ===== print("CASE {}: Drop non-existing row.".format(case_number)) (mark, result) = asst3.run_test_case(db.drop, ("User", 300), case_number, 1, mark, True, exceptions.ObjectDoesNotExist) # This should raise an ObjectDoesNotExist error. case_number = case_number + 1 # ===== CASE 2: Table name does not exist. ===== print("CASE {}: Table name does not exist.".format(case_number)) (mark, result) = asst3.run_test_case(db.drop, ("Hahaha", 1), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 3: Everything is ok. ===== print("CASE {}: Everything is ok.".format(case_number)) # This should not raise any error. try: db.drop("User", 2) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) db.close() else: db.close() # Dump database rows. rows = asst3.dump(server, 1) not_deleted = False for row in rows: if row[0] == "2": not_deleted = True break if not_deleted: print("CASE {} FAIL: the dropped row is still on the server".format(case_number)) else: print("CASE {} PASS".format(case_number)) mark = mark + 1 server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('orm delete command test', TOTAL_MARK) # Import student's code. import orm mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start("preload.txt") # Set up student's database object and connect to the database. db = orm.setup("easydb", asst3_schema) asst3.try_connect(db, server) # Create a row object for testing. james = asst3_schema.User(db, firstName="James", lastName="Hartley", height=180.3, age=38) james.pk = 1 james.version = 1 # ===== CASE 1: Delete a row object with no error. ===== print("CASE {}: Delete a row object with no error.".format(case_number)) # This should not raise any error. try: james.delete() except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() # Dump database rows. rows = asst3.dump(server, 1) not_deleted = False for row in rows: if row[0] == "1": not_deleted = True break if not_deleted: print( "CASE {} FAIL: the deleted row is still on the server".format( case_number)) else: print("CASE {} PASS".format(case_number)) mark = mark + 1 db.connect(server.host, server.port) case_number = case_number + 1 # ===== CASE 2: Delete a row object whose pk is None. ===== print("CASE {}: Delete a row object whose pk is None.".format(case_number)) (mark, result) = asst3.run_test_case( james.delete, (), case_number, 1, mark, True, orm.exceptions.PacketError, True) # This should raise an error other than PacketError. db.close() server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('easydb connection test', TOTAL_MARK) # Import student's code. from orm import easydb case_number = 1 # Start the server. server = asst3.Server() server.start() # database tables tb = ( ("User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ("Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), )) ) # ===== CASE 1: Connect to the database. ===== print("CASE {}: Connect to the database.".format(case_number)) try: db = easydb.Database(tb) asst3.try_connect(db, server) except OSError: print("CASE {} FAIL: could not connect to database".format(case_number)) server.end() return else: if server.expect("Connected to 127.0.0.1:") is None: print("CASE {} FAIL: server did not receive connection".format(case_number)) server.end() return else: test.add_mark(1) print("CASE 1 PASS") case_number = case_number + 1 # ===== CASE 2: Close the connection to database. ===== print("CASE {}: Close the connection to database.".format(case_number)) db.close() if server.look(r"Request { table_id: \d+, command: Exit }") is None: print("CASE {} FAIL: server did not receive the Exit command".format(case_number)) else: print("CASE {} PASS".format(case_number)) test.add_mark(1) server.end()
def main(): # Set up the tester. test = asst3.start_test('orm basic test', TOTAL_MARK) # Import student's code. import orm import orm.exceptions mark = 0 case_number = 1 # ===== CASE 1: Default value is not the correct type. ===== print( "CASE {}: Default value is not the correct type.".format(case_number)) # This should raise an TypeError error. try: asst3_schema_bad1 = asst3.load_module('asst3_schema_bad1') orm.setup("easydb", asst3_schema_bad1) except TypeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected TypeError")) case_number = case_number + 1 # ===== CASE 2: Choices value is not the correct type. ===== print( "CASE {}: Choices value is not the correct type.".format(case_number)) # This should raise an TypeError error. try: asst3_schema_bad2 = asst3.load_module('asst3_schema_bad2') orm.setup("easydb", asst3_schema_bad2) except TypeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected TypeError")) case_number = case_number + 1 # ===== CASE 3: Field name contains underscore. ===== print("CASE {}: Field name contains underscore.".format(case_number)) # This should raise an AttributeError error. try: asst3_schema_bad3 = asst3.load_module('asst3_schema_bad3') orm.setup("easydb", asst3_schema_bad3) except AttributeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected AttributeError")) case_number = case_number + 1 # ===== CASE 4: Field name use reserved word. ===== print("CASE {}: Field name use reserved word.".format(case_number)) # This should raise an AttributeError error. try: asst3_schema_bad4 = asst3.load_module('asst3_schema_bad4') orm.setup("easydb", asst3_schema_bad4) except AttributeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected AttributeError")) case_number = case_number + 1 # ===== CASE 5: Setup with a correct schema ===== print("CASE {}: Setup with a correct schema".format(case_number)) # This should not raise any error. try: db = orm.setup("easydb", asst3_schema) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: contain = False for key in db.__dict__: # Check if the object saves the inpputed schema by checking if there is a tuple, list, or dict attribute. if isinstance(db.__dict__[key], (tuple, list, dict)): if len(db.__dict__[key]) > 0: contain = True break if contain: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: no tuple, list, or dict is found in the Database object indicating that the inputted schema is saved" .format(case_number)) case_number = case_number + 1 # ===== CASE 6: Create a row object with missing field. ===== print( "CASE {}: Create a row object with missing field.".format(case_number)) # This should raise an AttributeError error. try: jeffrey = asst3_schema.User(db, firstName="Jeffrey", height=999.9) except AttributeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected AttributeError")) case_number = case_number + 1 # ===== CASE 7: Create a row object with no error. ===== print("CASE {}: Create a row object with no error".format(case_number)) # This should not raise any error. try: jeffrey = asst3_schema.User(db, firstName="Jeffrey", lastName="Fang", height=999.9) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: try: if jeffrey.firstName == "Jeffrey": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the stored field of the row object is not correct" .format(case_number)) except Exception as e: print( "CASE {} FAIL: while accessing the field of the row object: {}" .format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 8: Create object with a foreign key. ===== print("CASE {}: Create object with a foreign key.".format(case_number)) # This should not raise any error. try: account1 = asst3_schema.Account(db, user=jeffrey) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: try: if account1.user.lastName == "Fang": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the field of stored foreign key field of the row object is not correct" .format(case_number)) except Exception as e: print( "CASE {} FAIL: while accessing the field of the row object: {}" .format(case_number, str(e))) case_number = case_number + 1 # ==== CASE 9: Check if the field with blank set to True is set properly. ===== print( "CASE {}: Check if the field with blank set to True is set properly.". format(case_number)) # This should not raise any error. try: if jeffrey.age == 0: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: while accessing the field of the row object: {}". format(case_number, str(e))) case_number = case_number + 1 # ==== CASE 10: Check if the field with default is set properly. ===== print("CASE {}: Check if the field with default is set properly.".format( case_number)) # This should not raise any error. try: if account1.type == "Normal": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the field is not set properly".format( case_number)) except Exception as e: print("CASE {} FAIL: while accessing the field of the row object: {}". format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 11: Set the field value to wrong type. ===== print("CASE {}: Set the field value to wrong type.".format(case_number)) # This should raise a TypeError error. try: jeffrey.age = 3.5 except TypeError: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected TypeError")) case_number = case_number + 1 # ===== CASE 12: Set a float field with int. ===== print("CASE {}: Set a float field with int.".format(case_number)) # This should not raise any error. try: jeffrey.height = 50 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: if isinstance(jeffrey.height, float) and (jeffrey.height == 50.0): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the field is not set properly (maybe field is not a float anymore?)" .format(case_number)) case_number = case_number + 1 # ===== CASE 13: Set the field value whose blank is True to None. ===== print("CASE {}: Set the field value whose blank is True to None.".format( case_number)) # This should not raise any error. try: jeffrey.height = None except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: if jeffrey.height == 0.0: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the field is not set properly (should set to default value of the type)" .format(case_number)) case_number = case_number + 1 # ===== CASE 14: Set the field value whose blank is False to None. ===== print("CASE {}: Set the field value whose blank is False to None.".format( case_number)) # This should raise an error. try: jeffrey.firstName = None except: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: {}".format(case_number, "expected an error")) case_number = case_number + 1 # ===== CASE 15: Export ===== print("CASE {}: Export".format(case_number)) # This should not raise any error. try: output = orm.export("easydb", asst3_schema) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: # Read the solution. with open(tester.datapath('export.txt', 'asst3'), 'r') as file: data = file.read() # Check if the format is correct. if output == data: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: the format is not correct".format(case_number)) test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('easydb insert command test', TOTAL_MARK) # Import student's code. from orm import easydb, exceptions mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start() # database tables tb = ( ("User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ("Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), )) ) # Set up student's database object and connect to the database. db = easydb.Database(tb) asst3.try_connect(db, server) # ===== CASE 1: There is an extra element in the inserted row. ===== print("CASE {}: There is an extra element in the inserted row.".format(case_number)) (mark, result) = asst3.run_test_case(db.insert, ("User", ["Jay", "Sung", 5.5, 31, 33]), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 2: There is missing an element in the inserted row. ===== print("CASE {}: There is missing an element in the inserted row.".format(case_number)) (mark, result) = asst3.run_test_case(db.insert, ("User", ["Jay", "Sung", 5.5]), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 3: Type of the column in the inserted row does not match. ===== print("CASE {}: Type of the column in the inserted row does not match.".format(case_number)) (mark, result) = asst3.run_test_case(db.insert, ("User", ["Jay", "Sung", "Dog", 31]), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 4: Table name is not a string. ===== print("CASE {}: Table name is not a string.".format(case_number)) (mark, result) = asst3.run_test_case(db.insert, (123, ["Jay", "Sung", 5.5, 31]), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 5: Table name does not exist in the database. ===== print("CASE 5: Table name does not exist in the database.".format(case_number)) (mark, result) = asst3.run_test_case(db.insert, ("Test", ["Jay", "Sung", 5.5, 31]), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 6: Everything is ok. ===== print("CASE {}: Everything is ok.".format(case_number)) # Randomly generate attribute information. first_name = ''.join(random.choice(string.ascii_lowercase) for i in range(6)).capitalize() last_name = ''.join(random.choice(string.ascii_lowercase) for i in range(6)).capitalize() height = random.randint(1200, 2000) / 10 age = random.randint(1, 100) # This should not raise any error. try: pk, version = db.insert("User", [first_name, last_name, height, age]) except Exception as e: db.close() print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() # Dump database rows. rows = asst3.dump(server, 1) if [str(pk), str(version), first_name, last_name, str(height), str(age)] in rows or [str(pk), str(version), first_name, last_name, str(int(height)), str(age)] in rows: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the inserted row cannot be found on the server".format(case_number)) server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('easydb get command test', TOTAL_MARK) # Import student's code. from orm import easydb, exceptions mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start(datafile="preload.txt") # database tables tb = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) # Set up student's database object and connect to the database. db = easydb.Database(tb) asst3.try_connect(db, server) # ===== CASE 1: Get non-existing row. ===== print("CASE {}: Get non-existing row.".format(case_number)) (mark, result) = asst3.run_test_case( db.get, ("User", 300), case_number, 1, mark, True, exceptions. ObjectDoesNotExist) # This should raise an ObjectDoesNotExist error. case_number = case_number + 1 # ===== CASE 2: Table name does not exist. ===== print("CASE {}: Table name does not exist.".format(case_number)) (mark, result) = asst3.run_test_case( db.get, ("Hahaha", 1), case_number, 1, mark, True, ValueError) # This should raise an ValueError error. case_number = case_number + 1 # ===== CASE 2: Everything is ok. ===== print("CASE {}: Everything is ok.".format(case_number)) # This should not raise any error. try: values, version = db.get("User", 1) except Exception as e: db.close() print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() if values == ["James", "Hartley", 180.3, 38]: if server.expect( 'Request { table_id: 1, command: Get(1) }') is not None: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: server did not receive the Get command". format(case_number)) else: print("CASE {} FAIL: the received row is not correct".format( case_number)) server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('orm save command test', TOTAL_MARK) # Import student's code. import orm mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start() # Set up student's database object and connect to the database. db = orm.setup("easydb", asst3_schema) asst3.try_connect(db, server) # Create some row objects for testing. joe = asst3_schema.User(db, firstName="Joe", lastName="Harris", age=32) jeffrey = asst3_schema.User(db, firstName="Jeffrey", lastName="Fang", height=999.9) alice = asst3_schema.User(db, firstName="Alice", lastName="Stuart", age=18) account1 = asst3_schema.Account(db, user=alice, type="Yes") account2 = asst3_schema.Account(db, user=jeffrey, type="Special") # ===== CASE 1: Save a row object whose field is not in choices. ===== print("CASE {}: Save a row object whose field is not in choices.".format(case_number)) (mark, result) = asst3.run_test_case(account1.save, (), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 2: Save a new row object to the database with no error (insert). ===== print("CASE {}: Save a new row object to the database with no error (insert).".format(case_number)) # This should not raise any error. try: joe.save() except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() # Dump database rows. rows = asst3.dump(server, 1) if [str(1), str(1), "Joe", "Harris", "0", "32"] in rows or [str(2), str(1), "Joe", "Harris", "0", "32"] in rows: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the saved row cannot be found on the server".format(case_number)) db.connect(server.host, server.port) case_number = case_number + 1 # ===== CASE 3: Cascade save new row objects to the database with no error (insert). ===== print("CASE {}: Cascade save new row objects to the database with no error (insert).".format(case_number)) # This should not raise any error. try: account2.save() except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() # Dump database rows. client = asst3.Client(server) user_rows = client.dump(1) account_rows = client.dump(2) client.close() if ([str(2), str(1), "Jeffrey", "Fang", "999.9", "0"] in user_rows or [str(3), str(1), "Jeffrey", "Fang", "999.9", "0"] in user_rows) \ and ([str(1), str(1), "2", "Special", "0"] in account_rows or [str(2), str(1), "2", "Special", "0"] in account_rows \ or [str(1), str(1), "3", "Special", "0"] in account_rows or [str(2), str(1), "3", "Special", "0"] in account_rows): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the saved row cannot be found on the server".format(case_number)) db.connect(server.host, server.port) case_number = case_number + 1 # ===== CASE 4: Save a existing row object to the database (update). ===== joe.height = 663.1 print("CASE {}: Save a existing row object to the database (update).".format(case_number)) # This should not raise any error. try: joe.save() except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() # Dump database rows. rows = asst3.dump(server, 1) if [str(1), str(2), "Joe", "Harris", "663.1", "32"] in rows or [str(2), str(2), "Joe", "Harris", "663.1", "32"] in rows: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the saved row cannot be found on the server".format(case_number)) db.connect(server.host, server.port) case_number = case_number + 1 # ===== CASE 5: Non-atomic save (non-atomic update) ===== joe.age = 50 print("CASE {}: Non-atomic save (non-atomic update)".format(case_number)) # This may raise a TransactionAbort error. try: joe.save() except orm.TransactionAbort: print("CASE {} PASS".format(case_number)) mark = mark + 1 except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: db.close() # Dump database rows. rows = asst3.dump(server, 1) if [str(1), str(3), "Joe", "Harris", "663.1", "50"] in rows or [str(2), str(3), "Joe", "Harris", "663.1", "50"] in rows: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: the saved row cannot be found on the server".format(case_number)) db.connect(server.host, server.port) case_number = case_number + 1 # ===== CASE 6: Row object's id does not exist in the database anymore (invalid reference). ===== jeffrey.pk = 888 print("CASE {}: Row object's id does not exist in the database anymore (invalid reference).".format(case_number)) # This should raise an InvalidReference error. try: account2.save() except orm.exceptions.InvalidReference: if jeffrey.pk is None: mark = mark + 1 print("CASE {} PASS".format(case_number)) else: print("CASE {} FAIL: pk is not set back to None".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: print("CASE {} FAIL: {}".format(case_number, "expected InvalidReference")) db.close() server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('easydb table format test', TOTAL_MARK) # Load student's code. from orm import easydb, exceptions mark = 0 case_number = 1 # ===== CASE 1: All the table formats are ok. ===== tb_1 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: All the table formats are ok.".format(case_number)) # This should not raise any error. try: db = easydb.Database(tb_1) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) else: contain = False for key in db.__dict__: # Check if the object saves the inpputed schema by checking if there is a tuple, list, or dict attribute. if isinstance(db.__dict__[key], (tuple, list, dict)): if len(db.__dict__[key]) > 0: contain = True break if contain: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print( "CASE {} FAIL: no tuple, list, or dict is found in the Database object indicating that the inputted schema is saved" .format(case_number)) case_number = case_number + 1 # ===== CASE 2: Table name is not a string. ===== tb_2 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( 33333, ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Table name is not a string.".format(case_number)) (mark, result) = asst3.run_test_case( easydb.Database, (tb_2, ), case_number, 1, mark, True, TypeError) # This should raise a TypeError error. case_number = case_number + 1 # ===== CASE 3: Column name is not a string. ===== tb_3 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ([1, 2], float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Column name is not a string.".format(case_number)) (mark, result) = asst3.run_test_case( easydb.Database, (tb_3, ), case_number, 1, mark, True, TypeError) # This should raise a TypeError error. case_number = case_number + 1 # ===== CASE 4: Type is not one of str, float, int. ===== tb_4 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", tuple), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Type is not one of str, float, int.".format(case_number)) (mark, result) = asst3.run_test_case( easydb.Database, (tb_4, ), case_number, 1, mark, True, ValueError) # This should raise a ValueError error. case_number = case_number + 1 # ===== CASE 5: Type is not a string referencing another table name (nonexistent table). ===== tb_5 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "Yes"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print( "CASE {}: Type is not a string referencing another table name (nonexistent table)." .format(case_number)) (mark, result) = asst3.run_test_case( easydb.Database, (tb_5, ), case_number, 1, mark, True, exceptions.IntegrityError) # This should raise a IntegrityError error. case_number = case_number + 1 # ===== CASE 6: Length of tables are not exactly 2. ===== tb_6 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), ), 3), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Length of tables are not exactly 2.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_6, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 7: Length of columns are not exactly 2. ===== tb_7 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str, 123, "nonono"), ("balance", float), ))) print("CASE {}: Length of columns are not exactly 2.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_7, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 8: Names of tables are not legal. ===== tb_8 = ( ( "_User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Names of tables are not legal.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_8, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 9: Names of columns are not legal. ===== tb_9 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("3height", float), ("age", int), )), ( "Account", ( ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Names of columns are not legal.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_9, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 10: Table names are duplicated. ===== tb_10 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "User", ( ("user", str), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Table names are duplicated.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_10, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 11: Column names are duplicated. ===== tb_11 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("weight", float), ("firstName", int), )), ( "Account", ( ("user", str), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Column names are duplicated.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_11, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 12: Column name is 'pk'. ===== tb_12 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("weight", float), ("pk", int), )), ( "Account", ( ("user", str), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Column name is 'pk'.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_12, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 13: Column name is 'id'. ===== tb_13 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("weight", float), ("age", int), )), ( "Account", ( ("user", str), # (column_name, table_reference) ("id", str), ("balance", float), ))) print("CASE {}: Column name is 'id'.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_13, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 14: Column name is 'save'. ===== tb_14 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("weight", float), ("age", int), )), ( "Account", ( ("user", str), # (column_name, table_reference) ("save", str), ("balance", float), ))) print("CASE {}: Column name is 'save'.".format(case_number)) (mark, result) = asst3.run_test_case(easydb.Database, (tb_14, ), case_number, 1, mark, True, Exception) # This should raise an error. case_number = case_number + 1 # ===== CASE 15: Foreign keys are causing a cycle. ===== tb_15 = ( ( "User", ( # table_name ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), )), ( "Account", ( ("user", "Account"), # (column_name, table_reference) ("type", str), ("balance", float), )), ( "Test1", ( ("user", "Account"), # (column_name, table_reference) ("type", str), ("balance", float), )), ( "Test2", ( ("user", "Test1"), # (column_name, table_reference) ("type", str), ("balance", float), ))) print("CASE {}: Foreign keys are causing a cycle.".format(case_number)) (mark, result) = asst3.run_test_case( easydb.Database, (tb_15, ), case_number, 1, mark, True, exceptions. IntegrityError) # This should raise an IntegrityError error. case_number = case_number + 1 # ===== CASE 16: Table reference is out of order. ===== tb_16 = ( ( "Account", ( # table_name ("user", "User"), # (column_name, table_reference) ("type", str), ("balance", float), )), ( "User", ( ("firstName", str), # (column_name, type) ("lastName", str), ("height", float), ("age", int), ))) print("CASE {}: Table reference is out of order.".format(case_number)) (mark, result) = asst3.run_test_case( easydb.Database, (tb_16, ), case_number, 1, mark, True, exceptions. IntegrityError) # This should raise an IntegrityError error. test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('orm filter command test', TOTAL_MARK) # Import student's code. import orm mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start("preload.txt") # Set up student's database object and connect to the database. db = orm.setup("easydb", asst3_schema) asst3.try_connect(db, server) # Create a row object for testing. charlie = asst3_schema.User(db, firstName="Charlie", lastName="George", height=180.3, age=26) alice = asst3_schema.User(db, firstName="Alice", lastName="Harris", height=163.2, age=21) alice.pk = 3 alice.version = 1 # ===== CASE 1: Filter with no argument with no error. ===== print("CASE {}: Filter with no argument with no error.".format(case_number)) # This should not raise any error. try: results = asst3_schema.User.filter(db) if len(results) == 3 and results[0].age in [38, 48, 21]: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 2: Filter with equal operator with no error. ===== print("CASE {}: Filter with equal operator with no error.".format(case_number)) # This should not raise any error. try: results = asst3_schema.User.filter(db, age=21) if (results[0].height == 163.2) and (len(results) == 1): print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 3: Filter with not eqaul operator with no error. ===== print("CASE {}: Filter with not eqaul operator with no error.".format(case_number)) # This should not raise any error. try: results = asst3_schema.Account.filter(db, balance__ne=89.1) if len(results) == 4 and results[0].type == "Normal": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 4: Filter with greater than operator with no error. ===== print("CASE {}: Filter with greater than operator with no error.".format(case_number)) # This should not raise any error. try: results = asst3_schema.Account.filter(db, balance__gt=89.1) if len(results) == 2 and results[0].type == "Normal": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 5: Filter with less than operator with no error. ===== print("CASE {}: Filter with less than operator with no error.".format(case_number)) # This should not raise any error. try: results = asst3_schema.Account.filter(db, balance__lt=210.3) if len(results) == 3 and results[0].type == "Normal": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ==== CASE 6: Filter with a foreign key object. ===== print("CASE {}: Filter with a foreign key object.".format(case_number)) # This should not raise any error. try: results = asst3_schema.Account.filter(db, user=alice) if len(results) == 3 and results[0].user.firstName == "Alice": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ==== CASE 7: Filter with a foreign key pk value. ===== print("CASE {}: Filter with a foreign key pk value.".format(case_number)) # This should not raise any error. try: results = asst3_schema.Account.filter(db, user=3) if len(results) == 3 and results[0].user.firstName == "Alice": print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: filter result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 8: Filter with a foreign key object whose pk is None. ===== print("CASE {}: Filter with a foreign key object whose pk is None.".format(case_number)) # This should raise an error. try: results = asst3_schema.Account.filter(db, user=charlie) except: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: {}".format(case_number, "expected an error")) db.close() server.end() test.add_mark(mark)
def main(): # Set up the tester. test = asst3.start_test('orm count command test', TOTAL_MARK) # Import student's code. import orm mark = 0 case_number = 1 # Start the server. server = asst3.Server() server.start("preload.txt") # Set up student's database object and connect to the database. db = orm.setup("easydb", asst3_schema) asst3.try_connect(db, server) # ===== CASE 1: Count with no argument with no error. ===== print("CASE {}: Count with no argument with no error.".format(case_number)) # This should not raise any error. try: result = asst3_schema.Account.count(db) if result == 4: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: count result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ===== CASE 2: Count with an argument with no error. ===== print("CASE {}: Count with an argument with no error.".format(case_number)) # This should not raise any error. try: result = asst3_schema.Account.count(db, balance__gt=89.1) if result == 2: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: count result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ==== CASE 3: Count with an existing pk. ===== print("CASE {}: Count with pk.".format(case_number)) # This should not raise any error. try: result = asst3_schema.Account.count(db, pk=3) if result == 1: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: count result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) case_number = case_number + 1 # ==== CASE 4: Count with an non-existing pk. ===== print("CASE {}: Count with pk.".format(case_number)) # This should not raise any error. try: result = asst3_schema.Account.count(db, pk=100) if result == 0: print("CASE {} PASS".format(case_number)) mark = mark + 1 else: print("CASE {} FAIL: count result is not correct".format(case_number)) except Exception as e: print("CASE {} FAIL: {}".format(case_number, str(e))) db.close() server.end() test.add_mark(mark)