2023-01-17 18:22:31 +01:00
|
|
|
import { faker } from "@faker-js/faker"
|
|
|
|
import {
|
|
|
|
generateMakeRequest,
|
|
|
|
MakeRequestResponse,
|
|
|
|
} from "../api/routes/public/tests/utils"
|
|
|
|
|
|
|
|
import * as setup from "../api/routes/tests/utilities"
|
|
|
|
import supertest from "supertest"
|
2023-01-18 13:19:40 +01:00
|
|
|
import { Datasource, FieldType, SourceName } from "@budibase/types"
|
2023-01-17 18:22:31 +01:00
|
|
|
|
|
|
|
const config = setup.getConfig()
|
2023-01-18 13:19:40 +01:00
|
|
|
let apiKey,
|
|
|
|
table,
|
|
|
|
app,
|
|
|
|
makeRequest: MakeRequestResponse,
|
|
|
|
postgresDatasource: Datasource
|
2023-01-17 18:22:31 +01:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
app = await config.init()
|
|
|
|
table = await config.updateTable()
|
|
|
|
apiKey = await config.generateApiKey()
|
2023-01-18 13:19:40 +01:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
})
|
2023-01-17 18:22:31 +01:00
|
|
|
makeRequest = generateMakeRequest(apiKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
require("../app").default
|
|
|
|
await config.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("row api", () => {
|
|
|
|
let request: supertest.SuperTest<supertest.Test>
|
|
|
|
let server: any
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
server = require("../app").default
|
|
|
|
})
|
|
|
|
afterAll(() => {
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
request = supertest(server)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("create a row", () => {
|
|
|
|
test("Given than no row exists, adding a new rows persists it", async () => {
|
|
|
|
const tableName = faker.lorem.word()
|
2023-01-18 13:19:40 +01:00
|
|
|
|
2023-01-17 18:39:59 +01:00
|
|
|
const table = await config.createTable({
|
2023-01-17 18:22:31 +01:00
|
|
|
name: tableName,
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
name: "value",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
},
|
|
|
|
},
|
2023-01-18 13:19:40 +01:00
|
|
|
sourceId: postgresDatasource._id,
|
2023-01-17 18:22:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const newRow = {
|
|
|
|
name: faker.name.fullName(),
|
|
|
|
description: faker.lorem.paragraphs(),
|
2023-01-17 18:39:59 +01:00
|
|
|
value: +faker.random.numeric(),
|
2023-01-17 18:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const res = await makeRequest("post", `/tables/${table._id}/rows`, newRow)
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
2023-01-17 18:39:59 +01:00
|
|
|
|
|
|
|
const persistedRows = await config.getRows(table._id!)
|
|
|
|
expect(persistedRows).toHaveLength(1)
|
|
|
|
expect(persistedRows).toEqual([
|
|
|
|
expect.objectContaining({
|
|
|
|
...res.body.data,
|
|
|
|
...newRow,
|
|
|
|
}),
|
|
|
|
])
|
2023-01-17 18:22:31 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|