diff options
| author | Julien Mougenot <jum@odoo.com> | 2020-07-06 08:41:00 +0000 |
|---|---|---|
| committer | Julien Mougenot <jum@odoo.com> | 2020-07-07 08:33:28 +0000 |
| commit | cd1d339763d2251735f7b92d5c2d9a49eef69ce6 (patch) | |
| tree | 76980f0c70b5e16710696077e35817bfc2fa0a89 /addons/web/static/src/js/views | |
| parent | e93b9aa2716222231d963b8e6b52a4c6fbce2a79 (diff) | |
[FIX] web: Make sample server tests deterministic
Before this commit, there were a few tests in the "sample server" suite
that asserted that every group resulting of a "read_group" would contain
at least one record, while there was actually a slight chance of it not
happening.
Now, the sample server used in the tests has been rendered deterministic
and these kind of issues are completely avoided.
closes odoo/odoo#54164
X-original-commit: a6d32ff46558b68ce3f19458f29c311ee5c658fd
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Signed-off-by: Julien Mougenot (jum) <jum@odoo.com>
Diffstat (limited to 'addons/web/static/src/js/views')
| -rw-r--r-- | addons/web/static/src/js/views/sample_server.js | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/addons/web/static/src/js/views/sample_server.js b/addons/web/static/src/js/views/sample_server.js index 6eaa023c47a..f6c0ca7903a 100644 --- a/addons/web/static/src/js/views/sample_server.js +++ b/addons/web/static/src/js/views/sample_server.js @@ -198,7 +198,7 @@ odoo.define('web.SampleServer', function (require) { const field = this.data[modelName].fields[fieldName]; switch (field.type) { case "boolean": - return fieldName === 'active' ? true : Math.random() < 0.5; + return fieldName === 'active' ? true : this._getRandomBool(); case "char": case "text": if (["display_name", "name"].includes(fieldName)) { @@ -227,23 +227,22 @@ odoo.define('web.SampleServer', function (require) { return false; case "date": case "datetime": { - const delta = Math.floor((Math.random() - Math.random()) * SampleServer.DATE_DELTA); - const date = new moment().add(delta, "hour").format( - field.type === "date" ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm:ss" - ); - return date; + const format = field.type === "date" ? + "YYYY-MM-DD" : + "YYYY-MM-DD HH:mm:ss"; + return this._getRandomDate(format); } case "float": - return Math.random() * SampleServer.MAX_FLOAT; + return this._getRandomFloat(SampleServer.MAX_FLOAT); case "integer": { let max = SampleServer.MAX_INTEGER; if (fieldName.includes('color')) { - max = Math.random() < 0.5 ? SampleServer.MAX_COLOR_INT : 0; + max = this._getRandomBool() ? SampleServer.MAX_COLOR_INT : 0; } - return Math.floor(Math.random() * max); + return this._getRandomInt(max); } case "monetary": - return Math.floor(Math.random() * SampleServer.MAX_MONETARY); + return this._getRandomInt(SampleServer.MAX_MONETARY); case "many2one": if (field.relation === 'res.currency') { return session.company_currency_id; @@ -256,8 +255,7 @@ odoo.define('web.SampleServer', function (require) { } case "selection": { if (field.selection.length > 0) { - const index = Math.floor(Math.random() * field.selection.length); - return field.selection[index][0]; + return this._getRandomArrayEl(field.selection)[0]; } return false; } @@ -267,7 +265,55 @@ odoo.define('web.SampleServer', function (require) { } /** - * Generates a random id in the range of ids generated for sub models. + * @private + * @param {any[]} array + * @returns {any} + */ + _getRandomArrayEl(array) { + return array[Math.floor(Math.random() * array.length)]; + } + + /** + * @private + * @returns {boolean} + */ + _getRandomBool() { + return Math.random() < 0.5; + } + + /** + * @private + * @param {string} format + * @returns {moment} + */ + _getRandomDate(format) { + const delta = Math.floor( + (Math.random() - Math.random()) * SampleServer.DATE_DELTA + ); + return new moment() + .add(delta, "hour") + .format(format); + } + + /** + * @private + * @param {number} max + * @returns {number} float in [O, max[ + */ + _getRandomFloat(max) { + return Math.random() * max; + } + + /** + * @private + * @param {number} max + * @returns {number} int in [0, max[ + */ + _getRandomInt(max) { + return Math.floor(Math.random() * max); + } + + /** * @private * @returns {number} id in [1, SUB_RECORDSET_SIZE] */ |
