Dry tests
This commit is contained in:
parent
b15ce4f4e4
commit
18df47b1f4
|
@ -6,73 +6,89 @@ 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,
|
|
||||||
makeRequest: MakeRequestResponse,
|
|
||||||
postgresDatasource: Datasource,
|
|
||||||
postgresTable: Table
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
describe("row api - postgres", () => {
|
||||||
await config.init()
|
let apiKey,
|
||||||
apiKey = await config.generateApiKey()
|
makeRequest: MakeRequestResponse,
|
||||||
postgresDatasource = await config.createDatasource({
|
postgresDatasource: Datasource,
|
||||||
type: "datasource",
|
postgresTable: Table
|
||||||
source: SourceName.POSTGRES,
|
|
||||||
plus: true,
|
|
||||||
config: {
|
|
||||||
host: "192.168.1.98",
|
|
||||||
port: 54321,
|
|
||||||
database: "postgres",
|
|
||||||
user: "root",
|
|
||||||
password: "root",
|
|
||||||
schema: "public",
|
|
||||||
ssl: false,
|
|
||||||
rejectUnauthorized: false,
|
|
||||||
ca: false,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
makeRequest = generateMakeRequest(apiKey)
|
beforeEach(async () => {
|
||||||
|
await config.init()
|
||||||
|
apiKey = await config.generateApiKey()
|
||||||
|
postgresDatasource = await config.createDatasource({
|
||||||
|
type: "datasource",
|
||||||
|
source: SourceName.POSTGRES,
|
||||||
|
plus: true,
|
||||||
|
config: {
|
||||||
|
host: "192.168.1.98",
|
||||||
|
port: 54321,
|
||||||
|
database: "postgres",
|
||||||
|
user: "root",
|
||||||
|
password: "root",
|
||||||
|
schema: "public",
|
||||||
|
ssl: false,
|
||||||
|
rejectUnauthorized: false,
|
||||||
|
ca: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
postgresTable = await config.createTable({
|
makeRequest = generateMakeRequest(apiKey)
|
||||||
name: faker.lorem.word(),
|
|
||||||
schema: {
|
postgresTable = await config.createTable({
|
||||||
name: {
|
name: faker.lorem.word(),
|
||||||
name: "name",
|
schema: {
|
||||||
type: FieldType.STRING,
|
name: {
|
||||||
constraints: {
|
name: "name",
|
||||||
presence: true,
|
type: FieldType.STRING,
|
||||||
|
constraints: {
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
name: "description",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
value: {
|
||||||
|
name: "value",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
description: {
|
sourceId: postgresDatasource._id,
|
||||||
name: "description",
|
})
|
||||||
type: FieldType.STRING,
|
|
||||||
},
|
|
||||||
value: {
|
|
||||||
name: "value",
|
|
||||||
type: FieldType.NUMBER,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
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