"""
Loads an XML document and installs any contained template in the engine
"""
- if hasattr(document, 'documentElement'):
+ if not isinstance(document, basestring):
+ # assume lxml.etree.Element
dom = document
elif document.startswith("<?xml"):
dom = etree.fromstring(document)
else:
- dom = etree.parse(document)
+ dom = etree.parse(document).getroot()
for node in dom:
if node.get('t-name'):
# -*- coding: utf-8 -*-
import cgi
+import json
+import os.path
+import glob
+import re
from lxml import etree
+import openerp.addons.base.ir.ir_qweb
+import openerp.modules
from openerp.tests import common
from openerp.addons.base.ir import ir_qweb
self.engine.render_node(field, self.context({
'company': None
}))
+
+class TestQWeb(common.TransactionCase):
+ matcher = re.compile('^qweb-test-(.*)\.xml$')
+
+ @classmethod
+ def get_cases(cls):
+ path = cls.qweb_test_file_path()
+
+ return (
+ cls("test_qweb_{}".format(cls.matcher.match(f).group(1)))
+ for f in os.listdir(path)
+ # js inheritance
+ if f != 'qweb-test-extend.xml'
+ if cls.matcher.match(f)
+ )
+ @classmethod
+ def qweb_test_file_path(cls):
+ path = os.path.dirname(
+ openerp.modules.get_module_resource(
+ 'web', 'static', 'lib', 'qweb', 'qweb2.js'))
+ return path
+
+ def __getattr__(self, item):
+ if not item.startswith('test_qweb_'):
+ raise AttributeError("No {} on {}".format(item, self))
+
+ f = 'qweb-test-{}.xml'.format(item[10:])
+ path = self.qweb_test_file_path()
+
+ return lambda: self.run_test_file(os.path.join(path, f))
+
+ def run_test_file(self, path):
+ context = openerp.addons.base.ir.ir_qweb.QWebContext(self.cr, self.uid, {})
+ qweb = self.env['ir.qweb']
+ doc = etree.parse(path).getroot()
+ qweb.load_document(doc, None, context)
+ for template in context.templates:
+ if template.startswith('_'): continue
+ param = doc.find('params[@id="{}"]'.format(template))
+ params = {} if param is None else json.loads(param.text)
+
+ ctx = context.copy()
+ ctx.update(params)
+ result = doc.find('result[@id="{}"]'.format(template)).text
+ self.assertEqual(
+ qweb.render(template, qwebcontext=ctx).strip(),
+ (result or u'').strip().encode('utf-8'),
+ template
+ )
+
+def load_tests(loader, suite, _):
+ # can't override TestQWeb.__dir__ because dir() called on *class* not
+ # instance
+ suite.addTests(TestQWeb.get_cases())
+ return suite