def test_append_end(self): """The FileSet should be able to append a new file to its end.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) new_file = 'new_file.jpg' mock_isfile.return_value = True test_set.add_file(new_file, (3, 4)) mock_assert_msg( mock_move_range.assert_not_called, [], msg= "The FileSet tries to make space for a new file even though that's unnecessary to append the file to the end." ) mock_assert_msg( mock_rename.assert_called_once_with, [new_file, 'test (4).jpg'], msg= "The FileSet doesn't correctly add the file to the end of the set." ) self.assertEqual( test_set.max_index, 4, "The add_file method has to update the max index itself when appending a file to the end and thus not calling move_range. This did not happen." )
def test_collision_with_files(self): """THe FileSet should recognize when the moved range collides with existing files and undo its operation.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, t): if t == 3: raise FileSet.IndexAssignedError(f, t, "Index assigned.") mock_change_index.side_effect = mock_change_index_side_effect with self.assertRaises( FileSet.FileCollisionError, msg= "The FileSet fails to recognize when a file is colliding due to the movement operation." ): test_set.move_range((1, 2), 3) assertion_calls = [(mock_change_index.assert_any_call, [2, 4]), (mock_change_index.assert_any_call, [1, 3])] mock_assert_many_msg( assertion_calls, "The FileSet does not actually try to move the range.") mock_assert_msg( mock_change_index.assert_any_call, [4, 2], "The FileSet does not properly undo its operations after discovering a file collision." )
def test_big_multi_assigned_real_world(self): """The FileSet should be able to correctly deal with a multi-assigned indexes of at least three files, given that change_index actively updates the file set's file list.""" test_files = ['test (0).gif', 'test (0).jpg', 'test (0).png'] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([], [(0, test_set.files[0])]) def mock_change_index_side_effect(f, t, ft): """If the index is actually changed, delete the given file type from the list. I.e. update the files list with every call!""" if ft is None: del test_set.files[f] elif f != t: types_list = test_set.files[f] types_list.remove(ft) mock_change_index.side_effect = mock_change_index_side_effect test_set.fix(True) mock_assert_msg( mock_move_range.assert_not_called, [], "The FileSet tries to make space even though the multi-assigned index is at the end, making this unnecessary." ) assertion_calls = [(mock_change_index.assert_any_call, [0, 0, 'gif']), (mock_change_index.assert_any_call, [0, 1, 'jpg']), (mock_change_index.assert_any_call, [0, 2, 'png'])] mock_assert_many_msg( assertion_calls, "The FileSet fails to correctly expand the multi-assigned index at the front of the set if there are real world side effects." ) self.assertEqual( mock_change_index.call_count, 3, "The FileSet tries to change even more indexes than the multi-assigned one." )
def test_gap_at_front(self): """The FileSet should be able to gix a single gap at the front.""" test_files = [ 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([(0, 1)], []) test_set.fix() assertion_calls = [(mock_change_index.assert_any_call, [2, 0]), (mock_change_index.assert_any_call, [3, 1]), (mock_change_index.assert_any_call, [4, 2]), (mock_change_index.assert_any_call, [5, 3]), (mock_change_index.assert_any_call, [6, 4]), (mock_change_index.assert_any_call, [7, 5]), (mock_change_index.assert_any_call, [8, 6])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move the files correctly to close a gap at the front." ) self.assertEqual( mock_change_index.call_count, 7, "The FileSet tries to deal with multi-assigned indexes, even though that's not explicitly wished for." ) mock_assert_msg( mock_move_range.assert_not_called, [], "The FileSet tries to move ranges even though there is no reason to make any space." )
def test_set_enormous_max_index(self): """The FileSet should be able to fix itself even if it has a max index greater than 1000.""" test_files = [ 'test (234).jpg', 'test (346).jpg', 'test (934).jpg', 'test (1038).jpg' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.side_effect = FileSet.TooManyFilesError() try: test_set.fix() except FileSet.TooManyFilesError: self.fail( "The FileSet raises a TooManyFilesError even though it could have fixed the set of only four files." ) assertion_calls = [(mock_change_index.assert_any_call, [234, 0]), (mock_change_index.assert_any_call, [346, 1]), (mock_change_index.assert_any_call, [934, 2]), (mock_change_index.assert_any_call, [1038, 3])] mock_assert_many_msg( assertion_calls, "The FileSet fails to fix the gaps of a file set with a max_index above 1000." ) mock_assert_msg( mock_move_range.assert_not_called, [], "The FileSet tries to move ranges even though there is no reason to make any space." )
def test_multi_assigned_index_at_front_fix(self): """The FileSet should be able to fix a multi-assigned index at the front of the set.""" test_files = [ 'test (0).jpg', 'test (0).png', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([], [(0, ['jpg', 'png'])]) test_set.fix(True) mock_assert_msg( mock_move_range.assert_called_once_with, [(1, 5), 2], "The FileSet fails to make space for the multi-assigned index to expand if it's at the front." ) assertion_calls = [(mock_change_index.assert_any_call, [0, 0, 'jpg']), (mock_change_index.assert_any_call, [0, 1, 'png'])] mock_assert_many_msg( assertion_calls, "The FileSet fails to correctly expand the multi-assigned index at the front of the set." ) self.assertEqual( mock_change_index.call_count, 2, "The FileSet tries to change even more indexes than the multi-assigned one." )
def test_multi_assigned_index_at_end_fix(self): """The FileSet should be able to fix a multi_assigned index at the end of the set.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (5).png' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([], [(5, ['jpg', 'png'])]) test_set.fix(True) mock_assert_msg( mock_move_range.assert_not_called, [], "The FileSet tries to make space for the multi-assigned index to expand even though that's unnecessary at the end." ) assertion_calls = [(mock_change_index.assert_any_call, [5, 5, 'jpg']), (mock_change_index.assert_any_call, [5, 6, 'png'])] mock_assert_many_msg( assertion_calls, "The FileSet fails to correctly expand the multi-assigned index at the end of the set." ) self.assertEqual( mock_change_index.call_count, 2, "The FileSet tries to change even more indexes than the multi-assigned one." )
def test_move_gap_onto_files(self): """The FileSet should NOT find a collision when moving a gap onto other files.""" test_files = ['test (0).jpg', 'test (4).jpg'] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, t): raise FileSet.IndexUnassignedError(f, t, "Index does not exist.") mock_change_index.side_effect = mock_change_index_side_effect test_set.move_range((1, 2), 4) assertion_calls = [(mock_change_index.assert_any_call, [2, 5]), (mock_change_index.assert_any_call, [1, 4])] mock_assert_many_msg( assertion_calls, "The FileSet does not actually try to move the range.") with self.assertRaises( AssertionError, msg= "The FileSet tries to undo its operation even though there shouldn't be a problem." ): ## This assertion should fail, since the call should NOT exist! mock_assert_msg(mock_change_index.assert_any_call(5, 2))
def test_end(self): """The FileSet should be able to append files to the end.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (3, 4)) mock_assert_msg(mock_move_range.assert_not_called, [], "The FileSet unnecessarily tries to make space when appending the files to the end.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (4).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (5).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically append the files to the end.") assertion_calls = [ (mock_add_logically.assert_any_call, [4, 'add']), (mock_add_logically.assert_any_call, [5, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when appending to the end.") self.assertEqual(test_set.max_index, 5, "The FileSet fails to update the max_index when adding to the end of the set.")
def test_files_with_various_gaps(self): """The FileSet should be able to fix multiple gaps at once.""" test_files = [ 'test (0).jpg', 'test (2).jpg', 'test (5).jpg', 'test (6).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([(1, 1), (3, 4), (7, 7)], []) test_set.fix() assertion_calls = [(mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [2, 1]), (mock_change_index.assert_any_call, [5, 2]), (mock_change_index.assert_any_call, [6, 3]), (mock_change_index.assert_any_call, [8, 4])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move the files correctly to close multiple gaps." ) self.assertEqual( mock_change_index.call_count, 5, "The FileSet tries to deal with multi-assigned indexes, even though that's not explicitly wished for." ) mock_assert_msg( mock_move_range.assert_not_called, [], "The FileSet tries to move ranges even though there is no reason to make any space." )
def test_remove_into_given_filled_file_set(self): """The FileSet should append the removed file to a given filled FileSet.""" removed_file_set = FileSet( ('CustomRemoved (', ')'), ['CustomRemoved (0).jpg', 'CustomRemoved (3).png']) test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg)', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.remove_file(1, removed_file_set) mock_assert_msg( mock_add_file.assert_called_once_with, ['test (1).jpg', 4], msg= "The FileSet fails to properly remove and append the file into another FileSet." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 1], msg= "The FileSet fails to close the resulting gap from the remove operation when removing into another FileSet." ) self.assertEqual( removed_file_set.pattern, ('CustomRemoved (', ')'), "The set pattern of the removed_file_set is not the same as demanded." )
def test_remove_into_given_empty_file_set(self): """The FileSet should manage to add a removed file into a given empty file set.""" removed_file_set = FileSet(('CustomRemoved (', ')'), []) test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg)', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.remove_file(1, removed_file_set) mock_assert_msg( mock_add_file.assert_called_once_with, ['test (1).jpg', 0], msg= "The FileSet fails to properly remove the file into another FileSet." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 1], msg= "The FileSet fails to close the resulting gap from the remove operation when removing into another FileSet." ) self.assertEqual( removed_file_set.pattern, ('CustomRemoved (', ')'), "The default pattern of the removed_file_set is incorrect.")
def test_default_remove_set_is_active_other_set_is_given( self, mock_expand_pattern): """The CLI should be able to remove files from the default remove set if another file set to remove the files into is given.""" test_args = ['-', '-a', 'custom*set', '1-2'] mock_expand_range.return_value = (1, 2) mock_expand_pattern.return_value = ('custom', 'set') default_remove_set = FileSet(CLI.DEFAULT_REMOVE_PATTERN, ['RMVD1.jpg', 'RMVD2.jpg']) CLI.default_remove_set = default_remove_set custom_remove_set = FileSet(('custom', 'set'), []) CLI.file_set_cache = [custom_remove_set] # set already exists try: remove(default_remove_set, test_args) except CLIRuntimeError: self.fail( "The CLI fails to perform a remove operation if it's removing from the default remove set even though a range to append the removed files to is given." ) mock_assert_msg( mock_remove_files.assert_called_once_with, [[1, 2], custom_remove_set], "The CLI fails to perform a removal operation from the default remove set to a given custom remove set." )
def test_gap_in_foreign_add_files_preserve_gaps(self): """The FileSet should be able to preserve gaps if the corresponding keyword is set to True.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) try: amount_added = test_set.add_file_set(add_set, (1, 2), range(0, 2+1), preserve_gaps = True) except FileSet.IndexUnassignedError: self.fail("The FileSet raises an exception when encountering a gap even though preserve_gaps was set to True.") mock_assert_msg(mock_move_range.assert_called_once_with, [(2, 3), 5], "The FileSet fails to make space for the files and gaps when the latter are preserved.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (4).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files and gaps from the foreign FileSet when the gaps are preserved.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [4, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 3, "The FileSet fails to return the correct number of indexes (files and gaps) that were actually added when the files to be added contain gaps.")
def test_gap_in_foreign_add_all_indexes(self): """The FileSet should automatically ignore gaps in the foreign file set and add all files if add_indexes is set to 'ALL' (default).""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) try: amount_added = test_set.add_file_set(add_set, (1, 2)) except FileSet.IndexUnassignedError as e: raise AssertionError(e.args, "The FileSet fails to add a FileSet with gaps, even though indexes='ALL' has been used.") mock_assert_msg(mock_move_range.assert_called_once_with, [(2, 3), 4], msg="The FileSet fails to make the correct space for the two files with a gap.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (3).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files with a gap into the FileSet.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when the files to be added contain gaps.")
def test_wide_gap_in_foreign_add_files_strip_gaps(self): """The FileSet should still correctly strip all gaps in the foreign FileSet if strip_gaps is set to True, even if the gap is wider than just one.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (3).add'] add_set = FileSet(('add (', ')'), add_files) try: amount_added = test_set.add_file_set(add_set, (1, 2), range(0, 3+1), strip_gaps=True) except FileSet.IndexUnassignedError as e: raise AssertionError(e.args, "The FileSet raises an exception when encountering a gap even though strip_gaps was set to True.") mock_assert_msg(mock_move_range.assert_called_once_with, [(2, 3), 4], "The FileSet fails to make the correct amount of space for the newly added files when the file set to be added from contains wide gaps and strip_gaps is set to True.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (3).add', 'test (3).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files from the foreign FileSet with gaps if strip_gaps is True.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when the files to be added contain gaps with strip_gaps set to True.")
def test_too_small_gap(self): """The FileSet should be able to fill a small gap and automatically make the necessary extra-space if the amount of files requires it.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (4).jpg', 'test (5).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (1, 2)) mock_assert_msg(mock_move_range.assert_called_once_with, [(4, 5), 5], "The FileSet fails to make space for the added files when the gap is too small.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (3).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (4).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files into a gap that had to be widened.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']), (mock_add_logically.assert_any_call, [4, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 3, "The FileSet fails to return the number of indexes that were actually added when adding into a too-small gap.")
def test_add_into_empty_set(self): """The FileSet should be able to add files even if it is empty.""" test_files = [] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (-1, 0)) mock_assert_msg(mock_move_range.assert_not_called, [], "The FileSet unnecessarily tries to make space when adding into an empty set.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (0).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (1).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (2).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files into a an empty FileSet.") assertion_calls = [ (mock_add_logically.assert_any_call, [0, 'add']), (mock_add_logically.assert_any_call, [1, 'add']), (mock_add_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 3, "The FileSet fails to return the number of indexes that were actually added when adding into an empty set.") self.assertEqual(test_set.max_index, 2, "The FileSet fails to update the max_index when adding into an empty file set.")
def test_front(self): """The FileSet should be able to add files to the front of the set.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (-1, 0)) mock_assert_msg(mock_move_range.assert_called_once_with, [(0, 3), 2], "The FileSet fails to make space for the new files at the front.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (0).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (1).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files to the front.") assertion_calls = [ (mock_add_logically.assert_any_call, [0, 'add']), (mock_add_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when adding to the front.")
def test_fitting_gap(self): """The FileSet should be able to add files into a gap, filling it up completely without having to make extra space.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (4).jpg', 'test (5).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (1, 2)) mock_assert_msg(mock_move_range.assert_not_called, [], "The FileSet unnecessarily tries to make space when adding the files into a perfectly fitting gap.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (3).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files into a perfectly fitting gap.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when adding into a perfectly fitting gap.")
def test_new_custom_remove_set_already_exists(self, mock_expand_pattern, mock_files_detected): """The CLI should recognize and raise an error when a supposedly new custom remove set already exists.""" test_args = ['-', '-n', 'custom*set', '2-3'] mock_expand_range.return_value = (2, 3) mock_expand_pattern.return_value = ('custom', 'set') CLI.default_remove_set = self.default_remove_set custom_remove_set = FileSet(('custom', 'set'), []) mock_files_detected.return_value = custom_remove_set CLI.file_set_cache = [custom_remove_set] # set already exists with self.assertRaises( CLIRuntimeError, msg= "The CLI fails to recognize when a supposedly new file set to remove files into already exists." ): remove(self.test_set, test_args) mock_assert_msg( mock_files_detected.assert_not_called, [], "The CLI tries to create the new removed set even though it evidently already exists and an error was raised." ) mock_assert_msg( mock_remove_files.assert_not_called, [], "The CLI tries to remove files even though there was an error.")
def test_add_multiple_files(self): """The method should be able to add multiple files at once.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = [ 'new_file1.add1', 'new_file2.add2', 'another_file.add3' ] mock_isfile.return_value = True mock_check_spot.return_value = (1, 2) test_set.add_files(files_to_add, (1, 2)) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 5], "The method fails to make space for the files to be added.") assertion_calls = [ (mock_rename.assert_any_call, ['new_file1.add1', 'test (2).add1']), (mock_rename.assert_any_call, ['new_file2.add2', 'test (3).add2']), (mock_rename.assert_any_call, ['another_file.add3', 'test (4).add3']) ] mock_assert_many_msg(assertion_calls, "The method fails to physically add the files.") assertion_calls = [(mock_add_logically.assert_any_call, [2, 'add1']), (mock_add_logically.assert_any_call, [3, 'add2']), (mock_add_logically.assert_any_call, [4, 'add3'])] mock_assert_many_msg(assertion_calls, "The method fails to logically add the files.")
def test_non_existent_files_ignore_mode(self): """When ignore_unfound_files=True, the method should ignore non-existent files and add the rest flawlessly.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = ['new_file1.add1', 'not_existent', 'another_file.add3'] mock_isfile.side_effect = lambda f: not (f == 'not_existent') mock_check_spot.return_value = (1, 2) try: test_set.add_files(files_to_add, (1, 2), ignore_unfound_files=True) except FileSet.FileNotFoundError: self.fail( "The method raises a FileNotFoundError even though ignore_unfound_files was set to True." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 4], "The method fails to make space for the files to be added.") assertion_calls = [(mock_rename.assert_any_call, ['new_file1.add1', 'test (2).add1']), (mock_rename.assert_any_call, ['another_file.add3', 'test (3).add3'])] mock_assert_many_msg(assertion_calls, "The method fails to physically add the files.") assertion_calls = [(mock_add_logically.assert_any_call, [2, 'add1']), (mock_add_logically.assert_any_call, [3, 'add3'])] mock_assert_many_msg(assertion_calls, "The method fails to logically add the files.")
def test_multi_assigned_indexes_mode_fix(self): """The FileSet should be able to fix a multi-assigned index in its middle if fix_multi_idx is set to True.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (3).png', 'test (4).jpg', 'test (5).jpg' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([], [(3, ['jpg', 'png'])]) test_set.fix(True) mock_assert_msg( mock_move_range.assert_called_once_with, [(4, 5), 5], "The FileSet fails to correctly make space for the multi-assigned index to expand." ) assertion_calls = [(mock_change_index.assert_any_call, [3, 3, 'jpg']), (mock_change_index.assert_any_call, [3, 4, 'png'])] mock_assert_many_msg( assertion_calls, "The FileSet fails to correctly expand the multi-assigned index.") self.assertEqual( mock_change_index.call_count, 2, "The FileSet tries to change even more indexes than the multi-assigned one." )
def test_valid_arguments(self): """The method should correctly perform its operation if its given valid arguments.""" test_args = ['rename', 'new*'] mock_expand_pattern.return_value = self.new_pattern CLI.rename(self.test_set, test_args) mock_assert_msg(mock_change_pattern.assert_called_once_with, [self.new_pattern], "The method fails to perform the change_pattern operation correctly.")
def test_too_few_arguments(self): """The method should recognize and raise an error if it is given too few arguments.""" test_args = ['rename'] with self.assertRaises(CLI.ArgumentAmountError, msg="The method fails to recognize and raise an error if too few arguments are given."): CLI.rename(self.test_set, test_args) mock_assert_msg(mock_change_pattern.assert_not_called, [], "The method tries to perform an operation even though an error was raised.")
def test_default_strict_true(self): """The switch_files method should use allow_gaps=False by default.""" self.test_set.switch_files(1, 2) mock_assert_msg( mock_switch_file_ranges.assert_called_once_with, [(1, 1), (2, 2), preserve_gaps(False)], "The method fails to pass the correct arguments along with True for mode strict as the default argument to switch_file_ranges." )
def test_valid_single_file_range(self): """The CLI should be able to perform a movement operation given a single-file range.""" test_args = ['3', '>', '1/2'] mock_expand_spot.return_value = (1, 2) mock_expand_range.return_value = (3, 3) move(self.test_set, test_args) mock_assert_msg(mock_move_files.assert_called_once_with, [(3, 3), (1, 2)], "The CLI fails to perform a movement operation with a valid single-file range.")
def test_too_few_arguments(self): """The CLI should recognize and raise an error when too few arguments are given.""" test_args = ['2-3', '>'] mock_expand_range.return_value = (2, 3) with self.assertRaises(ArgumentAmountError, msg="The FileSet fails to recognize when too few arguments are given."): move(self.test_set, test_args) mock_assert_msg(mock_move_files.assert_not_called, [], "The CLI attempts to perform a switch operation even though there was an invalid number of arguments.")
def test_correct_argument_passing(self): """The switch_files method should pass its arguments to switch_file_ranges, since it's actually just a special use case of that method.""" self.test_set.switch_files(1, 2, True) mock_assert_msg( mock_switch_file_ranges.assert_called_once_with, [(1, 1), (2, 2), preserve_gaps(True)], "The method fails to pass the correct arguments to switch_file_ranges." )