def test_engine_func(self): def engine(data, template=None): return 'text/plain', data render = Engines() render['raw'] = engine self.assertEqual(render.raw("foo"), ('text/plain', 'foo')) del render.raw
def test_engine_class(self): class Engine(object): def __call__(self, data, template=None): return 'text/plain', data render = Engines() render['raw'] = Engine self.assertEqual(render.raw("foo"), ('text/plain', 'foo')) del render.raw
class TestEngines(TestCase): def setUp(self): self.render = Engines() def test_kajiki_xml(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki.xml') self.assertEqual(rendered, (u'text/xml', u'<html><body><h1>Hello world!</h1></body></html>')) def test_kajiki_text(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki.txt') self.assertEqual(rendered, (u'text/plain', u'Hello world!')) def test_kajiki_include(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki-include.xml') self.assertEqual(rendered, (u'text/xml', u'<html><body><h1>Hello world!</h1></body></html>'))
def test_builtin_serializers(): render = Engines() tests = [ (render.bencode, ("foo", ), dict(kind='basic'), ('application/x-bencode', '3:foo')), (render.bencode, (u"foo", ), {}, ('application/x-bencode', 'u3:foo')), (render.json, ("foo", ), {}, ('application/json', '"foo"')), (render.pickle, ("foo", ), {}, ('application/octet-stream', "S'foo'\np0\n.")), (render.cpickle, ("foo", ), {}, ('application/octet-stream', "S'foo'\np1\n.")), (render.yaml, ("foo", ), {}, ('application/x-yaml', 'foo\n...\n')) ] # Marshal is version-specific. if sys.version_info < (2, 6): tests.append((render.marshal, ("foo", ), {}, ('application/octet-stream', 't\x03\x00\x00\x00foo'))) elif sys.version_info < (3, 0): tests.append((render.marshal, ("foo", ), {}, ('application/octet-stream', 's\x03\x00\x00\x00foo'))) for engine, args, kw, expected in tests: yield partial(serializer, engine, expected, *args, **kw)
class TestTemplating(TestCase): def setUp(self): self.render = Engines() def test_formatter_string(self): self.assertEqual(self.render.formatter(dict(name="world"), string="Hello {name}!"), ('text/plain', 'Hello world!')) def test_formatter_file(self): self.assertEqual(self.render.formatter(dict(name="world"), './templates/hello-formatter.txt'), ('text/plain', 'Hello world!')) def test_sprintf_string(self): self.assertEqual(self.render.sprintf(dict(name="world"), string="Hello %(name)s!"), ('text/plain', 'Hello world!')) def test_sprintf_file(self): self.assertEqual(self.render.sprintf(dict(name="world"), './templates/hello-sprintf.txt'), ('text/plain', 'Hello world!')) def test_template_string(self): self.assertEqual(self.render.template(dict(name="world"), string="Hello $name!"), ('text/plain', 'Hello world!')) def test_template_string_unsafe(self): self.assertEqual(self.render.template(dict(name="world"), string="Hello $name!", safe=False), ('text/plain', 'Hello world!')) def test_template_file(self): self.assertEqual(self.render.template(dict(name="world"), './templates/hello-template.txt'), ('text/plain', 'Hello world!'))
class TestEngines(TestCase): def setUp(self): self.render = Engines() def test_kajiki_xml(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki.xml') self.assertEqual(rendered, (u'text/xml', u'<html><body><h1>Hello world!</h1></body></html>')) def test_kajiki_text(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki.txt') self.assertEqual(rendered, (u'text/plain', u'Hello world!')) def test_kajiki_include(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki-include.xml') self.assertEqual(rendered, (u'text/xml', u'<html><body><h1>Hello world!</h1></body></html>')) def test_mako(self): self.assertEqual(self.render.mako(dict(name="world"), './templates/hello-mako.txt', content_type='text/plain'), ('text/plain', u'Hello world!')) def test_mako_include(self): self.assertEqual(self.render.mako(dict(name="world"), './templates/hello-mako-include.txt', content_type='text/plain'), ('text/plain', u'Hello world!')) def test_cheetah(self): self.assertEqual(self.render.cheetah(dict(name="world"), './templates/hello-cheetah.tmpl', content_type='text/plain'), ('text/plain', u'Hello world!\n'))
def test_render_no_engine(self): render = Engines() try: self.assertEqual( render('./templates/hello-sprintf.txt', dict(name='world')), ('text/plain', "Hello world!")) except ValueError: pass except: self.fail()
class TestTemplating(TestCase): def setUp(self): self.render = Engines() def test_formatter_string(self): self.assertEqual( self.render.formatter(dict(name="world"), string="Hello {name}!"), ('text/plain', 'Hello world!')) def test_formatter_file(self): self.assertEqual( self.render.formatter(dict(name="world"), './templates/hello-formatter.txt'), ('text/plain', 'Hello world!')) def test_sprintf_string(self): self.assertEqual( self.render.sprintf(dict(name="world"), string="Hello %(name)s!"), ('text/plain', 'Hello world!')) def test_sprintf_file(self): self.assertEqual( self.render.sprintf(dict(name="world"), './templates/hello-sprintf.txt'), ('text/plain', 'Hello world!')) def test_template_string(self): self.assertEqual( self.render.template(dict(name="world"), string="Hello $name!"), ('text/plain', 'Hello world!')) def test_template_string_unsafe(self): self.assertEqual( self.render.template(dict(name="world"), string="Hello $name!", safe=False), ('text/plain', 'Hello world!')) def test_template_file(self): self.assertEqual( self.render.template(dict(name="world"), './templates/hello-template.txt'), ('text/plain', 'Hello world!'))
class TestEngines(TestCase): def setUp(self): self.render = Engines() def test_kajiki_xml(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki.xml') self.assertEqual( rendered, (u'text/xml', u'<html><body><h1>Hello world!</h1></body></html>')) def test_kajiki_text(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki.txt') self.assertEqual(rendered, (u'text/plain', u'Hello world!')) def test_kajiki_include(self): rendered = self.render.kajiki(dict(name="world"), 'templates/hello-kajiki-include.xml') self.assertEqual( rendered, (u'text/xml', u'<html><body><h1>Hello world!</h1></body></html>')) def test_mako(self): self.assertEqual( self.render.mako(dict(name="world"), './templates/hello-mako.txt', content_type='text/plain'), ('text/plain', u'Hello world!')) def test_mako_include(self): self.assertEqual( self.render.mako(dict(name="world"), './templates/hello-mako-include.txt', content_type='text/plain'), ('text/plain', u'Hello world!')) def test_cheetah(self): self.assertEqual( self.render.cheetah(dict(name="world"), './templates/hello-cheetah.tmpl', content_type='text/plain'), ('text/plain', u'Hello world!\n'))
def test_no_defaults(self): render = Engines() self.assertTrue('json' in render) self.assertTrue(callable(render.json)) self.assertEqual(render.options['json'], dict())
def test_render(self): render = Engines() self.assertEqual( render('sprintf:./templates/hello-sprintf.txt', dict(name='world')), ('text/plain', "Hello world!"))
def test_bad_engine_name(self): render = Engines() self.assertRaises(ValueError, lambda: render('text/html:'))
def test_defaults(self): render = Engines(json=dict(content_type="foo")) self.assertEqual(render.options['json'], dict(content_type="foo")) render.options['json'] = dict(content_type="bar") self.assertEqual(render.options['json'], dict(content_type="bar"))
def setUp(self): self.render = Engines()