Skip to content

Commit

Permalink
Fix excludeOperators (prox1 at example) (#1072)
Browse files Browse the repository at this point in the history
* Fix excludeOperators (prox1 at example)

* test

* chlog
  • Loading branch information
ukrbublik authored Jun 26, 2024
1 parent 82c56b0 commit de2d0c7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Added settings `reverseOperatorsForNot` and `canShortMongoQuery`
- Scope CSS classes to `.query-builder` (PR #1070) (issue #1018)
- Update packages (PR #1071)
- Fixed issue with `excludeOperators` (affects `prox1` at example) (PR #1072)
- 6.6.0
- Optimizations for rendering and export utils (PR #1054) (issue #342)
- Added support of JsonLogic export for ternary mode (PR #1013) (issue #978)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/modules/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ const operators = {
formatOp: (field, op, values, valueSrc, valueType, opDef, operatorOptions, isForDisplay) => {
const val1 = values.first();
const val2 = values.get(1);
const prox = operatorOptions.get("proximity");
const prox = operatorOptions?.get("proximity");
return `${field} ${val1} NEAR/${prox} ${val2}`;
},
sqlFormatOp: function (field, op, values, valueSrc, valueType, opDef, operatorOptions, fieldDef) {
Expand All @@ -520,7 +520,7 @@ const operators = {
const val2 = values.get(1);
const aVal1 = this.utils.SqlString.trim(val1);
const aVal2 = this.utils.SqlString.trim(val2);
const prox = operatorOptions.get("proximity");
const prox = operatorOptions?.get("proximity");
return `CONTAINS(${field}, 'NEAR((${aVal1}, ${aVal2}), ${prox})')`;
},
mongoFormatOp: undefined, // not supported
Expand Down
6 changes: 5 additions & 1 deletion packages/core/modules/utils/configExtend.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ function extendFieldConfig(fieldConfig, config, path = [], isFuncArg = false, is
});

// copy/merge widgets
const excludeOperatorsForType = typeConfig.excludeOperators || [];
let excludeOperatorsForType = (typeConfig.excludeOperators || []);
if (fieldConfig.operators) {
// `operators` from field can override `excludeOperators` from type, see `prox1` at examples
excludeOperatorsForType = excludeOperatorsForType.filter(op => !fieldConfig.operators.includes(op));
}
if (!fieldConfig.widgets)
fieldConfig.widgets = {};
for (let widget in typeConfig.widgets) {
Expand Down
47 changes: 37 additions & 10 deletions packages/tests/specs/QueryWithProximity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,50 @@ import { with_qb, export_checks } from "../support/utils";

describe("proximity", () => {

describe("export", () => {
describe("when text type has proximity operator", () => {
export_checks(configs.with_prox, inits.with_prox, "default", {
"query": "str \"a\" NEAR/3 \"b\"",
"queryHuman": "String a NEAR/3 b",
"sql": "CONTAINS(str, 'NEAR((a, b), 3)')"
});

it("change NEAR", async () => {
await with_qb(configs.with_prox, inits.with_prox, "default", (qb, {expect_queries}) => {
qb
.find(".rule .rule--operator-options .rule--operator .operator--options select")
.simulate("change", { target: { value: "5" } });
expect_queries([
"str \"a\" NEAR/3 \"b\"",
"str \"a\" NEAR/5 \"b\""
]);
});
});
});

it("change NEAR", async () => {
await with_qb(configs.with_prox, inits.with_prox, "default", (qb, {expect_queries}) => {
qb
.find(".rule .rule--operator-options .rule--operator .operator--options select")
.simulate("change", { target: { value: "5" } });
expect_queries([
"str \"a\" NEAR/3 \"b\"",
"str \"a\" NEAR/5 \"b\""
]);
describe("when text type has no proximity operator, but prox field has", () => {
export_checks(configs.with_prox1, inits.with_prox1, "default", {
"query": "prox1 \"a\" NEAR/3 \"b\"",
"queryHuman": "Prox1 a NEAR/3 b",
"sql": "CONTAINS(prox1, 'NEAR((a, b), 3)')"
});

it("renders widgets correctly", async () => {
await with_qb([
configs.with_prox1, configs.with_dont_fix_on_load, configs.with_show_error,
], inits.with_prox1_no_values, "default", async (qb) => {
expect(qb.find(".rule .rule--value .widget--widget")).to.have.length(2);
}, {
expectedLoadErrors: [
"Prox1 ? NEAR/3 ? >> [rhs] Incomplete RHS"
],
sanitizeOptions: {
// don't fix tree in `load_tree`
removeEmptyGroups: false,
removeEmptyRules: false,
removeIncompleteRules: false,
},
});
});
});

});
22 changes: 22 additions & 0 deletions packages/tests/support/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,28 @@ export const with_dropdown = (AntdConfig) => {
};
};

export const with_prox1 = (BasicConfig) => ({
...BasicConfig,
types: {
...BasicConfig.types,
text: {
...BasicConfig.types.text,
excludeOperators: ["proximity"]
}
},
fields: {
str: {
label: "String",
type: "text",
},
prox1: {
label: "Prox1",
type: "text",
operators: ["proximity"],
}
},
});

export const with_prox = (BasicConfig) => ({
...BasicConfig,
fields: {
Expand Down
33 changes: 33 additions & 0 deletions packages/tests/support/inits.js
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,39 @@ export const with_prox = {
}
};

export const with_prox1 = {
type: "group",
children1: [
{
type: "rule",
properties: {
field: "prox1",
operator: "proximity",
value: [ "a", "b" ],
operatorOptions: {
proximity: 3
}
}
},
],
};

export const with_prox1_no_values = {
type: "group",
children1: [
{
type: "rule",
properties: {
field: "prox1",
operator: "proximity",
operatorOptions: {
proximity: 3
}
}
},
],
};

export const with_jl_value = {
"==": [
{ "var": "num" }, { "+": [1, 2] }
Expand Down

0 comments on commit de2d0c7

Please sign in to comment.