def test_file_store_load(self): with TempDir() as cache_dir, TempDir() as d: cache = Directory(path=cache_dir) foo_file = os.path.join(d, 'foo_file') bar_file = os.path.join(d, 'bar_file') with open(foo_file, 'w') as f: f.write('foo') with open(bar_file, 'w') as f: f.write('bar') self.assertFalse(os.listdir(cache_dir)) cache.store_file(foo_file, 'foo_key') cache.store_file(bar_file, 'bar_key') self.assertTrue(os.listdir(cache_dir)) os.remove(foo_file) os.remove(bar_file) cache.load_file('foo_key', foo_file) cache.load_file('bar_key', bar_file) with open(foo_file, 'r') as f: self.assertEqual(f.read(), 'foo') with open(bar_file, 'r') as f: self.assertEqual(f.read(), 'bar')
def test_key_exists(self): with TempDir() as cache_dir, TempDir() as d: cache = Directory(path=cache_dir) foo_file = os.path.join(d, 'foo_file') with open(foo_file, 'w') as f: f.write('foo') key = 'foo_key' self.assertFalse(cache.exists(key)) cache.store_file(foo_file, key) self.assertTrue(cache.exists(key))
def test_key_lock_unlock(self): with TempDir() as cache_dir, TempDir() as d: cache = Directory(path=cache_dir) foo_file = os.path.join(d, 'foo_file') with open(foo_file, 'w') as f: f.write('foo') key = 'foo_key' cache.store_file(foo_file, key) self.assertTrue(self.try_lock_from_another_process(cache_dir, key)) with cache.lease(key): self.assertFalse(self.try_lock_from_another_process(cache_dir, key)) self.assertTrue(self.try_lock_from_another_process(cache_dir, key))
def test_store_raise_error_on_invalid_path(self): with TempDir() as cache_dir, TempDir() as d: cache = Directory(path=cache_dir) not_file = os.path.join(d, 'doesnt_exist') self.assertRaises(SourceNotFound, lambda: cache.store_file(not_file, 'key'))