[FIX] web: mock active_test for search and search_read
The support for "active_test" context key has been introduced in c2918c1
However, the context key is ignored in many mocked ORM methods (if not all).
This commit adds support for the `search` and `search_read` methods.
Note: this is done to easily write some tests in odoo/enterprise#16363
and it might be usefull to test futures features (or fixes) using this
context key.
closes odoo/odoo#66393
X-original-commit: 0ad1c1631eb233b3e8f98b40f8c0813d0ad7acbe
Related: odoo/enterprise#16447
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
This commit is contained in:
@@ -1641,7 +1641,10 @@ var MockServer = Class.extend({
|
||||
*/
|
||||
_mockSearch: function (model, args, kwargs) {
|
||||
const limit = kwargs.limit || Number.MAX_VALUE;
|
||||
return this._getRecords(model, args[0]).map(r => r.id).slice(0, limit);
|
||||
const { context } = kwargs;
|
||||
const active_test =
|
||||
context && "active_test" in context ? context.active_test : true;
|
||||
return this._getRecords(model, args[0], { active_test }).map(r => r.id).slice(0, limit);
|
||||
},
|
||||
/**
|
||||
* Simulate a 'search_count' operation
|
||||
@@ -1695,7 +1698,12 @@ var MockServer = Class.extend({
|
||||
*/
|
||||
_mockSearchReadController: function (args) {
|
||||
var self = this;
|
||||
var records = this._getRecords(args.model, args.domain || []);
|
||||
const { context } = args;
|
||||
const active_test =
|
||||
context && "active_test" in context ? context.active_test : true;
|
||||
var records = this._getRecords(args.model, args.domain || [], {
|
||||
active_test,
|
||||
});
|
||||
var fields = args.fields && args.fields.length ? args.fields : _.keys(this.data[args.model].fields);
|
||||
var nbRecords = records.length;
|
||||
var offset = args.offset || 0;
|
||||
|
||||
@@ -16,8 +16,16 @@ QUnit.module("MockServer", {
|
||||
string: "Email",
|
||||
type: "string",
|
||||
},
|
||||
active: {
|
||||
string: "Active",
|
||||
type: "bool",
|
||||
default: true,
|
||||
},
|
||||
},
|
||||
records: [{ id: 1, name: "Jean-Michel", email: "jean.michel@example.com" }],
|
||||
records: [
|
||||
{ id: 1, name: "Jean-Michel", email: "jean.michel@example.com" },
|
||||
{ id: 2, name: "Raoul", email: "raoul@example.com", active: false },
|
||||
],
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -131,5 +139,63 @@ QUnit.module("MockServer", {
|
||||
});
|
||||
assert.deepEqual(result, [[null, "False"], [1, "Jean-Michel"]]);
|
||||
});
|
||||
|
||||
QUnit.test("performRpc: search with active_test=false", async function (assert) {
|
||||
assert.expect(1);
|
||||
const server = new MockServer(this.data, {});
|
||||
const result = await server.performRpc("", {
|
||||
model: "res.partner",
|
||||
method: "search",
|
||||
args: [[]],
|
||||
kwargs: {
|
||||
context: { active_test: false },
|
||||
},
|
||||
});
|
||||
assert.deepEqual(result, [1, 2]);
|
||||
});
|
||||
|
||||
QUnit.test("performRpc: search with active_test=true", async function (assert) {
|
||||
assert.expect(1);
|
||||
const server = new MockServer(this.data, {});
|
||||
const result = await server.performRpc("", {
|
||||
model: "res.partner",
|
||||
method: "search",
|
||||
args: [[]],
|
||||
kwargs: {
|
||||
context: { active_test: true },
|
||||
},
|
||||
});
|
||||
assert.deepEqual(result, [1]);
|
||||
});
|
||||
|
||||
QUnit.test("performRpc: search_read with active_test=false", async function (assert) {
|
||||
assert.expect(1);
|
||||
const server = new MockServer(this.data, {});
|
||||
const result = await server.performRpc("", {
|
||||
model: "res.partner",
|
||||
method: "search_read",
|
||||
args: [[]],
|
||||
kwargs: {
|
||||
fields: ["name"],
|
||||
context: { active_test: false },
|
||||
},
|
||||
});
|
||||
assert.deepEqual(result, [{id: 1, name: "Jean-Michel"}, {id: 2, name: "Raoul"}]);
|
||||
});
|
||||
|
||||
QUnit.test("performRpc: search_read with active_test=true", async function (assert) {
|
||||
assert.expect(1);
|
||||
const server = new MockServer(this.data, {});
|
||||
const result = await server.performRpc("", {
|
||||
model: "res.partner",
|
||||
method: "search_read",
|
||||
args: [[]],
|
||||
kwargs: {
|
||||
fields: ["name"],
|
||||
context: { active_test: true },
|
||||
},
|
||||
});
|
||||
assert.deepEqual(result, [{id: 1, name: "Jean-Michel"}]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user