def testDirectCall(self): ''' tests directly calling the cache object using __call__ ''' CACHE_DIR = get_cache_dir(4) cached_str = DiskCache(CACHE_DIR, fn=str) assert cached_str(7) == "7" assert cached_str.getKey(7) in cached_str
class SkipTestDiskCached(TestCached): @staticmethod @DiskCached(get_cache_dir(1)) def add(a=1, b=2): return a + b @staticmethod @DiskCached(get_cache_dir(2)) def sub(a, b): return a - b def setUp(self): self.diskCache = DiskCache(get_cache_dir(4)) def tearDown(self): ''' remove the cache directories ''' for cacheDirNo in range(10): if exists(get_cache_dir(cacheDirNo)): rmtree(get_cache_dir(cacheDirNo)) def testObjectKeyGeneration(self): ''' ensures that the diskcache object's location does not change ''' CACHE_DIR = get_cache_dir(3) d = DiskCache(CACHE_DIR) getCacheLocation = lambda x: join(CACHE_DIR, Cache.getObjectId(x)) d.fetchObjectId(1, str, 1) assert exists(getCacheLocation(1)) d.fetch(str, 2) assert exists(getCacheLocation(((2, ), ()))) def testContains(self): ''' verifies that 'key' in cache works ''' # diskcache assert self.diskCache.fetchObjectId(1, str, 1) == "1" assert 1 in self.diskCache assert 2 not in self.diskCache # diskcached assert self.add(12, 14) == 26 assert self.add.getKey(12, 14) in self.add assert 9 not in self.add def testDelItem(self): ''' verifies that delitem works ''' # diskcache assert self.diskCache.fetch(str, 2) == "2" key = self.diskCache.getKey(2) assert key in self.diskCache del self.diskCache[key] assert key not in self.diskCache # diskcached assert self.add(12, 13) == 25 key = self.add.getKey(12, 13) assert key == ((12, 13), ()) assert key in self.add del self.add[key] assert key not in self.add def testDirectCall(self): ''' tests directly calling the cache object using __call__ ''' CACHE_DIR = get_cache_dir(4) cached_str = DiskCache(CACHE_DIR, fn=str) assert cached_str(7) == "7" assert cached_str.getKey(7) in cached_str def testIterableCache(self): ''' tests the iterable cache ''' CACHE_DIR = get_cache_dir(5) i = IterableCache(CACHE_DIR) getTestIterator = lambda x: list(range(x)) for iteratorSize in (4, 5, 6): cachedIterator = i.fetch(getTestIterator, iteratorSize) for x, y in zip(cachedIterator, getTestIterator(iteratorSize)): assert x == y @pytest.mark.slow def testThreadSafety(self): ''' tests whether everything is thread safe ''' for a in range(1000): c = DiskCache(get_cache_dir(6)) p = Pool(12) p.map(f, 60 * [c]) p.map(g, 60 * [c]) p.close() p.join()
class SkipTestDiskCached(TestCached): @staticmethod @DiskCached(get_cache_dir(1)) def add(a=1, b=2): return a+b @staticmethod @DiskCached(get_cache_dir(2)) def sub(a, b): return a-b def setUp(self): self.diskCache = DiskCache(get_cache_dir(4)) def tearDown(self): ''' remove the cache directories ''' for cacheDirNo in range(10): if exists(get_cache_dir(cacheDirNo)): rmtree(get_cache_dir(cacheDirNo)) def testObjectKeyGeneration(self): ''' ensures that the diskcache object's location does not change ''' CACHE_DIR = get_cache_dir(3) d = DiskCache(CACHE_DIR) getCacheLocation = lambda x: join(CACHE_DIR, Cache.getObjectId(x)) d.fetchObjectId(1, str, 1) assert exists( getCacheLocation(1) ) d.fetch(str, 2) assert exists( getCacheLocation( ((2,), ()) )) def testContains(self): ''' verifies that 'key' in cache works ''' # diskcache assert self.diskCache.fetchObjectId(1, str, 1 ) == "1" assert 1 in self.diskCache assert 2 not in self.diskCache # diskcached assert self.add(12,14) == 26 assert self.add.getKey(12,14) in self.add assert 9 not in self.add def testDelItem(self): ''' verifies that delitem works ''' # diskcache assert self.diskCache.fetch(str, 2) == "2" key = self.diskCache.getKey(2) assert key in self.diskCache del self.diskCache[key] assert key not in self.diskCache # diskcached assert self.add(12,13) == 25 key = self.add.getKey(12, 13) assert key == ((12, 13), ()) assert key in self.add del self.add[key] assert key not in self.add def testDirectCall(self): ''' tests directly calling the cache object using __call__ ''' CACHE_DIR = get_cache_dir(4) cached_str = DiskCache(CACHE_DIR, fn=str) assert cached_str(7) == "7" assert cached_str.getKey(7) in cached_str def testIterableCache(self): ''' tests the iterable cache ''' CACHE_DIR = get_cache_dir(5) i = IterableCache(CACHE_DIR) getTestIterator = lambda x: range(x) for iteratorSize in (4, 5, 6): cachedIterator = i.fetch( getTestIterator, iteratorSize ) for x,y in zip(cachedIterator, getTestIterator(iteratorSize)): assert x == y @pytest.mark.slow def testThreadSafety(self): ''' tests whether everything is thread safe ''' for a in range(1000): c = DiskCache(get_cache_dir(6)) p = Pool(12) p.map(f, 60*[c] ) p.map(g, 60*[c] ) p.close() p.join()