def test_prompt_user(capsys): """Ensures rename.prompt_user outputs the correct message according to the user's response.""" mocked_remove = rename.FileManager.remove_duplicates rename.FileManager.remove_duplicates = lambda *a, **kw: a mocked_delete = rename.FileManager.delete_duplicates rename.FileManager.delete_duplicates = lambda *a, **kw: a duplicates = list(range(10)) file_manager = rename.FileManager(SAMPLES) # Test positive answer rename.input = lambda *a, **kw: 'y' rename.prompt_user(duplicates, file_manager) captured = capsys.readouterr() assert captured.out == '10 duplicate files deleted.\n' # Test negative answer rename.input = lambda *a, **kw: 'n' rename.prompt_user(duplicates, file_manager) captured = capsys.readouterr() assert captured.out == 'No duplicate file was removed.\n' rename.input = input rename.FileManager.remove_duplicates = mocked_remove rename.FileManager.delete_duplicates = mocked_delete
def test_find_duplicates_no_duplicate(): """Asserts if rename.FileManager.find_duplicates correctly indentifies all the copies of the same file as duplicate.""" copy_samples() file_manager = rename.FileManager(TESTS) duplicates = file_manager.find_duplicates() assert len(duplicates) == 0 clean_up()
def test_find_duplicates_all_duplicates(): """Asserts if rename.FileManager.find_duplicates correctly indentifies all the copies of the same file as duplicate.""" copies = 5 copy_sample_n_times(copies) file_manager = rename.FileManager(TESTS) duplicates = file_manager.find_duplicates() # One copy will be considered as the original assert len(duplicates) == copies - 1 clean_up()
def test_constructor_raises_exception(): """Ensures rename.FileManger will raise rename.NotFileToRenameError, with the correct message, if the given directory is empty.""" temp = TESTS / 'temp' temp.mkdir() with pytest.raises(rename.NoFileToRenameError) as error: rename.FileManager(temp) message = f'{temp} has no file to rename.' assert error.value.args[0] == message temp.rmdir()
def test_delete_duplicate_before_remove(): """Ensures that FileManager.delete_duplicate will raise rename.DuplicateNotRemovedError with the correct message if the user attempts to delete a duplicate file that was not first removed from FileManager.files""" copy_sample_n_times(2) file_manager = rename.FileManager(TESTS) duplicates = file_manager.find_duplicates() with pytest.raises(rename.DuplicateNotRemovedError) as error: file_manager.delete_duplicates(duplicates) message = (f'Remove {duplicates[0].path} from FileManager.files with ' 'FileManager.remove_duplicates before attempting to ' 'deleting it.') assert error.value.args[0] == message clean_up()
def test_rename_files_subsequent_call(): """Tests if FileManager.rename_files raises rename.NoFileToRenameError, with the correct message, when the file manager has been depleted.""" temp = TESTS / 'temp' temp.mkdir() copy_samples(temp) file_manager = rename.FileManager(temp) file_manager.resolve_targets() file_manager.rename_files() assert file_manager.depleted is True with pytest.raises(rename.NoFileToRenameError) as error: file_manager.rename_files() assert error.value.args[0] == ('FileManager is depleted. There is no ' 'file to be renamed') clean_up(temp) temp.rmdir()
def test_resolve_targets_all_uniques(samples_create_date): """Tests if FileManager.resolve_targets correctly classifies all samples target as unique.""" copy_samples() file_manager = rename.FileManager(TESTS) file_manager.resolve_targets() result = [ file_.target for file_ in file_manager.files if file_.path.suffix == '.jpg' ] expected = [] for create_date in samples_create_date: path = TESTS / f'{create_date}.jpg' expected.append(path) assert sorted(result) == sorted(expected) clean_up()
def test_resolve_targets_all_duplicates(samples_create_date): """Tests if FileManager.resolve_targets correctly increments the targets when all samples are duplicates.""" copies = 5 copy_sample_n_times(copies) file_manager = rename.FileManager(TESTS) file_manager.resolve_targets() result = [ file_.target for file_ in file_manager.files if file_.path.suffix == '.jpg' ] expected = [] for index in range(copies): path = TESTS / f'{samples_create_date[0]} {index + 1}.jpg' expected.append(path) assert sorted(result) == sorted(expected) clean_up()