Skip to content
Myr

test_bytecode_cache.py

This file

Path

tests/test_bytecode_cache.py

79 lines · 2.0 KiB · Python

Last changed by

Merge pull request #1121 from pallets/the-great-renameDavid Lordbefore adoption457b59b

History of this file

Take it

1# -*- coding: utf-8 -*-2import pytest34from jinja import Environment5from jinja.bccache import Bucket6from jinja.bccache import FileSystemBytecodeCache7from jinja.bccache import MemcachedBytecodeCache8from jinja.exceptions import TemplateNotFound91011@pytest.fixture12def env(package_loader, tmp_path):13    bytecode_cache = FileSystemBytecodeCache(str(tmp_path))14    return Environment(loader=package_loader, bytecode_cache=bytecode_cache)151617@pytest.mark.byte_code_cache18class TestByteCodeCache(object):19    def test_simple(self, env):20        tmpl = env.get_template("test.html")21        assert tmpl.render().strip() == "BAR"22        pytest.raises(TemplateNotFound, env.get_template, "missing.html")232425class MockMemcached(object):26    class Error(Exception):27        pass2829    key = None30    value = None31    timeout = None3233    def get(self, key):34        return self.value3536    def set(self, key, value, timeout=None):37        self.key = key38        self.value = value39        self.timeout = timeout4041    def get_side_effect(self, key):42        raise self.Error()4344    def set_side_effect(self, *args):45        raise self.Error()464748class TestMemcachedBytecodeCache(object):49    def test_dump_load(self):50        memcached = MockMemcached()51        m = MemcachedBytecodeCache(memcached)5253        b = Bucket(None, "key", "")54        b.code = "code"55        m.dump_bytecode(b)56        assert memcached.key == "jinja/bytecode/key"5758        b = Bucket(None, "key", "")59        m.load_bytecode(b)60        assert b.code == "code"6162    def test_exception(self):63        memcached = MockMemcached()64        memcached.get = memcached.get_side_effect65        memcached.set = memcached.set_side_effect66        m = MemcachedBytecodeCache(memcached)67        b = Bucket(None, "key", "")68        b.code = "code"6970        m.dump_bytecode(b)71        m.load_bytecode(b)7273        m.ignore_memcache_errors = False7475        with pytest.raises(MockMemcached.Error):76            m.dump_bytecode(b)7778        with pytest.raises(MockMemcached.Error):79            m.load_bytecode(b)