def test_fetch_path_itemlist(self): registry = mocks.setup_testing_mold_templates_registry(self) engine = Engine(registry) tmpl = engine.fetch_path('nunja.testing.mold/itemlist/template.nja') self.assertTrue(tmpl.startswith('<ul')) script = engine.fetch_path('nunja.testing.mold/itemlist/index.js') self.assertTrue(script.startswith('define(['))
class MoldTableTestCase(unittest.TestCase): def setUp(self): self.engine = Engine() def tearDown(self): pass def test_null_rendering(self): result = self.engine.execute('nunja.molds/table', data={ 'css': {}, }) self.assertEqual( result, '<div data-nunja="nunja.molds/table">\n' '<table class="">\n' ' <thead>\n' ' <tr class="">\n' ' \n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' \n' ' </tbody>\n' '</table>\n' '</div>') def test_basic_table_contents(self): data = model.DummyTableData([ ['id', 'Id'], ['name', 'Given Name'], ], [ ['1', 'John Smith'], ['2', 'Eve Adams'], ]) result = self.engine.execute('nunja.molds/table', data=data.to_jsonable()) self.assertEqual( result, '<div data-nunja="nunja.molds/table">\n' '<table class="">\n' ' <thead>\n' ' <tr class="">\n' ' <td>Id</td><td>Given Name</td>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr class="">\n' ' <td>1</td><td>John Smith</td>\n' ' </tr><tr class="">\n' ' <td>2</td><td>Eve Adams</td>\n' ' </tr>\n' ' </tbody>\n' '</table>\n' '</div>')
class EngineTestCase(unittest.TestCase): """ The core engine test case for testing the integration with the loader. """ def setUp(self): (registry, self.main_template, self.sub_template) = mocks.setup_tmp_mold_templates_registry(self) self.engine = Engine(registry) def test_unregistered_not_found(self): with self.assertRaises(TemplateNotFound): self.engine.load_template('some/id') def test_base_loading(self): template = self.engine.load_template('tmp/mold/sub.nja') result = template.render(data='Hello World!') self.assertEqual(result, '<span>Hello World!</span>') def test_nested_loading(self): template = self.engine.load_template('tmp/mold/template.nja') result = template.render(data='Hello World!') self.assertEqual(result, '<div><span>Hello World!</span></div>') def test_base_auto_reload(self): template = self.engine.load_template('tmp/mold/template.nja') result = template.render(data='Hello World!') self.assertEqual(result, '<div><span>Hello World!</span></div>') with open(self.sub_template, 'w') as fd: fd.write('<div>{{ data }}</div>') result = template.render(data='Hello World!') self.assertEqual(result, '<div><div>Hello World!</div></div>') remove(self.sub_template) with self.assertRaises(TemplateNotFound): # as that was removed template.render(data='Hello World!') def test_fetch_path_basic(self): tmpl = self.engine.fetch_path('tmp/mold/template.nja') self.assertEqual('<div>{% include "tmp/mold/sub.nja" %}</div>', tmpl) def test_filter_dump(self): # To mimic the JSON.stringify filter in nunjucks, provide the # filter under the same name through json.dumps. This will # test that feature. template = self.engine.load_template('tmp/mold/filter_dump.nja') result = template.render(data={ 'some': 'object', 'items': ['Hello', '<World>']}) self.assertEqual(result, ( '<div>{' '"items":["Hello","<World>"],' '"some":"object"' '}</div>' ))
def setUp(self): (registry, self.main_template, self.sub_template) = mocks.setup_tmp_mold_templates_registry(self) self.engine = Engine(registry)
def setUp(self): self.engine = Engine()
class MoldTableTestCase(unittest.TestCase): def setUp(self): self.engine = Engine() def tearDown(self): pass def test_null_rendering(self): result = self.engine.execute('nunja.molds/navtable', data={ 'css': {}, }) self.assertEqual( result, '<div data-nunja="nunja.molds/navtable">\n' '<table class="">\n' ' <thead>\n' ' <tr class="">\n' ' \n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' \n' ' </tbody>\n' '</table>\n' '</div>') def test_basic_navtable_contents(self): data = model.DummyTableData([ ['@id', ''], ['name', 'Name'], ['desc', 'Description'], ['size', 'Size'], ], [ ['http://example.com/documents', 'documents', 'My Documents', '9'], ['http://example.com/pictures', 'pictures', 'My Pictures', '43'], ['http://example.com/videos', 'videos', 'My Videos', '0'], ['', 'file', 'A File', '100'], ]) result = self.engine.execute('nunja.molds/navtable', data=data.to_jsonable()) self.assertEqual( result, '<div data-nunja="nunja.molds/navtable">\n' '<table class="">\n' ' <thead>\n' ' <tr class="">\n' ' <td>Name</td><td>Description</td><td>Size</td>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr class="">\n' ' <td><a href="http://example.com/documents">documents</a>' '</td><td>My Documents</td><td>9</td>\n' ' </tr><tr class="">\n' ' <td><a href="http://example.com/pictures">pictures</a>' '</td><td>My Pictures</td><td>43</td>\n' ' </tr><tr class="">\n' ' <td><a href="http://example.com/videos">videos</a>' '</td><td>My Videos</td><td>0</td>\n' ' </tr><tr class="">\n' ' <td>file</td><td>A File</td><td>100</td>\n' ' </tr>\n' ' </tbody>\n' '</table>\n' '</div>')
class BaseTestCase(unittest.TestCase): """ Full test, uses the Jinja2 renderer which will then be able to load from the custom paths defined for nunja. """ def setUp(self): registry = mocks.setup_testing_mold_templates_registry(self) self.engine = Engine(registry) def test_base_rendering_render(self): result = self.engine.render('nunja.testing.mold/basic', data={'value': 'Hello World!'}) self.assertEqual(result, '<span>Hello World!</span>') def test_base_rendering_execute(self): result = self.engine.execute('nunja.testing.mold/basic', data={'value': 'Hello World!'}) self.assertEqual( result, '<div data-nunja="nunja.testing.mold/basic">\n' '<span>Hello World!</span>\n' '</div>') def test_template_retrieval_and_render(self): # As this was retrieved directly, none of the "mold" bits are # provided by the result. result = self.engine.load_template( 'nunja.testing.mold/basic/template.nja').render( value='Hello World!') self.assertEqual(result, '<span>Hello World!</span>') def test_template_retrieval_not_found(self): with self.assertRaises(TemplateNotFound): self.engine.load_template( 'nunja.testing.mold/basic/no_such_template.nja') def test_base_xss_handling(self): result = self.engine.execute('nunja.testing.mold/basic', data={'value': '<xss>'}) self.assertEqual( result, '<div data-nunja="nunja.testing.mold/basic">\n' '<span><xss></span>\n' '</div>') def test_manual_include(self): data = { 'list_template': self.engine.load_mold('nunja.testing.mold/itemlist'), 'itemlists': [['list_1', ['Item 1', 'Item 2']]], } result = self.engine.execute('nunja.testing.mold/include_by_value', data=data) self.assertEqual( result, '<div data-nunja="nunja.testing.mold/include_by_value">\n' '<dl id="">\n\n' ' <dt>list_1</dt>\n' ' <dd><ul id="list_1">\n\n' ' <li>Item 1</li>\n' ' <li>Item 2</li>\n' '</ul></dd>\n' '</dl>\n' '</div>') def test_manual_include_multilist(self): data = { 'list_template': self.engine.load_mold('nunja.testing.mold/itemlist'), 'list_id': 'root_id', 'itemlists': [ ['list_1', ['Item 1', 'Item 2']], ['list_2', ['Item 3', 'Item 4']], ['list_3', ['Item 5', 'Item 6']], ], } result = self.engine.execute('nunja.testing.mold/include_by_value', data=data) self.assertEqual( result, '<div data-nunja="nunja.testing.mold/include_by_value">\n' '<dl id="root_id">\n\n' ' <dt>list_1</dt>\n' ' <dd><ul id="list_1">\n\n' ' <li>Item 1</li>\n' ' <li>Item 2</li>\n' '</ul></dd>\n' ' <dt>list_2</dt>\n' ' <dd><ul id="list_2">\n\n' ' <li>Item 3</li>\n' ' <li>Item 4</li>\n' '</ul></dd>\n' ' <dt>list_3</dt>\n' ' <dd><ul id="list_3">\n\n' ' <li>Item 5</li>\n' ' <li>Item 6</li>\n' '</ul></dd>\n' '</dl>\n' '</div>') def test_named_include_multilist(self): data = { 'list_id': 'root_id', 'itemlists': [ ['list_1', ['Item 1', 'Item 2']], ['list_2', ['Item 3', 'Item 4']], ['list_3', ['Item 5', 'Item 6']], ], } result = self.engine.execute('nunja.testing.mold/include_by_name', data=data) self.assertEqual( result, '<div data-nunja="nunja.testing.mold/include_by_name">\n' '<dl id="root_id">\n\n' ' <dt>list_1</dt>\n' ' <dd><ul id="list_1">\n\n' ' <li>Item 1</li>\n' ' <li>Item 2</li>\n' '</ul></dd>\n' ' <dt>list_2</dt>\n' ' <dd><ul id="list_2">\n\n' ' <li>Item 3</li>\n' ' <li>Item 4</li>\n' '</ul></dd>\n' ' <dt>list_3</dt>\n' ' <dd><ul id="list_3">\n\n' ' <li>Item 5</li>\n' ' <li>Item 6</li>\n' '</ul></dd>\n' '</dl>\n' '</div>')
def setUp(self): registry = mocks.setup_testing_mold_templates_registry(self) self.engine = Engine(registry)