1
0
Fork 0
mirror of synced 2024-09-10 14:35:47 +12:00

Use api for testing

This commit is contained in:
Adria Navarro 2023-09-12 19:03:24 +02:00
parent c3808682f8
commit cb762cc336
2 changed files with 59 additions and 40 deletions

View file

@ -177,11 +177,12 @@ describe.each([
const rowUsage = await getRowUsage() const rowUsage = await getRowUsage()
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const tableConfig = await generateTableConfig()
const newTable = await config.createTable({ const newTable = await config.createTable({
...table, ...tableConfig,
name: "TestTableAuto", name: "TestTableAuto",
schema: { schema: {
...table.schema, ...tableConfig.schema,
"Row ID": { "Row ID": {
name: "Row ID", name: "Row ID",
type: FieldType.NUMBER, type: FieldType.NUMBER,
@ -356,7 +357,7 @@ describe.each([
inclusion: ["Alpha", "Beta", "Gamma"], inclusion: ["Alpha", "Beta", "Gamma"],
}, },
}, },
table = await config.createTable({ table = await config.api.table.create({
name: "TestTable2", name: "TestTable2",
type: "table", type: "table",
schema: { schema: {
@ -392,6 +393,7 @@ describe.each([
optsFieldNull: optsField, optsFieldNull: optsField,
optsFieldStrKnown: optsField, optsFieldStrKnown: optsField,
}, },
...(await tableDatasourceConfig()),
}) })
row = { row = {
@ -470,7 +472,7 @@ describe.each([
}) })
describe("view save", () => { describe("view save", () => {
function orderTable(): Table { async function orderTable(): Promise<Table> {
return { return {
name: "orders", name: "orders",
primary: ["OrderID"], primary: ["OrderID"],
@ -488,11 +490,12 @@ describe.each([
name: "Story", name: "Story",
}, },
}, },
...(await tableDatasourceConfig()),
} }
} }
it("views have extra data trimmed", async () => { it("views have extra data trimmed", async () => {
const table = await config.createTable(orderTable()) const table = await config.api.table.create(await orderTable())
const createViewResponse = await config.api.viewV2.create({ const createViewResponse = await config.api.viewV2.create({
tableId: table._id, tableId: table._id,
@ -887,7 +890,7 @@ describe.each([
describe("exportData", () => { describe("exportData", () => {
it("should allow exporting all columns", async () => { it("should allow exporting all columns", async () => {
const existing = await config.createRow() const existing = await createRow()
const res = await request const res = await request
.post(`/api/${table._id}/rows/exportRows?format=json`) .post(`/api/${table._id}/rows/exportRows?format=json`)
.set(config.defaultHeaders()) .set(config.defaultHeaders())
@ -910,7 +913,7 @@ describe.each([
}) })
it("should allow exporting only certain columns", async () => { it("should allow exporting only certain columns", async () => {
const existing = await config.createRow() const existing = await createRow()
const res = await request const res = await request
.post(`/api/${table._id}/rows/exportRows?format=json`) .post(`/api/${table._id}/rows/exportRows?format=json`)
.set(config.defaultHeaders()) .set(config.defaultHeaders())
@ -980,7 +983,7 @@ describe.each([
describe("create", () => { describe("create", () => {
it("should persist a new row with only the provided view fields", async () => { it("should persist a new row with only the provided view fields", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const view = await config.api.viewV2.create({ const view = await config.api.viewV2.create({
tableId: table._id!, tableId: table._id!,
schema: { schema: {
@ -992,7 +995,7 @@ describe.each([
const data = randomRowData() const data = randomRowData()
const newRow = await config.api.row.save(view.id, { const newRow = await config.api.row.save(view.id, {
tableId: config.table!._id, tableId: table!._id,
_viewId: view.id, _viewId: view.id,
...data, ...data,
}) })
@ -1002,7 +1005,7 @@ describe.each([
name: data.name, name: data.name,
surname: data.surname, surname: data.surname,
address: data.address, address: data.address,
tableId: config.table!._id, tableId: table!._id,
_id: newRow._id, _id: newRow._id,
_rev: newRow._rev, _rev: newRow._rev,
id: newRow.id, id: newRow.id,
@ -1016,7 +1019,7 @@ describe.each([
describe("patch", () => { describe("patch", () => {
it("should update only the view fields for a row", async () => { it("should update only the view fields for a row", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const tableId = table._id! const tableId = table._id!
const view = await config.api.viewV2.create({ const view = await config.api.viewV2.create({
tableId, tableId,
@ -1058,7 +1061,7 @@ describe.each([
describe("destroy", () => { describe("destroy", () => {
it("should be able to delete a row", async () => { it("should be able to delete a row", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const tableId = table._id! const tableId = table._id!
const view = await config.api.viewV2.create({ const view = await config.api.viewV2.create({
tableId, tableId,
@ -1083,7 +1086,7 @@ describe.each([
}) })
it("should be able to delete multiple rows", async () => { it("should be able to delete multiple rows", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const tableId = table._id! const tableId = table._id!
const view = await config.api.viewV2.create({ const view = await config.api.viewV2.create({
tableId, tableId,
@ -1148,13 +1151,17 @@ describe.each([
} }
it("returns empty rows from view when no schema is passed", async () => { it("returns empty rows from view when no schema is passed", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(
await config.api.row.save(table._id!, { tableId: table._id })
)
} }
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
})
const response = await config.api.viewV2.search(createViewResponse.id) const response = await config.api.viewV2.search(createViewResponse.id)
expect(response.body.rows).toHaveLength(10) expect(response.body.rows).toHaveLength(10)
@ -1174,10 +1181,10 @@ describe.each([
}) })
it("searching respects the view filters", async () => { it("searching respects the view filters", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const expectedRows = [] const expectedRows = []
for (let i = 0; i < 10; i++) for (let i = 0; i < 10; i++)
await config.createRow({ await config.api.row.save(table._id!, {
tableId: table._id, tableId: table._id,
name: generator.name(), name: generator.name(),
age: generator.integer({ min: 10, max: 30 }), age: generator.integer({ min: 10, max: 30 }),
@ -1185,7 +1192,7 @@ describe.each([
for (let i = 0; i < 5; i++) for (let i = 0; i < 5; i++)
expectedRows.push( expectedRows.push(
await config.createRow({ await config.api.row.save(table._id!, {
tableId: table._id, tableId: table._id,
name: generator.name(), name: generator.name(),
age: 40, age: 40,
@ -1193,6 +1200,7 @@ describe.each([
) )
const createViewResponse = await config.api.viewV2.create({ const createViewResponse = await config.api.viewV2.create({
tableId: table._id!,
query: [{ operator: "equal", field: "age", value: 40 }], query: [{ operator: "equal", field: "age", value: 40 }],
schema: viewSchema, schema: viewSchema,
}) })
@ -1289,7 +1297,7 @@ describe.each([
it.each(sortTestOptions)( it.each(sortTestOptions)(
"allow sorting (%s)", "allow sorting (%s)",
async (sortParams, expected) => { async (sortParams, expected) => {
await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const users = [ const users = [
{ name: "Alice", age: 25 }, { name: "Alice", age: 25 },
{ name: "Bob", age: 30 }, { name: "Bob", age: 30 },
@ -1297,13 +1305,14 @@ describe.each([
{ name: "Danny", age: 15 }, { name: "Danny", age: 15 },
] ]
for (const user of users) { for (const user of users) {
await config.createRow({ await config.api.row.save(table._id!, {
tableId: config.table!._id, tableId: table._id,
...user, ...user,
}) })
} }
const createViewResponse = await config.api.viewV2.create({ const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
sort: sortParams, sort: sortParams,
schema: viewSchema, schema: viewSchema,
}) })
@ -1320,7 +1329,7 @@ describe.each([
it.each(sortTestOptions)( it.each(sortTestOptions)(
"allow override the default view sorting (%s)", "allow override the default view sorting (%s)",
async (sortParams, expected) => { async (sortParams, expected) => {
await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const users = [ const users = [
{ name: "Alice", age: 25 }, { name: "Alice", age: 25 },
{ name: "Bob", age: 30 }, { name: "Bob", age: 30 },
@ -1328,13 +1337,14 @@ describe.each([
{ name: "Danny", age: 15 }, { name: "Danny", age: 15 },
] ]
for (const user of users) { for (const user of users) {
await config.createRow({ await config.api.row.save(table._id!, {
tableId: config.table!._id, tableId: table._id,
...user, ...user,
}) })
} }
const createViewResponse = await config.api.viewV2.create({ const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
sort: { sort: {
field: "name", field: "name",
order: SortOrder.ASCENDING, order: SortOrder.ASCENDING,
@ -1361,11 +1371,11 @@ describe.each([
) )
it("when schema is defined, defined columns and row attributes are returned", async () => { it("when schema is defined, defined columns and row attributes are returned", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push( rows.push(
await config.createRow({ await config.api.row.save(table._id!, {
tableId: table._id, tableId: table._id,
name: generator.name(), name: generator.name(),
age: generator.age(), age: generator.age(),
@ -1374,6 +1384,7 @@ describe.each([
} }
const view = await config.api.viewV2.create({ const view = await config.api.viewV2.create({
tableId: table._id,
schema: { name: { visible: true } }, schema: { name: { visible: true } },
}) })
const response = await config.api.viewV2.search(view.id) const response = await config.api.viewV2.search(view.id)
@ -1393,23 +1404,27 @@ describe.each([
}) })
it("views without data can be returned", async () => { it("views without data can be returned", async () => {
await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
})
const response = await config.api.viewV2.search(createViewResponse.id) const response = await config.api.viewV2.search(createViewResponse.id)
expect(response.body.rows).toHaveLength(0) expect(response.body.rows).toHaveLength(0)
}) })
it("respects the limit parameter", async () => { it("respects the limit parameter", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await createRow(table._id, { tableId: table._id }))
} }
const limit = generator.integer({ min: 1, max: 8 }) const limit = generator.integer({ min: 1, max: 8 })
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
})
const response = await config.api.viewV2.search(createViewResponse.id, { const response = await config.api.viewV2.search(createViewResponse.id, {
limit, limit,
query: {}, query: {},
@ -1419,13 +1434,15 @@ describe.each([
}) })
it("can handle pagination", async () => { it("can handle pagination", async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await createRow(table._id, { tableId: table._id }))
} }
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
})
const allRows = (await config.api.viewV2.search(createViewResponse.id)) const allRows = (await config.api.viewV2.search(createViewResponse.id))
.body.rows .body.rows
@ -1483,13 +1500,15 @@ describe.each([
let tableId: string let tableId: string
beforeAll(async () => { beforeAll(async () => {
const table = await config.createTable(await userTable()) const table = await config.api.table.create(await userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
rows.push(await config.createRow({ tableId: table._id })) rows.push(await createRow(table._id, { tableId: table._id }))
} }
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create({
tableId: table._id,
})
tableId = table._id! tableId = table._id!
viewId = createViewResponse.id viewId = createViewResponse.id

View file

@ -23,8 +23,8 @@ export class ViewV2API extends TestAPI {
if (!tableId && !this.config.table) { if (!tableId && !this.config.table) {
throw "Test requires table to be configured." throw "Test requires table to be configured."
} }
const table = this.config.table
tableId = table!._id! tableId = tableId || this.config.table!._id!
const view = { const view = {
tableId, tableId,
name: generator.guid(), name: generator.guid(),