def test_unique_filename(self): def attempts(unique_on_attempt=0): # noinspection PyUnresolvedReferences,PyUnusedLocal def exists(filename): if exists.attempt == unique_on_attempt: return False exists.attempt += 1 return True exists.attempt = 0 return exists assert 'foo.bar' == get_unique_filename('foo.bar', attempts(0)) assert 'foo.bar-1' == get_unique_filename('foo.bar', attempts(1)) assert 'foo.bar-10' == get_unique_filename('foo.bar', attempts(10))
def test_unique_filename(self): def make_exists(unique_on_attempt=0): # noinspection PyUnresolvedReferences,PyUnusedLocal def exists(filename): if exists.attempt == unique_on_attempt: return False exists.attempt += 1 return True exists.attempt = 0 return exists self.assertEqual(get_unique_filename('foo.bar', exists=make_exists()), 'foo.bar') self.assertEqual(get_unique_filename('foo.bar', exists=make_exists(1)), 'foo.bar-1') self.assertEqual( get_unique_filename('foo.bar', exists=make_exists(10)), 'foo.bar-10')
def test_unique_filename(self): def make_exists(unique_on_attempt=0): # noinspection PyUnresolvedReferences,PyUnusedLocal def exists(filename): if exists.attempt == unique_on_attempt: return False exists.attempt += 1 return True exists.attempt = 0 return exists self.assertEqual( get_unique_filename('foo.bar', exists=make_exists()), 'foo.bar' ) self.assertEqual( get_unique_filename('foo.bar', exists=make_exists(1)), 'foo.bar-1' ) self.assertEqual( get_unique_filename('foo.bar', exists=make_exists(10)), 'foo.bar-10' )
def test_unique_filename(self, get_filename_max_length, orig_name, unique_on_attempt, expected): def attempts(unique_on_attempt=0): # noinspection PyUnresolvedReferences,PyUnusedLocal def exists(filename): if exists.attempt == unique_on_attempt: return False exists.attempt += 1 return True exists.attempt = 0 return exists get_filename_max_length.return_value = 10 actual = get_unique_filename(orig_name, attempts(unique_on_attempt)) assert expected == actual