From 38994104f06c9a9f6e15bcc3d93b887ba3e99b4d Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 10 Dec 2012 16:18:28 +0100 Subject: [PATCH 1/2] [IMP] js tests: when testing pyeval on datetimes, avoid comparison with an indepent call to new Date() bzr revid: rco@openerp.com-20121210151828-lfzyfy75oldkjxk5 --- addons/web/static/test/evals.js | 40 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 4fc7140f5e0..2e71f226744 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -4,27 +4,33 @@ openerp.testing.section('eval.types', { instance.session.uid = 42; } }, function (test) { - test('strftime', function (instance) { - var d = new Date(); + var check_datetime = function(expr, date_func) { + // evaluate expr between two calls to new Date(), and check that + // the result is between the transformed dates + var d0 = new Date; var context = instance.web.pyeval.context(); - strictEqual( - py.eval("time.strftime('%Y')", context), - String(d.getFullYear())); - strictEqual( - py.eval("time.strftime('%Y')+'-01-30'", context), - 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', + var result = py.eval(expr, context); + var d1 = new Date; + ok(date_func(d0) <= result && result <= date_func(d1)); + }; + test('strftime', function (instance) { + check_datetime("time.strftime('%Y')", function(d) { + return String(d.getFullYear()); + }); + check_datetime("time.strftime('%Y')+'-01-30'", function(d) { + return String(d.getFullYear()) + '-01-30'; + }); + check_datetime("time.strftime('%Y-%m-%d %H:%M:%S')", function(d) { + return _.str.sprintf('%04d-%02d-%02d %02d:%02d:%02d', d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), - d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds())); + d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()); + }); }); test('context_today', function (instance) { - var d = new Date(); - var context = instance.web.pyeval.context(); - strictEqual( - py.eval("context_today().strftime('%Y-%m-%d')", context), - String(_.str.sprintf('%04d-%02d-%02d', d.getFullYear(), d.getMonth() + 1, d.getDate()))); + check_datetime("context_today().strftime('%Y-%m-%d')", function(d) { + return String(_.str.sprintf('%04d-%02d-%02d', + d.getFullYear(), d.getMonth() + 1, d.getDate())); + }); }); // Port from pypy/lib_pypy/test_datetime.py var makeEq = function (instance, c2) { From 43ca577f991c02edf7f6473a7c63c5405aca1bbe Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 10 Dec 2012 16:43:09 +0100 Subject: [PATCH 2/2] [FIX] js tests: add an abstraction over instance to create properly an assertion function bzr revid: rco@openerp.com-20121210154309-mubr65ildpiw2wpx --- addons/web/static/test/evals.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index 2e71f226744..055180f004b 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -4,30 +4,34 @@ openerp.testing.section('eval.types', { instance.session.uid = 42; } }, function (test) { - var check_datetime = function(expr, date_func) { - // evaluate expr between two calls to new Date(), and check that - // the result is between the transformed dates - var d0 = new Date; + var makeTimeCheck = function (instance) { var context = instance.web.pyeval.context(); - var result = py.eval(expr, context); - var d1 = new Date; - ok(date_func(d0) <= result && result <= date_func(d1)); + return function (expr, func, message) { + // evaluate expr between two calls to new Date(), and check that + // the result is between the transformed dates + var d0 = new Date; + var result = py.eval(expr, context); + var d1 = new Date; + ok(func(d0) <= result && result <= func(d1), message); + }; }; test('strftime', function (instance) { - check_datetime("time.strftime('%Y')", function(d) { + var check = makeTimeCheck(instance); + check("time.strftime('%Y')", function(d) { return String(d.getFullYear()); }); - check_datetime("time.strftime('%Y')+'-01-30'", function(d) { + check("time.strftime('%Y')+'-01-30'", function(d) { return String(d.getFullYear()) + '-01-30'; }); - check_datetime("time.strftime('%Y-%m-%d %H:%M:%S')", function(d) { + check("time.strftime('%Y-%m-%d %H:%M:%S')", function(d) { return _.str.sprintf('%04d-%02d-%02d %02d:%02d:%02d', d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()); }); }); test('context_today', function (instance) { - check_datetime("context_today().strftime('%Y-%m-%d')", function(d) { + var check = makeTimeCheck(instance); + check("context_today().strftime('%Y-%m-%d')", function(d) { return String(_.str.sprintf('%04d-%02d-%02d', d.getFullYear(), d.getMonth() + 1, d.getDate())); });