Skip to content
Myr

test_bytecode_cache.py

This file

Path

tests/test_bytecode_cache.py

77 lines · 1.9 KiB · Python

Last changed by

Not known.

History of this file

Take it

1import pytest23from jinja2 import Environment4from jinja2.bccache import Bucket5from jinja2.bccache import FileSystemBytecodeCache6from jinja2.bccache import MemcachedBytecodeCache7from jinja2.exceptions import TemplateNotFound8910@pytest.fixture11def env(package_loader, tmp_path):12    bytecode_cache = FileSystemBytecodeCache(str(tmp_path))13    return Environment(loader=package_loader, bytecode_cache=bytecode_cache)141516class TestByteCodeCache:17    def test_simple(self, env):18        tmpl = env.get_template("test.html")19        assert tmpl.render().strip() == "BAR"20        pytest.raises(TemplateNotFound, env.get_template, "missing.html")212223class MockMemcached:24    class Error(Exception):25        pass2627    key = None28    value = None29    timeout = None3031    def get(self, key):32        return self.value3334    def set(self, key, value, timeout=None):35        self.key = key36        self.value = value37        self.timeout = timeout3839    def get_side_effect(self, key):40        raise self.Error()4142    def set_side_effect(self, *args):43        raise self.Error()444546class TestMemcachedBytecodeCache:47    def test_dump_load(self):48        memcached = MockMemcached()49        m = MemcachedBytecodeCache(memcached)5051        b = Bucket(None, "key", "")52        b.code = "code"53        m.dump_bytecode(b)54        assert memcached.key == "jinja2/bytecode/key"5556        b = Bucket(None, "key", "")57        m.load_bytecode(b)58        assert b.code == "code"5960    def test_exception(self):61        memcached = MockMemcached()62        memcached.get = memcached.get_side_effect63        memcached.set = memcached.set_side_effect64        m = MemcachedBytecodeCache(memcached)65        b = Bucket(None, "key", "")66        b.code = "code"6768        m.dump_bytecode(b)69        m.load_bytecode(b)7071        m.ignore_memcache_errors = False7273        with pytest.raises(MockMemcached.Error):74            m.dump_bytecode(b)7576        with pytest.raises(MockMemcached.Error):77            m.load_bytecode(b)