Merge branch 'master' into global-bindings

This commit is contained in:
deanhannigan 2024-01-09 10:38:34 +00:00 committed by GitHub
commit 8a6a83156b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 286 additions and 90 deletions

View File

@ -41,7 +41,7 @@
{ label: "False", value: "false" }, { label: "False", value: "false" },
]} ]}
/> />
{:else if schema.type === "array"} {:else if schemaHasOptions(schema) && schema.type === "array"}
<Multiselect <Multiselect
bind:value={value[field]} bind:value={value[field]}
options={schema.constraints.inclusion} options={schema.constraints.inclusion}
@ -77,7 +77,7 @@
on:change={e => onChange(e, field)} on:change={e => onChange(e, field)}
useLabel={false} useLabel={false}
/> />
{:else if ["string", "number", "bigint", "barcodeqr"].includes(schema.type)} {:else if ["string", "number", "bigint", "barcodeqr", "array"].includes(schema.type)}
<svelte:component <svelte:component
this={isTestModal ? ModalBindableInput : DrawerBindableInput} this={isTestModal ? ModalBindableInput : DrawerBindableInput}
panel={AutomationBindingPanel} panel={AutomationBindingPanel}

View File

@ -103,8 +103,7 @@ function typeCoercion(filters: SearchFilters, table: Table) {
return filters return filters
} }
for (let key of Object.keys(filters)) { for (let key of Object.keys(filters)) {
// @ts-ignore const searchParam = filters[key as keyof SearchFilters]
const searchParam = filters[key]
if (typeof searchParam === "object") { if (typeof searchParam === "object") {
for (let [property, value] of Object.entries(searchParam)) { for (let [property, value] of Object.entries(searchParam)) {
// We need to strip numerical prefixes here, so that we can look up // We need to strip numerical prefixes here, so that we can look up
@ -117,11 +116,17 @@ function typeCoercion(filters: SearchFilters, table: Table) {
continue continue
} }
if (column.type === FieldTypes.NUMBER) { if (column.type === FieldTypes.NUMBER) {
if (key === "oneOf") {
searchParam[property] = value
.split(",")
.map(item => parseFloat(item))
} else {
searchParam[property] = parseFloat(value) searchParam[property] = parseFloat(value)
} }
} }
} }
} }
}
return filters return filters
} }

View File

@ -143,7 +143,11 @@ export const buildLuceneQuery = (filter: SearchFilter[]) => {
oneOf: {}, oneOf: {},
containsAny: {}, containsAny: {},
} }
if (Array.isArray(filter)) {
if (!Array.isArray(filter)) {
return query
}
filter.forEach(expression => { filter.forEach(expression => {
let { operator, field, type, value, externalType, onEmptyFilter } = let { operator, field, type, value, externalType, onEmptyFilter } =
expression expression
@ -174,10 +178,10 @@ export const buildLuceneQuery = (filter: SearchFilter[]) => {
return return
} }
} }
if (type === "number" && typeof value === "string") { if (type === "number" && typeof value === "string" && !isHbs) {
if (operator === "oneOf") { if (operator === "oneOf") {
value = value.split(",").map(item => parseFloat(item)) value = value.split(",").map(item => parseFloat(item))
} else if (!isHbs) { } else {
value = parseFloat(value) value = parseFloat(value)
} }
} }
@ -236,7 +240,7 @@ export const buildLuceneQuery = (filter: SearchFilter[]) => {
} }
} }
}) })
}
return query return query
} }

View File

@ -1,6 +1,11 @@
import { SearchQuery, SearchQueryOperators } from "@budibase/types" import {
import { runLuceneQuery } from "../filters" SearchQuery,
import { expect, describe, it } from "vitest" SearchQueryOperators,
FieldType,
SearchFilter,
} from "@budibase/types"
import { buildLuceneQuery, runLuceneQuery } from "../filters"
import { expect, describe, it, test } from "vitest"
describe("runLuceneQuery", () => { describe("runLuceneQuery", () => {
const docs = [ const docs = [
@ -167,4 +172,186 @@ describe("runLuceneQuery", () => {
}) })
expect(runLuceneQuery(docs, query).map(row => row.order_id)).toEqual([2, 3]) expect(runLuceneQuery(docs, query).map(row => row.order_id)).toEqual([2, 3])
}) })
test.each([[523, 259], "523,259"])(
"should return rows with matches on numeric oneOf filter",
input => {
let query = buildQuery("oneOf", {
customer_id: input,
})
expect(runLuceneQuery(docs, query).map(row => row.customer_id)).toEqual([
259, 523,
])
}
)
})
describe("buildLuceneQuery", () => {
it("should return a basic search query template if the input is not an array", () => {
const filter: any = "NOT_AN_ARRAY"
expect(buildLuceneQuery(filter)).toEqual({
string: {},
fuzzy: {},
range: {},
equal: {},
notEqual: {},
empty: {},
notEmpty: {},
contains: {},
notContains: {},
oneOf: {},
containsAny: {},
})
})
it("should parseFloat if the type is a number, but the value is a numeric string", () => {
const filter: SearchFilter[] = [
{
operator: SearchQueryOperators.EQUAL,
field: "customer_id",
type: FieldType.NUMBER,
value: "1212",
},
{
operator: SearchQueryOperators.ONE_OF,
field: "customer_id",
type: FieldType.NUMBER,
value: "1000,1212,3400",
},
]
expect(buildLuceneQuery(filter)).toEqual({
string: {},
fuzzy: {},
range: {},
equal: {
customer_id: 1212,
},
notEqual: {},
empty: {},
notEmpty: {},
contains: {},
notContains: {},
oneOf: {
customer_id: [1000, 1212, 3400],
},
containsAny: {},
})
})
it("should not parseFloat if the type is a number, but the value is a handlebars binding string", () => {
const filter: SearchFilter[] = [
{
operator: SearchQueryOperators.EQUAL,
field: "customer_id",
type: FieldType.NUMBER,
value: "{{ customer_id }}",
},
{
operator: SearchQueryOperators.ONE_OF,
field: "customer_id",
type: FieldType.NUMBER,
value: "{{ list_of_customer_ids }}",
},
]
expect(buildLuceneQuery(filter)).toEqual({
string: {},
fuzzy: {},
range: {},
equal: {
customer_id: "{{ customer_id }}",
},
notEqual: {},
empty: {},
notEmpty: {},
contains: {},
notContains: {},
oneOf: {
customer_id: "{{ list_of_customer_ids }}",
},
containsAny: {},
})
})
it("should cast string to boolean if the type is boolean", () => {
const filter: SearchFilter[] = [
{
operator: SearchQueryOperators.EQUAL,
field: "a",
type: FieldType.BOOLEAN,
value: "not_true",
},
{
operator: SearchQueryOperators.NOT_EQUAL,
field: "b",
type: FieldType.BOOLEAN,
value: "not_true",
},
{
operator: SearchQueryOperators.EQUAL,
field: "c",
type: FieldType.BOOLEAN,
value: "true",
},
]
expect(buildLuceneQuery(filter)).toEqual({
string: {},
fuzzy: {},
range: {},
equal: {
b: true,
c: true,
},
notEqual: {
a: true,
},
empty: {},
notEmpty: {},
contains: {},
notContains: {},
oneOf: {},
containsAny: {},
})
})
it("should split the string for contains operators", () => {
const filter: SearchFilter[] = [
{
operator: SearchQueryOperators.CONTAINS,
field: "description",
type: FieldType.ARRAY,
value: "Large box,Heavy box,Small box",
},
{
operator: SearchQueryOperators.NOT_CONTAINS,
field: "description",
type: FieldType.ARRAY,
value: "Large box,Heavy box,Small box",
},
{
operator: SearchQueryOperators.CONTAINS_ANY,
field: "description",
type: FieldType.ARRAY,
value: "Large box,Heavy box,Small box",
},
]
expect(buildLuceneQuery(filter)).toEqual({
string: {},
fuzzy: {},
range: {},
equal: {},
notEqual: {},
empty: {},
notEmpty: {},
contains: {
description: ["Large box", "Heavy box", "Small box"],
},
notContains: {
description: ["Large box", "Heavy box", "Small box"],
},
oneOf: {},
containsAny: {
description: ["Large box", "Heavy box", "Small box"],
},
})
})
}) })