Пример #1
0
    def test_nodb_cache(self):
        self.assertTrue(True)
        nodb = NoDB()
        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)

        nodb._get_base_cache_path()
Пример #2
0
    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()