Test sorting
This commit is contained in:
parent
fde008f4d1
commit
b778921028
|
@ -156,7 +156,14 @@ 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: view.query || {} }),
|
() =>
|
||||||
|
sdk.rows.search({
|
||||||
|
tableId,
|
||||||
|
query: view.query || {},
|
||||||
|
sort: view.sort?.field,
|
||||||
|
sortOrder: view.sort?.order,
|
||||||
|
sortType: view.sort?.type,
|
||||||
|
}),
|
||||||
{
|
{
|
||||||
datasourceId: tableId,
|
datasourceId: tableId,
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,8 @@ import {
|
||||||
Row,
|
Row,
|
||||||
Table,
|
Table,
|
||||||
FieldType,
|
FieldType,
|
||||||
|
SortType,
|
||||||
|
SortOrder,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { generator, structures } from "@budibase/backend-core/tests"
|
import { generator, structures } from "@budibase/backend-core/tests"
|
||||||
|
|
||||||
|
@ -694,12 +696,12 @@ describe("/rows", () => {
|
||||||
schema: {
|
schema: {
|
||||||
name: {
|
name: {
|
||||||
type: FieldType.STRING,
|
type: FieldType.STRING,
|
||||||
name: "Name",
|
name: "name",
|
||||||
constraints: { type: "string" },
|
constraints: { type: "string" },
|
||||||
},
|
},
|
||||||
age: {
|
age: {
|
||||||
type: FieldType.NUMBER,
|
type: FieldType.NUMBER,
|
||||||
name: "Age",
|
name: "age",
|
||||||
constraints: {},
|
constraints: {},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -757,5 +759,66 @@ describe("/rows", () => {
|
||||||
rows: expect.arrayContaining(expectedRows.map(expect.objectContaining)),
|
rows: expect.arrayContaining(expectedRows.map(expect.objectContaining)),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[
|
||||||
|
{
|
||||||
|
field: "name",
|
||||||
|
order: SortOrder.ASCENDING,
|
||||||
|
type: SortType.STRING,
|
||||||
|
},
|
||||||
|
["Alice", "Bob", "Charly", "Danny"],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
field: "name",
|
||||||
|
},
|
||||||
|
["Alice", "Bob", "Charly", "Danny"],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
field: "name",
|
||||||
|
order: SortOrder.DESCENDING,
|
||||||
|
},
|
||||||
|
["Danny", "Charly", "Bob", "Alice"],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
field: "name",
|
||||||
|
order: SortOrder.DESCENDING,
|
||||||
|
type: SortType.STRING,
|
||||||
|
},
|
||||||
|
["Danny", "Charly", "Bob", "Alice"],
|
||||||
|
],
|
||||||
|
])("allow sorting", async (sortParams, expected) => {
|
||||||
|
await config.createTable(userTable())
|
||||||
|
const users = [
|
||||||
|
{ name: "Alice", age: 25 },
|
||||||
|
{ name: "Bob", age: 30 },
|
||||||
|
{ name: "Charly", age: 27 },
|
||||||
|
{ name: "Danny", age: 15 },
|
||||||
|
]
|
||||||
|
for (const user of users) {
|
||||||
|
await config.createRow({
|
||||||
|
tableId: config.table!._id,
|
||||||
|
...user,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const createViewResponse = await config.api.viewV2.create({
|
||||||
|
sort: sortParams,
|
||||||
|
})
|
||||||
|
|
||||||
|
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(4)
|
||||||
|
expect(response.body).toEqual({
|
||||||
|
rows: expected.map(name => expect.objectContaining({ name })),
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { SearchFilters } from "@budibase/types"
|
import { SearchFilters, SortType } from "@budibase/types"
|
||||||
import { isExternalTable } from "../../../integrations/utils"
|
import { isExternalTable } from "../../../integrations/utils"
|
||||||
import * as internal from "./search/internal"
|
import * as internal from "./search/internal"
|
||||||
import * as external from "./search/external"
|
import * as external from "./search/external"
|
||||||
|
@ -11,8 +11,8 @@ export interface SearchParams {
|
||||||
bookmark?: string
|
bookmark?: string
|
||||||
limit?: number
|
limit?: number
|
||||||
sort?: string
|
sort?: string
|
||||||
sortOrder?: string
|
sortOrder?: "ascending" | "descending"
|
||||||
sortType?: string
|
sortType?: SortType
|
||||||
version?: string
|
version?: string
|
||||||
disableEscaping?: boolean
|
disableEscaping?: boolean
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { SortType } from "../../api"
|
import { SortOrder, SortType } from "../../api"
|
||||||
import { SearchFilters, SortDirection } from "../../sdk"
|
import { SearchFilters } from "../../sdk"
|
||||||
import { Document } from "../document"
|
import { Document } from "../document"
|
||||||
|
|
||||||
export interface View {
|
export interface View {
|
||||||
|
@ -20,7 +20,7 @@ export interface ViewV2 extends Document {
|
||||||
query?: SearchFilters
|
query?: SearchFilters
|
||||||
sort?: {
|
sort?: {
|
||||||
field: string
|
field: string
|
||||||
order?: SortDirection
|
order?: SortOrder
|
||||||
type?: SortType
|
type?: SortType
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue