Add initial view tests

This commit is contained in:
Adria Navarro 2024-12-18 16:36:20 +01:00
parent 14da90296b
commit 8da96ab271
1 changed files with 98 additions and 21 deletions

View File

@ -4,15 +4,29 @@ import {
RelationshipType, RelationshipType,
SourceName, SourceName,
Table, Table,
ViewV2,
} from "@budibase/types" } from "@budibase/types"
import { buildSqlFieldList } from "../sqlUtils" import { buildSqlFieldList } from "../sqlUtils"
import { structures } from "../../../../routes/tests/utilities" import { structures } from "../../../../routes/tests/utilities"
import { sql } from "@budibase/backend-core" import { sql } from "@budibase/backend-core"
import { generator } from "@budibase/backend-core/tests"
import { generateViewID } from "../../../../../db/utils"
import sdk from "../../../../../sdk"
jest.mock("../../../../../sdk/app/views", () => ({
...jest.requireActual("../../../../../sdk/app/views"),
getTable: jest.fn(),
}))
const getTableMock = sdk.views.getTable as jest.MockedFunction<
typeof sdk.views.getTable
>
describe("buildSqlFieldList", () => { describe("buildSqlFieldList", () => {
let table: Table & { _id: string } let table: Table & { _id: string }
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks()
table = { table = {
...structures.tableForDatasource({ ...structures.tableForDatasource({
type: "datasource", type: "datasource",
@ -96,7 +110,7 @@ describe("buildSqlFieldList", () => {
]) ])
}) })
it("includes relationships fields when flag", async () => { it("includes relationships fields when flagged", async () => {
const otherTable: Table = { const otherTable: Table = {
...table, ...table,
name: "linkedTable", name: "linkedTable",
@ -265,4 +279,67 @@ describe("buildSqlFieldList", () => {
]) ])
}) })
}) })
describe("view", () => {
let view: ViewV2
beforeEach(() => {
getTableMock.mockResolvedValueOnce(table)
view = {
version: 2,
id: generateViewID(table._id),
name: generator.word(),
tableId: table._id,
}
})
it("extracts fields from table schema", async () => {
view.schema = {
name: { visible: false },
amount: { visible: true },
}
const result = await buildSqlFieldList(view, {})
expect(result).toEqual(["table.amount"])
})
it("includes all fields if there is a formula column", async () => {
table.schema.formula = {
name: "formula",
type: FieldType.FORMULA,
formula: "any",
}
view.schema = {
name: { visible: false },
amount: { visible: true },
formula: { visible: true },
}
const result = await buildSqlFieldList(view, {})
expect(result).toEqual([
"table.name",
"table.description",
"table.amount",
])
})
it("does not includes all fields if the formula column is not included", async () => {
table.schema.formula = {
name: "formula",
type: FieldType.FORMULA,
formula: "any",
}
view.schema = {
name: { visible: false },
amount: { visible: true },
formula: { visible: false },
}
const result = await buildSqlFieldList(view, {})
expect(result).toEqual(["table.amount"])
})
})
}) })