test_bytecode_cache.py
This file
Path
tests/test_bytecode_cache.py
92 lines · 2.2 KiB · Python
Last changed by
Merge pull request #672 from alexk307/tests-memcached-bytecode-cacheDavid Lordbefore adoption5828b8b
History of this fileTake it
1# -*- coding: utf-8 -*-2"""3 jinja2.testsuite.bytecode_cache4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 Test bytecode caching78 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""1112import pytest1314from jinja2 import Environment15from jinja2.bccache import Bucket, FileSystemBytecodeCache, \16 MemcachedBytecodeCache17from jinja2.exceptions import TemplateNotFound181920@pytest.fixture21def env(package_loader):22 bytecode_cache = FileSystemBytecodeCache()23 return Environment(24 loader=package_loader,25 bytecode_cache=bytecode_cache,26 )272829@pytest.mark.byte_code_cache30class TestByteCodeCache(object):3132 def test_simple(self, env):33 tmpl = env.get_template('test.html')34 assert tmpl.render().strip() == 'BAR'35 pytest.raises(TemplateNotFound, env.get_template, 'missing.html')363738class MockMemcached(object):39 class Error(Exception):40 pass4142 key = None43 value = None44 timeout = None4546 def get(self, key):47 return self.value4849 def set(self, key, value, timeout=None):50 self.key = key51 self.value = value52 self.timeout = timeout5354 def get_side_effect(self, key):55 raise self.Error()5657 def set_side_effect(self, *args):58 raise self.Error()596061class TestMemcachedBytecodeCache(object):62 def test_dump_load(self):63 memcached = MockMemcached()64 m = MemcachedBytecodeCache(memcached)6566 b = Bucket(None, 'key', '')67 b.code = 'code'68 m.dump_bytecode(b)69 assert memcached.key == 'jinja2/bytecode/key'7071 b = Bucket(None, 'key', '')72 m.load_bytecode(b)73 assert b.code == 'code'7475 def test_exception(self):76 memcached = MockMemcached()77 memcached.get = memcached.get_side_effect78 memcached.set = memcached.set_side_effect79 m = MemcachedBytecodeCache(memcached)80 b = Bucket(None, 'key', '')81 b.code = 'code'8283 m.dump_bytecode(b)84 m.load_bytecode(b)8586 m.ignore_memcache_errors = False8788 with pytest.raises(MockMemcached.Error):89 m.dump_bytecode(b)9091 with pytest.raises(MockMemcached.Error):92 m.load_bytecode(b)