Ejemplo n.º 1
0
    def test_memo(self):
        fn = lambda: "abc"
        cache_key = "a123"
        obj = CacheHandler.memo(cache_key, fn)
        self.assertEqual("abc", obj)

        os.remove(
            CacheHandler._build_filename(cache_key, CacheObjectType.OTHER))
Ejemplo n.º 2
0
    def test_memo_with_object_type(self):
        fn = lambda: "abc"
        cache_key = "a123"
        obj = CacheHandler.memo(cache_key, fn, CacheObjectType.ENCODING)
        self.assertEqual("abc", obj)
        self.assertTrue(
            os.path.isfile(EnvironmentSettings.get_cache_path() /
                           f"encoding/{cache_key}.pickle"))

        os.remove(
            CacheHandler._build_filename(cache_key, CacheObjectType.ENCODING))
Ejemplo n.º 3
0
    def test_add_by_key(self):
        params = ("k1", 1), ("k2", ("k3", 2))
        obj = "object_example"

        h = CacheHandler._hash(params)
        CacheHandler.add_by_key(h, obj)
        filename = CacheHandler._build_filename(h, CacheObjectType.OTHER)
        with open(filename, "rb") as file:
            obj2 = pickle.load(file)

        self.assertEqual(obj, obj2)
        os.remove(filename)
Ejemplo n.º 4
0
    def test_get_by_key(self):
        params = (("k1", 1), ("k2", 2))
        obj = "object_example"

        h = CacheHandler._hash(params)
        filename = CacheHandler._build_filename(h, CacheObjectType.OTHER)
        with open(filename, "wb") as file:
            pickle.dump(obj, file)

        obj2 = CacheHandler.get_by_key(h, CacheObjectType.OTHER)
        self.assertEqual(obj, obj2)
        os.remove(filename)