diff options
| author | Xavier Morel <xmo@openerp.com> | 2012-10-25 17:47:45 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2012-10-25 17:47:45 +0200 |
| commit | 99a2dd3938c7d7d4b7e9c724ec8acbb5eeedf35f (patch) | |
| tree | aca872a490542cb318626f806e8a3d3d2db0c1fd /addons/web/controllers | |
| parent | 1cf9b2a60da502de27aa67e8318f41ad6692e5a9 (diff) | |
[ADD] first 90% of non-sucky testing API, last 90% left
also all existing tests shoud now be completely broken and need to be fixed
bzr revid: xmo@openerp.com-20121025154745-rw2gktfd6tp68k2m
Diffstat (limited to 'addons/web/controllers')
| -rw-r--r-- | addons/web/controllers/__init__.py | 1 | ||||
| -rw-r--r-- | addons/web/controllers/main.py | 8 | ||||
| -rw-r--r-- | addons/web/controllers/testing.py | 155 |
3 files changed, 158 insertions, 6 deletions
diff --git a/addons/web/controllers/__init__.py b/addons/web/controllers/__init__.py index 12a7e529b67..74c27518ece 100644 --- a/addons/web/controllers/__init__.py +++ b/addons/web/controllers/__init__.py @@ -1 +1,2 @@ from . import main +from . import testing diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 72ffd9567f6..114a896e77a 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -800,8 +800,7 @@ class Database(openerpweb.Controller): @openerpweb.jsonrequest def get_list(self, req): - dbs = db_list(req) - return {"db_list": dbs} + return db_list(req) @openerpweb.jsonrequest def create(self, req, fields): @@ -922,10 +921,7 @@ class Session(openerpweb.Controller): @openerpweb.jsonrequest def get_lang_list(self, req): try: - return { - 'lang_list': (req.session.proxy("db").list_lang() or []), - 'error': "" - } + return req.session.proxy("db").list_lang() or [] except Exception, e: return {"error": e, "title": "Languages"} diff --git a/addons/web/controllers/testing.py b/addons/web/controllers/testing.py new file mode 100644 index 00000000000..0cfc633a492 --- /dev/null +++ b/addons/web/controllers/testing.py @@ -0,0 +1,155 @@ +# coding=utf-8 +# -*- encoding: utf-8 -*- + +import glob +import itertools +import json +import operator +import os + +from mako.template import Template +from openerp.modules import module + +from .main import module_topological_sort +from ..http import Controller, httprequest + +NOMODULE_TEMPLATE = Template(u"""<!DOCTYPE html> +<html> + <head> + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <title>OpenERP Testing</title> + </head> + <body> + <form action="/web/tests" method="GET"> + <button name="mod" value="*">Run all tests</button> + <ul> + % for name, module in modules: + <li>${name} <button name="mod" value="${module}"> + Run Tests</button></li> + % endfor + </ul> + </form> + </body> +</html> +""") +NOTFOUND = Template(u""" +<p>Unable to find the module [${module}], please check that the module + name is correct and the module is on OpenERP's path.</p> +<a href="/web/tests"><< Back to tests</a> +""") +TESTING = Template(u"""<!DOCTYPE html> +<html style="height: 100%"> +<%def name="to_path(module, p)">/${module}/${p}</%def> +<head> + <meta http-equiv="X-UA-Compatible" content="IE=edge"/> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <title>OpenERP Web Tests</title> + <link rel="shortcut icon" href="/web/static/src/img/favicon.ico" type="image/x-icon"/> + + <link rel="stylesheet" href="/web/static/lib/qunit/qunit.css"> + <script src="/web/static/lib/qunit/qunit.js"></script> + + <script type="text/javascript"> + // List of modules, each module is preceded by its dependencies + var oe_all_dependencies = ${dependencies}; + QUnit.config.testTimeout = 2000; + </script> +</head> +<body id="oe" class="openerp"> + <div id="qunit"></div> + <div id="qunit-fixture"></div> +</body> +% for module, jss, tests, templates in files: + % for js in jss: + <script src="${to_path(module, js)}"></script> + % endfor + % if tests or templates: + <script> + openerp.testing.current_module = "${module}"; + % for template in templates: + openerp.testing.add_template("${to_path(module, template)}"); + % endfor + </script> + % endif + % if tests: + % for test in tests: + <script type="text/javascript" src="${to_path(module, test)}"></script> + % endfor + % endif +% endfor +</html> +""") + +class TestRunnerController(Controller): + _cp_path = '/web/tests' + + @httprequest + def index(self, req, mod=None, **kwargs): + ms = module.get_modules() + manifests = dict( + (name, desc) + for name, desc in zip(ms, map(self.load_manifest, ms)) + if desc # remove not-actually-openerp-modules + ) + + if not mod: + return NOMODULE_TEMPLATE.render(modules=( + (manifest['name'], name) + for name, manifest in manifests.iteritems() + if any(testfile.endswith('.js') + for testfile in manifest['test']) + )) + sorted_mods = module_topological_sort(dict( + (name, manifest.get('depends', [])) + for name, manifest in manifests.iteritems() + )) + # to_load and to_test should be zippable lists of the same length. + # A falsy value in to_test indicate nothing to test at that index (just + # load the corresponding part of to_load) + to_test = sorted_mods + if mod != '*': + if mod not in manifests: + return req.not_found(NOTFOUND.render(module=mod)) + idx = sorted_mods.index(mod) + to_test = [None] * len(sorted_mods) + to_test[idx] = mod + + tests_candicates = [ + filter(lambda path: path.endswith('.js'), + manifests[mod]['test'] if mod else []) + for mod in to_test] + # remove trailing test-less modules + tests = reversed(list( + itertools.dropwhile( + operator.not_, + reversed(tests_candicates)))) + + files = [ + (mod, manifests[mod]['js'], tests, manifests[mod]['qweb']) + for mod, tests in itertools.izip(sorted_mods, tests) + ] + + return TESTING.render(files=files, dependencies=json.dumps( + [name for name in sorted_mods + if module.get_module_resource(name, 'static') + if manifests[name]['js']])) + + def load_manifest(self, name): + manifest = module.load_information_from_description_file(name) + if manifest: + path = module.get_module_path(name) + manifest['js'] = list( + self.expand_patterns(path, manifest.get('js', []))) + manifest['test'] = list( + self.expand_patterns(path, manifest.get('test', []))) + manifest['qweb'] = list( + self.expand_patterns(path, manifest.get('qweb', []))) + return manifest + + def expand_patterns(self, root, patterns): + for pattern in patterns: + normalized_pattern = os.path.normpath(os.path.join(root, pattern)) + for path in glob.glob(normalized_pattern): + # replace OS path separators (from join & normpath) by URI ones + yield path[len(root):].replace(os.path.sep, '/') |
