This commit is contained in:
Adria Navarro 2023-09-08 15:54:27 +02:00
parent c26a4c3a11
commit b63b61655b
2 changed files with 42 additions and 22 deletions

View File

@ -1094,11 +1094,20 @@ describe.each([
describe("view search", () => { describe("view search", () => {
const viewSchema = { age: { visible: true }, name: { visible: true } } const viewSchema = { age: { visible: true }, name: { visible: true } }
function userTable(): Table { async function userTable(): Promise<Table> {
return { return {
name: "user", name: `users_${generator.guid()}`,
type: "user", type: "table",
primary: ["id"],
schema: { schema: {
id: {
type: FieldType.AUTO,
name: "id",
autocolumn: true,
constraints: {
presence: true,
},
},
name: { name: {
type: FieldType.STRING, type: FieldType.STRING,
name: "name", name: "name",
@ -1110,11 +1119,12 @@ describe.each([
constraints: {}, constraints: {},
}, },
}, },
...(await tableDatasourceConfig()),
} }
} }
it("returns table rows from view", async () => { it.only("returns table rows from view", async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await config.createRow({ tableId: table._id }))
@ -1130,7 +1140,7 @@ describe.each([
}) })
it("searching respects the view filters", async () => { it("searching respects the view filters", async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(await userTable())
const expectedRows = [] const expectedRows = []
for (let i = 0; i < 10; i++) for (let i = 0; i < 10; i++)
await config.createRow({ await config.createRow({
@ -1235,7 +1245,7 @@ describe.each([
it.each(sortTestOptions)( it.each(sortTestOptions)(
"allow sorting (%s)", "allow sorting (%s)",
async (sortParams, expected) => { async (sortParams, expected) => {
await config.createTable(userTable()) await config.createTable(await userTable())
const users = [ const users = [
{ name: "Alice", age: 25 }, { name: "Alice", age: 25 },
{ name: "Bob", age: 30 }, { name: "Bob", age: 30 },
@ -1266,7 +1276,7 @@ describe.each([
it.each(sortTestOptions)( it.each(sortTestOptions)(
"allow override the default view sorting (%s)", "allow override the default view sorting (%s)",
async (sortParams, expected) => { async (sortParams, expected) => {
await config.createTable(userTable()) await config.createTable(await userTable())
const users = [ const users = [
{ name: "Alice", age: 25 }, { name: "Alice", age: 25 },
{ name: "Bob", age: 30 }, { name: "Bob", age: 30 },
@ -1307,7 +1317,7 @@ describe.each([
) )
it("when schema is defined, defined columns and row attributes are returned", async () => { it("when schema is defined, defined columns and row attributes are returned", async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push( rows.push(
@ -1337,7 +1347,7 @@ describe.each([
}) })
it("views without data can be returned", async () => { it("views without data can be returned", async () => {
await config.createTable(userTable()) await config.createTable(await userTable())
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create()
const response = await config.api.viewV2.search(createViewResponse.id) const response = await config.api.viewV2.search(createViewResponse.id)
@ -1346,7 +1356,7 @@ describe.each([
}) })
it("respects the limit parameter", async () => { it("respects the limit parameter", async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await config.createRow({ tableId: table._id }))
@ -1363,7 +1373,7 @@ describe.each([
}) })
it("can handle pagination", async () => { it("can handle pagination", async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await config.createRow({ tableId: table._id }))
@ -1428,7 +1438,7 @@ describe.each([
let tableId: string let tableId: string
beforeAll(async () => { beforeAll(async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await config.createRow({ tableId: table._id }))

View File

@ -51,6 +51,8 @@ import {
UserRoles, UserRoles,
Automation, Automation,
View, View,
FieldType,
RelationshipType,
} from "@budibase/types" } from "@budibase/types"
import API from "./api" import API from "./api"
@ -76,7 +78,6 @@ class TestConfiguration {
globalUserId: any globalUserId: any
userMetadataId: any userMetadataId: any
table?: Table table?: Table
linkedTable: any
automation: any automation: any
datasource?: Datasource datasource?: Datasource
tenantId?: string tenantId?: string
@ -559,25 +560,34 @@ class TestConfiguration {
return this._req(null, { tableId }, controllers.table.find) return this._req(null, { tableId }, controllers.table.find)
} }
async createLinkedTable(relationshipType?: string, links: any = ["link"]) { async createLinkedTable(
config?: Table,
relationshipType = RelationshipType.ONE_TO_MANY,
links: any = ["link"]
) {
if (!this.table) { if (!this.table) {
throw "Must have created a table first." throw "Must have created a table first."
} }
const tableConfig: any = basicTable() const tableConfig = config || basicTable()
tableConfig.primaryDisplay = "name" tableConfig.primaryDisplay = "name"
for (let link of links) { for (let link of links) {
tableConfig.schema[link] = { tableConfig.schema[link] = {
type: "link", type: FieldType.LINK,
fieldName: link, fieldName: link,
tableId: this.table._id, tableId: this.table._id,
name: link, name: link,
} relationshipType,
if (relationshipType) {
tableConfig.schema[link].relationshipType = relationshipType
} }
} }
const linkedTable = await this.createTable(tableConfig)
this.linkedTable = linkedTable if (this.datasource && !tableConfig.sourceId) {
tableConfig.sourceId = this.datasource._id
if (this.datasource.plus) {
tableConfig.type = "external"
}
}
const linkedTable = await this.api.table.create(tableConfig)
return linkedTable return linkedTable
} }