def main(): import sys args = List(sys.argv) if len(args) >= 2 and args[1] == "run": import code port = args.get(2, 1234) host = args.get(3, "localhost") # create db object db = orm.setup("easydb", schema) # keep only objects defined in the schema module items = { k: v for k, v in globals().items() if getattr(v, '__module__', None) == "schema" } items['db'] = db # start the database connection db.connect(host, port) # start interactive mode code.interact(local=items) # close database connection db.close() elif len(args) in [2, 3] and args[1] == "export": output = orm.export("easydb", schema) if len(args) == 3: # write outout of export() to file with open(args[2], "wt") as f: f.write(output) else: print(output) else: print("usage:", sys.argv[0], "run [PORT=1234] [HOST=localhost]") print("\tstarts interactive shell") print("usage:", sys.argv[0], "export [FILE]") print("\texports schema to FILE or print to console")
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 setUp(self): self.db = orm.setup("easydb", schema) self.db.connect("127.0.0.1", 8080)
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)
# run.py from app.constants import APP, cache from orm import setup if __name__ == '__main__': setup() cache.init_app(APP) APP.run(debug=True, host='0.0.0.0') # Do not write code after this point
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('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('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('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)