def test_run_sort_high_low(file_system): """ Test it can sort files high to low """ instructions = { "Directory": "*", "Recursive": "False", "RegexExpression": r"file(\d)", "OutputName": "out.txt", "SortType": "HighLow" } workfolder = file_system["main"]["dir"].as_posix() var = MergeFiles(workfolder, instructions) run_success = var.run() assert run_success, "Expected to run successfully" output_file = file_system["main"]["dir"] / "out.txt" assert output_file.exists() output_file = output_file.as_posix() result = [] with open(output_file, 'r') as file_read: for line in file_read: result.append(line) expected_result = ["3\n", "2\n", "1\n"] assert result == expected_result
def test_run_instructions_none(file_system): """ Testing run fails if necessary instructions is not given """ workfolder = file_system["main"]["dir"].as_posix() var = MergeFiles(workfolder, None) run_success = var.run() assert not run_success
def test_run_workfolder_none(): """ Test it cant run when no valid workfolder is given """ instructions = { "Directory": "*", "Recursive": "False", "RegexExpression": r"file(\d)", "OutputName": "out.txt" } var = MergeFiles(None, instructions) run_success = var.run() assert not run_success
def test_run_sub_directory(file_system): """ Test it run in relative path """ instructions = { "Directory": "sub", "Recursive": "False", "RegexExpression": r"file(\d)", "OutputName": "out.txt" } workfolder = file_system["main"]["dir"].as_posix() var = MergeFiles(workfolder, instructions) run_success = var.run() assert run_success, "Expected to run successfully" assert (file_system["sub"]["dir"] / "out.txt").exists()
def test_delete_files_invalid_path(): """ Test it returns true if invalid paths is given """ test_files = ["invalid/path/test.txt", "invalid/path/test2.txt"] ran_success = MergeFiles.delete_files(test_files) assert ran_success, "Expected True"
def test_merge_files_arg_files_none(file_system): """ Test it dont merge files if no files is given """ output_file = (file_system["main"]["dir"] / "output.txt").as_posix() ran_success = MergeFiles.merge_files(output_file, None) assert not ran_success, "Expected to return False"
def test_match_files_with_regex_arg_files_none(): """ Test it returns no files """ regex = re.compile(r".*(\d)") files = MergeFiles.match_files_with_regex(None, regex) assert not files
def test_sort_files_arg_files_none(): """ Test it returns empty list if no files to sort """ regex = re.compile(r"no match(\d)") sorted_files = MergeFiles.sort_files(None, regex) assert not sorted_files
def test_merge_files_invalid_files(file_system): """ Test it returns false if no valid files is given """ test_files = ["invalid/test1.txt", "invalid/test2.txt"] output_file = (file_system["main"]["dir"] / "output.txt").as_posix() ran_success = MergeFiles.merge_files(output_file, test_files) assert not ran_success, "Expected to return False"
def test_match_files_with_regex_arg_regex_none(): """ Test it returns no files """ test_files = [] test_files.append("c:/test/path/test1.txt") test_files.append("c:/test/path/test2.txt") test_files.append("c:/test/path/message.txt") files = MergeFiles.match_files_with_regex(test_files, None) assert not files
def test_run_delete(file_system): """ Test delete files when running """ instructions = { "Directory": "*", "Recursive": "False", "RegexExpression": r"file(\d)", "OutputName": "out.txt", "Delete": "True" } workfolder = file_system["main"]["dir"].as_posix() var = MergeFiles(workfolder, instructions) run_success = var.run() assert run_success, "Expected to run successfully" assert (file_system["main"]["dir"] / "out.txt").exists() assert not file_system["main"]["file1"].exists() assert not file_system["main"]["file2"].exists() assert not file_system["main"]["file3"].exists()
def test_match_files_with_regex_no_match(): """ Test it returns no files """ regex = re.compile(r"no match.*(\d)") test_files = [] test_files.append("c:/test/path/test1.txt") test_files.append("c:/test/path/test2.txt") test_files.append("c:/test/path/message.txt") files = MergeFiles.match_files_with_regex(test_files, regex) assert not files
def test_merge_files_invalid_new_path(file_system): """ Test it returns false if no valid path is given for ned file """ test_files = [] test_files.append(file_system["main"]["file1"].as_posix()) test_files.append(file_system["main"]["file2"].as_posix()) test_files.append(file_system["main"]["file3"].as_posix()) output_file = "Invalid/path.txt" ran_success = MergeFiles.merge_files(output_file, test_files) assert not ran_success, "Expected to return False"
def test_merge_files_arg_new_filepath_none(file_system): """ Test it dont merge files if new_file_path is none """ test_files = [] test_files.append(file_system["main"]["file1"].as_posix()) test_files.append(file_system["main"]["file2"].as_posix()) test_files.append(file_system["main"]["file3"].as_posix()) ran_success = MergeFiles.merge_files(None, test_files) assert not ran_success, "Expected to return False"
def test_sort_files_arg_regex_none(): """ Test it returns the same file order if regex is none """ test_files = [] test_files.append("c:/test/path/test2.txt") test_files.append("c:/test/path/test3.txt") test_files.append("c:/test/path/test1.txt") sorted_files = MergeFiles.sort_files(test_files, None) assert len(sorted_files) == 3 assert sorted_files[0] == "c:/test/path/test2.txt" assert sorted_files[1] == "c:/test/path/test3.txt" assert sorted_files[2] == "c:/test/path/test1.txt"
def test_match_files_with_regex(): """ Test return is only matched files """ regex = re.compile(r".*(\d)") test_files = [] test_files.append("c:/test/path/test1.txt") test_files.append("c:/test/path/test2.txt") test_files.append("c:/test/path/message.txt") files = MergeFiles.match_files_with_regex(test_files, regex) assert len(files) == 2 assert files[0] == "c:/test/path/test1.txt" assert files[1] == "c:/test/path/test2.txt"
def test_delete_files(file_system): """ Testing delete files """ test_files = [] test_files.append(file_system["main"]["file1"].as_posix()) test_files.append(file_system["main"]["file2"].as_posix()) test_files.append(file_system["main"]["file3"].as_posix()) ran_success = MergeFiles.delete_files(test_files) assert ran_success, "Expected to run successfully" assert not file_system["main"]["file1"].exists(), "Expected to be deleted" assert not file_system["main"]["file2"].exists(), "Expected to be deleted" assert not file_system["main"]["file3"].exists(), "Expected to be deleted"
def test_sort_files_no_regex_match(): """ Test it returns the same file order if no match """ regex = re.compile(r"no match(\d)") test_files = [] test_files.append("c:/test/path/test2.txt") test_files.append("c:/test/path/test3.txt") test_files.append("c:/test/path/test1.txt") sorted_files = MergeFiles.sort_files(test_files, regex) assert len(sorted_files) == 3 assert sorted_files[0] == "c:/test/path/test2.txt" assert sorted_files[1] == "c:/test/path/test3.txt" assert sorted_files[2] == "c:/test/path/test1.txt"
def test_sort_files(): """ Test can sort files """ regex = re.compile(r".*(\d)") test_files = [] test_files.append("c:/test/path/test2.txt") test_files.append("c:/test/path/test3.txt") test_files.append("c:/test/path/test1.txt") sorted_files = MergeFiles.sort_files(test_files, regex) assert len(sorted_files) == 3 assert sorted_files[0] == "c:/test/path/test1.txt" assert sorted_files[1] == "c:/test/path/test2.txt" assert sorted_files[2] == "c:/test/path/test3.txt"
def test_merge_files(file_system): """ Test can merge files """ test_files = [] test_files.append(file_system["main"]["file1"].as_posix()) test_files.append(file_system["main"]["file2"].as_posix()) test_files.append(file_system["main"]["file3"].as_posix()) output_file = (file_system["main"]["dir"] / "output.txt") ran_success = MergeFiles.merge_files(output_file, test_files) assert ran_success, "Expected to return True" assert output_file.exists() output_file = output_file.as_posix() result = [] with open(output_file, 'r') as file_read: for line in file_read: result.append(line) expected_result = ["1\n", "2\n", "3\n"] assert result == expected_result
def test_delete_files_arg_none(): """ Testing delete files returns false if files er none """ ran_success = MergeFiles.delete_files(None) assert not ran_success, "Expected False"