def test_double_duplicate_files(): # Here we use mock to patch out the time.localtime() function so that it # always returns the same value. This allows us to consistently simulate this test cases # where the last two of the three files are imported at the same time as the timestamp. with mock.patch('airtime_analyzer.filemover_analyzer.time') as mock_time: mock_time.localtime.return_value = time.localtime() #date(2010, 10, 8) mock_time.side_effect = lambda *args, **kw: time(*args, **kw) filename = os.path.basename(DEFAULT_AUDIO_FILE) #Import the file once FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, dict()) #Copy it back to the original location shutil.copy("./" + filename, DEFAULT_AUDIO_FILE) #Import it again. It shouldn't overwrite the old file and instead create a new first_dup_metadata = dict() first_dup_metadata = FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, first_dup_metadata) #Copy it back again! shutil.copy("./" + filename, DEFAULT_AUDIO_FILE) #Reimport for the third time, which should have the same timestamp as the second one #thanks to us mocking out time.localtime() second_dup_metadata = dict() second_dup_metadata = FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, second_dup_metadata) #Cleanup: move the file (eg. 44100Hz-16bit-mono.mp3) back shutil.move("./" + filename, DEFAULT_AUDIO_FILE) #Remove the renamed duplicate, eg. 44100Hz-16bit-mono_03-26-2014-11-58.mp3 os.remove(first_dup_metadata["full_path"]) os.remove(second_dup_metadata["full_path"]) assert os.path.exists(DEFAULT_AUDIO_FILE)
def test_bad_permissions_destination_dir(): filename = os.path.basename(DEFAULT_AUDIO_FILE) dest_dir = u'/sys/foobar' # /sys is using sysfs on Linux, which is unwritable FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, dest_dir, filename, dict()) #Move the file back shutil.move(os.path.join(dest_dir, filename), DEFAULT_AUDIO_FILE) assert os.path.exists(DEFAULT_AUDIO_FILE)
def test_move_samefile(src_dir): FileMoverAnalyzer.move( os.path.join(src_dir, AUDIO_FILENAME), src_dir, AUDIO_FILENAME, dict(), ) assert os.path.exists(os.path.join(src_dir, AUDIO_FILENAME))
def test_move_bad_permissions_dest_dir(src_dir): with pytest.raises(OSError): # /sys is using sysfs on Linux, which is unwritable FileMoverAnalyzer.move( os.path.join(src_dir, AUDIO_FILENAME), "/sys/foobar", AUDIO_FILENAME, dict(), )
def test_duplicate_file(): filename = os.path.basename(DEFAULT_AUDIO_FILE) #Import the file once FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, dict()) #Copy it back to the original location shutil.copy("./" + filename, DEFAULT_AUDIO_FILE) #Import it again. It shouldn't overwrite the old file and instead create a new metadata = dict() metadata = FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, metadata) #Cleanup: move the file (eg. 44100Hz-16bit-mono.mp3) back shutil.move("./" + filename, DEFAULT_AUDIO_FILE) #Remove the renamed duplicate, eg. 44100Hz-16bit-mono_03-26-2014-11-58.mp3 os.remove(metadata["full_path"]) assert os.path.exists(DEFAULT_AUDIO_FILE)
def import_and_restore(src_dir, dest_dir) -> dict: """ Small helper to test the FileMoverAnalyzer.move function. Move the file and restore it back to it's origine. """ # Import the file metadata = FileMoverAnalyzer.move( os.path.join(src_dir, AUDIO_FILENAME), dest_dir, AUDIO_FILENAME, dict(), ) # Copy it back to the original location shutil.copy( os.path.join(dest_dir, AUDIO_FILENAME), os.path.join(src_dir, AUDIO_FILENAME), ) return metadata
def test_basic_samefile(): filename = os.path.basename(DEFAULT_AUDIO_FILE) FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'tests/test_data', filename, dict()) assert os.path.exists(DEFAULT_AUDIO_FILE)
def test_basic(): filename = os.path.basename(DEFAULT_AUDIO_FILE) FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, dict()) #Move the file back shutil.move("./" + filename, DEFAULT_AUDIO_FILE) assert os.path.exists(DEFAULT_AUDIO_FILE)
def test_move_wrong_string_param3(): FileMoverAnalyzer.move('', '', '', dict())
def test_move_wrong_dict_param(): FileMoverAnalyzer.move('', '', '', 12345)
def test_move_wrong_string_param2(): FileMoverAnalyzer.move(u'', 23, u'', dict())
def test_dont_use_analyze(): FileMoverAnalyzer.analyze(u'foo', dict())
def test_move_wrong_params(params, exception): with pytest.raises(exception): FileMoverAnalyzer.move(*params)
def test_analyze(): with pytest.raises(Exception): FileMoverAnalyzer.analyze("foo", dict())