Migrate some of RowAPI, WIP

This commit is contained in:
Sam Rose 2024-03-01 15:20:07 +00:00
parent 16e9c5ff4e
commit 5d42804020
No known key found for this signature in database
12 changed files with 78 additions and 100 deletions

View File

@ -211,7 +211,7 @@ export async function validate(ctx: Ctx<Row, ValidateResponse>) {
} }
} }
export async function fetchEnrichedRow(ctx: any) { export async function fetchEnrichedRow(ctx: UserCtx<void, Row>) {
const tableId = utils.getTableId(ctx) const tableId = utils.getTableId(ctx)
ctx.body = await pickApi(tableId).fetchEnrichedRow(ctx) ctx.body = await pickApi(tableId).fetchEnrichedRow(ctx)
} }

View File

@ -106,9 +106,6 @@ describe.each([
mocks.licenses.useCloudFree() mocks.licenses.useCloudFree()
}) })
const loadRow = (id: string, tbl_Id: string, status = 200) =>
config.api.row.get(tbl_Id, id, { expectStatus: status })
const getRowUsage = async () => { const getRowUsage = async () => {
const { total } = await config.doInContext(undefined, () => const { total } = await config.doInContext(undefined, () =>
quotas.getCurrentUsageValues(QuotaUsageType.STATIC, StaticQuotaName.ROWS) quotas.getCurrentUsageValues(QuotaUsageType.STATIC, StaticQuotaName.ROWS)
@ -235,7 +232,7 @@ describe.each([
const res = await config.api.row.get(tableId, existing._id!) const res = await config.api.row.get(tableId, existing._id!)
expect(res.body).toEqual({ expect(res).toEqual({
...existing, ...existing,
...defaultRowFields, ...defaultRowFields,
}) })
@ -265,7 +262,7 @@ describe.each([
await config.createRow() await config.createRow()
await config.api.row.get(tableId, "1234567", { await config.api.row.get(tableId, "1234567", {
expectStatus: 404, status: 404,
}) })
}) })
@ -395,7 +392,7 @@ describe.each([
const createdRow = await config.createRow(row) const createdRow = await config.createRow(row)
const id = createdRow._id! const id = createdRow._id!
const saved = (await loadRow(id, table._id!)).body const saved = await config.api.row.get(id, table._id!)
expect(saved.stringUndefined).toBe(undefined) expect(saved.stringUndefined).toBe(undefined)
expect(saved.stringNull).toBe(null) expect(saved.stringNull).toBe(null)
@ -476,8 +473,8 @@ describe.each([
) )
const row = await config.api.row.get(table._id!, createRowResponse._id!) const row = await config.api.row.get(table._id!, createRowResponse._id!)
expect(row.body.Story).toBeUndefined() expect(row.Story).toBeUndefined()
expect(row.body).toEqual({ expect(row).toEqual({
...defaultRowFields, ...defaultRowFields,
OrderID: 1111, OrderID: 1111,
Country: "Aussy", Country: "Aussy",
@ -524,10 +521,10 @@ describe.each([
expect(row.name).toEqual("Updated Name") expect(row.name).toEqual("Updated Name")
expect(row.description).toEqual(existing.description) expect(row.description).toEqual(existing.description)
const savedRow = await loadRow(row._id!, table._id!) const savedRow = await config.api.row.get(row._id!, table._id!)
expect(savedRow.body.description).toEqual(existing.description) expect(savedRow.description).toEqual(existing.description)
expect(savedRow.body.name).toEqual("Updated Name") expect(savedRow.name).toEqual("Updated Name")
await assertRowUsage(rowUsage) await assertRowUsage(rowUsage)
}) })
@ -582,8 +579,8 @@ describe.each([
}) })
let getResp = await config.api.row.get(table._id!, row._id!) let getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.body.user1[0]._id).toEqual(user1._id) expect(getResp.user1[0]._id).toEqual(user1._id)
expect(getResp.body.user2[0]._id).toEqual(user2._id) expect(getResp.user2[0]._id).toEqual(user2._id)
let patchResp = await config.api.row.patch(table._id!, { let patchResp = await config.api.row.patch(table._id!, {
_id: row._id!, _id: row._id!,
@ -595,8 +592,8 @@ describe.each([
expect(patchResp.user2[0]._id).toEqual(user2._id) expect(patchResp.user2[0]._id).toEqual(user2._id)
getResp = await config.api.row.get(table._id!, row._id!) getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.body.user1[0]._id).toEqual(user2._id) expect(getResp.user1[0]._id).toEqual(user2._id)
expect(getResp.body.user2[0]._id).toEqual(user2._id) expect(getResp.user2[0]._id).toEqual(user2._id)
}) })
it("should be able to update relationships when both columns are same name", async () => { it("should be able to update relationships when both columns are same name", async () => {
@ -609,7 +606,7 @@ describe.each([
description: "test", description: "test",
relationship: [row._id], relationship: [row._id],
}) })
row = (await config.api.row.get(table._id!, row._id!)).body row = await config.api.row.get(table._id!, row._id!)
expect(row.relationship.length).toBe(1) expect(row.relationship.length).toBe(1)
const resp = await config.api.row.patch(table._id!, { const resp = await config.api.row.patch(table._id!, {
_id: row._id!, _id: row._id!,
@ -685,7 +682,7 @@ describe.each([
const res = await config.api.row.delete(table._id!, [row1, row2]) const res = await config.api.row.delete(table._id!, [row1, row2])
expect(res.body.length).toEqual(2) expect(res.body.length).toEqual(2)
await loadRow(row1._id!, table._id!, 404) await config.api.row.get(row1._id!, table._id!, { status: 404 })
await assertRowUsage(rowUsage - 2) await assertRowUsage(rowUsage - 2)
}) })
@ -704,7 +701,7 @@ describe.each([
]) ])
expect(res.body.length).toEqual(3) expect(res.body.length).toEqual(3)
await loadRow(row1._id!, table._id!, 404) await config.api.row.get(row1._id!, table._id!, { status: 404 })
await assertRowUsage(rowUsage - 3) await assertRowUsage(rowUsage - 3)
}) })
@ -715,7 +712,7 @@ describe.each([
const res = await config.api.row.delete(table._id!, row1) const res = await config.api.row.delete(table._id!, row1)
expect(res.body.id).toEqual(row1._id) expect(res.body.id).toEqual(row1._id)
await loadRow(row1._id!, table._id!, 404) await config.api.row.get(row1._id!, table._id!, { status: 404 })
await assertRowUsage(rowUsage - 1) await assertRowUsage(rowUsage - 1)
}) })
@ -841,8 +838,8 @@ describe.each([
linkedTable._id!, linkedTable._id!,
secondRow._id! secondRow._id!
) )
expect(resBasic.body.link.length).toBe(1) expect(resBasic.link.length).toBe(1)
expect(resBasic.body.link[0]).toEqual({ expect(resBasic.link[0]).toEqual({
_id: firstRow._id, _id: firstRow._id,
primaryDisplay: firstRow.name, primaryDisplay: firstRow.name,
}) })
@ -852,10 +849,10 @@ describe.each([
linkedTable._id!, linkedTable._id!,
secondRow._id! secondRow._id!
) )
expect(resEnriched.body.link.length).toBe(1) expect(resEnriched.link.length).toBe(1)
expect(resEnriched.body.link[0]._id).toBe(firstRow._id) expect(resEnriched.link[0]._id).toBe(firstRow._id)
expect(resEnriched.body.link[0].name).toBe("Test Contact") expect(resEnriched.link[0].name).toBe("Test Contact")
expect(resEnriched.body.link[0].description).toBe("original description") expect(resEnriched.link[0].description).toBe("original description")
await assertRowUsage(rowUsage) await assertRowUsage(rowUsage)
}) })
}) })
@ -1000,7 +997,7 @@ describe.each([
}) })
const row = await config.api.row.get(table._id!, newRow._id!) const row = await config.api.row.get(table._id!, newRow._id!)
expect(row.body).toEqual({ expect(row).toEqual({
name: data.name, name: data.name,
surname: data.surname, surname: data.surname,
address: data.address, address: data.address,
@ -1010,9 +1007,9 @@ describe.each([
id: newRow.id, id: newRow.id,
...defaultRowFields, ...defaultRowFields,
}) })
expect(row.body._viewId).toBeUndefined() expect(row._viewId).toBeUndefined()
expect(row.body.age).toBeUndefined() expect(row.age).toBeUndefined()
expect(row.body.jobTitle).toBeUndefined() expect(row.jobTitle).toBeUndefined()
}) })
}) })
@ -1042,7 +1039,7 @@ describe.each([
}) })
const row = await config.api.row.get(tableId, newRow._id!) const row = await config.api.row.get(tableId, newRow._id!)
expect(row.body).toEqual({ expect(row).toEqual({
...newRow, ...newRow,
name: newData.name, name: newData.name,
address: newData.address, address: newData.address,
@ -1051,9 +1048,9 @@ describe.each([
id: newRow.id, id: newRow.id,
...defaultRowFields, ...defaultRowFields,
}) })
expect(row.body._viewId).toBeUndefined() expect(row._viewId).toBeUndefined()
expect(row.body.age).toBeUndefined() expect(row.age).toBeUndefined()
expect(row.body.jobTitle).toBeUndefined() expect(row.jobTitle).toBeUndefined()
}) })
}) })
@ -1076,7 +1073,7 @@ describe.each([
await assertRowUsage(rowUsage - 1) await assertRowUsage(rowUsage - 1)
await config.api.row.get(tableId, createdRow._id!, { await config.api.row.get(tableId, createdRow._id!, {
expectStatus: 404, status: 404,
}) })
}) })
@ -1102,12 +1099,12 @@ describe.each([
await assertRowUsage(rowUsage - 2) await assertRowUsage(rowUsage - 2)
await config.api.row.get(tableId, rows[0]._id!, { await config.api.row.get(tableId, rows[0]._id!, {
expectStatus: 404, status: 404,
}) })
await config.api.row.get(tableId, rows[2]._id!, { await config.api.row.get(tableId, rows[2]._id!, {
expectStatus: 404, status: 404,
}) })
await config.api.row.get(tableId, rows[1]._id!, { expectStatus: 200 }) await config.api.row.get(tableId, rows[1]._id!, { status: 200 })
}) })
}) })
@ -1754,7 +1751,7 @@ describe.each([
} }
const row = await config.api.row.save(tableId, rowData) const row = await config.api.row.save(tableId, rowData)
const { body: retrieved } = await config.api.row.get(tableId, row._id!) const retrieved = await config.api.row.get(tableId, row._id!)
expect(retrieved).toEqual({ expect(retrieved).toEqual({
name: rowData.name, name: rowData.name,
description: rowData.description, description: rowData.description,
@ -1781,7 +1778,7 @@ describe.each([
} }
const row = await config.api.row.save(tableId, rowData) const row = await config.api.row.save(tableId, rowData)
const { body: retrieved } = await config.api.row.get(tableId, row._id!) const retrieved = await config.api.row.get(tableId, row._id!)
expect(retrieved).toEqual({ expect(retrieved).toEqual({
name: rowData.name, name: rowData.name,
description: rowData.description, description: rowData.description,

View File

@ -663,8 +663,7 @@ describe("/tables", () => {
expect(migratedTable.schema["user column"]).toBeDefined() expect(migratedTable.schema["user column"]).toBeDefined()
expect(migratedTable.schema["user relationship"]).not.toBeDefined() expect(migratedTable.schema["user relationship"]).not.toBeDefined()
const resp = await config.api.row.get(table._id!, testRow._id!) const migratedRow = await config.api.row.get(table._id!, testRow._id!)
const migratedRow = resp.body as Row
expect(migratedRow["user column"]).toBeDefined() expect(migratedRow["user column"]).toBeDefined()
expect(migratedRow["user relationship"]).not.toBeDefined() expect(migratedRow["user relationship"]).not.toBeDefined()
@ -716,15 +715,13 @@ describe("/tables", () => {
expect(migratedTable.schema["user column"]).toBeDefined() expect(migratedTable.schema["user column"]).toBeDefined()
expect(migratedTable.schema["user relationship"]).not.toBeDefined() expect(migratedTable.schema["user relationship"]).not.toBeDefined()
const row1Migrated = (await config.api.row.get(table._id!, row1._id!)) const row1Migrated = await config.api.row.get(table._id!, row1._id!)
.body as Row
expect(row1Migrated["user relationship"]).not.toBeDefined() expect(row1Migrated["user relationship"]).not.toBeDefined()
expect(row1Migrated["user column"].map((r: Row) => r._id)).toEqual( expect(row1Migrated["user column"].map((r: Row) => r._id)).toEqual(
expect.arrayContaining([users[0]._id, users[1]._id]) expect.arrayContaining([users[0]._id, users[1]._id])
) )
const row2Migrated = (await config.api.row.get(table._id!, row2._id!)) const row2Migrated = await config.api.row.get(table._id!, row2._id!)
.body as Row
expect(row2Migrated["user relationship"]).not.toBeDefined() expect(row2Migrated["user relationship"]).not.toBeDefined()
expect(row2Migrated["user column"].map((r: Row) => r._id)).toEqual( expect(row2Migrated["user column"].map((r: Row) => r._id)).toEqual(
expect.arrayContaining([users[1]._id, users[2]._id]) expect.arrayContaining([users[1]._id, users[2]._id])
@ -773,15 +770,13 @@ describe("/tables", () => {
expect(migratedTable.schema["user column"]).toBeDefined() expect(migratedTable.schema["user column"]).toBeDefined()
expect(migratedTable.schema["user relationship"]).not.toBeDefined() expect(migratedTable.schema["user relationship"]).not.toBeDefined()
const row1Migrated = (await config.api.row.get(table._id!, row1._id!)) const row1Migrated = await config.api.row.get(table._id!, row1._id!)
.body as Row
expect(row1Migrated["user relationship"]).not.toBeDefined() expect(row1Migrated["user relationship"]).not.toBeDefined()
expect(row1Migrated["user column"].map((r: Row) => r._id)).toEqual( expect(row1Migrated["user column"].map((r: Row) => r._id)).toEqual(
expect.arrayContaining([users[0]._id, users[1]._id]) expect.arrayContaining([users[0]._id, users[1]._id])
) )
const row2Migrated = (await config.api.row.get(table._id!, row2._id!)) const row2Migrated = await config.api.row.get(table._id!, row2._id!)
.body as Row
expect(row2Migrated["user relationship"]).not.toBeDefined() expect(row2Migrated["user relationship"]).not.toBeDefined()
expect(row2Migrated["user column"].map((r: Row) => r._id)).toEqual([ expect(row2Migrated["user column"].map((r: Row) => r._id)).toEqual([
users[2]._id, users[2]._id,

View File

@ -24,7 +24,7 @@ describe("test the create row action", () => {
expect(res.id).toBeDefined() expect(res.id).toBeDefined()
expect(res.revision).toBeDefined() expect(res.revision).toBeDefined()
expect(res.success).toEqual(true) expect(res.success).toEqual(true)
const gottenRow = await config.getRow(table._id, res.id) const gottenRow = await config.api.row.get(table._id, res.id)
expect(gottenRow.name).toEqual("test") expect(gottenRow.name).toEqual("test")
expect(gottenRow.description).toEqual("test") expect(gottenRow.description).toEqual("test")
}) })

View File

@ -36,7 +36,7 @@ describe("test the update row action", () => {
it("should be able to run the action", async () => { it("should be able to run the action", async () => {
const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, inputs) const res = await setup.runStep(setup.actions.UPDATE_ROW.stepId, inputs)
expect(res.success).toEqual(true) expect(res.success).toEqual(true)
const updatedRow = await config.getRow(table._id!, res.id) const updatedRow = await config.api.row.get(table._id!, res.id)
expect(updatedRow.name).toEqual("Updated name") expect(updatedRow.name).toEqual("Updated name")
expect(updatedRow.description).not.toEqual("") expect(updatedRow.description).not.toEqual("")
}) })
@ -87,8 +87,8 @@ describe("test the update row action", () => {
}) })
let getResp = await config.api.row.get(table._id!, row._id!) let getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.body.user1[0]._id).toEqual(user1._id) expect(getResp.user1[0]._id).toEqual(user1._id)
expect(getResp.body.user2[0]._id).toEqual(user2._id) expect(getResp.user2[0]._id).toEqual(user2._id)
let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, {
rowId: row._id, rowId: row._id,
@ -103,8 +103,8 @@ describe("test the update row action", () => {
expect(stepResp.success).toEqual(true) expect(stepResp.success).toEqual(true)
getResp = await config.api.row.get(table._id!, row._id!) getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.body.user1[0]._id).toEqual(user2._id) expect(getResp.user1[0]._id).toEqual(user2._id)
expect(getResp.body.user2[0]._id).toEqual(user2._id) expect(getResp.user2[0]._id).toEqual(user2._id)
}) })
it("should overwrite links if those links are not set and we ask it do", async () => { it("should overwrite links if those links are not set and we ask it do", async () => {
@ -140,8 +140,8 @@ describe("test the update row action", () => {
}) })
let getResp = await config.api.row.get(table._id!, row._id!) let getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.body.user1[0]._id).toEqual(user1._id) expect(getResp.user1[0]._id).toEqual(user1._id)
expect(getResp.body.user2[0]._id).toEqual(user2._id) expect(getResp.user2[0]._id).toEqual(user2._id)
let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, { let stepResp = await setup.runStep(setup.actions.UPDATE_ROW.stepId, {
rowId: row._id, rowId: row._id,
@ -163,7 +163,7 @@ describe("test the update row action", () => {
expect(stepResp.success).toEqual(true) expect(stepResp.success).toEqual(true)
getResp = await config.api.row.get(table._id!, row._id!) getResp = await config.api.row.get(table._id!, row._id!)
expect(getResp.body.user1[0]._id).toEqual(user2._id) expect(getResp.user1[0]._id).toEqual(user2._id)
expect(getResp.body.user2).toBeUndefined() expect(getResp.user2).toBeUndefined()
}) })
}) })

View File

@ -100,7 +100,7 @@ describe("test the link controller", () => {
const { _id } = await config.createRow( const { _id } = await config.createRow(
basicLinkedRow(t1._id!, row._id!, linkField) basicLinkedRow(t1._id!, row._id!, linkField)
) )
return config.getRow(t1._id!, _id!) return config.api.row.get(t1._id!, _id!)
} }
it("should be able to confirm if two table schemas are equal", async () => { it("should be able to confirm if two table schemas are equal", async () => {

View File

@ -398,7 +398,7 @@ describe("postgres integrations", () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.body).toEqual(updatedRow) expect(res.body).toEqual(updatedRow)
const persistedRow = await config.getRow( const persistedRow = await config.api.row.get(
primaryPostgresTable._id!, primaryPostgresTable._id!,
row.id row.id
) )

View File

@ -98,7 +98,10 @@ describe("sdk >> rows >> internal", () => {
}, },
}) })
const persistedRow = await config.getRow(table._id!, response.row._id!) const persistedRow = await config.api.row.get(
table._id!,
response.row._id!
)
expect(persistedRow).toEqual({ expect(persistedRow).toEqual({
...row, ...row,
type: "row", type: "row",
@ -157,7 +160,10 @@ describe("sdk >> rows >> internal", () => {
}, },
}) })
const persistedRow = await config.getRow(table._id!, response.row._id!) const persistedRow = await config.api.row.get(
table._id!,
response.row._id!
)
expect(persistedRow).toEqual({ expect(persistedRow).toEqual({
...row, ...row,
type: "row", type: "row",

View File

@ -712,11 +712,6 @@ export default class TestConfiguration {
return this.api.row.save(tableId, config) return this.api.row.save(tableId, config)
} }
async getRow(tableId: string, rowId: string): Promise<Row> {
const res = await this.api.row.get(tableId, rowId)
return res.body
}
async getRows(tableId: string) { async getRows(tableId: string) {
if (!tableId && this.table) { if (!tableId && this.table) {
tableId = this.table._id! tableId = this.table._id!

View File

@ -3,9 +3,6 @@ import { SuperTest, Test, Response } from "supertest"
import { ReadStream } from "fs" import { ReadStream } from "fs"
type Headers = Record<string, string | string[] | undefined> type Headers = Record<string, string | string[] | undefined>
type SuccessStatus = 200 | 201 | 204
type ErrorStatus = 400 | 401 | 403 | 404 | 500 | 502 | 503 | 504
type Status = SuccessStatus | ErrorStatus
type Method = "get" | "post" | "put" | "patch" | "delete" type Method = "get" | "post" | "put" | "patch" | "delete"
export interface AttachedFile { export interface AttachedFile {
@ -25,7 +22,7 @@ function isAttachedFile(file: any): file is AttachedFile {
} }
export interface Expectations { export interface Expectations {
status?: Status status?: number
headers?: Record<string, string | RegExp> headers?: Record<string, string | RegExp>
headersNotPresent?: string[] headersNotPresent?: string[]
body?: Record<string, any> body?: Record<string, any>

View File

@ -16,9 +16,12 @@ export class QueryAPI extends TestAPI {
queryId: string, queryId: string,
body?: ExecuteQueryRequest body?: ExecuteQueryRequest
): Promise<ExecuteQueryResponse> => { ): Promise<ExecuteQueryResponse> => {
return await this._post<ExecuteQueryResponse>(`/api/queries/${queryId}`, { return await this._post<ExecuteQueryResponse>(
body, `/api/v2/queries/${queryId}`,
}) {
body,
}
)
} }
previewQuery = async (queryPreview: PreviewQueryRequest) => { previewQuery = async (queryPreview: PreviewQueryRequest) => {

View File

@ -9,42 +9,27 @@ import {
SearchRowResponse, SearchRowResponse,
SearchParams, SearchParams,
} from "@budibase/types" } from "@budibase/types"
import TestConfiguration from "../TestConfiguration" import { Expectations, TestAPI } from "./base"
import { TestAPI } from "./base"
export class RowAPI extends TestAPI { export class RowAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
get = async ( get = async (
sourceId: string, sourceId: string,
rowId: string, rowId: string,
{ expectStatus } = { expectStatus: 200 } expectations?: Expectations
) => { ) => {
const request = this.request return await this._get<Row>(`/api/${sourceId}/rows/${rowId}`, {
.get(`/api/${sourceId}/rows/${rowId}`) expectations,
.set(this.config.defaultHeaders()) })
.expect(expectStatus)
if (expectStatus !== 404) {
request.expect("Content-Type", /json/)
}
return request
} }
getEnriched = async ( getEnriched = async (
sourceId: string, sourceId: string,
rowId: string, rowId: string,
{ expectStatus } = { expectStatus: 200 } expectations?: Expectations
) => { ) => {
const request = this.request return await this._get<Row>(`/api/${sourceId}/${rowId}/enrich`, {
.get(`/api/${sourceId}/${rowId}/enrich`) expectations,
.set(this.config.defaultHeaders()) })
.expect(expectStatus)
if (expectStatus !== 404) {
request.expect("Content-Type", /json/)
}
return request
} }
save = async ( save = async (