Reuse tables

This commit is contained in:
Adria Navarro 2023-09-12 20:40:00 +02:00
parent 458de1282e
commit b5e6b42db2
2 changed files with 109 additions and 99 deletions

View File

@ -133,19 +133,26 @@ describe.each([
} }
: undefined : undefined
describe("save, load, update", () => { let tableId: string
beforeAll(async () => {
const tableConfig = generateTableConfig()
const table = await config.createTable(tableConfig)
tableId = table._id!
})
it("returns a success message when the row is created", async () => { it("returns a success message when the row is created", async () => {
const rowUsage = await getRowUsage() const rowUsage = await getRowUsage()
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const res = await request const res = await request
.post(`/api/${config.table!._id}/rows`) .post(`/api/${tableId}/rows`)
.send(basicRow(config.table!._id!)) .send(basicRow(tableId))
.set(config.defaultHeaders()) .set(config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect((res as any).res.statusMessage).toEqual( expect((res as any).res.statusMessage).toEqual(
`${table.name} saved successfully` `${config.table!.name} saved successfully`
) )
expect(res.body.name).toEqual("Test Contact") expect(res.body.name).toEqual("Test Contact")
expect(res.body._rev).toBeDefined() expect(res.body._rev).toBeDefined()
@ -158,7 +165,8 @@ describe.each([
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const tableConfig = generateTableConfig() const tableConfig = generateTableConfig()
const newTable = await config.createTable({ const newTable = await config.createTable(
{
...tableConfig, ...tableConfig,
name: "TestTableAuto", name: "TestTableAuto",
schema: { schema: {
@ -179,7 +187,9 @@ describe.each([
}, },
}, },
}, },
}) },
{ skipReassigning: true }
)
const ids = [1, 2, 3] const ids = [1, 2, 3]
@ -214,22 +224,14 @@ describe.each([
const rowUsage = await getRowUsage() const rowUsage = await getRowUsage()
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const res = await request const res = await config.api.row.save(tableId, {
.post(`/api/${table._id}/rows`)
.send({
_id: existing._id, _id: existing._id,
_rev: existing._rev, _rev: existing._rev,
tableId: table._id, tableId,
name: "Updated Name", name: "Updated Name",
}) })
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect((res as any).res.statusMessage).toEqual( expect(res.name).toEqual("Updated Name")
`${table.name} updated successfully.`
)
expect(res.body.name).toEqual("Updated Name")
await assertRowUsage(rowUsage) await assertRowUsage(rowUsage)
await assertQueryUsage(queryUsage + 1) await assertQueryUsage(queryUsage + 1)
}) })
@ -238,11 +240,7 @@ describe.each([
const existing = await config.createRow() const existing = await config.createRow()
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const res = await request const res = await config.api.row.get(tableId, existing._id!)
.get(`/api/${table._id}/rows/${existing._id}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body).toEqual({ expect(res.body).toEqual({
...existing, ...existing,
@ -252,24 +250,24 @@ describe.each([
}) })
it("should list all rows for given tableId", async () => { it("should list all rows for given tableId", async () => {
const table = await config.createTable(generateTableConfig(), {
skipReassigning: true,
})
const tableId = table._id!
const newRow = { const newRow = {
tableId: table._id, tableId,
name: "Second Contact", name: "Second Contact",
status: "new", description: "new",
} }
const firstRow = await config.createRow() const firstRow = await config.createRow({ tableId })
await config.createRow(newRow) await config.createRow(newRow)
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const res = await request const res = await config.api.row.fetch(tableId)
.get(`/api/${table._id}/rows`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toBe(2) expect(res.length).toBe(2)
expect(res.body.find((r: Row) => r.name === newRow.name)).toBeDefined() expect(res.find((r: Row) => r.name === newRow.name)).toBeDefined()
expect(res.body.find((r: Row) => r.name === firstRow.name)).toBeDefined() expect(res.find((r: Row) => r.name === firstRow.name)).toBeDefined()
await assertQueryUsage(queryUsage + 1) await assertQueryUsage(queryUsage + 1)
}) })
@ -277,7 +275,7 @@ describe.each([
await config.createRow() await config.createRow()
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
await config.api.row.get(table._id!, "1234567", { await config.api.row.get(tableId, "1234567", {
expectStatus: 404, expectStatus: 404,
}) })
await assertQueryUsage(queryUsage) // no change await assertQueryUsage(queryUsage) // no change
@ -333,8 +331,8 @@ describe.each([
presence: false, presence: false,
inclusion: ["Alpha", "Beta", "Gamma"], inclusion: ["Alpha", "Beta", "Gamma"],
}, },
}, }
table = await config.createTable({ const table = await config.createTable({
name: "TestTable2", name: "TestTable2",
type: "table", type: "table",
schema: { schema: {

View File

@ -76,4 +76,16 @@ export class RowAPI extends TestAPI {
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(expectStatus) .expect(expectStatus)
} }
fetch = async (
sourceId: string,
{ expectStatus } = { expectStatus: 200 }
): Promise<Row[]> => {
const request = this.request
.get(`/api/${sourceId}/rows`)
.set(this.config.defaultHeaders())
.expect(expectStatus)
return (await request).body
}
} }