From a425c58d99d4c03aeddb53ea38a8304968336379 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Tue, 9 Jun 2020 15:32:35 +0000 Subject: [PATCH] [IMP] web: mock server: enhance default get Before this commit, the mock server "_mockDefaultGet" method was a bit naive: - it accepted any 'default_' field coming from the 'kwargs.context' regardless of the existence of the field - it accepted any value for these fields Now the method is looks a bit more like its original server-side counterpart: - the context keys are added if they correspond to actual fields - a pseudo validation removes many2one fields values not corresponding to any record --- .../web/static/tests/helpers/mock_server.js | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/addons/web/static/tests/helpers/mock_server.js b/addons/web/static/tests/helpers/mock_server.js index 6f6329525ab..2b7bcec2014 100644 --- a/addons/web/static/tests/helpers/mock_server.js +++ b/addons/web/static/tests/helpers/mock_server.js @@ -482,27 +482,38 @@ var MockServer = Class.extend({ * @private * @param {string} modelName * @param {array[]} args a list with a list of fields in the first position - * @param {Object} [kwargs] + * @param {Object} [kwargs={}] * @param {Object} [kwargs.context] the context to eventually read default * values * @returns {Object} */ - _mockDefaultGet: function (modelName, args, kwargs) { - var result = {}; - var fields = args[0]; - var model = this.data[modelName]; - _.each(fields, function (name) { - var field = model.fields[name]; + _mockDefaultGet: function (modelName, args, kwargs={}) { + const fields = args[0]; + const model = this.data[modelName]; + const result = {}; + for (const fieldName of fields) { + const key = "default_" + fieldName; + if (kwargs.context && key in kwargs.context) { + result[fieldName] = kwargs.context[key]; + continue; + } + const field = model.fields[fieldName]; if ('default' in field) { - result[name] = field.default; + result[fieldName] = field.default; + continue; } - }); - if (kwargs && kwargs.context) - _.each(kwargs.context, function (value, key) { - if ('default_' === key.slice(0, 8)) { - result[key.slice(8)] = value; + } + for (const fieldName in result) { + const field = model.fields[fieldName]; + if (field.type === "many2one") { + const recordExists = this.data[field.relation].records.some( + (r) => r.id === result[fieldName] + ); + if (!recordExists) { + delete result[fieldName]; + } } - }); + } return result; }, /**