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.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,
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ import {
|
|||
Row,
|
||||
Table,
|
||||
FieldType,
|
||||
SortType,
|
||||
SortOrder,
|
||||
} from "@budibase/types"
|
||||
import { generator, structures } from "@budibase/backend-core/tests"
|
||||
|
||||
|
@ -694,12 +696,12 @@ describe("/rows", () => {
|
|||
schema: {
|
||||
name: {
|
||||
type: FieldType.STRING,
|
||||
name: "Name",
|
||||
name: "name",
|
||||
constraints: { type: "string" },
|
||||
},
|
||||
age: {
|
||||
type: FieldType.NUMBER,
|
||||
name: "Age",
|
||||
name: "age",
|
||||
constraints: {},
|
||||
},
|
||||
},
|
||||
|
@ -757,5 +759,66 @@ describe("/rows", () => {
|
|||
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 * as internal from "./search/internal"
|
||||
import * as external from "./search/external"
|
||||
|
@ -11,8 +11,8 @@ export interface SearchParams {
|
|||
bookmark?: string
|
||||
limit?: number
|
||||
sort?: string
|
||||
sortOrder?: string
|
||||
sortType?: string
|
||||
sortOrder?: "ascending" | "descending"
|
||||
sortType?: SortType
|
||||
version?: string
|
||||
disableEscaping?: boolean
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { SortType } from "../../api"
|
||||
import { SearchFilters, SortDirection } from "../../sdk"
|
||||
import { SortOrder, SortType } from "../../api"
|
||||
import { SearchFilters } from "../../sdk"
|
||||
import { Document } from "../document"
|
||||
|
||||
export interface View {
|
||||
|
@ -20,7 +20,7 @@ export interface ViewV2 extends Document {
|
|||
query?: SearchFilters
|
||||
sort?: {
|
||||
field: string
|
||||
order?: SortDirection
|
||||
order?: SortOrder
|
||||
type?: SortType
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue