Example #1
0
def test_newly_added_tests(helper):
    db = DatabaseHelper()
    db.init_conn()

    helper.change_file("changes/test_car/add_test_passengers.txt", "tests/test_car.py")

    new_tests = common.read_newly_added_tests(db)
    db.close_conn()

    assert new_tests == {"tests/test_car.py::test_passengers"}
Example #2
0
def test_split_changes():
    db = DatabaseHelper()
    db.init_conn()

    change_list = ["src/car.py", "random_file.txt", "tests/test_car.py"]
    for path in change_list:
        with open(path, "w") as f:
            f.write("nothing")

    test_files, src_files = common.split_changes(change_list, db)
    db.close_conn()

    for t in test_files:
        assert t[1] in {"tests/test_car.py"}

    for s in src_files:
        assert s[1] in {"src/car.py"}
Example #3
0
def test_tests_from_changed_sourcefiles(helper):
    db = DatabaseHelper()
    db.init_conn()

    helper.change_file("changes/car/change_accelerate.txt", "src/car.py")

    changed_files = ["src/car.py"]
    test_files, src_files = common.split_changes(changed_files, db)
    diff_dict = common.file_diff_dict_current(src_files)

    (
        test_set,
        changed_lines_dict,
        new_line_map_dict,
        files_to_warn,
    ) = common.tests_from_changed_srcfiles(diff_dict, src_files, db)
    db.close_conn()

    assert test_set == {"tests/test_car.py::test_acceleration"}
Example #4
0
def test_delete_ran_lines():
    conn = sqlite3.connect(DB_FILE_NAME)
    c = conn.cursor()
    file_id = 1
    sql = "SELECT line_id FROM test_map WHERE file_id = ?"
    old_line_ids = [x[0] for x in c.execute(sql, (file_id, )).fetchall()]
    conn.close()

    db = DatabaseHelper()
    db.init_conn()
    db.delete_ran_lines(old_line_ids, file_id)
    db.close_conn()

    conn = sqlite3.connect(DB_FILE_NAME)
    c = conn.cursor()
    new_line_ids = [x[0] for x in c.execute(sql, (file_id, )).fetchall()]
    conn.close()

    assert not new_line_ids
Example #5
0
def test_update_db_from_test_mapping():
    # shift all lines and compare old and new lines in db
    conn = sqlite3.connect(DB_FILE_NAME)
    c = conn.cursor()
    testfile_id = 2
    funclines_sql = "SELECT id,start,end FROM test_function WHERE test_file_id = ?"
    filename_sql = "SELECT path FROM test_file WHERE id = ?"

    testfile_name = c.execute(filename_sql, (testfile_id, )).fetchone()[0]
    old_func_line_dict = {
        x[0]: (x[1], x[2])
        for x in c.execute(funclines_sql, (testfile_id, )).fetchall()
    }

    conn.close()

    shift = 5
    line_count = sum(1 for line in open(testfile_name))
    line_map = {x: x + shift for x in range(1, line_count + 1)}

    db = DatabaseHelper()
    db.init_conn()
    db.update_db_from_test_mapping(line_map, testfile_id)
    db.close_conn()

    conn = sqlite3.connect(DB_FILE_NAME)
    c = conn.cursor()
    new_func_line_dict = {
        x[0]: (x[1], x[2])
        for x in c.execute(funclines_sql, (testfile_id, )).fetchall()
    }
    conn.close()

    assert len(old_func_line_dict) == len(new_func_line_dict)
    for key in old_func_line_dict.keys():
        old_func_linenumbers = old_func_line_dict[key]
        new_func_linenumbers = new_func_line_dict[key]
        assert old_func_linenumbers[0] + shift == new_func_linenumbers[0]
        assert old_func_linenumbers[1] + shift == new_func_linenumbers[1]
Example #6
0
def test_tests_from_changed_testfiles(helper):
    db = DatabaseHelper()
    db.init_conn()

    helper.change_file(
        "changes/test_shop/change_test_normal_shop_purchase.txt",
        "tests/test_shop.py",
    )

    changed_files = [
        "tests/test_shop.py",
    ]
    test_files, src_files = common.split_changes(changed_files, db)
    diff_dict = common.file_diff_dict_current(test_files)

    (
        test_set,
        changed_lines_dict,
        new_line_map_dict,
    ) = common.tests_from_changed_testfiles(diff_dict, test_files, db)
    db.close_conn()

    assert test_set == {"tests/test_shop.py::test_normal_shop_purchase"}
Example #7
0
def test_update_db_from_src_mapping():
    # shift all lines and compare old and new lines in db
    conn = sqlite3.connect(DB_FILE_NAME)
    c = conn.cursor()
    file_id = 1
    sql = "SELECT line_id FROM test_map WHERE file_id = ?"
    old_line_ids = [x[0] for x in c.execute(sql, (file_id, )).fetchall()]
    conn.close()

    shift = 5
    line_map = {x: x + shift for x in old_line_ids}

    db = DatabaseHelper()
    db.init_conn()
    db.update_db_from_src_mapping(line_map, file_id)
    db.close_conn()

    conn = sqlite3.connect(DB_FILE_NAME)
    c = conn.cursor()
    new_line_ids = [x[0] for x in c.execute(sql, (file_id, )).fetchall()]
    conn.close()

    assert new_line_ids == [k + shift for k in old_line_ids]
Example #8
0
 def get_newly_added_tests_from_tool(self):
     db = DatabaseHelper()
     db.init_conn()
     new_tests = read_newly_added_tests(db)
     db.close_conn()
     return new_tests
Example #9
0
 def get_all_tests_for_srcfile(self, src_file_id):
     db = DatabaseHelper()
     db.init_conn()
     all_tests = db.query_all_tests_srcfile(src_file_id)
     db.close_conn()
     return all_tests
Example #10
0
 def get_tests_from_tool_committed(self):
     db = DatabaseHelper()
     db.init_conn()
     change_data = get_tests_and_data_committed(db)
     db.close_conn()
     return change_data.test_set