def test_nodb_cache(self): nodb = NoDB('dummy') nodb.index = "Name" nodb.cache = True jeff = {"Name": "Jeff", "age": 19} serialized = nodb._serialize(jeff) real_index = nodb._format_index_value("Jeff") base_cache_path = os.path.join(tempfile.gettempdir(), '.nodb') if not os.path.isdir(base_cache_path): os.makedirs(base_cache_path) cache_path = os.path.join(base_cache_path, real_index) if not os.path.exists(cache_path): f = open(cache_path, 'a') f.close() with open(cache_path, "wb") as in_file: in_file.write(serialized.encode(NoDB.encoding)) nodb.load("Jeff") loaded = nodb.load("Jeff", default={}) self.assertEqual(loaded, jeff) loaded = nodb.load("Jeff", default="Booty") self.assertEqual(loaded, jeff) bcp = nodb._get_base_cache_path()
def test_nodb_cache(self): bucket_name = 'dummy' nodb = NoDB(bucket_name) self._create_mock_bucket(bucket_name) nodb.index = "Name" nodb.cache = True jeff = {"Name": "Jeff", "age": 19} serialized = nodb._serialize(jeff) real_index = nodb._format_index_value("Jeff") base_cache_path = os.path.join(tempfile.gettempdir(), '.nodb') if not os.path.isdir(base_cache_path): os.makedirs(base_cache_path) cache_path = os.path.join(base_cache_path, real_index) if not os.path.exists(cache_path): f = open(cache_path, 'a') f.close() with open(cache_path, "wb") as in_file: in_file.write(serialized.encode(NoDB.encoding)) nodb.load("Jeff") loaded = nodb.load("Jeff", default={}) self.assertEqual(loaded, jeff) loaded = nodb.load("Jeff", default="Booty") self.assertEqual(loaded, jeff) # test the cached item is deleted nodb.delete('Jeff') loaded = nodb.load("Jeff") self.assertIsNone(loaded) # test read from bucket when cache enabled # remove cached file nodb.save(jeff) if os.path.isfile(cache_path): os.remove(cache_path) nodb.load('Jeff') bcp = nodb._get_base_cache_path()