Skip to content
Myr

test_bytecode_cache.py

This file

Path

tests/test_bytecode_cache.py

78 lines · 2.0 KiB · Python

Last changed by

remove custom pytest marksDavid Lordbefore adoptiondd5a050

History of this file

Take it

1# -*- coding: utf-8 -*-2import pytest34from jinja2 import Environment5from jinja2.bccache import Bucket6from jinja2.bccache import FileSystemBytecodeCache7from jinja2.bccache import MemcachedBytecodeCache8from jinja2.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)151617class TestByteCodeCache(object):18    def test_simple(self, env):19        tmpl = env.get_template("test.html")20        assert tmpl.render().strip() == "BAR"21        pytest.raises(TemplateNotFound, env.get_template, "missing.html")222324class MockMemcached(object):25    class Error(Exception):26        pass2728    key = None29    value = None30    timeout = None3132    def get(self, key):33        return self.value3435    def set(self, key, value, timeout=None):36        self.key = key37        self.value = value38        self.timeout = timeout3940    def get_side_effect(self, key):41        raise self.Error()4243    def set_side_effect(self, *args):44        raise self.Error()454647class TestMemcachedBytecodeCache(object):48    def test_dump_load(self):49        memcached = MockMemcached()50        m = MemcachedBytecodeCache(memcached)5152        b = Bucket(None, "key", "")53        b.code = "code"54        m.dump_bytecode(b)55        assert memcached.key == "jinja2/bytecode/key"5657        b = Bucket(None, "key", "")58        m.load_bytecode(b)59        assert b.code == "code"6061    def test_exception(self):62        memcached = MockMemcached()63        memcached.get = memcached.get_side_effect64        memcached.set = memcached.set_side_effect65        m = MemcachedBytecodeCache(memcached)66        b = Bucket(None, "key", "")67        b.code = "code"6869        m.dump_bytecode(b)70        m.load_bytecode(b)7172        m.ignore_memcache_errors = False7374        with pytest.raises(MockMemcached.Error):75            m.dump_bytecode(b)7677        with pytest.raises(MockMemcached.Error):78            m.load_bytecode(b)