[IMP] web: improve like operators capabilities in the mock server

Before this PR, the `like` operators from the mock server could not handle
request using the `%`.
This PR introduce it.

Part-of: odoo/odoo#118794
This commit is contained in:
Didier (did)
2023-10-03 19:19:50 +00:00
parent 2114ad3a03
commit 7c4a8f6118
2 changed files with 18 additions and 6 deletions
+9 -5
View File
@@ -298,7 +298,11 @@ function matchCondition(record, condition) {
return matchCondition(record[names[0]], [names.slice(1).join("."), operator, value]);
}
}
let likeRegexp, ilikeRegexp;
if (["like", "not like", "ilike", "not ilike"].includes(operator)) {
likeRegexp = new RegExp(`(.*)${value.replaceAll("%", "(.*)")}(.*)`, "g");
ilikeRegexp = new RegExp(`(.*)${value.replaceAll("%", "(.*)")}(.*)`, "gi");
}
const fieldValue = typeof field === "number" ? field : record[field];
switch (operator) {
case "=?":
@@ -337,12 +341,12 @@ function matchCondition(record, condition) {
if (fieldValue === false) {
return false;
}
return fieldValue.indexOf(value) >= 0;
return Boolean(fieldValue.match(likeRegexp));
case "not like":
if (fieldValue === false) {
return false;
}
return fieldValue.indexOf(value) === -1;
return Boolean(!fieldValue.match(likeRegexp));
case "=like":
if (fieldValue === false) {
return false;
@@ -352,12 +356,12 @@ function matchCondition(record, condition) {
if (fieldValue === false) {
return false;
}
return fieldValue.toLowerCase().indexOf(value.toLowerCase()) >= 0;
return Boolean(fieldValue.match(ilikeRegexp));
case "not ilike":
if (fieldValue === false) {
return false;
}
return fieldValue.toLowerCase().indexOf(value.toLowerCase()) === -1;
return Boolean(!fieldValue.match(ilikeRegexp));
case "=ilike":
if (fieldValue === false) {
return false;
+9 -1
View File
@@ -99,8 +99,10 @@ QUnit.module("domain", {}, () => {
});
QUnit.test("like, =like, ilike, =ilike, not like and not ilike", function (assert) {
assert.expect(28);
assert.expect(36);
assert.ok(new Domain([["a", "like", "test%value"]]).contains({ a: "test value" }));
assert.notOk(new Domain([["a", "like", "test%value"]]).contains({ a: "value test" }));
assert.ok(new Domain([["a", "like", "value"]]).contains({ a: "value" }));
assert.ok(new Domain([["a", "like", "value"]]).contains({ a: "some value" }));
assert.notOk(new Domain([["a", "like", "value"]]).contains({ a: "Some Value" }));
@@ -111,6 +113,8 @@ QUnit.module("domain", {}, () => {
assert.notOk(new Domain([["a", "=like", "%value"]]).contains({ a: "Some Value" }));
assert.notOk(new Domain([["a", "=like", "%value"]]).contains({ a: false }));
assert.ok(new Domain([["a", "ilike", "test%value"]]).contains({ a: "test value" }));
assert.notOk(new Domain([["a", "ilike", "test%value"]]).contains({ a: "value test" }));
assert.ok(new Domain([["a", "ilike", "value"]]).contains({ a: "value" }));
assert.ok(new Domain([["a", "ilike", "value"]]).contains({ a: "some value" }));
assert.ok(new Domain([["a", "ilike", "value"]]).contains({ a: "Some Value" }));
@@ -121,11 +125,15 @@ QUnit.module("domain", {}, () => {
assert.ok(new Domain([["a", "=ilike", "%value"]]).contains({ a: "Some Value" }));
assert.notOk(new Domain([["a", "=ilike", "%value"]]).contains({ a: false }));
assert.notOk(new Domain([["a", "not like", "test%value"]]).contains({ a: "test value" }));
assert.ok(new Domain([["a", "not like", "test%value"]]).contains({ a: "value test" }));
assert.notOk(new Domain([["a", "not like", "value"]]).contains({ a: "value" }));
assert.notOk(new Domain([["a", "not like", "value"]]).contains({ a: "some value" }));
assert.ok(new Domain([["a", "not like", "value"]]).contains({ a: "Some Value" }));
assert.ok(new Domain([["a", "not like", "value"]]).contains({ a: "something" }));
assert.ok(new Domain([["a", "not like", "value"]]).contains({ a: "Something" }));
assert.notOk(new Domain([["a", "not ilike", "test%value"]]).contains({ a: "test value" }));
assert.ok(new Domain([["a", "not ilike", "test%value"]]).contains({ a: "value test" }));
assert.notOk(new Domain([["a", "not like", "value"]]).contains({ a: false }));
assert.notOk(new Domain([["a", "not ilike", "value"]]).contains({ a: "value" }));