Add initial view tests
This commit is contained in:
parent
14da90296b
commit
8da96ab271
|
@ -4,37 +4,51 @@ 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",
|
||||||
source: SourceName.POSTGRES,
|
source: SourceName.POSTGRES,
|
||||||
}),
|
}),
|
||||||
name: "table",
|
name: "table",
|
||||||
_id: sql.utils.buildExternalTableId("ds_id", "table"),
|
_id: sql.utils.buildExternalTableId("ds_id", "table"),
|
||||||
schema: {
|
schema: {
|
||||||
name: {
|
name: {
|
||||||
name: "name",
|
name: "name",
|
||||||
type: FieldType.STRING,
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
name: "description",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
amount: {
|
||||||
|
name: "amount",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
description: {
|
}
|
||||||
name: "description",
|
|
||||||
type: FieldType.STRING,
|
|
||||||
},
|
|
||||||
amount: {
|
|
||||||
name: "amount",
|
|
||||||
type: FieldType.NUMBER,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("table", () => {
|
describe("table", () => {
|
||||||
|
@ -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"])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue