From 4ae43f1a96d4c650dcd49b89cad3d8ce669a52a5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 18 Jan 2023 16:46:40 +0000 Subject: [PATCH] Retrieve row test --- .../server/src/integration-test/row.spec.ts | 125 +++++++++--------- .../src/tests/utilities/TestConfiguration.ts | 4 +- 2 files changed, 63 insertions(+), 66 deletions(-) diff --git a/packages/server/src/integration-test/row.spec.ts b/packages/server/src/integration-test/row.spec.ts index c9686f4ae3..7b176549d1 100644 --- a/packages/server/src/integration-test/row.spec.ts +++ b/packages/server/src/integration-test/row.spec.ts @@ -5,10 +5,13 @@ import { } from "../api/routes/public/tests/utils" import * as setup from "../api/routes/tests/utilities" -import { Datasource, FieldType, SourceName } from "@budibase/types" +import { Datasource, FieldType, SourceName, Table } from "@budibase/types" const config = setup.getConfig() -let apiKey, makeRequest: MakeRequestResponse, postgresDatasource: Datasource +let apiKey, + makeRequest: MakeRequestResponse, + postgresDatasource: Datasource, + postgresTable: Table beforeEach(async () => { await config.init() @@ -31,50 +34,56 @@ beforeEach(async () => { }) makeRequest = generateMakeRequest(apiKey) + + postgresTable = await config.createTable({ + name: faker.lorem.word(), + schema: { + name: { + name: "name", + type: FieldType.STRING, + constraints: { + presence: true, + }, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + value: { + name: "value", + type: FieldType.NUMBER, + }, + }, + sourceId: postgresDatasource._id, + }) }) afterAll(async () => { await config.end() }) +function createRandomRow() { + return { + name: faker.name.fullName(), + description: faker.lorem.paragraphs(), + value: +faker.random.numeric(), + } +} + describe("row api", () => { describe("create a row", () => { test("Given than no row exists, adding a new rows persists it", async () => { - const tableName = faker.lorem.word() + const newRow = createRandomRow() - const table = await config.createTable({ - name: tableName, - schema: { - name: { - name: "name", - type: FieldType.STRING, - constraints: { - presence: true, - }, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - value: { - name: "value", - type: FieldType.NUMBER, - }, - }, - sourceId: postgresDatasource._id, - }) - - const newRow = { - name: faker.name.fullName(), - description: faker.lorem.paragraphs(), - value: +faker.random.numeric(), - } - - const res = await makeRequest("post", `/tables/${table._id}/rows`, newRow) + const res = await makeRequest( + "post", + `/tables/${postgresTable._id}/rows`, + newRow + ) expect(res.status).toBe(200) - const persistedRows = await config.getRows(table._id!) + const persistedRows = await config.getRows(postgresTable._id!) expect(persistedRows).toHaveLength(1) expect(persistedRows).toEqual([ expect.objectContaining({ @@ -85,51 +94,39 @@ describe("row api", () => { }) test("Given than no row exists, multiple rows can be persisted", async () => { - const tableName = faker.lorem.word() - - const table = await config.createTable({ - name: tableName, - schema: { - name: { - name: "name", - type: FieldType.STRING, - constraints: { - presence: true, - }, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - value: { - name: "value", - type: FieldType.NUMBER, - }, - }, - sourceId: postgresDatasource._id, - }) - const numberOfRows = 10 - const newRows = Array(numberOfRows).fill({ - name: faker.name.fullName(), - description: faker.lorem.paragraphs(), - value: +faker.random.numeric(), - }) + const newRows = Array(numberOfRows).fill(createRandomRow()) for (const newRow of newRows) { const res = await makeRequest( "post", - `/tables/${table._id}/rows`, + `/tables/${postgresTable._id}/rows`, newRow ) expect(res.status).toBe(200) } - const persistedRows = await config.getRows(table._id!) + const persistedRows = await config.getRows(postgresTable._id!) expect(persistedRows).toHaveLength(numberOfRows) expect(persistedRows).toEqual( expect.arrayContaining(newRows.map(expect.objectContaining)) ) }) }) + + describe("Retrieve a row", () => { + test("Given than a table have a single row, the row can be retrieved successfully", async () => { + const rowData = createRandomRow() + const row = await config.createRow(rowData) + + const res = await makeRequest( + "get", + `/tables/${postgresTable._id}/rows/${row._id}` + ) + + expect(res.status).toBe(200) + + expect(res.body.data).toEqual(expect.objectContaining(rowData)) + }) + }) }) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 5fa7629d2a..993d5a788f 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -39,7 +39,7 @@ import newid from "../../db/newid" import { generateUserMetadataID } from "../../db/utils" import { startup } from "../../startup" import supertest from "supertest" -import { Datasource, SourceName, Table } from "@budibase/types" +import { Datasource, Row, SourceName, Table } from "@budibase/types" const GLOBAL_USER_ID = "us_uuid1" const EMAIL = "babs@babs.com" @@ -463,7 +463,7 @@ class TestConfiguration { // ROW - async createRow(config: any = null) { + async createRow(config: any = null): Promise { if (!this.table) { throw "Test requires table to be configured." }