Beispiel #1
0
def main():
    test = tester.Core('arguments test', 15)
    wizard = tester.datapath('wizard.txt', 'asst1')
    shoe = tester.datapath('shoe.txt', 'asst1')

    test.start_program('./asst1 -i %d' % (random.randint(-int(maxsize), 0)))
    check_and_wait(test, r'Error: SEED must be a non-negative integer\.')

    test.start_program('./asst1 -a %s 0' % wizard)
    check_and_wait(test, r'Error: NUM must be a natural number\.')

    test.start_program('./asst1 -a %s' % wizard)
    check_and_wait(
        test,
        r'Error: must specify number of hands when playing automatically\.')

    test.start_program('./asst1 -s')
    check_and_wait(
        test,
        r'Error: silent mode is only available when playing automatically\.')

    seed = 123
    test.start_program('./asst1 -f %s -i %d' % (shoe, seed))
    check_and_wait(test,
                   r'Error: cannot choose both file and random-based shoe\.',
                   0)

    temp = "record.txt"
    test.start_program('./asst1 -f %s -r %s' % (shoe, temp))
    check_and_wait(
        test, r'Error: recording is only available for random-based shoe\.', 0)
    try:
        os.remove(temp)
    except OSError as e:
        # the student may not have created this file yet
        pass

    test.start_program('./asst1 -f')
    check_and_wait(test, r"\.\/asst1: option requires an argument -- 'f'", 0)

    test.start_program('./asst1 -r')
    check_and_wait(test, r"\.\/asst1: option requires an argument -- 'r'", 0)

    test.start_program('./asst1 -i')
    check_and_wait(test, r"\.\/asst1: option requires an argument -- 'i'", 0)

    test.start_program('./asst1 -a')
    check_and_wait(test, r"\.\/asst1: option requires an argument -- 'a'", 0)

    test.start_program('\./asst1 -h')
    check_and_wait(
        test,
        r"usage: \.\/asst1 \[-h\] \[-f FILE\|-i SEED \[-r FILE\]\] \[\[-s\] -a FILE NUM\]",
        0)
 def __init__(self, filename=None):
     self.host = "localhost"
     self.port = random.randint(1024, 9999)
     if filename is None:
         self.schema = tester.datapath('export.txt', 'asst3')
     else:
         self.schema = filename
def load_module(modname):
    path = tester.datapath(modname + ".py", 'asst3')
    spec = importlib.util.spec_from_file_location(modname, path)
    mod = importlib.util.module_from_spec(spec)
    tester.includepath()
    spec.loader.exec_module(mod)
    return mod
Beispiel #4
0
def load_result(name):
    mark = 5
    tester.includepath()
    import easybj
    result = easybj.calculate()[name]
    path = tester.datapath(name + '.p', 'asst2')
    f = open(path, 'rb')
    refobj = pickle.load(f)
    f.close()
    return result, refobj
def main():
    test = tester.Core('memory test', 5)
    path = tester.datapath("wizard.txt", 'asst1')
    test.start_program('valgrind ./asst1 -s -i %d -a %s %d' % (4, path, 10000),
                       timeout=10)

    find_or_exit(test, 'Hands Played: 10000')
    find_or_exit(test, 'Final Balance: \+\$1103\.00')
    find_or_exit(test, 'Player Advantage: 11\.03\%')

    test.set_timeout(1)
    exit_on_leak(test, 'definitely lost: .+? bytes in (\d+) blocks')
    exit_on_leak(test, 'indirectly lost: .+? bytes in (\d+) blocks')
    exit_on_leak(test, 'possibly lost: .+? bytes in (\d+) blocks')
    test.add_mark(3)
    exit_on_leak(test, 'still reachable: .+? bytes in (\d+) blocks')
    test.add_mark(2)
 def start(self, datafile=None):
     if datafile is not None:
         self.datafile = tester.datapath(datafile, 'asst3')
     else:
         self.datafile = ""
     path = "%s -g %d %s localhost %s"%(EASYDB_PATH, self.port, self.schema,
         self.datafile)
     self.program = pexpect.spawn(path, [], encoding='utf-8')
     self.program.logfile = open('tester.log', 'a')
     self.program.logfile.write("\n-------- %s --------\n\n"%sys.argv[0])
     idx = self.program.expect([r"\]", pexpect.EOF])
     self.program.logfile.flush()
     if idx != 0:
         self.program.close(force=True)
         self.program.logfile.close()
         del self.program
         return False
     return True
Beispiel #7
0
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)
Beispiel #8
0
def begin(test, shoefile):
    shoe = tester.datapath(shoefile, 'asst1')
    test.start_program('./asst1 -f %s' % shoe)