Add enrich test

This commit is contained in:
Adria Navarro 2023-01-19 20:09:39 +00:00
parent af42e789ff
commit 5fb20abcfd
3 changed files with 101 additions and 4 deletions

View File

@ -3,7 +3,7 @@ import * as rowController from "../controllers/row"
import authorized from "../../middleware/authorized"
import { paramResource, paramSubResource } from "../../middleware/resourceId"
import { permissions } from "@budibase/backend-core"
const { internalSearchValidator } = require("./utils/validators")
import { internalSearchValidator } from "./utils/validators"
const { PermissionType, PermissionLevel } = permissions
const router: Router = new Router()

View File

@ -5,7 +5,14 @@ import {
} from "../api/routes/public/tests/utils"
import * as setup from "../api/routes/tests/utilities"
import { Datasource, FieldType, Row, SourceName, Table } from "@budibase/types"
import {
Datasource,
FieldType,
RelationshipTypes,
Row,
SourceName,
Table,
} from "@budibase/types"
import _ from "lodash"
const config = setup.getConfig()
@ -16,7 +23,8 @@ describe("row api - postgres", () => {
let apiKey,
makeRequest: MakeRequestResponse,
postgresDatasource: Datasource,
postgresTable: Table
postgresTable: Table,
auxPostgresTable: Table
beforeEach(async () => {
await config.init()
@ -61,6 +69,31 @@ describe("row api - postgres", () => {
},
sourceId: postgresDatasource._id,
})
auxPostgresTable = await config.createTable({
name: faker.lorem.word(),
schema: {
title: {
name: "title",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
linkedField: {
type: FieldType.LINK,
constraints: {
type: "array",
presence: true,
},
fieldName: "foreignField",
name: "linkedField",
relationshipType: RelationshipTypes.MANY_TO_MANY,
tableId: postgresTable._id,
},
},
sourceId: postgresDatasource._id,
})
})
afterAll(async () => {
@ -269,6 +302,31 @@ describe("row api - postgres", () => {
expect(res.body).toEqual(expect.objectContaining(rowData))
})
test("given having rows with relation data, only the ids are retrieved", async () => {
let [{ row }] = await populateRows(1)
const foreignRow = await config.createRow({
tableId: auxPostgresTable._id,
title: faker.random.alphaNumeric(10),
linkedField: row._id,
})
const res = await getRow(postgresTable._id, row._id)
expect(res.status).toBe(200)
expect(res.body).toEqual({
...row,
foreignField: [
{
_id: foreignRow._id,
},
],
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
})
})
describe("search for rows", () => {
@ -455,6 +513,39 @@ describe("row api - postgres", () => {
})
})
describe("enrich a row", () => {
const getAll = (tableId: string | undefined, rowId: string | undefined) =>
makeRequest("get", `/api/${tableId}/${rowId}/enrich`)
test("given having rows with relation data, enrich populates the", async () => {
let [{ row }] = await populateRows(1)
const foreignRow = await config.createRow({
tableId: auxPostgresTable._id,
title: faker.random.alphaNumeric(10),
linkedField: row._id,
})
const res = await getAll(postgresTable._id, row._id)
expect(res.status).toBe(200)
expect(res.body).toEqual({
...row,
foreignField: [
{
...foreignRow,
linkedField: [{ _id: row._id }],
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
],
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
})
})
describe("get all rows", () => {
const getAll = (tableId: string | undefined) =>
makeRequest("get", `/api/${tableId}/rows`)

View File

@ -3,6 +3,12 @@ import { View } from "./view"
import { RenameColumn } from "../../sdk"
import { FieldType } from "./row"
export enum RelationshipTypes {
ONE_TO_MANY = "one-to-many",
MANY_TO_ONE = "many-to-one",
MANY_TO_MANY = "many-to-many",
}
export interface FieldSchema {
type: FieldType
externalType?: string
@ -10,7 +16,7 @@ export interface FieldSchema {
name: string
sortable?: boolean
tableId?: string
relationshipType?: string
relationshipType?: RelationshipTypes
through?: string
foreignKey?: string
icon?: string