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

274 lines
6.7 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-19 15:49:46 +02:00
import { generator } 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-08-01 10:45:00 +02:00
schema: {
2023-07-24 11:11:00 +02:00
name: {
2023-08-01 10:45:00 +02:00
name: "name",
type: FieldType.STRING,
2023-07-24 11:11:00 +02:00
visible: true,
},
},
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
})
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 18:26:24 +02:00
const res = await config.api.viewV2.create(newView)
2023-07-12 16:13:00 +02:00
2023-07-19 12:38:01 +02:00
expect(res).toEqual({
...newView,
2023-07-19 18:02:15 +02:00
id: expect.stringMatching(new RegExp(`${config.table?._id!}_`)),
2023-07-19 15:43:21 +02:00
version: 2,
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-08-01 10:45:00 +02:00
query: viewFilters.query,
sort: viewFilters.sort,
2023-07-18 14:34:23 +02:00
}
2023-08-01 10:45:00 +02:00
delete newView.schema
2023-07-19 18:26:24 +02:00
const res = await config.api.viewV2.create(newView)
2023-07-18 14:34:23 +02:00
2023-07-19 12:50:52 +02:00
expect(res).toEqual({
...newView,
2023-08-01 10:45:00 +02:00
query: viewFilters.query,
sort: viewFilters.sort,
2023-07-19 15:43:21 +02:00
id: expect.any(String),
version: 2,
2023-07-18 14:34:23 +02:00
})
})
2023-08-01 10:45:00 +02:00
it("persist schema overrides", async () => {
const newView: CreateViewRequest = {
name: generator.name(),
tableId: config.table!._id!,
schema: {
name: {
name: "name",
type: FieldType.STRING,
visible: true,
},
lastname: {
name: "lastname",
type: FieldType.STRING,
visible: false,
},
},
}
const createdView = await config.api.viewV2.create(newView)
expect(await config.api.viewV2.get(createdView.id)).toEqual({
...newView,
schema: undefined,
columns: ["name", "lastname"],
schemaUI: {
name: {
name: "name",
type: FieldType.STRING,
visible: true,
},
lastname: {
name: "lastname",
type: FieldType.STRING,
visible: false,
},
},
id: createdView.id,
version: 2,
})
})
2023-07-12 16:13:00 +02:00
})
2023-07-12 18:09:13 +02:00
2023-07-25 15:34:25 +02:00
describe("update", () => {
let view: ViewV2
2023-07-25 15:41:04 +02:00
beforeEach(async () => {
2023-07-25 15:34:25 +02:00
await config.createTable(priceTable())
view = await config.api.viewV2.create({ name: "View A" })
})
2023-07-25 15:35:48 +02:00
it("can update an existing view data", async () => {
const tableId = config.table!._id!
await config.api.viewV2.update({
...view,
query: { equal: { newField: "thatValue" } },
})
expect(await config.api.table.get(tableId)).toEqual({
...config.table,
views: {
[view.name]: {
...view,
query: { equal: { newField: "thatValue" } },
schema: expect.anything(),
},
},
_rev: expect.any(String),
updatedAt: expect.any(String),
})
})
2023-07-25 15:34:25 +02:00
it("can update an existing view name", async () => {
const tableId = config.table!._id!
await config.api.viewV2.update({ ...view, name: "View B" })
2023-07-25 15:41:04 +02:00
expect(await config.api.table.get(tableId)).toEqual(
expect.objectContaining({
views: {
"View B": { ...view, name: "View B", schema: expect.anything() },
},
})
)
})
it("cannot update an unexisting views nor edit ids", async () => {
const tableId = config.table!._id!
await config.api.viewV2.update(
{ ...view, id: generator.guid() },
{ expectStatus: 404 }
)
expect(await config.api.table.get(tableId)).toEqual(
expect.objectContaining({
views: {
[view.name]: {
...view,
schema: expect.anything(),
},
},
})
)
})
it("cannot update views with the wrong tableId", async () => {
const tableId = config.table!._id!
await config.api.viewV2.update(
{
...view,
tableId: generator.guid(),
query: { equal: { newField: "thatValue" } },
2023-07-25 15:34:25 +02:00
},
2023-07-25 15:41:04 +02:00
{ expectStatus: 404 }
)
expect(await config.api.table.get(tableId)).toEqual(
expect.objectContaining({
views: {
[view.name]: {
...view,
schema: expect.anything(),
},
},
})
)
2023-07-25 15:34:25 +02:00
})
2023-07-25 15:49:32 +02:00
it("cannot update views v1", async () => {
const viewV1 = await config.createView()
await config.api.viewV2.update(
{
...viewV1,
},
{
expectStatus: 400,
handleResponse: r => {
expect(r.body).toEqual({
message: "Only views V2 can be updated",
status: 400,
})
},
}
)
})
2023-07-25 19:46:46 +02:00
it("cannot update the a view with unmatching ids between url and body", async () => {
const anotherView = await config.api.viewV2.create()
const result = await config
.request!.put(`/api/v2/views/${anotherView.id}`)
.send(view)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(400)
expect(result.body).toEqual({
message: "View id does not match between the body and the uri path",
status: 400,
})
})
2023-07-25 15:34:25 +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:47:45 +02:00
const tableId = config.table!._id!
const getPersistedView = async () =>
(await config.api.table.get(tableId)).views![view.name]
2023-07-12 18:09:13 +02:00
2023-07-19 15:47:45 +02:00
expect(await getPersistedView()).toBeDefined()
2023-07-12 18:09:13 +02:00
2023-07-19 18:02:15 +02:00
await config.api.viewV2.delete(view.id)
2023-07-19 15:47:45 +02:00
expect(await getPersistedView()).toBeUndefined()
2023-07-12 18:09:13 +02:00
})
})
2023-07-12 16:13:00 +02:00
})