Retrieve row test

This commit is contained in:
Adria Navarro 2023-01-18 16:46:40 +00:00
parent 0bfef72480
commit 029453eab5
2 changed files with 63 additions and 66 deletions

View File

@ -5,10 +5,13 @@ 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, SourceName } from "@budibase/types" import { Datasource, FieldType, SourceName, Table } from "@budibase/types"
const config = setup.getConfig() const config = setup.getConfig()
let apiKey, makeRequest: MakeRequestResponse, postgresDatasource: Datasource let apiKey,
makeRequest: MakeRequestResponse,
postgresDatasource: Datasource,
postgresTable: Table
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
@ -31,19 +34,9 @@ beforeEach(async () => {
}) })
makeRequest = generateMakeRequest(apiKey) makeRequest = generateMakeRequest(apiKey)
})
afterAll(async () => { postgresTable = await config.createTable({
await config.end() name: faker.lorem.word(),
})
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 table = await config.createTable({
name: tableName,
schema: { schema: {
name: { name: {
name: "name", name: "name",
@ -63,18 +56,34 @@ describe("row api", () => {
}, },
sourceId: postgresDatasource._id, sourceId: postgresDatasource._id,
}) })
})
const newRow = { afterAll(async () => {
await config.end()
})
function createRandomRow() {
return {
name: faker.name.fullName(), name: faker.name.fullName(),
description: faker.lorem.paragraphs(), description: faker.lorem.paragraphs(),
value: +faker.random.numeric(), value: +faker.random.numeric(),
} }
}
const res = await makeRequest("post", `/tables/${table._id}/rows`, newRow) describe("row api", () => {
describe("create a row", () => {
test("Given than no row exists, adding a new rows persists it", async () => {
const newRow = createRandomRow()
const res = await makeRequest(
"post",
`/tables/${postgresTable._id}/rows`,
newRow
)
expect(res.status).toBe(200) 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).toHaveLength(1)
expect(persistedRows).toEqual([ expect(persistedRows).toEqual([
expect.objectContaining({ expect.objectContaining({
@ -85,51 +94,39 @@ describe("row api", () => {
}) })
test("Given than no row exists, multiple rows can be persisted", async () => { 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 numberOfRows = 10
const newRows = Array(numberOfRows).fill({ const newRows = Array(numberOfRows).fill(createRandomRow())
name: faker.name.fullName(),
description: faker.lorem.paragraphs(),
value: +faker.random.numeric(),
})
for (const newRow of newRows) { for (const newRow of newRows) {
const res = await makeRequest( const res = await makeRequest(
"post", "post",
`/tables/${table._id}/rows`, `/tables/${postgresTable._id}/rows`,
newRow newRow
) )
expect(res.status).toBe(200) 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).toHaveLength(numberOfRows)
expect(persistedRows).toEqual( expect(persistedRows).toEqual(
expect.arrayContaining(newRows.map(expect.objectContaining)) 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))
})
})
}) })

View File

@ -39,7 +39,7 @@ import newid from "../../db/newid"
import { generateUserMetadataID } from "../../db/utils" import { generateUserMetadataID } from "../../db/utils"
import { startup } from "../../startup" import { startup } from "../../startup"
import supertest from "supertest" 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 GLOBAL_USER_ID = "us_uuid1"
const EMAIL = "babs@babs.com" const EMAIL = "babs@babs.com"
@ -463,7 +463,7 @@ class TestConfiguration {
// ROW // ROW
async createRow(config: any = null) { async createRow(config: any = null): Promise<Row> {
if (!this.table) { if (!this.table) {
throw "Test requires table to be configured." throw "Test requires table to be configured."
} }