Dry tests
This commit is contained in:
parent
b15ce4f4e4
commit
18df47b1f4
|
@ -6,14 +6,17 @@ import {
|
||||||
|
|
||||||
import * as setup from "../api/routes/tests/utilities"
|
import * as setup from "../api/routes/tests/utilities"
|
||||||
import { Datasource, FieldType, SourceName, Table } from "@budibase/types"
|
import { Datasource, FieldType, SourceName, Table } from "@budibase/types"
|
||||||
|
import _ from "lodash"
|
||||||
|
|
||||||
const config = setup.getConfig()
|
const config = setup.getConfig()
|
||||||
let apiKey,
|
|
||||||
|
describe("row api - postgres", () => {
|
||||||
|
let apiKey,
|
||||||
makeRequest: MakeRequestResponse,
|
makeRequest: MakeRequestResponse,
|
||||||
postgresDatasource: Datasource,
|
postgresDatasource: Datasource,
|
||||||
postgresTable: Table
|
postgresTable: Table
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await config.init()
|
await config.init()
|
||||||
apiKey = await config.generateApiKey()
|
apiKey = await config.generateApiKey()
|
||||||
postgresDatasource = await config.createDatasource({
|
postgresDatasource = await config.createDatasource({
|
||||||
|
@ -56,23 +59,36 @@ beforeEach(async () => {
|
||||||
},
|
},
|
||||||
sourceId: postgresDatasource._id,
|
sourceId: postgresDatasource._id,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await config.end()
|
await config.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
function createRandomRow() {
|
function createRandomRow() {
|
||||||
return {
|
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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function populateRows(count: number) {
|
||||||
|
return await Promise.all(
|
||||||
|
Array(count)
|
||||||
|
.fill({})
|
||||||
|
.map(async () => {
|
||||||
|
const rowData = createRandomRow()
|
||||||
|
return {
|
||||||
|
rowData,
|
||||||
|
row: await config.createRow(rowData),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
describe("row api", () => {
|
|
||||||
describe("create a row", () => {
|
describe("create a row", () => {
|
||||||
test("Given than no row exists, adding a new rows persists it", async () => {
|
test("Given than no row exists, adding a new row persists it", async () => {
|
||||||
const newRow = createRandomRow()
|
const newRow = createRandomRow()
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await makeRequest(
|
||||||
|
@ -114,10 +130,9 @@ describe("row api", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Retrieve a row", () => {
|
describe("retrieve a row", () => {
|
||||||
test("Given than a table have a single row, the row can be retrieved successfully", async () => {
|
test("Given than a table have a single row, the row can be retrieved successfully", async () => {
|
||||||
const rowData = createRandomRow()
|
const [{ rowData, row }] = await populateRows(1)
|
||||||
const row = await config.createRow(rowData)
|
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await makeRequest(
|
||||||
"get",
|
"get",
|
||||||
|
@ -130,10 +145,8 @@ describe("row api", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
|
test("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
|
||||||
await Promise.all(Array(5).map(() => config.createRow(createRandomRow())))
|
const rows = await populateRows(10)
|
||||||
const rowData = createRandomRow()
|
const { rowData, row } = _.sample(rows)!
|
||||||
const row = await config.createRow(rowData)
|
|
||||||
await Promise.all(Array(2).map(() => config.createRow(createRandomRow())))
|
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await makeRequest(
|
||||||
"get",
|
"get",
|
||||||
|
|
Loading…
Reference in New Issue