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_range_with_gaps_upwards(self): """The FileSet should be able to move a range with gaps upwards, preserving the gaps.""" test_files = [ 'test (0).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, _): if f in [1, 2]: raise FileSet.IndexUnassignedError() mock_change_index.side_effect = mock_change_index_side_effect try: test_set.move_range((0, 4), 6) except FileSet.IndexUnassignedError: self.fail( "The FileSet fails to move a range upwards if it contains gaps as it doesn't handle the IndexUnassignedError." ) assertion_calls = [(mock_change_index.assert_any_call, [4, 10]), (mock_change_index.assert_any_call, [3, 9]), (mock_change_index.assert_called_with, [0, 6])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move a range upwards if it contains gaps.")
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_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_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_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_move_into_too_small_gap(self): """The FileSet should recognize when moving a range into a gap that is too tight and raise an error after undoing its operation.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, t): if t == 4: raise FileSet.IndexAssignedError(t, f, "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 range will collide with other files by being moved into a tight gap." ): test_set.move_range((6, 8), 2) assertion_calls = [(mock_change_index.assert_any_call, [6, 2]), (mock_change_index.assert_any_call, [7, 3]), (mock_change_index.assert_any_call, [8, 4])] mock_assert_many_msg( assertion_calls, "The FileSet doesn't actually try to move the range into the tight gap." ) assertion_calls = [(mock_change_index.assert_any_call, [3, 7]), (mock_change_index.assert_any_call, [2, 6])] mock_assert_many_msg( assertion_calls, "The FileSet fails to undo its operations after detecting a collision when being moved into the too-tight gap." )
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_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_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_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_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_multiple_multi_assigned_indexes_fix(self): """The FileSet should be able to fix multiple multi-assigned indexes (this will require the method to use an internal offset, since every fix moves the existing indexes a bit).""" #------------------------------------------------------------------------------ # Make sure FileSet's max index is updated after mocked call of move_range # or else this test fails due to the tested method's logic not working out #------------------------------------------------------------------------------ def mock_move_range_side_effect(range_tuple, to_position): left_range, _ = range_tuple move_amount = to_position - left_range test_set.max_index += move_amount mock_move_range.side_effect = mock_move_range_side_effect #------------------------------------------------------------------------------ test_files = [ 'test (0).jpg', 'test (0).png', 'test (1).jpg', 'test (2).gif', 'test (2).jpg', 'test (2).png', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg' ] test_set = FileSet(self.pattern, test_files) mock_find_flaws.return_value = ([], [(0, ['jpg', 'png']), (2, ['gif', 'jpg', 'png'])]) test_set.fix(True) mock_assert_msg( mock_move_range.assert_any_call, [(3, 5), 5], "The FileSet fails to correctly make space for the first multi-assigned index if there are multiple of them." ) assertion_calls = [(mock_change_index.assert_any_call, [2, 2, 'gif']), (mock_change_index.assert_any_call, [2, 3, 'jpg']), (mock_change_index.assert_any_call, [2, 4, 'png'])] mock_assert_many_msg( assertion_calls, "The FileSet fails to correctly expand the first multi-assigned index." ) mock_assert_msg( mock_move_range.assert_any_call, [(1, 7), 2], "The FileSet fails to make space for the second multi-assigned index." ) 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 second multi-assigned index." ) self.assertEqual( mock_change_index.call_count, 5, "The FileSet tries to change even more indexes than the multi-assigned ones." )
def test_set_enormous_max_index_auto_fix_multi_indexes(self): """The FileSet should be able to fix both gaps and multi-assigned indexes in a file set with a max_index greater than 1000, but with less than 1000 files.""" test_files = [ 'test (0).jpg', 'test (0).png', 'test (101).jpg', 'test (4444).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_find_flaws_side_effect(): global mock_find_flaws_call_count if mock_find_flaws_call_count == 0: mock_find_flaws_call_count += 1 test_set.max_index = 2 raise FileSet.TooManyFilesError() elif mock_find_flaws_call_count == 1: flaws = ([], [(0, ['jpg', 'png'])]) return flaws mock_find_flaws.side_effect = mock_find_flaws_side_effect try: test_set.fix(True) except FileSet.TooManyFilesError: self.fail( "The FileSet raises a TooManyFilesError even though in actuality there are much less than 1000." ) assertion_calls = [(mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [101, 1]), (mock_change_index.assert_any_call, [4444, 2])] mock_assert_many_msg( assertion_calls, "The FileSet fails to fix the gaps of a file set with a max_index greater than 1000." ) self.assertEqual( mock_find_flaws.call_count, 2, "The FileSet doesn't call find_flaws another time after fixing the gaps even though now there clearly are less than 1000 files." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(1, 2), 2], "The FileSet fails to make space for the expansion of the multi-assigned index after fixing the wide gaps of a file set with max_index > 1000." ) 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 after having fixed a file set with a max_index greater than 1000." )
def test_move_range_downwards(self): """The FileSet should be able to move a range downwards.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.move_range((7, 8), 2) assertion_calls = [(mock_change_index.assert_any_call, [7, 2]), (mock_change_index.assert_any_call, [8, 3])] mock_assert_many_msg(assertion_calls, "The FileSet fails to move the range downwards.")
def test_various_sets(self): """The CLI should be able to recognize and create all available file sets in the directory.""" test_files = [('TEPPYZG09M.png', True), ('test (0).gif', True), ('dirt41.gif', True), ('test (1).mp4', True), ('V57zC.pdf', True), ('test (2).pdf', True), ('dirt90.m4a', True), ('test (3).gif', True), ('test (4).m4a', True), ('test (5).jpg', True), ('test (6).m4a', True), ('test (7).mp4', True)] mock_scandir.return_value = mock_scandir_gen(test_files) detect_file_sets() assertion_calls = [ (mock_FileSet.assert_any_call, [('test (', ')'), ['test (0).gif', 'test (1).mp4', 'test (2).pdf', 'test (3).gif', 'test (4).m4a', 'test (5).jpg', 'test (6).m4a', 'test (7).mp4']]), (mock_FileSet.assert_any_call, [('TEPPYZG', 'M'), ['TEPPYZG09M.png']]), (mock_FileSet.assert_any_call, [('dirt', ''), ['dirt41.gif', 'dirt90.m4a']]), (mock_FileSet.assert_any_call, [('V', 'zC'), ['V57zC.pdf']]) ] mock_assert_many_msg(assertion_calls, "The CLI fails to find all existent FileSets in a directory.")
def test_detect_remove_file_set(self): """The CLI should correctly identify a file set that is used for the remove operation by default and set it as a global attribute accordingly.""" test_files = [('test (0).jpg', True), ('test (1).jpg', True), ('test (2).jpg', True), ('test (3).jpg', True), ('RMVD0.jpg', True), ('RMVD1.jpg', True), ('RMVD2.jpg', True), ('RMVD3.jpg', True)] mock_scandir.return_value = mock_scandir_gen(test_files) detect_file_sets() assertion_calls = [ (mock_FileSet.assert_any_call, [('test (', ')'), ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg']]), (mock_FileSet.assert_any_call, [('RMVD', ''), ['RMVD0.jpg', 'RMVD1.jpg', 'RMVD2.jpg', 'RMVD3.jpg']]) ] mock_assert_many_msg(assertion_calls, "The CLI fails to recognize and create the two file sets.") self.assertNotEqual(CLI.default_remove_set, None, "The CLI fails to recognize and set the default remove set after stumbling upon it.")
def test_files_in_set_and_uncategorized_mixed(self): """The CLI should be able to deal with a mixture of files in a set and non-categorized files both at once.""" test_args = [ '+', 'set1.jpg', 'file.png', 'otherSet0.gif', 'nextFile.jpg', 'otherSet1.jpg', '1/2' ] mock_expand_spot.return_value = (1, 2) mock_isfile.return_value = (True) def mock_file_in_set_set_one(file): if file == 'set1.jpg': return (True, 1) else: return (False, None) def mock_file_in_set_set_two(file): if file == 'otherSet0.gif': return (True, 0) elif file == 'otherSet1.jpg': return (True, 1) else: return (False, None) other_test_set_one = FileSet(('set', ''), ['set1.jpg']) other_test_set_two = FileSet(('otherSet', ''), ['otherSet0.gif', 'otherSet1.jpg']) other_test_set_one.file_in_set = mock_file_in_set_set_one other_test_set_two.file_in_set = mock_file_in_set_set_two CLI.file_set_cache = [other_test_set_one, other_test_set_two] add(self.test_set, test_args) assertion_calls = [(mock_add_file_set.assert_any_call, [other_test_set_one, (1, 2), [1]]), (mock_add_file_set.assert_any_call, [other_test_set_two, (2, 3), [0]]), (mock_add_file_set.assert_any_call, [other_test_set_two, (3, 4), [1]])] mock_assert_many_msg( assertion_calls, "The CLI fails to correctly identify and add the files which already belong to another set." ) mock_assert_msg( mock_add_files.assert_called_once_with, [['file.png', 'nextFile.jpg'], (4, 5)], "The CLI fails to correctly identify and add the files which don't belong to any other set." )
def test_move_into_perfectly_fitting_gap(self): """The FileSet should be able to move a range into a range that has the exact same size.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (7).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.move_range((0, 2), 4) assertion_calls = [(mock_change_index.assert_any_call, [0, 4]), (mock_change_index.assert_any_call, [1, 5]), (mock_change_index.assert_any_call, [2, 6])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move a range into a gap of exactly the same size." )
def test_move_into_big_gap(self): """The FileSet should be able to move a range into a gap within the set that has more than enough space for it.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (10).jpg', 'test (11).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.move_range((0, 2), 6) assertion_calls = [(mock_change_index.assert_any_call, [0, 6]), (mock_change_index.assert_any_call, [1, 7]), (mock_change_index.assert_any_call, [2, 8])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move a range into the middle of a big gap which surrounds it in spaces." )
def test_remove_fully_unassigned_range_preserve_gaps(self): """The FileSet should try to 'remove' the empty range of gaps into the removed file set if preserve_gaps=True, effectively just fixing the gap in the original file set.""" test_files = ['test (0).png', 'test (4).m4a', 'test (5).pdf', 'test (6).gif'] test_set = FileSet(self.pattern, test_files) mock_add_file_set.side_effect = self._mock_add_files_side_effect_remove_files test_set.remove_files(range(1, 3+1), preserve_gaps=True) mock_assert_msg(mock_add_file_set.assert_called_once_with, [test_set, (-1, 0), range(1, 3+1), strip_gaps(False), preserve_gaps(True)], "The FileSet fails to try removing the range of files/gaps into the removed_file_set.") assertion_calls = [ (mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [4, 1]), (mock_change_index.assert_any_call, [5, 2]), (mock_change_index.assert_any_call, [6, 3]) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to correctly attempt closing resulting gaps when removing a fully unassigned range from the middle, preserving the gaps.")
def test_beginning_to_end(self): """The FileSet should be able to move a range.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.move_range((0, 2), 7) assertion_calls = [(mock_change_index.assert_any_call, [0, 7]), (mock_change_index.assert_any_call, [1, 8]), (mock_change_index.assert_any_call, [2, 9])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move a range from the beginning to the end of the set." )
def test_range_wrong_order(self): """The FileSet should still move the range correctly even if the range is given from higher to lower.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.move_range((2, 0), 7) assertion_calls = [(mock_change_index.assert_any_call, [0, 7]), (mock_change_index.assert_any_call, [1, 8]), (mock_change_index.assert_any_call, [2, 9])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move a range from the beginning to the end of the set if the range is given in the wrong order." )
def test_range_end(self): """The FileSet should be able to remove a range from the end of the set.""" test_files = ['test (0).png', 'test (1).jpg', 'test (2).gif', 'test (3).m4a', 'test (4).m4a', 'test (5).pdf', 'test (6).gif'] test_set = FileSet(self.pattern, test_files) mock_add_file_set.side_effect = self._mock_add_files_side_effect_remove_files test_set.remove_files(range(4, 6+1)) mock_assert_msg(mock_add_file_set.assert_called_once_with, [test_set, (-1, 0), range(4, 6+1), strip_gaps(False), preserve_gaps(False)], "The FileSet fails to remove a range of files if they are at the end of the set.") assertion_calls = [ (mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [1, 1]), (mock_change_index.assert_any_call, [2, 2]), (mock_change_index.assert_any_call, [3, 3]) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to correctly attempt closing resulting gaps when removing files from the end.")
def test_move_downwards_implicit_self_collision(self): """The FileSet should be able to move a range downwards, even if its new position will overlap with its old position. (i.e. in this case, the current 3 will be moved to the current 2, which is assigned at the moment)""" test_files = ['test (0).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) try: test_set.move_range((2, 3), 1) except FileSet.IndexAssignedError: self.fail( "The FileSet can't move a range downwards if its new position causes a file to seemingly 'collide' with the range's old position." ) assertion_calls = [(mock_change_index.assert_any_call, [2, 1]), (mock_change_index.assert_called_with, [3, 2])] mock_assert_many_msg( assertion_calls, "The FileSet fails to actually move the range if there is an implicit self-collision." )
def test_multiple_sets(self): """The method should list all the sets if multiple of them have been found.""" test_set1 = FileSet(self.pattern, []) test_set2 = FileSet(('second', ''), []) test_set3 = FileSet(('x', 'y'), []) CLI.file_set_cache = [test_set1, test_set2, test_set3] CLI._enumerate_available_sets() assertion_calls = [ (mock_print.assert_any_call, [0, '\t', str(test_set1)]), (mock_print.assert_any_call, [1, '\t', str(test_set2)]), (mock_print.assert_any_call, [2, '\t', str(test_set3)]) ] mock_assert_many_msg( assertion_calls, "The method fails to list all of the multiple available file sets." )
def test_1_length_range(self): """The FileSet should be able to remove a range that contains only one file / follows the scheme: (n, n).""" test_files = ['test (0).png', 'test (1).jpg', 'test (2).gif', 'test (3).m4a', 'test (4).m4a', 'test (5).pdf', 'test (6).gif'] test_set = FileSet(self.pattern, test_files) mock_add_file_set.side_effect = self._mock_add_files_side_effect_remove_files test_set.remove_files(range(2, 2+1)) mock_assert_msg(mock_add_file_set.assert_called_once_with, [test_set, (-1, 0), range(2, 2+1), strip_gaps(False), preserve_gaps(False)], "The FileSet fails to remove a range of files if they are at the end of the set.") assertion_calls = [ (mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [1, 1]), (mock_change_index.assert_any_call, [3, 2]), (mock_change_index.assert_any_call, [4, 3]), (mock_change_index.assert_any_call, [5, 4]), (mock_change_index.assert_any_call, [6, 5]) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to correctly attempt closing resulting gaps when removing a single file range from the middle.")
def test_range_middle(self): """The FileSet should be able to remove a range from the middle of the set.""" test_files = ['test (0).png', 'test (1).jpg', 'test (2).gif', 'test (3).m4a', 'test (4).m4a', 'test (5).pdf', 'test (6).gif'] test_set = FileSet(self.pattern, test_files) mock_add_file_set.side_effect = self._mock_add_files_side_effect_remove_files removed_file_set, _ = test_set.remove_files(range(2, 4+1)) mock_assert_msg(mock_add_file_set.assert_called_once_with, [test_set, (-1, 0), range(2, 4+1), strip_gaps(False), preserve_gaps(False)], "The FileSet fails to remove the range of files into the removed_file_set.") assertion_calls = [ (mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [1, 1]), (mock_change_index.assert_any_call, [5, 2]), (mock_change_index.assert_any_call, [6, 3]) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to close resulting gaps when removing multiple files from the middle.") self.assertEqual(removed_file_set.pattern, ('removed', ''), "The default pattern of the removed_file_set is incorrect.")
def test_generic_index_iterable(self): """The FileSet should be able to remove files given any valid index iterable.""" test_files = ['test (0).png', 'test (1).jpg', 'test (2).gif', 'test (3).m4a', 'test (4).m4a', 'test (5).pdf', 'test (6).gif'] test_set = FileSet(self.pattern, test_files) mock_add_file_set.side_effect = self._mock_add_files_side_effect_remove_files remove_indexes = [4, 3, 1, 5] # 1, 3, 4, 5 removed_file_set, _ = test_set.remove_files(remove_indexes) mock_assert_msg(mock_add_file_set.assert_called_once_with, [test_set, (-1, 0), remove_indexes, strip_gaps(False), preserve_gaps(False)], "The FileSet fails to remove the range of files into the removed_file_set if a generic index iterable is given.") assertion_calls = [ (mock_change_index.assert_any_call, [0, 0]), (mock_change_index.assert_any_call, [2, 1]), (mock_change_index.assert_any_call, [6, 2]) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to correctly attempt closing resulting gaps when removing using a generic index iterable.") self.assertEqual(removed_file_set.pattern, ('removed', ''), "The default pattern of the removed_file_set is incorrect when given a generic index iterable.")