Honor query on view search

This commit is contained in:
Adria Navarro 2023-07-18 14:49:03 +02:00
parent ebd93eb109
commit 16d39c6fd2
2 changed files with 49 additions and 16 deletions

View File

@ -156,7 +156,7 @@ export async function searchView(ctx: Ctx<void, SearchResponse>) {
ctx.status = 200 ctx.status = 200
ctx.body = await quotas.addQuery( ctx.body = await quotas.addQuery(
() => sdk.rows.search({ tableId, query: {} }), () => sdk.rows.search({ tableId, query: view.query || {} }),
{ {
datasourceId: tableId, datasourceId: tableId,
} }

View File

@ -15,7 +15,7 @@ import {
Table, Table,
FieldType, FieldType,
} from "@budibase/types" } from "@budibase/types"
import { structures } from "@budibase/backend-core/tests" import { generator, structures } from "@budibase/backend-core/tests"
describe("/rows", () => { describe("/rows", () => {
let request = setup.getRequest() let request = setup.getRequest()
@ -687,29 +687,27 @@ describe("/rows", () => {
}) })
describe("view search", () => { describe("view search", () => {
function priceTable(): Table { function userTable(): Table {
return { return {
name: "table", name: "user",
type: "table", type: "user",
schema: { schema: {
Price: { name: {
type: FieldType.NUMBER,
name: "Price",
constraints: {},
},
Category: {
type: FieldType.STRING, type: FieldType.STRING,
name: "Category", name: "Name",
constraints: { constraints: { type: "string" },
type: "string",
}, },
age: {
type: FieldType.NUMBER,
name: "Age",
constraints: {},
}, },
}, },
} }
} }
it("returns table rows from view", async () => { it("returns table rows from view", async () => {
const table = await config.createTable(priceTable()) const table = await config.createTable(userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await config.createRow({ tableId: table._id }))
@ -724,5 +722,40 @@ describe("/rows", () => {
rows: expect.arrayContaining(rows.map(expect.objectContaining)), rows: expect.arrayContaining(rows.map(expect.objectContaining)),
}) })
}) })
it("searching respects the view filters", async () => {
const table = await config.createTable(userTable())
const expectedRows = []
for (let i = 0; i < 10; i++)
await config.createRow({
tableId: table._id,
name: generator.name(),
age: generator.integer({ min: 10, max: 30 }),
})
for (let i = 0; i < 5; i++)
expectedRows.push(
await config.createRow({
tableId: table._id,
name: generator.name(),
age: 40,
})
)
const createViewResponse = await config.api.viewV2.create({
query: { equal: { age: 40 } },
})
const response = await request
.get(`/api/views/v2/${createViewResponse._id}/search`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(response.body.rows).toHaveLength(5)
expect(response.body).toEqual({
rows: expect.arrayContaining(expectedRows.map(expect.objectContaining)),
})
})
}) })
}) })