From 6b2b2d889c94b0cbab5fbe14a4555f4c8dbe179c Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 12:49:23 +0100 Subject: [PATCH] [IMP] move nonliteral domains and contexts to JS test case bzr revid: xmo@openerp.com-20121123114923-t4047w6jztria91x --- addons/web/controllers/testing.py | 19 ------- addons/web/static/src/js/pyeval.js | 15 ++++-- addons/web/static/test/evals.js | 81 +++++++++++++----------------- 3 files changed, 47 insertions(+), 68 deletions(-) diff --git a/addons/web/controllers/testing.py b/addons/web/controllers/testing.py index b8e3da8ce24..5c7fdf46e1d 100644 --- a/addons/web/controllers/testing.py +++ b/addons/web/controllers/testing.py @@ -82,18 +82,6 @@ TESTING = Template(u""" """) -nonliteral_test_contexts = [ - "{'default_opportunity_id': active_id, 'default_duration': 1.0, 'lng': lang}", -] -nonliteral_test_domains = [ - "['|', '&', ('date', '!=', False), ('date', '<=', time.strftime('%Y-%m-%d')), ('is_overdue_quantity', '=', True)]", - "[('company_id', '=', context.get('company_id',False))]", - "[('year','=',time.strftime('%Y'))]", - "[('state','=','draft'),('date_order','<',time.strftime('%Y-%m-%d %H:%M:%S'))]", - "[('state','!=','cancel'),('opening_date','>',datetime.date.today().strftime('%Y-%m-%d'))]", - "[('type','=','in'),('day','<=', time.strftime('%Y-%m-%d')),('day','>',(datetime.date.today()-datetime.timedelta(days=15)).strftime('%Y-%m-%d'))]", -] - class TestRunnerController(http.Controller): _cp_path = '/web/tests' @@ -154,13 +142,6 @@ class TestRunnerController(http.Controller): if module.get_module_resource(name, 'static') if manifests[name]['js']]), db_info=json.dumps(db_info)) - @http.jsonrequest - def load_context(self, req, index): - return nonliterals.Context(req.session, nonliteral_test_contexts[index]) - @http.jsonrequest - def load_domain(self, req, index): - return nonliterals.Domain(req.session, nonliteral_test_domains[index]) - def load_manifest(self, name): manifest = module.load_information_from_description_file(name) if manifest: diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index e16880889d2..3c6b6619513 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -602,10 +602,13 @@ openerp.web.pyeval = function (instance) { var eval_contexts = function (contexts, evaluation_context) { evaluation_context = evaluation_context || {}; return _(contexts).reduce(function (result_context, ctx) { - // TODO: wrap string contexts in nonlit? // __eval_context evaluations can lead to some of `contexts`'s // values being null, skip them as well as empty contexts if (_.isEmpty(ctx)) { return result_context; } + if (_.isString(ctx)) { + // wrap raw strings in context + ctx = { __ref: 'context', __debug: ctx }; + } var evaluated = ctx; switch(ctx.__ref) { case 'context': @@ -627,7 +630,10 @@ openerp.web.pyeval = function (instance) { var eval_domains = function (domains, evaluation_context) { var result_domain = []; _(domains).each(function (domain) { - // TODO: wrap string domains in nonlit? + if (_.isString(domain)) { + // wrap raw strings in domain + domain = { __ref: 'domain', __debug: domain }; + } switch(domain.__ref) { case 'domain': evaluation_context.context = py.dict.fromJSON(evaluation_context); @@ -650,7 +656,10 @@ openerp.web.pyeval = function (instance) { var eval_groupbys = function (contexts, evaluation_context) { var result_group = []; _(contexts).each(function (ctx) { - // TODO: wrap string contexts in nonlit? + if (_.isString(ctx)) { + // wrap raw strings in context + ctx = { __ref: 'context', __debug: ctx }; + } var group; var evaluated = ctx; switch(ctx.__ref) { diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 86da3ae2876..e65f697eea2 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -385,7 +385,6 @@ openerp.testing.section('eval.edc', { }); openerp.testing.section('eval.edc.nonliterals', { dependencies: ['web.data'], - rpc: 'rpc', setup: function (instance) { instance.session.user_context = { lang: 'en_US', @@ -398,29 +397,20 @@ openerp.testing.section('eval.edc.nonliterals', { contexts: contexts || [], domains: domains || [] }); - }, - load_context: function (index) { - return this.session.rpc('/web/tests/load_context', {index: index}); - }, - load_domain: function (index) { - return this.session.rpc('/web/tests/load_domain', {index: index}); - }, + } }); } }, function (test) { test('domain with time', {asserts: 1}, function (instance) { - return instance.load_domain(0).then(function (d) { - strictEqual(d.__ref, 'domain'); - return instance.edc([ - [['type', '=', 'contract']], - { "__domains": [["|"], [["state", "in", ["open", "draft"]]], [["state", "=", "pending"]]], - "__eval_context": null, - "__ref": "compound_domain" - }, - d, - [['user_id', '=', 1]] - ]); - }).then(function (result) { + return instance.edc([ + [['type', '=', 'contract']], + { "__domains": [["|"], [["state", "in", ["open", "draft"]]], [["state", "=", "pending"]]], + "__eval_context": null, + "__ref": "compound_domain" + }, + "['|', '&', ('date', '!=', False), ('date', '<=', time.strftime('%Y-%m-%d')), ('is_overdue_quantity', '=', True)]", + [['user_id', '=', 1]] + ]).then(function (result) { var d = new Date(); var today = _.str.sprintf("%04d-%02d-%02d", d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()); @@ -437,29 +427,30 @@ openerp.testing.section('eval.edc.nonliterals', { }); }); test('conditional context', {asserts: 2}, function (instance) { - return instance.load_domain(1).then(function (d) { - var e1 = instance.edc([d]).then(function (result) { - deepEqual(result.domain, [ - ['company_id', '=', false] - ]); - }); - var cd = new instance.web.CompoundDomain(d); - cd.set_eval_context({company_id: 42}); - var e2 = instance.edc([cd]).then(function (result) { - deepEqual(result.domain, [ - ['company_id', '=', 42] - ]); - }); - - return $.when(e1, e2); + var d = { + __ref: 'domain', + __debug: "[('company_id', '=', context.get('company_id',False))]" + }; + var e1 = instance.edc([d]).then(function (result) { + deepEqual(result.domain, [ + ['company_id', '=', false] + ]); }); + var cd = new instance.web.CompoundDomain(d); + cd.set_eval_context({company_id: 42}); + var e2 = instance.edc([cd]).then(function (result) { + deepEqual(result.domain, [ + ['company_id', '=', 42] + ]); + }); + + return $.when(e1, e2); }); test('substitution in context', {asserts: 1}, function (instance) { - return instance.load_context(0).then(function (c) { - var cc = new instance.web.CompoundContext(c); - cc.set_eval_context({active_id: 42}); - return instance.edc([], [cc]); - }).then(function (result) { + var c = "{'default_opportunity_id': active_id, 'default_duration': 1.0, 'lng': lang}"; + var cc = new instance.web.CompoundContext(c); + cc.set_eval_context({active_id: 42}); + return instance.edc([], [cc]).then(function (result) { deepEqual(result.context, { lang: "en_US", tz: false, @@ -471,9 +462,8 @@ openerp.testing.section('eval.edc.nonliterals', { }); }); test('date', {asserts: 1}, function (instance) { - return instance.load_domain(4).then(function (d) { - return instance.edc([d]); - }).then(function (result) { + var d = "[('state','!=','cancel'),('opening_date','>',datetime.date.today().strftime('%Y-%m-%d'))]"; + return instance.edc([d]).then(function (result) { var d = new Date(); var today = _.str.sprintf("%04d-%02d-%02d", d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()); @@ -484,9 +474,8 @@ openerp.testing.section('eval.edc.nonliterals', { }); }); test('delta', {asserts: 1}, function (instance) { - return instance.load_domain(5).then(function (d) { - return instance.edc([d]); - }).then(function (result) { + var d = "[('type','=','in'),('day','<=', time.strftime('%Y-%m-%d')),('day','>',(datetime.date.today()-datetime.timedelta(days=15)).strftime('%Y-%m-%d'))]"; + return instance.edc([d]).then(function (result) { var d = new Date(); var today = _.str.sprintf("%04d-%02d-%02d", d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate());