More tests.

This commit is contained in:
Sam Rose 2024-10-18 17:50:08 +01:00
parent 4b6a66627a
commit 80875fd9a0
No known key found for this signature in database
3 changed files with 133 additions and 18 deletions

View File

@ -1602,7 +1602,7 @@ describe.each([
}) })
}) })
describe.each([FieldType.ARRAY, FieldType.OPTIONS])("%s", () => { describe("arrays", () => {
beforeAll(async () => { beforeAll(async () => {
tableOrViewId = await createTableOrView({ tableOrViewId = await createTableOrView({
numbers: { numbers: {
@ -2192,7 +2192,7 @@ describe.each([
}) })
describe("contains", () => { describe("contains", () => {
it("successfully finds a row", async () => { it.only("successfully finds a row", async () => {
await expectQuery({ await expectQuery({
contains: { users: [user1._id] }, contains: { users: [user1._id] },
}).toContainExactly([ }).toContainExactly([

View File

@ -33,6 +33,7 @@ import {
UISearchFilter, UISearchFilter,
LegacyFilter, LegacyFilter,
SearchViewRowRequest, SearchViewRowRequest,
ArrayOperator,
} from "@budibase/types" } from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests" import { generator, mocks } from "@budibase/backend-core/tests"
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
@ -3686,11 +3687,6 @@ describe.each([
number: { name: "number", type: FieldType.NUMBER }, number: { name: "number", type: FieldType.NUMBER },
bigint: { name: "bigint", type: FieldType.BIGINT }, bigint: { name: "bigint", type: FieldType.BIGINT },
datetime: { name: "datetime", type: FieldType.DATETIME }, datetime: { name: "datetime", type: FieldType.DATETIME },
timeOnly: {
name: "timeOnly",
type: FieldType.DATETIME,
timeOnly: true,
},
boolean: { name: "boolean", type: FieldType.BOOLEAN }, boolean: { name: "boolean", type: FieldType.BOOLEAN },
user: { user: {
name: "user", name: "user",
@ -3701,6 +3697,9 @@ describe.each([
name: "users", name: "users",
type: FieldType.BB_REFERENCE, type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER, subtype: BBReferenceFieldSubType.USER,
constraints: {
type: JsonFieldSubType.ARRAY,
},
}, },
}, },
}) })
@ -3709,9 +3708,9 @@ describe.each([
interface TestCase { interface TestCase {
name: string name: string
query: UISearchFilter query: UISearchFilter | (() => UISearchFilter)
insert: Row[] insert: Row[] | (() => Row[])
expected: Row[] expected: Row[] | (() => Row[])
searchOpts?: Partial<SearchViewRowRequest> searchOpts?: Partial<SearchViewRowRequest>
} }
@ -3855,11 +3854,130 @@ describe.each([
), ),
expected: [{ number: 2 }], expected: [{ number: 2 }],
}, },
{
name: "can find longform values",
insert: [{ longform: "foo" }, { longform: "bar" }],
query: simpleQuery({
operator: BasicOperator.EQUAL,
field: "longform",
value: "foo",
}),
expected: [{ longform: "foo" }],
},
{
name: "can find options values",
insert: [{ options: "a" }, { options: "b" }],
query: simpleQuery({
operator: BasicOperator.EQUAL,
field: "options",
value: "a",
}),
expected: [{ options: "a" }],
},
{
name: "can find array values",
insert: [
// Number field here is just to guarantee order.
{ number: 1, array: ["a"] },
{ number: 2, array: ["b"] },
{ number: 3, array: ["a", "c"] },
],
query: simpleQuery({
operator: ArrayOperator.CONTAINS,
field: "array",
value: "a",
}),
searchOpts: {
sort: "number",
sortOrder: SortOrder.ASCENDING,
},
expected: [{ array: ["a"] }, { array: ["a", "c"] }],
},
{
name: "can find bigint values",
insert: [{ bigint: "1" }, { bigint: "2" }],
query: simpleQuery({
operator: BasicOperator.EQUAL,
field: "bigint",
type: FieldType.BIGINT,
value: "1",
}),
expected: [{ bigint: "1" }],
},
{
name: "can find datetime values",
insert: [
{ datetime: "2021-01-01T00:00:00.000Z" },
{ datetime: "2021-01-02T00:00:00.000Z" },
],
query: simpleQuery({
operator: BasicOperator.EQUAL,
field: "datetime",
type: FieldType.DATETIME,
value: "2021-01-01",
}),
expected: [{ datetime: "2021-01-01T00:00:00.000Z" }],
},
{
name: "can find boolean values",
insert: [{ boolean: true }, { boolean: false }],
query: simpleQuery({
operator: BasicOperator.EQUAL,
field: "boolean",
value: true,
}),
expected: [{ boolean: true }],
},
{
name: "can find user values",
insert: () => [{ user: config.getUser() }],
query: () =>
simpleQuery({
operator: BasicOperator.EQUAL,
field: "user",
value: config.getUser()._id,
}),
expected: () => [
{
user: expect.objectContaining({ _id: config.getUser()._id }),
},
],
},
{
name: "can find users values",
insert: () => [{ users: [config.getUser()] }],
query: () =>
simpleQuery({
operator: ArrayOperator.CONTAINS,
field: "users",
value: [config.getUser()._id],
}),
expected: () => [
{
users: [
expect.objectContaining({ _id: config.getUser()._id }),
],
},
],
},
] ]
it.only.each(testCases)( it.each(testCases)(
"$name", "$name",
async ({ query, insert, expected, searchOpts }) => { async ({ query, insert, expected, searchOpts }) => {
// Some values can't be specified outside of a test (e.g. getting
// config.getUser(), it won't be initialised), so we use functions
// in those cases.
if (typeof insert === "function") {
insert = insert()
}
if (typeof expected === "function") {
expected = expected()
}
if (typeof query === "function") {
query = query()
}
await config.api.row.bulkImport(table._id!, { rows: insert }) await config.api.row.bulkImport(table._id!, { rows: insert })
const view = await config.api.viewV2.create({ const view = await config.api.viewV2.create({
@ -3874,7 +3992,6 @@ describe.each([
number: { visible: true }, number: { visible: true },
bigint: { visible: true }, bigint: { visible: true },
datetime: { visible: true }, datetime: { visible: true },
timeOnly: { visible: true },
boolean: { visible: true }, boolean: { visible: true },
user: { visible: true }, user: { visible: true },
users: { visible: true }, users: { visible: true },

View File

@ -339,11 +339,7 @@ function buildCondition(filter?: SearchFilter): SearchFilters | undefined {
if (!value) { if (!value) {
return return
} }
try {
value = new Date(value).toISOString() value = new Date(value).toISOString()
} catch (error) {
return
}
} }
break break
case FieldType.NUMBER: case FieldType.NUMBER:
@ -490,7 +486,9 @@ export function buildQuery(
query.onEmptyFilter = onEmptyFilter query.onEmptyFilter = onEmptyFilter
} }
const operator = allOr ? LogicalOperator.OR : LogicalOperator.AND const operator = allOr ? LogicalOperator.OR : LogicalOperator.AND
return { [operator]: { conditions: filters.map(buildCondition) } } return {
[operator]: { conditions: filters.map(buildCondition).filter(f => f) },
}
}), }),
} }