[ADD] running of XML cases to python qweb
authorXavier Morel <xmo@openerp.com>
Thu, 11 Sep 2014 06:15:46 +0000 (08:15 +0200)
committerXavier Morel <xmo@openerp.com>
Mon, 6 Oct 2014 17:13:43 +0000 (19:13 +0200)
openerp/addons/base/ir/ir_qweb.py
openerp/addons/base/tests/test_qweb.py

index b82fe64..d91d4cd 100644 (file)
@@ -163,12 +163,13 @@ class QWeb(orm.AbstractModel):
         """
         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'):
index 9c393f1..a923a83 100644 (file)
@@ -1,7 +1,13 @@
 # -*- 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
@@ -73,3 +79,58 @@ class TestQWebTField(common.TransactionCase):
             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