From 6f4abdfbd519e8f8a416e24fdb541a88ef3104c6 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 6 Aug 2012 11:46:27 +0200 Subject: [IMP] move eval functions for domains, groupbys and contexts outside of corelib, as well as the standard evaluation context building (and related objects) bzr revid: xmo@openerp.com-20120806094627-8c9lwtfkbfws2bmj --- addons/web/static/test/evals.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 7bc6554e074..ee4aef4bf79 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -3,16 +3,14 @@ $(document).ready(function () { module("eval.contexts", { setup: function () { - openerp = window.openerp.init([]); - window.openerp.web.corelib(openerp); - window.openerp.web.coresetup(openerp); + openerp = window.openerp.testing.instanceFor('coresetup'); } }); test('context_sequences', function () { // Context n should have base evaluation context + all of contexts // 0..n-1 in its own evaluation context var active_id = 4; - var result = openerp.connection.test_eval_contexts([ + var result = openerp.web.pyeval.eval('contexts', [ { "__contexts": [ { @@ -56,7 +54,7 @@ $(document).ready(function () { }); }); test('non-literal_eval_contexts', function () { - var result = openerp.connection.test_eval_contexts([{ + var result = openerp.web.pyeval.eval('contexts', [{ "__ref": "compound_context", "__contexts": [ {"__ref": "context", "__debug": "{'type':parent.type}", @@ -141,9 +139,8 @@ $(document).ready(function () { }); test('current_date', function () { var current_date = openerp.web.date_to_str(new Date()); - var result = openerp.connection.test_eval_domains( - [[],{"__ref":"domain","__debug":"[('name','>=',current_date),('name','<=',current_date)]","__id":"5dedcfc96648"}], - openerp.connection.test_eval_get_context()); + var result = openerp.web.pyeval.eval('domains', + [[],{"__ref":"domain","__debug":"[('name','>=',current_date),('name','<=',current_date)]","__id":"5dedcfc96648"}]); deepEqual(result, [ ['name', '>=', current_date], ['name', '<=', current_date] -- cgit v1.2.3 From 10bc6ddfe4e12e792d856369b482ae74f73197d0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 5 Oct 2012 16:28:08 +0200 Subject: [ADD] groupby eval tests bzr revid: xmo@openerp.com-20121005142808-yx6o8nfnn0o4ms56 --- addons/web/static/test/evals.js | 73 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index ee4aef4bf79..dc8ceb83daf 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -145,5 +145,76 @@ $(document).ready(function () { ['name', '>=', current_date], ['name', '<=', current_date] ]); - }) + }); + + module('eval.groupbys', { + setup: function () { + openerp = window.openerp.testing.instanceFor('coresetup'); + } + }); + test('groupbys_00', function () { + var result = openerp.web.pyeval.eval('groupbys', [ + {group_by: 'foo'}, + {group_by: ['bar', 'qux']}, + {group_by: null}, + {group_by: 'grault'} + ]); + deepEqual(result, ['foo', 'bar', 'qux', 'grault']); + }); + test('groupbys_01', function () { + var result = openerp.web.pyeval.eval('groupbys', [ + {group_by: 'foo'}, + { __ref: 'context', __debug: '{"group_by": "bar"}' }, + {group_by: 'grault'} + ]); + deepEqual(result, ['foo', 'bar', 'grault']); + }); + test('groupbys_02', function () { + var result = openerp.web.pyeval.eval('groupbys', [ + {group_by: 'foo'}, + { + __ref: 'compound_context', + __contexts: [ {group_by: 'bar'} ], + __eval_context: null + }, + {group_by: 'grault'} + ]); + deepEqual(result, ['foo', 'bar', 'grault']); + }); + test('groupbys_03', function () { + var result = openerp.web.pyeval.eval('groupbys', [ + {group_by: 'foo'}, + { + __ref: 'compound_context', + __contexts: [ + { __ref: 'context', __debug: '{"group_by": value}' } + ], + __eval_context: { value: 'bar' } + }, + {group_by: 'grault'} + ]); + deepEqual(result, ['foo', 'bar', 'grault']); + }); + test('groupbys_04', function () { + var result = openerp.web.pyeval.eval('groupbys', [ + {group_by: 'foo'}, + { + __ref: 'compound_context', + __contexts: [ + { __ref: 'context', __debug: '{"group_by": value}' } + ], + __eval_context: { value: 'bar' } + }, + {group_by: 'grault'} + ], { value: 'bar' }); + deepEqual(result, ['foo', 'bar', 'grault']); + }); + test('groupbys_05', function () { + var result = openerp.web.pyeval.eval('groupbys', [ + {group_by: 'foo'}, + { __ref: 'context', __debug: '{"group_by": value}' }, + {group_by: 'grault'} + ], { value: 'bar' }); + deepEqual(result, ['foo', 'bar', 'grault']); + }); }); -- cgit v1.2.3 From 7bcbadb099a71b731812ace299b98317edc49ae4 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 8 Oct 2012 14:06:19 +0200 Subject: [FIX] update py.js to 0.6, port existin classes to it, attempt to implement timedelta bzr revid: xmo@openerp.com-20121008120619-lhmexjnujjrigjn9 --- addons/web/static/test/evals.js | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index dc8ceb83daf..3c3bface529 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -1,9 +1,51 @@ $(document).ready(function () { var openerp; + module("eval.types", { + setup: function () { + openerp = window.openerp.testing.instanceFor('coresetup'); + openerp.session.uid = 42; + } + }); + test('datetime.datetime', function () { + }); + test('datetime.date', function () { + + }); + test('datetime.timedelta', function () { + var d = new Date(); + d.setUTCDate(d.getUTCDate() - 15); + strictEqual( + py.eval("(datetime.date.today()" + + "- datetime.timedelta(days=15))" + + ".strftime('%Y-%m-%d')", openerp.web.pyeval.context()), + _.str.strftime('%04d-%02d-%02d', + d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate())); + + }); + test('relativedelta', function () { + + }); + test('strftime', function () { + var d = new Date(); + var context = openerp.web.pyeval.context(); + strictEqual( + py.eval("time.strftime('%Y')", context), + String(d.getUTCFullYear())); + strictEqual( + py.eval("time.strftime('%Y')+'-01-30'", context), + String(d.getUTCFullYear()) + '-01-30'); + strictEqual( + py.eval("time.strftime('%Y-%m-%d %H:%M:%S')", context), + _.str.sprintf('%04d-%02d-%02d %02d:%02d:%02d', + d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), + d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds())); + }); + module("eval.contexts", { setup: function () { openerp = window.openerp.testing.instanceFor('coresetup'); + openerp.session.uid = 42; } }); test('context_sequences', function () { @@ -135,6 +177,7 @@ $(document).ready(function () { setup: function () { openerp = window.openerp.testing.instanceFor('coresetup'); window.openerp.web.dates(openerp); + openerp.session.uid = 42; } }); test('current_date', function () { @@ -150,6 +193,7 @@ $(document).ready(function () { module('eval.groupbys', { setup: function () { openerp = window.openerp.testing.instanceFor('coresetup'); + openerp.session.uid = 42; } }); test('groupbys_00', function () { -- cgit v1.2.3 From 894bd34e6275fe838bb0de30e25de4b0e09d4636 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 8 Oct 2012 14:55:23 +0200 Subject: [REM] timedelta, because pain bzr revid: xmo@openerp.com-20121008125523-ed1g42vjxbcfo62r --- addons/web/static/test/evals.js | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 3c3bface529..72c4cd7323a 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -6,25 +6,6 @@ $(document).ready(function () { openerp = window.openerp.testing.instanceFor('coresetup'); openerp.session.uid = 42; } - }); - test('datetime.datetime', function () { - }); - test('datetime.date', function () { - - }); - test('datetime.timedelta', function () { - var d = new Date(); - d.setUTCDate(d.getUTCDate() - 15); - strictEqual( - py.eval("(datetime.date.today()" + - "- datetime.timedelta(days=15))" + - ".strftime('%Y-%m-%d')", openerp.web.pyeval.context()), - _.str.strftime('%04d-%02d-%02d', - d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate())); - - }); - test('relativedelta', function () { - }); test('strftime', function () { var d = new Date(); -- cgit v1.2.3 From 1fa00288b269e68a47849eaa3fefc090759a357d Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 10 Oct 2012 16:59:45 +0200 Subject: [FIX] add 'context' free variable in evaluation contexts for domains and contexts bzr revid: xmo@openerp.com-20121010145945-16shdry3udum0mn1 --- addons/web/static/test/evals.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 72c4cd7323a..72503d5313b 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -29,6 +29,21 @@ $(document).ready(function () { openerp.session.uid = 42; } }); + test('context_recursive', function () { + var context_to_eval = [{ + __ref: 'context', + __debug: '{"foo": context.get("bar", "qux")}' + }]; + deepEqual( + openerp.web.pyeval.eval('contexts', context_to_eval, {bar: "ok"}), + {foo: 'ok'}); + deepEqual( + openerp.web.pyeval.eval('contexts', context_to_eval, {bar: false}), + {foo: false}); + deepEqual( + openerp.web.pyeval.eval('contexts', context_to_eval), + {foo: 'qux'}); + }); test('context_sequences', function () { // Context n should have base evaluation context + all of contexts // 0..n-1 in its own evaluation context @@ -170,6 +185,21 @@ $(document).ready(function () { ['name', '<=', current_date] ]); }); + test('context_freevar', function () { + var domains_to_eval = [{ + __ref: 'domain', + __debug: '[("foo", "=", context.get("bar", "qux"))]' + }, [['bar', '>=', 42]]]; + deepEqual( + openerp.web.pyeval.eval('domains', domains_to_eval, {bar: "ok"}), + [['foo', '=', 'ok'], ['bar', '>=', 42]]); + deepEqual( + openerp.web.pyeval.eval('domains', domains_to_eval, {bar: false}), + [['foo', '=', false], ['bar', '>=', 42]]); + deepEqual( + openerp.web.pyeval.eval('domains', domains_to_eval), + [['foo', '=', 'qux'], ['bar', '>=', 42]]); + }); module('eval.groupbys', { setup: function () { -- cgit v1.2.3 From b94e9c30a78e0047457977bf8ff5bd6bdda81372 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 11 Oct 2012 09:57:59 +0200 Subject: [FIX] after discussions with odo, date/datetime/time for locally-evaluated contexts and domains should be local, not UTC bzr revid: xmo@openerp.com-20121011075759-qfwkfws5tu9yidab --- addons/web/static/test/evals.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 72503d5313b..276636c1ed2 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -12,15 +12,15 @@ $(document).ready(function () { var context = openerp.web.pyeval.context(); strictEqual( py.eval("time.strftime('%Y')", context), - String(d.getUTCFullYear())); + String(d.getFullYear())); strictEqual( py.eval("time.strftime('%Y')+'-01-30'", context), - String(d.getUTCFullYear()) + '-01-30'); + String(d.getFullYear()) + '-01-30'); strictEqual( py.eval("time.strftime('%Y-%m-%d %H:%M:%S')", context), _.str.sprintf('%04d-%02d-%02d %02d:%02d:%02d', - d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), - d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds())); + d.getFullYear(), d.getMonth() + 1, d.getDate(), + d.getHours(), d.getMinutes(), d.getSeconds())); }); module("eval.contexts", { -- cgit v1.2.3 From 3909d1ff0218203755f554eba8ee2a86dbd1405c Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 22 Nov 2012 12:16:28 +0100 Subject: [ADD] a bunch of eval domain and context tests bzr revid: xmo@openerp.com-20121122111628-4khivcp97xoy121i --- addons/web/static/test/evals.js | 250 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 249 insertions(+), 1 deletion(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index bef8edb1568..2fe3168a357 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -17,6 +17,254 @@ openerp.testing.section('eval.types', { d.getHours(), d.getMinutes(), d.getSeconds())); }); }); +openerp.testing.section('eval.edc', { + dependencies: ['web.data'], + rpc: 'rpc', + setup: function (instance) { + instance.edc = function (domains, contexts) { + return this.session.rpc('/web/session/eval_domain_and_context', { + contexts: contexts || [], + domains: domains || [] + }); + } + } +}, function (test) { + test('empty, basic', {asserts: 3}, function (instance) { + return instance.edc().then(function (result) { + // default values for new db + deepEqual(result.context, { + lang: 'en_US', + tz: false, + uid: 1 + }); + deepEqual(result.domain, []); + deepEqual(result.group_by, []); + }); + }); + test('empty, context altered', { + asserts: 3, + setup: function (instance) { + var lang = new instance.web.Model('res.lang'); + var users = new instance.web.Model('res.users'); + return lang.call('load_lang', ['ru_RU']).then(function () { + return users.call('write', [instance.session.uid, { + lang: 'ru_RU', + tz: 'America/Santarem' + }]); + }).then(function () { + return instance.session.session_reload(); + }); + } + }, function (instance) { + return instance.edc().then(function (result) { + // default values for new db + deepEqual(result.context, { + lang: 'ru_RU', + tz: 'America/Santarem', + uid: 1 + }); + deepEqual(result.domain, []); + deepEqual(result.group_by, []); + }); + }); + test('context_merge_00', {asserts: 1}, function (instance) { + var ctx = [ + { + "__contexts": [ + { "lang": "en_US", "tz": false, "uid": 1 }, + { + "active_id": 8, + "active_ids": [ 8 ], + "active_model": "sale.order", + "bin_raw": true, + "default_composition_mode": "comment", + "default_model": "sale.order", + "default_res_id": 8, + "default_template_id": 18, + "default_use_template": true, + "edi_web_url_view": "faaaake", + "lang": "en_US", + "mark_so_as_sent": null, + "show_address": null, + "tz": false, + "uid": null + }, + {} + ], + "__eval_context": null, + "__ref": "compound_context" + }, + { "active_id": 9, "active_ids": [ 9 ], "active_model": "mail.compose.message" } + ]; + return instance.edc([], ctx).then(function (result) { + deepEqual(result.context, { + active_id: 9, + active_ids: [9], + active_model: 'mail.compose.message', + bin_raw: true, + default_composition_mode: 'comment', + default_model: 'sale.order', + default_res_id: 8, + default_template_id: 18, + default_use_template: true, + edi_web_url_view: "faaaake", + lang: 'en_US', + mark_so_as_sent: null, + show_address: null, + tz: false, + uid: null + }); + }); + }); + test('context_merge_01', {asserts: 1}, function (instance) { + var ctx = [{ + "__contexts": [ + { + "lang": "en_US", + "tz": false, + "uid": 1 + }, + { + "default_attachment_ids": [], + "default_body": "", + "default_content_subtype": "html", + "default_model": "res.users", + "default_parent_id": false, + "default_res_id": 1 + }, + {} + ], + "__eval_context": null, + "__ref": "compound_context" + }]; + return instance.edc([], ctx).then(function (result) { + deepEqual(result.context, { + "default_attachment_ids": [], + "default_body": "", + "default_content_subtype": "html", + "default_model": "res.users", + "default_parent_id": false, + "default_res_id": 1, + "lang": "en_US", + "tz": false, + "uid": 1 + }); + }); + }); +}); +openerp.testing.section('eval.edc.nonliterals', { + dependencies: ['web.data'], + rpc: 'rpc', + setup: function (instance) { + _.extend(instance, { + edc: function (domains, contexts) { + return this.session.rpc('/web/session/eval_domain_and_context', { + 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: 2}, 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) { + var d = new Date(); + var today = _.str.sprintf("%04d-%02d-%02d", + d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()); + deepEqual(result.domain, [ + ["type", "=", "contract"], + "|", ["state", "in", ["open", "draft"]], + ["state", "=", "pending"], + "|", + "&", ["date", "!=", false], + ["date", "<=", today], + ["is_overdue_quantity", "=", true], + ["user_id", "=", 1] + ]); + }); + }); + 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); + }); + }); + 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) { + deepEqual(result.context, { + lang: "en_US", + tz: false, + uid: 1, + default_opportunity_id: 42, + default_duration: 1.0, + lng: "en_US" + }); + }); + }); + test('date', {asserts: 1}, function (instance) { + return instance.load_domain(4).then(function (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()); + deepEqual(result.domain, [ + ['state', '!=', 'cancel'], + ['opening_date', '>', today] + ]); + }); + }); + test('delta', {asserts: 1}, function (instance) { + return instance.load_domain(5).then(function (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()); + d.setDate(d.getDate() - 15); + var ago_15_d = _.str.sprintf("%04d-%02d-%02d", + d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()); + deepEqual(result.domain, [ + ['type', '=', 'in'], + ['day', '<=', today], + ['day', '>', ago_15_d] + ]); + }); + }); +}); openerp.testing.section('eval.contexts', { dependencies: ['web.coresetup'] }, function (test) { @@ -161,7 +409,7 @@ openerp.testing.section('eval.contexts', { deepEqual(result, {type: 'out_invoice'}); }); }); -openerp.testing.section('eval.contexts', { +openerp.testing.section('eval.domains', { dependencies: ['web.coresetup', 'web.dates'] }, function (test) { test('current_date', function (instance) { -- cgit v1.2.3 From 815b908759451efd2d74cdb8d16a46bb83fe25c0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 08:59:38 +0100 Subject: [ADD] partial but sufficient (I think) implementation of datetime.timedelta bzr revid: xmo@openerp.com-20121123075938-oazwt4bn2l639r3a --- addons/web/static/test/evals.js | 149 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 148 insertions(+), 1 deletion(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 2fe3168a357..ba9888617ee 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -1,5 +1,8 @@ openerp.testing.section('eval.types', { - dependencies: ['web.coresetup'] + dependencies: ['web.coresetup'], + setup: function (instance) { + instance.session.uid = 42; + } }, function (test) { test('strftime', function (instance) { var d = new Date(); @@ -16,6 +19,150 @@ openerp.testing.section('eval.types', { d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds())); }); + // Port from pypy/lib_pypy/test_datetime.py + var makeEq = function (instance, c2) { + var ctx = instance.web.pyeval.context(); + var c = _.extend({ td: ctx.datetime.timedelta }, c2 || {}); + return function (a, b, message) { + ok(py.eval(a + ' == ' + b, c), message); + }; + }; + test('timedelta.test_constructor', function (instance) { + var eq = makeEq(instance); + + // keyword args to constructor + eq('td()', 'td(weeks=0, days=0, hours=0, minutes=0, seconds=0, ' + + 'milliseconds=0, microseconds=0)'); + eq('td(1)', 'td(days=1)'); + eq('td(0, 1)', 'td(seconds=1)'); + eq('td(0, 0, 1)', 'td(microseconds=1)'); + eq('td(weeks=1)', 'td(days=7)'); + eq('td(days=1)', 'td(hours=24)'); + eq('td(hours=1)', 'td(minutes=60)'); + eq('td(minutes=1)', 'td(seconds=60)'); + eq('td(seconds=1)', 'td(milliseconds=1000)'); + eq('td(milliseconds=1)', 'td(microseconds=1000)'); + + // Check float args to constructor + eq('td(weeks=1.0/7)', 'td(days=1)'); + eq('td(days=1.0/24)', 'td(hours=1)'); + eq('td(hours=1.0/60)', 'td(minutes=1)'); + eq('td(minutes=1.0/60)', 'td(seconds=1)'); + eq('td(seconds=0.001)', 'td(milliseconds=1)'); + eq('td(milliseconds=0.001)', 'td(microseconds=1)'); + }); + test('timedelta.test_computations', function (instance) { + var c = instance.web.pyeval.context(); + var zero = py.float.fromJSON(0); + var eq = makeEq(instance, { + // one week + a: py.PY_call(c.datetime.timedelta, [ + py.float.fromJSON(7)]), + // one minute + b: py.PY_call(c.datetime.timedelta, [ + zero, py.float.fromJSON(60)]), + // one millisecond + c: py.PY_call(c.datetime.timedelta, [ + zero, zero, py.float.fromJSON(1000)]), + }); + + eq('a+b+c', 'td(7, 60, 1000)'); + eq('a-b', 'td(6, 24*3600 - 60)'); + eq('-a', 'td(-7)'); + eq('+a', 'td(7)'); + eq('-b', 'td(-1, 24*3600 - 60)'); + eq('-c', 'td(-1, 24*3600 - 1, 999000)'); +// eq('abs(a)', 'a'); +// eq('abs(-a)', 'a'); + eq('td(6, 24*3600)', 'a'); + eq('td(0, 0, 60*1000000)', 'b'); + eq('a*10', 'td(70)'); + eq('a*10', '10*a'); + // eq('a*10L', '10*a'); + eq('b*10', 'td(0, 600)'); + eq('10*b', 'td(0, 600)'); + // eq('b*10L', 'td(0, 600)'); + eq('c*10', 'td(0, 0, 10000)'); + eq('10*c', 'td(0, 0, 10000)'); + // eq('c*10L', 'td(0, 0, 10000)'); + eq('a*-1', '-a'); + eq('b*-2', '-b-b'); + eq('c*-2', '-c+-c'); + eq('b*(60*24)', '(b*60)*24'); + eq('b*(60*24)', '(60*b)*24'); + eq('c*1000', 'td(0, 1)'); + eq('1000*c', 'td(0, 1)'); + eq('a//7', 'td(1)'); + eq('b//10', 'td(0, 6)'); + eq('c//1000', 'td(0, 0, 1)'); + eq('a//10', 'td(0, 7*24*360)'); + eq('a//3600000', 'td(0, 0, 7*24*1000)'); + + // Issue #11576 + eq('td(999999999, 86399, 999999) - td(999999999, 86399, 999998)', 'td(0, 0, 1)'); + eq('td(999999999, 1, 1) - td(999999999, 1, 0)', + 'td(0, 0, 1)') + }); + test('timedelta.test_basic_attributes', function (instance) { + var ctx = instance.web.pyeval.context(); + strictEqual(py.eval('datetime.timedelta(1, 7, 31).days', ctx), 1); + strictEqual(py.eval('datetime.timedelta(1, 7, 31).seconds', ctx), 7); + strictEqual(py.eval('datetime.timedelta(1, 7, 31).microseconds', ctx), 31); + }); + test('timedelta.test_total_seconds', function (instance) { + var c = { timedelta: instance.web.pyeval.context().datetime.timedelta }; + strictEqual(py.eval('timedelta(365).total_seconds()', c), 31536000); + strictEqual( + py.eval('timedelta(seconds=123456.789012).total_seconds()', c), + 123456.789012); + strictEqual( + py.eval('timedelta(seconds=-123456.789012).total_seconds()', c), + -123456.789012); + strictEqual( + py.eval('timedelta(seconds=0.123456).total_seconds()', c), 0.123456); + strictEqual(py.eval('timedelta().total_seconds()', c), 0); + strictEqual( + py.eval('timedelta(seconds=1000000).total_seconds()', c), 1e6); + }); + test('timedelta.test_str', function (instance) { + var c = { td: instance.web.pyeval.context().datetime.timedelta }; + + strictEqual(py.eval('str(td(1))', c), "1 day, 0:00:00"); + strictEqual(py.eval('str(td(-1))', c), "-1 day, 0:00:00"); + strictEqual(py.eval('str(td(2))', c), "2 days, 0:00:00"); + strictEqual(py.eval('str(td(-2))', c), "-2 days, 0:00:00"); + + strictEqual(py.eval('str(td(hours=12, minutes=58, seconds=59))', c), + "12:58:59"); + strictEqual(py.eval('str(td(hours=2, minutes=3, seconds=4))', c), + "2:03:04"); + strictEqual( + py.eval('str(td(weeks=-30, hours=23, minutes=12, seconds=34))', c), + "-210 days, 23:12:34"); + + strictEqual(py.eval('str(td(milliseconds=1))', c), "0:00:00.001000"); + strictEqual(py.eval('str(td(microseconds=3))', c), "0:00:00.000003"); + + strictEqual( + py.eval('str(td(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999))', c), + "999999999 days, 23:59:59.999999"); + }); + test('timedelta.test_massive_normalization', function (instance) { + var td = py.PY_call( + instance.web.pyeval.context().datetime.timedelta, + {microseconds: py.float.fromJSON(-1)}); + strictEqual(td.days, -1); + strictEqual(td.seconds, 24 * 3600 - 1); + strictEqual(td.microseconds, 999999); + }); + test('timedelta.test_bool', function (instance) { + var c = { td: instance.web.pyeval.context().datetime.timedelta }; + ok(py.eval('bool(td(1))', c)); + ok(py.eval('bool(td(0, 1))', c)); + ok(py.eval('bool(td(0, 0, 1))', c)); + ok(py.eval('bool(td(microseconds=1))', c)); + ok(py.eval('bool(not td(0))', c)); + }); }); openerp.testing.section('eval.edc', { dependencies: ['web.data'], -- cgit v1.2.3 From 328537a7ba845d3cdf90826998d066dddec5c0de Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 10:52:06 +0100 Subject: [ADD] arithmetics between date and timedelta bzr revid: xmo@openerp.com-20121123095206-dtvoiignb7xnzsl0 --- addons/web/static/test/evals.js | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index ba9888617ee..d4f72b1c374 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -163,6 +163,87 @@ openerp.testing.section('eval.types', { ok(py.eval('bool(td(microseconds=1))', c)); ok(py.eval('bool(not td(0))', c)); }); + + test('date.test_computations', function (instance) { + var d = instance.web.pyeval.context().datetime; + + var a = d.date.fromJSON(2002, 1, 31); + var b = d.date.fromJSON(1956, 1, 31); + strictEqual( + py.eval('(a - b).days', {a: a, b: b}), + 46 * 365 + 12); + strictEqual(py.eval('(a - b).seconds', {a: a, b: b}), 0); + strictEqual(py.eval('(a - b).microseconds', {a: a, b: b}), 0); + + var day = py.PY_call(d.timedelta, [py.float.fromJSON(1)]); + var week = py.PY_call(d.timedelta, [py.float.fromJSON(7)]); + a = d.date.fromJSON(2002, 3, 2); + var ctx = { + a: a, + day: day, + week: week, + date: d.date + }; + ok(py.eval('a + day == date(2002, 3, 3)', ctx)); + ok(py.eval('day + a == date(2002, 3, 3)', ctx)); // 5 + ok(py.eval('a - day == date(2002, 3, 1)', ctx)); + ok(py.eval('-day + a == date(2002, 3, 1)', ctx)); + ok(py.eval('a + week == date(2002, 3, 9)', ctx)); + console.group(9) + ok(py.eval('a - week == date(2002, 2, 23)', ctx)); + console.groupEnd(); + ok(py.eval('a + 52*week == date(2003, 3, 1)', ctx)); // 10 + ok(py.eval('a - 52*week == date(2001, 3, 3)', ctx)); + ok(py.eval('(a + week) - a == week', ctx)); + ok(py.eval('(a + day) - a == day', ctx)); + ok(py.eval('(a - week) - a == -week', ctx)); + ok(py.eval('(a - day) - a == -day', ctx)); // 15 + ok(py.eval('a - (a + week) == -week', ctx)); + ok(py.eval('a - (a + day) == -day', ctx)); + ok(py.eval('a - (a - week) == week', ctx)); + ok(py.eval('a - (a - day) == day', ctx)); + + raises(function () { + py.eval('a + 1', ctx); + }, /^Error: TypeError:/); // 20 + raises(function () { + py.eval('a - 1', ctx); + }, /^Error: TypeError:/); + raises(function () { + py.eval('1 + a', ctx); + }, /^Error: TypeError:/); + raises(function () { + py.eval('1 - a', ctx); + }, /^Error: TypeError:/); + + // delta - date is senseless. + raises(function () { + py.eval('day - a', ctx); + }, /^Error: TypeError:/); + // mixing date and (delta or date) via * or // is senseless + raises(function () { + py.eval('day * a', ctx); + }, /^Error: TypeError:/); // 25 + raises(function () { + py.eval('a * day', ctx); + }, /^Error: TypeError:/); + raises(function () { + py.eval('day // a', ctx); + }, /^Error: TypeError:/); + raises(function () { + py.eval('a // day', ctx); + }, /^Error: TypeError:/); + raises(function () { + py.eval('a * a', ctx); + }, /^Error: TypeError:/); + raises(function () { + py.eval('a // a', ctx); + }, /^Error: TypeError:/); // 30 + // date + date is senseless + raises(function () { + py.eval('a + a', ctx); + }, /^Error: TypeError:/); + }); }); openerp.testing.section('eval.edc', { dependencies: ['web.data'], -- cgit v1.2.3 From 242a5da5a9f4b71b3ced9f583a461c397f6be22a Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 10:58:24 +0100 Subject: [REM] stray logging bzr revid: xmo@openerp.com-20121123095824-xg6afc1l01t3y48y --- addons/web/static/test/evals.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index d4f72b1c374..994c122f1e6 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -189,9 +189,7 @@ openerp.testing.section('eval.types', { ok(py.eval('a - day == date(2002, 3, 1)', ctx)); ok(py.eval('-day + a == date(2002, 3, 1)', ctx)); ok(py.eval('a + week == date(2002, 3, 9)', ctx)); - console.group(9) ok(py.eval('a - week == date(2002, 2, 23)', ctx)); - console.groupEnd(); ok(py.eval('a + 52*week == date(2003, 3, 1)', ctx)); // 10 ok(py.eval('a - 52*week == date(2001, 3, 3)', ctx)); ok(py.eval('(a + week) - a == week', ctx)); -- cgit v1.2.3 From 709f2506ff40d799aada58df74dd78522d965868 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 12:39:32 +0100 Subject: [REM] server-side eval_domain_and_context bzr revid: xmo@openerp.com-20121123113932-siz0u2vp2u7z6wk2 --- addons/web/static/test/evals.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 994c122f1e6..86da3ae2876 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -247,8 +247,13 @@ openerp.testing.section('eval.edc', { dependencies: ['web.data'], rpc: 'rpc', setup: function (instance) { + instance.session.user_context = { + lang: 'en_US', + tz: false, + uid: 1 + }; instance.edc = function (domains, contexts) { - return this.session.rpc('/web/session/eval_domain_and_context', { + return instance.web.pyeval.eval_domains_and_contexts({ contexts: contexts || [], domains: domains || [] }); @@ -382,9 +387,14 @@ openerp.testing.section('eval.edc.nonliterals', { dependencies: ['web.data'], rpc: 'rpc', setup: function (instance) { + instance.session.user_context = { + lang: 'en_US', + tz: false, + uid: 1 + }; _.extend(instance, { edc: function (domains, contexts) { - return this.session.rpc('/web/session/eval_domain_and_context', { + return instance.web.pyeval.eval_domains_and_contexts({ contexts: contexts || [], domains: domains || [] }); @@ -398,7 +408,7 @@ openerp.testing.section('eval.edc.nonliterals', { }); } }, function (test) { - test('domain with time', {asserts: 2}, function (instance) { + test('domain with time', {asserts: 1}, function (instance) { return instance.load_domain(0).then(function (d) { strictEqual(d.__ref, 'domain'); return instance.edc([ @@ -409,7 +419,7 @@ openerp.testing.section('eval.edc.nonliterals', { }, d, [['user_id', '=', 1]] - ]) + ]); }).then(function (result) { var d = new Date(); var today = _.str.sprintf("%04d-%02d-%02d", -- cgit v1.2.3 From 6b2b2d889c94b0cbab5fbe14a4555f4c8dbe179c Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 12:49:23 +0100 Subject: [IMP] move nonliteral domains and contexts to JS test case bzr revid: xmo@openerp.com-20121123114923-t4047w6jztria91x --- addons/web/static/test/evals.js | 81 ++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 46 deletions(-) (limited to 'addons/web/static/test/evals.js') 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()); -- cgit v1.2.3 From 2c7f30a047bd5b4e2ada23448029549dc6c28f61 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 13:05:00 +0100 Subject: [REM] session reload dependency on db bzr revid: xmo@openerp.com-20121123120500-k7fhwoocu0zbiali --- addons/web/static/test/evals.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index e65f697eea2..e71457a07e6 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -245,19 +245,34 @@ openerp.testing.section('eval.types', { }); openerp.testing.section('eval.edc', { dependencies: ['web.data'], - rpc: 'rpc', - setup: function (instance) { - instance.session.user_context = { - lang: 'en_US', - tz: false, - uid: 1 - }; + rpc: 'mock', + setup: function (instance, $fix, mock) { + var user = { login: 'admin', id: 1, lang: 'en_US', tz: false }; instance.edc = function (domains, contexts) { return instance.web.pyeval.eval_domains_and_contexts({ contexts: contexts || [], domains: domains || [] }); - } + }; + mock('res.lang:load_lang', function () { return true; }); + mock('res.users:write', function (args) { + _.extend(user, args[1]); + return true; + }); + mock('/web/session/get_session_info', function () { + return { + session_id: 'foobar', + db: '3', + login: user.login, + uid: user.id, + context: { + uid: user.id, + lang: user.lang, + tz: user.tz + } + }; + }); + return instance.session.session_reload(); } }, function (test) { test('empty, basic', {asserts: 3}, function (instance) { @@ -282,9 +297,7 @@ openerp.testing.section('eval.edc', { lang: 'ru_RU', tz: 'America/Santarem' }]); - }).then(function () { - return instance.session.session_reload(); - }); + }).then(instance.session.session_reload.bind(instance.session)); } }, function (instance) { return instance.edc().then(function (result) { -- cgit v1.2.3 From aab69ae7a9cb27211392c84bcd97a42fe0917f11 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 26 Nov 2012 09:41:59 +0100 Subject: [TEST] for crazy bzr revid: xmo@openerp.com-20121126084159-lbgfa56vafg4bxc4 --- addons/web/static/test/evals.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'addons/web/static/test/evals.js') diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index e71457a07e6..8e836dade7e 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -502,6 +502,41 @@ openerp.testing.section('eval.edc.nonliterals', { ]); }); }); + test('horror from the deep', {asserts: 1}, function (instance) { + var cs = [ + {"__ref": "compound_context", + "__contexts": [ + {"__ref": "context", "__debug": "{'k': 'foo,' + str(context.get('test_key', False))}"}, + {"__ref": "compound_context", + "__contexts": [ + {"lang": "en_US", "tz": false, "uid": 1}, + {"lang": "en_US", "tz": false, "uid": 1, + "active_model": "sale.order", "default_type": "out", + "show_address": 1, "contact_display": "partner_address", + "active_ids": [9], "active_id": 9}, + {} + ], "__eval_context": null }, + {"active_id": 8, "active_ids": [8], + "active_model": "stock.picking.out"}, + {"__ref": "context", "__debug": "{'default_ref': 'stock.picking.out,'+str(context.get('active_id', False))}", "__id": "54d6ad1d6c45"} + ], "__eval_context": null} + ]; + return instance.edc([], cs).then(function (result) { + deepEqual(result.context, { + k: 'foo,False', + lang: 'en_US', + tz: false, + uid: 1, + active_model: 'stock.picking.out', + active_id: 8, + active_ids: [8], + default_type: 'out', + show_address: 1, + contact_display: 'partner_address', + default_ref: 'stock.picking.out,8' + }); + }); + }); }); openerp.testing.section('eval.contexts', { dependencies: ['web.coresetup'] -- cgit v1.2.3