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 authorized from "../../middleware/authorized"
import { paramResource, paramSubResource } from "../../middleware/resourceId" import { paramResource, paramSubResource } from "../../middleware/resourceId"
import { permissions } from "@budibase/backend-core" import { permissions } from "@budibase/backend-core"
const { internalSearchValidator } = require("./utils/validators") import { internalSearchValidator } from "./utils/validators"
const { PermissionType, PermissionLevel } = permissions const { PermissionType, PermissionLevel } = permissions
const router: Router = new Router() const router: Router = new Router()

View File

@ -5,7 +5,14 @@ import {
} from "../api/routes/public/tests/utils" } from "../api/routes/public/tests/utils"
import * as setup from "../api/routes/tests/utilities" 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" import _ from "lodash"
const config = setup.getConfig() const config = setup.getConfig()
@ -16,7 +23,8 @@ describe("row api - postgres", () => {
let apiKey, let apiKey,
makeRequest: MakeRequestResponse, makeRequest: MakeRequestResponse,
postgresDatasource: Datasource, postgresDatasource: Datasource,
postgresTable: Table postgresTable: Table,
auxPostgresTable: Table
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
@ -61,6 +69,31 @@ describe("row api - postgres", () => {
}, },
sourceId: postgresDatasource._id, 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 () => { afterAll(async () => {
@ -269,6 +302,31 @@ describe("row api - postgres", () => {
expect(res.body).toEqual(expect.objectContaining(rowData)) 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", () => { 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", () => { describe("get all rows", () => {
const getAll = (tableId: string | undefined) => const getAll = (tableId: string | undefined) =>
makeRequest("get", `/api/${tableId}/rows`) makeRequest("get", `/api/${tableId}/rows`)

View File

@ -3,6 +3,12 @@ import { View } from "./view"
import { RenameColumn } from "../../sdk" import { RenameColumn } from "../../sdk"
import { FieldType } from "./row" 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 { export interface FieldSchema {
type: FieldType type: FieldType
externalType?: string externalType?: string
@ -10,7 +16,7 @@ export interface FieldSchema {
name: string name: string
sortable?: boolean sortable?: boolean
tableId?: string tableId?: string
relationshipType?: string relationshipType?: RelationshipTypes
through?: string through?: string
foreignKey?: string foreignKey?: string
icon?: string icon?: string