def test_file_list(self): cache = self.make_cache()[0] # test file -> [file.name] with tempfile.NamedTemporaryFile() as f: assert io_cache.file_list(f) == [f.name] # test CacheEntry -> [CacheEntry.path] assert io_cache.file_list(cache[0]) == [cache[0].path] # test cache file -> pfnlist() with tempfile.NamedTemporaryFile(suffix='.lcf', mode='w') as f: io_cache.write_cache(cache, f) f.seek(0) assert io_cache.file_list(f.name) == cache.pfnlist() # test comma-separated list -> list assert io_cache.file_list('A,B,C,D') == ['A', 'B', 'C', 'D'] # test cache object -> pfnlist assert io_cache.file_list(cache) == cache.pfnlist() # test list -> list assert io_cache.file_list(['A', 'B', 'C', 'D']) == ['A', 'B', 'C', 'D'] # otherwise error with pytest.raises(ValueError): io_cache.file_list(1)
def test_is_cache_file(self): # check file(path) is return as True if parsed as Cache e = io_cache.CacheEntry.from_T050017('/tmp/A-B-12345-6.txt') with tempfile.NamedTemporaryFile() as f: # empty file should return False assert io_cache.is_cache(f) is False assert io_cache.is_cache(f.name) is False # cache file should return True io_cache.write_cache([e], f) f.seek(0) assert io_cache.is_cache(f) is True assert io_cache.is_cache(f.name) is True
def test_read_write_cache(self): cache = self.make_cache()[0] with tempfile.NamedTemporaryFile() as f: io_cache.write_cache(cache, f) f.seek(0) # read from fileobj c2 = io_cache.read_cache(f) assert cache == c2 # write with file name io_cache.write_cache(cache, f.name) # read from file name c3 = io_cache.read_cache(f.name) assert cache == c3
def test_is_cache(self): # sanity check assert io_cache.is_cache(None) is False # make sure Cache is returned as True cache = io_cache.Cache() assert io_cache.is_cache(cache) is True # check file(path) is return as True if parsed as Cache cache.append(io_cache.CacheEntry.from_T050017('/tmp/A-B-12345-6.txt')) with tempfile.NamedTemporaryFile() as f: # empty file should return False assert io_cache.is_cache(f) is False assert io_cache.is_cache(f.name) is False # cache file should return True io_cache.write_cache(cache, f) f.seek(0) assert io_cache.is_cache(f) is True assert io_cache.is_cache(f.name) is True # check ASCII file gets returned as False a = numpy.array([[1, 2], [3, 4]]) with tempfile.TemporaryFile() as f: numpy.savetxt(f, a) f.seek(0) assert io_cache.is_cache(f) is False # check HDF5 file gets returned as False try: import h5py except ImportError: pass else: fp = tempfile.mktemp() try: h5py.File(fp, 'w').close() assert io_cache.is_cache(fp) is False finally: if os.path.isfile(fp): os.remove(fp)