def test_copy_to_workdir(): eid = 'ddr-test-123-456-master-abc123' entity = identifier.Identifier(eid) # inputs src_path = os.path.join(BASEDIR, 'src', 'somefile.tif') tmp_path = os.path.join(BASEDIR, 'tmp', 'somefile.tif') tmp_path_renamed = os.path.join(BASEDIR, 'tmp', 'ddr-test-123-456-master-abc123.tif') log = ingest.addfile_logger(entity, base_dir=BASEDIR) # prep src_dir = os.path.dirname(src_path) tmp_dir = os.path.dirname(tmp_path) # clean slate if os.path.exists(src_dir): shutil.rmtree(src_dir, ignore_errors=True) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir, ignore_errors=True) os.makedirs(src_dir) os.makedirs(tmp_dir) with open(src_path, 'w') as f: f.write('test_copy_to_workdir') # tests assert os.path.exists(src_path) ingest.copy_to_workdir(src_path, tmp_path, tmp_path_renamed, log) assert os.path.exists(src_path) assert not os.path.exists(tmp_path) assert os.path.exists(tmp_path_renamed) # clean up if os.path.exists(src_dir): shutil.rmtree(src_dir, ignore_errors=True) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir, ignore_errors=True)
def test_move_files(): # this seems way too complicated # inputs files = [ (os.path.join(BASEDIR, 'src', 'file1.txt'), os.path.join(BASEDIR, 'dest', 'file1.txt')), (os.path.join(BASEDIR, 'src', 'file2.txt'), os.path.join(BASEDIR, 'dest', 'file2.txt')), ] log = ingest.addfile_logger(identifier.Identifier('ddr-test-123-456'), base_dir=BASEDIR) # fresh start for tmp,dest in files: shutil.rmtree(os.path.dirname(tmp), ignore_errors=True) shutil.rmtree(os.path.dirname(dest), ignore_errors=True) for tmp,dest in files: tmp_dir = os.path.dirname(tmp) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) dest_dir = os.path.dirname(dest) if not os.path.exists(dest_dir): os.makedirs(dest_dir) with open(tmp, 'w') as f: f.write('test file 1') # test for tmp,dest in files: assert os.path.exists(tmp) assert not os.path.exists(dest) ingest.move_files(files, log) for tmp,dest in files: assert not os.path.exists(tmp) assert os.path.exists(dest) # clean up for tmp,dest in files: shutil.rmtree(os.path.dirname(tmp), ignore_errors=True) shutil.rmtree(os.path.dirname(dest), ignore_errors=True)
def test_copy_to_workdir(): eid = "ddr-test-123-456-master-abc123" entity = identifier.Identifier(eid) # inputs src_path = os.path.join(BASEDIR, "src", "somefile.tif") tmp_path = os.path.join(BASEDIR, "tmp", "somefile.tif") tmp_path_renamed = os.path.join(BASEDIR, "tmp", "ddr-test-123-456-master-abc123.tif") log = ingest.addfile_logger(entity, base_dir=BASEDIR) # prep src_dir = os.path.dirname(src_path) tmp_dir = os.path.dirname(tmp_path) # clean slate if os.path.exists(src_dir): shutil.rmtree(src_dir, ignore_errors=True) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir, ignore_errors=True) os.makedirs(src_dir) os.makedirs(tmp_dir) with open(src_path, "w") as f: f.write("test_copy_to_workdir") # tests assert os.path.exists(src_path) ingest.copy_to_workdir(src_path, tmp_path, tmp_path_renamed, log) assert os.path.exists(src_path) assert not os.path.exists(tmp_path) assert os.path.exists(tmp_path_renamed) # clean up if os.path.exists(src_dir): shutil.rmtree(src_dir, ignore_errors=True) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir, ignore_errors=True)
def test_move_files(): # this seems way too complicated # inputs files = [ (os.path.join(BASEDIR, "src", "file1.txt"), os.path.join(BASEDIR, "dest", "file1.txt")), (os.path.join(BASEDIR, "src", "file2.txt"), os.path.join(BASEDIR, "dest", "file2.txt")), ] log = ingest.addfile_logger(identifier.Identifier("ddr-test-123-456"), base_dir=BASEDIR) # fresh start for tmp, dest in files: shutil.rmtree(os.path.dirname(tmp), ignore_errors=True) shutil.rmtree(os.path.dirname(dest), ignore_errors=True) for tmp, dest in files: tmp_dir = os.path.dirname(tmp) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) dest_dir = os.path.dirname(dest) if not os.path.exists(dest_dir): os.makedirs(dest_dir) with open(tmp, "w") as f: f.write("test file 1") # test for tmp, dest in files: assert os.path.exists(tmp) assert not os.path.exists(dest) ingest.move_files(files, log) for tmp, dest in files: assert not os.path.exists(tmp) assert os.path.exists(dest) # clean up for tmp, dest in files: shutil.rmtree(os.path.dirname(tmp), ignore_errors=True) shutil.rmtree(os.path.dirname(dest), ignore_errors=True)
def test_check_dir(): eid = 'ddr-test-123-456' log = ingest.addfile_logger(identifier.Identifier(eid), base_dir=BASEDIR) label = 'testing' assert ingest.check_dir('tmp', '/tmp', log) assert_raises( Exception, ingest.check_dir, 'var', '/var', log )
def test_make_access_file(): # inputs src_path = os.path.join(BASEDIR, 'src', 'somefile.png') access_dest_path = os.path.join(BASEDIR, 'ddr-test-123-456-master-abc123-a.jpg') expected = access_dest_path log = ingest.addfile_logger(identifier.Identifier('ddr-test-123-456'), base_dir=BASEDIR) # no src_path so fails assert ingest.make_access_file(src_path, access_dest_path, log) == None # prep if os.path.exists(access_dest_path): os.remove(access_dest_path) # arrange test jpg src_path = TEST_IMG_PATH if not os.path.exists(src_path): os.makedirs(os.path.dirname(src_path)) urllib.urlretrieve(TEST_IMG_URL, TEST_IMG_PATH) assert ingest.make_access_file(src_path, access_dest_path, log) == access_dest_path # clean up if os.path.exists(access_dest_path): os.remove(access_dest_path)
def test_make_access_file(): # inputs src_path = os.path.join(BASEDIR, "src", "somefile.png") access_dest_path = os.path.join(BASEDIR, "ddr-test-123-456-master-abc123-a.jpg") expected = access_dest_path log = ingest.addfile_logger(identifier.Identifier("ddr-test-123-456"), base_dir=BASEDIR) # no src_path so fails assert ingest.make_access_file(src_path, access_dest_path, log) == None # prep if os.path.exists(access_dest_path): os.remove(access_dest_path) # arrange test jpg src_path = TEST_IMG_PATH if not os.path.exists(src_path): os.makedirs(os.path.dirname(src_path)) urllib.urlretrieve(TEST_IMG_URL, TEST_IMG_PATH) assert ingest.make_access_file(src_path, access_dest_path, log) == access_dest_path # clean up if os.path.exists(access_dest_path): os.remove(access_dest_path)
def test_check_dir(): eid = "ddr-test-123-456" log = ingest.addfile_logger(identifier.Identifier(eid), base_dir=BASEDIR) label = "testing" assert ingest.check_dir("tmp", "/tmp", log) assert_raises(Exception, ingest.check_dir, "var", "/var", log)