budibase/packages/server/src/api/routes/tests/viewV2.spec.ts

138 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-07-12 16:13:00 +02:00
import * as setup from "./utilities"
2023-07-19 15:43:21 +02:00
import {
CreateViewRequest,
FieldType,
SortOrder,
SortType,
Table,
ViewV2,
} from "@budibase/types"
2023-07-12 18:01:46 +02:00
import { generator, structures } from "@budibase/backend-core/tests"
2023-07-12 16:13:00 +02:00
function priceTable(): Table {
return {
name: "table",
type: "table",
schema: {
Price: {
type: FieldType.NUMBER,
name: "Price",
constraints: {},
},
Category: {
type: FieldType.STRING,
name: "Category",
constraints: {
type: "string",
},
},
},
}
}
2023-07-17 22:26:57 +02:00
describe("/v2/views", () => {
2023-07-12 16:13:00 +02:00
const config = setup.getConfig()
2023-07-19 15:43:21 +02:00
const viewFilters: Omit<CreateViewRequest, "name" | "tableId"> = {
2023-07-18 15:03:57 +02:00
query: { allOr: false, equal: { field: "value" } },
sort: {
field: "fieldToSort",
2023-07-18 15:47:37 +02:00
order: SortOrder.DESCENDING,
2023-07-18 15:03:57 +02:00
type: SortType.STRING,
},
2023-07-18 15:47:37 +02:00
columns: ["name"],
2023-07-18 15:03:57 +02:00
}
2023-07-12 16:13:00 +02:00
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
2023-07-18 12:56:24 +02:00
await config.createTable(priceTable())
2023-07-12 16:13:00 +02:00
})
2023-07-12 17:48:11 +02:00
describe("getView", () => {
2023-07-18 09:58:43 +02:00
let view: ViewV2
2023-07-12 17:48:11 +02:00
beforeAll(async () => {
2023-07-19 15:43:21 +02:00
view = await config.api.viewV2.create(config.table?._id, {
2023-07-18 14:34:23 +02:00
query: { allOr: false, notEqual: { field: "value" } },
})
2023-07-12 17:48:11 +02:00
})
2023-07-12 18:01:46 +02:00
it("can fetch the expected view", async () => {
2023-07-19 15:43:21 +02:00
const res = await config.api.viewV2.get(view.id)
2023-07-12 17:48:11 +02:00
expect(res.status).toBe(200)
expect(res.body).toEqual({
2023-07-18 09:58:43 +02:00
data: {
...view,
_id: view._id,
2023-07-19 15:43:21 +02:00
_rev: expect.any(String),
2023-07-18 09:58:43 +02:00
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
2023-07-12 17:48:11 +02:00
})
})
2023-07-12 18:01:46 +02:00
2023-07-13 11:36:26 +02:00
it("will return 404 if the unnexisting id is provided", async () => {
2023-07-19 12:38:01 +02:00
await config.api.viewV2.get(structures.generator.guid(), {
expectStatus: 404,
})
2023-07-12 18:01:46 +02:00
})
2023-07-12 17:48:11 +02:00
})
2023-07-12 16:13:00 +02:00
describe("create", () => {
it("persist the view when the view is successfully created", async () => {
2023-07-19 15:43:21 +02:00
const newView: CreateViewRequest = {
2023-07-18 10:14:13 +02:00
name: generator.name(),
tableId: config.table!._id!,
}
2023-07-19 15:43:21 +02:00
const res = await config.api.viewV2.create(config.table?._id, newView)
2023-07-12 16:13:00 +02:00
2023-07-19 12:38:01 +02:00
expect(res).toEqual({
...newView,
2023-07-19 15:43:21 +02:00
id: expect.any(String),
version: 2,
2023-07-19 12:38:01 +02:00
_id: expect.any(String),
2023-07-19 15:43:21 +02:00
createdAt: expect.any(String),
updatedAt: expect.any(String),
2023-07-12 16:13:00 +02:00
})
})
2023-07-18 14:34:23 +02:00
it("can persist views with queries", async () => {
2023-07-19 15:43:21 +02:00
const newView: CreateViewRequest = {
2023-07-18 14:34:23 +02:00
name: generator.name(),
tableId: config.table!._id!,
2023-07-18 15:03:57 +02:00
...viewFilters,
2023-07-18 14:34:23 +02:00
}
2023-07-19 15:43:21 +02:00
const res = await config.api.viewV2.create(config.table!._id!, newView)
2023-07-18 14:34:23 +02:00
2023-07-19 12:50:52 +02:00
expect(res).toEqual({
...newView,
...viewFilters,
2023-07-19 15:43:21 +02:00
id: expect.any(String),
version: 2,
2023-07-19 12:50:52 +02:00
_id: expect.any(String),
2023-07-19 15:43:21 +02:00
createdAt: expect.any(String),
updatedAt: expect.any(String),
2023-07-18 14:34:23 +02:00
})
})
2023-07-12 16:13:00 +02:00
})
2023-07-12 18:09:13 +02:00
describe("delete", () => {
2023-07-18 09:58:43 +02:00
let view: ViewV2
2023-07-12 18:09:13 +02:00
beforeAll(async () => {
2023-07-18 10:14:13 +02:00
await config.createTable(priceTable())
2023-07-18 10:30:15 +02:00
view = await config.api.viewV2.create()
2023-07-12 18:09:13 +02:00
})
it("can delete an existing view", async () => {
2023-07-19 15:43:21 +02:00
await config.api.viewV2.get(view.id, { expectStatus: 200 })
2023-07-12 18:09:13 +02:00
2023-07-19 15:43:21 +02:00
await config.api.viewV2.delete(config.table?._id!, view.id)
2023-07-12 18:09:13 +02:00
2023-07-19 15:43:21 +02:00
await config.api.viewV2.get(view.id, { expectStatus: 404 })
2023-07-12 18:09:13 +02:00
})
})
2023-07-12 16:13:00 +02:00
})