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
Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 jinja2.testsuite.bytecode_cache4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 Test bytecode caching7Bump up the copyright to 2017Armin Ronacherbefore adoptionbbe0a41
line 8why, one step back8 :copyright: (c) 2017 by the Jinja Team.Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 9–10why, one step back9 :license: BSD, see LICENSE for more details.10"""Merge pull request #672 from alexk307/tests-memcached-bytecode-cacheDavid Lordbefore adoption5828b8b
line 11why, one step back11Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
line 12why, one step back12import pytestMerge pull request #672 from alexk307/tests-memcached-bytecode-cacheDavid Lordbefore adoption5828b8b
line 13why, one step back13Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
line 14why, one step back14from jinja2 import EnvironmentMerge pull request #672 from alexk307/tests-memcached-bytecode-cacheDavid Lordbefore adoption5828b8b
lines 15–16why, one step back15from jinja2.bccache import Bucket, FileSystemBytecodeCache, \16 MemcachedBytecodeCacheMerge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 17–29why, one step back17from 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_cacheCleaned up shitty syntax in testsuiteArmin Ronacherbefore adoption73e2b51
line 30why, one step back30class TestByteCodeCache(object):Merge branch 'lkarthee-pytest_migration'Markus Unterwaditzerbefore adoption1c5f1e8
lines 31–35why, one step back3132 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')Merge pull request #672 from alexk307/tests-memcached-bytecode-cacheDavid Lordbefore adoption5828b8b
lines 36–92why, one step back363738class 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)