2023-09-06 12:51:08 +02:00
|
|
|
import { databaseTestProviders } from "../../../integrations/tests/utils"
|
|
|
|
|
2023-07-13 12:17:24 +02:00
|
|
|
import tk from "timekeeper"
|
|
|
|
import { outputProcessing } from "../../../utilities/rowProcessor"
|
|
|
|
import * as setup from "./utilities"
|
2023-09-19 15:01:03 +02:00
|
|
|
import { context, InternalTable, roles, tenancy } from "@budibase/backend-core"
|
2023-07-13 12:17:24 +02:00
|
|
|
import { quotas } from "@budibase/pro"
|
|
|
|
import {
|
2023-10-06 09:12:45 +02:00
|
|
|
AutoFieldSubTypes,
|
|
|
|
FieldSchema,
|
2023-08-11 16:00:50 +02:00
|
|
|
FieldType,
|
2023-09-27 16:51:42 +02:00
|
|
|
FieldTypeSubtypes,
|
2023-11-07 19:34:51 +01:00
|
|
|
FormulaTypes,
|
2023-10-26 14:19:09 +02:00
|
|
|
INTERNAL_TABLE_SOURCE_ID,
|
2022-09-28 09:56:45 +02:00
|
|
|
MonthlyQuotaName,
|
2023-09-04 14:12:37 +02:00
|
|
|
PermissionLevel,
|
2023-08-11 16:00:50 +02:00
|
|
|
QuotaUsageType,
|
2023-09-13 14:09:48 +02:00
|
|
|
RelationshipType,
|
2023-07-13 12:17:24 +02:00
|
|
|
Row,
|
2023-10-12 20:00:53 +02:00
|
|
|
SaveTableRequest,
|
|
|
|
SearchQueryOperators,
|
2023-07-18 15:34:25 +02:00
|
|
|
SortOrder,
|
2023-08-11 16:00:50 +02:00
|
|
|
SortType,
|
|
|
|
StaticQuotaName,
|
|
|
|
Table,
|
2023-10-26 14:19:09 +02:00
|
|
|
TableSourceType,
|
2023-07-13 12:17:24 +02:00
|
|
|
} from "@budibase/types"
|
2023-07-25 16:32:26 +02:00
|
|
|
import {
|
2023-09-08 16:31:47 +02:00
|
|
|
expectAnyExternalColsAttributes,
|
2023-07-25 16:32:26 +02:00
|
|
|
expectAnyInternalColsAttributes,
|
|
|
|
generator,
|
2023-09-04 14:12:37 +02:00
|
|
|
mocks,
|
2023-07-25 16:32:26 +02:00
|
|
|
structures,
|
|
|
|
} from "@budibase/backend-core/tests"
|
2023-09-27 17:35:16 +02:00
|
|
|
import _ from "lodash"
|
2023-11-08 11:25:28 +01:00
|
|
|
import * as uuid from "uuid"
|
2021-03-26 15:11:24 +01:00
|
|
|
|
2023-08-11 16:00:50 +02:00
|
|
|
const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString()
|
|
|
|
tk.freeze(timestamp)
|
|
|
|
|
|
|
|
const { basicRow } = setup.structures
|
|
|
|
|
2023-09-05 12:09:11 +02:00
|
|
|
describe.each([
|
|
|
|
["internal", undefined],
|
|
|
|
["postgres", databaseTestProviders.postgres],
|
2023-09-27 17:35:16 +02:00
|
|
|
])("/rows (%s)", (__, dsProvider) => {
|
2023-09-08 10:04:49 +02:00
|
|
|
const isInternal = !dsProvider
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const request = setup.getRequest()
|
|
|
|
const config = setup.getConfig()
|
2023-07-13 12:17:24 +02:00
|
|
|
let table: Table
|
2023-09-12 21:09:25 +02:00
|
|
|
let tableId: string
|
2020-04-22 17:35:20 +02:00
|
|
|
|
2023-11-03 13:53:37 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
if (dsProvider) {
|
|
|
|
await dsProvider.stopContainer()
|
|
|
|
}
|
|
|
|
setup.afterAll()
|
|
|
|
})
|
2020-04-22 17:35:20 +02:00
|
|
|
|
2023-02-03 18:55:40 +01:00
|
|
|
beforeAll(async () => {
|
2021-03-05 13:11:44 +01:00
|
|
|
await config.init()
|
2023-02-03 18:55:40 +01:00
|
|
|
|
2023-09-05 12:09:11 +02:00
|
|
|
if (dsProvider) {
|
2023-09-12 19:18:19 +02:00
|
|
|
await config.createDatasource({
|
|
|
|
datasource: await dsProvider.getDsConfig(),
|
|
|
|
})
|
2023-09-05 12:09:11 +02:00
|
|
|
}
|
2023-09-12 19:18:19 +02:00
|
|
|
})
|
2023-09-08 15:46:10 +02:00
|
|
|
|
2023-09-12 19:18:19 +02:00
|
|
|
const generateTableConfig: () => SaveTableRequest = () => {
|
2023-09-08 15:46:10 +02:00
|
|
|
return {
|
2023-11-08 11:25:28 +01:00
|
|
|
name: uuid.v4(),
|
2023-09-08 15:46:10 +02:00
|
|
|
type: "table",
|
|
|
|
primary: ["id"],
|
2023-09-12 18:09:09 +02:00
|
|
|
primaryDisplay: "name",
|
2023-10-26 14:19:09 +02:00
|
|
|
sourceType: TableSourceType.INTERNAL,
|
2023-10-25 20:00:25 +02:00
|
|
|
sourceId: INTERNAL_TABLE_SOURCE_ID,
|
2023-09-08 15:46:10 +02:00
|
|
|
schema: {
|
|
|
|
id: {
|
|
|
|
type: FieldType.AUTO,
|
|
|
|
name: "id",
|
|
|
|
autocolumn: true,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "name",
|
|
|
|
constraints: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "description",
|
|
|
|
constraints: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 12:09:11 +02:00
|
|
|
|
2023-09-08 15:46:10 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
mocks.licenses.useCloudFree()
|
2020-05-27 18:23:01 +02:00
|
|
|
})
|
2020-04-24 19:02:51 +02:00
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const loadRow = (id: string, tbl_Id: string, status = 200) =>
|
|
|
|
config.api.row.get(tbl_Id, id, { expectStatus: status })
|
2020-09-10 10:36:14 +02:00
|
|
|
|
2022-05-11 12:32:53 +02:00
|
|
|
const getRowUsage = async () => {
|
2023-07-13 12:17:24 +02:00
|
|
|
const { total } = await config.doInContext(null, () =>
|
|
|
|
quotas.getCurrentUsageValues(QuotaUsageType.STATIC, StaticQuotaName.ROWS)
|
|
|
|
)
|
2022-09-28 12:57:58 +02:00
|
|
|
return total
|
2022-05-11 12:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const getQueryUsage = async () => {
|
2023-07-13 12:17:24 +02:00
|
|
|
const { total } = await config.doInContext(null, () =>
|
|
|
|
quotas.getCurrentUsageValues(
|
|
|
|
QuotaUsageType.MONTHLY,
|
|
|
|
MonthlyQuotaName.QUERIES
|
|
|
|
)
|
|
|
|
)
|
2022-05-26 18:31:57 +02:00
|
|
|
return total
|
2022-05-11 12:32:53 +02:00
|
|
|
}
|
|
|
|
|
2023-07-13 12:17:24 +02:00
|
|
|
const assertRowUsage = async (expected: number) => {
|
2022-05-11 12:32:53 +02:00
|
|
|
const usage = await getRowUsage()
|
|
|
|
expect(usage).toBe(expected)
|
|
|
|
}
|
|
|
|
|
2023-07-13 12:17:24 +02:00
|
|
|
const assertQueryUsage = async (expected: number) => {
|
2022-05-11 12:32:53 +02:00
|
|
|
const usage = await getQueryUsage()
|
|
|
|
expect(usage).toBe(expected)
|
|
|
|
}
|
2020-09-10 10:36:14 +02:00
|
|
|
|
2023-09-08 15:29:05 +02:00
|
|
|
const defaultRowFields = isInternal
|
|
|
|
? {
|
|
|
|
type: "row",
|
|
|
|
createdAt: timestamp,
|
|
|
|
updatedAt: timestamp,
|
2023-09-08 10:04:49 +02:00
|
|
|
}
|
2023-09-08 15:29:05 +02:00
|
|
|
: undefined
|
2023-09-08 10:04:49 +02:00
|
|
|
|
2023-10-26 19:21:06 +02:00
|
|
|
async function createTable(
|
|
|
|
cfg: Omit<SaveTableRequest, "sourceId" | "sourceType">,
|
|
|
|
opts?: { skipReassigning: boolean }
|
|
|
|
) {
|
2023-10-26 18:27:54 +02:00
|
|
|
let table
|
|
|
|
if (dsProvider) {
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await config.createExternalTable(cfg, opts)
|
2023-10-26 18:27:54 +02:00
|
|
|
} else {
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await config.createTable(cfg, opts)
|
2023-10-26 18:27:54 +02:00
|
|
|
}
|
2023-10-26 19:21:06 +02:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const tableConfig = generateTableConfig()
|
|
|
|
let table = await createTable(tableConfig)
|
2023-09-12 21:09:25 +02:00
|
|
|
tableId = table._id!
|
|
|
|
})
|
2023-09-12 20:40:00 +02:00
|
|
|
|
2023-09-12 21:09:25 +02:00
|
|
|
describe("save, load, update", () => {
|
2020-10-09 20:10:28 +02:00
|
|
|
it("returns a success message when the row is created", async () => {
|
2022-09-28 13:33:39 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
|
|
|
const res = await request
|
2023-09-12 20:40:00 +02:00
|
|
|
.post(`/api/${tableId}/rows`)
|
|
|
|
.send(basicRow(tableId))
|
2022-09-28 13:33:39 +02:00
|
|
|
.set(config.defaultHeaders())
|
2023-07-13 12:17:24 +02:00
|
|
|
.expect("Content-Type", /json/)
|
2022-09-28 13:33:39 +02:00
|
|
|
.expect(200)
|
2023-07-13 12:17:24 +02:00
|
|
|
expect((res as any).res.statusMessage).toEqual(
|
2023-09-12 20:40:00 +02:00
|
|
|
`${config.table!.name} saved successfully`
|
2023-07-13 12:17:24 +02:00
|
|
|
)
|
2022-09-28 13:33:39 +02:00
|
|
|
expect(res.body.name).toEqual("Test Contact")
|
|
|
|
expect(res.body._rev).toBeDefined()
|
|
|
|
await assertRowUsage(rowUsage + 1)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
2020-04-22 17:35:20 +02:00
|
|
|
})
|
|
|
|
|
2023-05-15 20:22:22 +02:00
|
|
|
it("Increment row autoId per create row request", async () => {
|
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-12 19:18:19 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
const newTable = await createTable(
|
2023-09-12 20:40:00 +02:00
|
|
|
{
|
|
|
|
...tableConfig,
|
|
|
|
name: "TestTableAuto",
|
|
|
|
schema: {
|
|
|
|
...tableConfig.schema,
|
|
|
|
"Row ID": {
|
|
|
|
name: "Row ID",
|
|
|
|
type: FieldType.NUMBER,
|
2023-10-06 09:12:45 +02:00
|
|
|
subtype: AutoFieldSubTypes.AUTO_ID,
|
2023-09-12 20:40:00 +02:00
|
|
|
icon: "ri-magic-line",
|
|
|
|
autocolumn: true,
|
|
|
|
constraints: {
|
|
|
|
type: "number",
|
|
|
|
presence: true,
|
|
|
|
numericality: {
|
|
|
|
greaterThanOrEqualTo: "",
|
|
|
|
lessThanOrEqualTo: "",
|
|
|
|
},
|
2023-05-15 20:22:22 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-07-13 12:17:24 +02:00
|
|
|
},
|
2023-09-12 20:40:00 +02:00
|
|
|
{ skipReassigning: true }
|
|
|
|
)
|
2023-05-15 20:22:22 +02:00
|
|
|
|
2023-07-13 12:17:24 +02:00
|
|
|
const ids = [1, 2, 3]
|
2023-05-15 20:22:22 +02:00
|
|
|
|
|
|
|
// Performing several create row requests should increment the autoID fields accordingly
|
2023-07-13 12:17:24 +02:00
|
|
|
const createRow = async (id: number) => {
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.save(newTable._id!, {
|
|
|
|
name: "row_" + id,
|
|
|
|
})
|
|
|
|
expect(res.name).toEqual("row_" + id)
|
|
|
|
expect(res._rev).toBeDefined()
|
|
|
|
expect(res["Row ID"]).toEqual(id)
|
2023-05-15 20:22:22 +02:00
|
|
|
}
|
|
|
|
|
2023-07-13 12:17:24 +02:00
|
|
|
for (let i = 0; i < ids.length; i++) {
|
2023-05-15 20:22:22 +02:00
|
|
|
await createRow(ids[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
await assertRowUsage(rowUsage + ids.length)
|
|
|
|
await assertQueryUsage(queryUsage + ids.length)
|
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
it("updates a row successfully", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const existing = await config.createRow()
|
2022-09-28 13:33:39 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
2020-04-24 19:02:51 +02:00
|
|
|
|
2023-09-12 20:40:00 +02:00
|
|
|
const res = await config.api.row.save(tableId, {
|
|
|
|
_id: existing._id,
|
|
|
|
_rev: existing._rev,
|
|
|
|
tableId,
|
|
|
|
name: "Updated Name",
|
|
|
|
})
|
2022-09-28 09:56:45 +02:00
|
|
|
|
2023-09-12 20:40:00 +02:00
|
|
|
expect(res.name).toEqual("Updated Name")
|
2022-09-28 13:33:39 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
2020-04-22 17:35:20 +02:00
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
it("should load a row", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const existing = await config.createRow()
|
2022-05-11 12:32:53 +02:00
|
|
|
const queryUsage = await getQueryUsage()
|
2020-04-24 19:02:51 +02:00
|
|
|
|
2023-09-12 20:40:00 +02:00
|
|
|
const res = await config.api.row.get(tableId, existing._id!)
|
2020-04-24 19:02:51 +02:00
|
|
|
|
|
|
|
expect(res.body).toEqual({
|
2023-09-12 19:18:19 +02:00
|
|
|
...existing,
|
2023-09-08 15:29:05 +02:00
|
|
|
...defaultRowFields,
|
2020-04-24 19:02:51 +02:00
|
|
|
})
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertQueryUsage(queryUsage + 1)
|
2020-04-22 17:35:20 +02:00
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
it("should list all rows for given tableId", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(generateTableConfig(), {
|
2023-09-12 20:40:00 +02:00
|
|
|
skipReassigning: true,
|
|
|
|
})
|
|
|
|
const tableId = table._id!
|
2020-10-09 20:10:28 +02:00
|
|
|
const newRow = {
|
2023-09-12 20:40:00 +02:00
|
|
|
tableId,
|
2020-04-24 19:02:51 +02:00
|
|
|
name: "Second Contact",
|
2023-09-12 20:40:00 +02:00
|
|
|
description: "new",
|
2020-04-24 19:02:51 +02:00
|
|
|
}
|
2023-09-12 20:40:00 +02:00
|
|
|
const firstRow = await config.createRow({ tableId })
|
2023-09-12 19:31:58 +02:00
|
|
|
await config.createRow(newRow)
|
2022-05-11 12:32:53 +02:00
|
|
|
const queryUsage = await getQueryUsage()
|
2020-04-22 17:35:20 +02:00
|
|
|
|
2023-09-12 20:40:00 +02:00
|
|
|
const res = await config.api.row.fetch(tableId)
|
2020-04-22 17:35:20 +02:00
|
|
|
|
2023-09-12 20:40:00 +02:00
|
|
|
expect(res.length).toBe(2)
|
|
|
|
expect(res.find((r: Row) => r.name === newRow.name)).toBeDefined()
|
|
|
|
expect(res.find((r: Row) => r.name === firstRow.name)).toBeDefined()
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertQueryUsage(queryUsage + 1)
|
2020-04-22 17:35:20 +02:00
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
it("load should return 404 when row does not exist", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
await config.createRow()
|
2022-05-11 12:32:53 +02:00
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-12 20:40:00 +02:00
|
|
|
await config.api.row.get(tableId, "1234567", {
|
2023-09-08 10:04:49 +02:00
|
|
|
expectStatus: 404,
|
|
|
|
})
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertQueryUsage(queryUsage) // no change
|
2020-04-22 17:35:20 +02:00
|
|
|
})
|
2020-10-05 18:28:23 +02:00
|
|
|
|
2023-09-12 11:02:44 +02:00
|
|
|
isInternal &&
|
|
|
|
it("row values are coerced", async () => {
|
2023-10-06 09:12:45 +02:00
|
|
|
const str: FieldSchema = {
|
2023-09-12 11:02:44 +02:00
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "str",
|
|
|
|
constraints: { type: "string", presence: false },
|
|
|
|
}
|
2023-10-06 09:12:45 +02:00
|
|
|
const attachment: FieldSchema = {
|
2023-09-12 11:02:44 +02:00
|
|
|
type: FieldType.ATTACHMENT,
|
|
|
|
name: "attachment",
|
|
|
|
constraints: { type: "array", presence: false },
|
|
|
|
}
|
2023-10-06 09:12:45 +02:00
|
|
|
const bool: FieldSchema = {
|
2023-09-12 11:02:44 +02:00
|
|
|
type: FieldType.BOOLEAN,
|
|
|
|
name: "boolean",
|
|
|
|
constraints: { type: "boolean", presence: false },
|
|
|
|
}
|
2023-10-06 09:12:45 +02:00
|
|
|
const number: FieldSchema = {
|
2023-09-12 11:02:44 +02:00
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "str",
|
|
|
|
constraints: { type: "number", presence: false },
|
|
|
|
}
|
2023-10-06 09:12:45 +02:00
|
|
|
const datetime: FieldSchema = {
|
2023-09-12 11:02:44 +02:00
|
|
|
type: FieldType.DATETIME,
|
|
|
|
name: "datetime",
|
2023-07-13 12:17:24 +02:00
|
|
|
constraints: {
|
|
|
|
type: "string",
|
|
|
|
presence: false,
|
2023-09-12 11:02:44 +02:00
|
|
|
datetime: { earliest: "", latest: "" },
|
2023-07-13 12:17:24 +02:00
|
|
|
},
|
2023-09-12 11:02:44 +02:00
|
|
|
}
|
2023-10-06 09:12:45 +02:00
|
|
|
const arrayField: FieldSchema = {
|
2023-09-12 11:02:44 +02:00
|
|
|
type: FieldType.ARRAY,
|
|
|
|
constraints: {
|
|
|
|
type: "array",
|
|
|
|
presence: false,
|
|
|
|
inclusion: ["One", "Two", "Three"],
|
2023-07-13 12:17:24 +02:00
|
|
|
},
|
2023-09-12 11:02:44 +02:00
|
|
|
name: "Sample Tags",
|
|
|
|
sortable: false,
|
|
|
|
}
|
2023-10-06 09:12:45 +02:00
|
|
|
const optsField: FieldSchema = {
|
2023-09-12 20:40:00 +02:00
|
|
|
name: "Sample Opts",
|
|
|
|
type: FieldType.OPTIONS,
|
|
|
|
constraints: {
|
|
|
|
type: "string",
|
|
|
|
presence: false,
|
|
|
|
inclusion: ["Alpha", "Beta", "Gamma"],
|
2023-09-12 11:02:44 +02:00
|
|
|
},
|
2023-09-12 20:40:00 +02:00
|
|
|
}
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable({
|
2023-09-12 20:40:00 +02:00
|
|
|
name: "TestTable2",
|
|
|
|
type: "table",
|
|
|
|
schema: {
|
|
|
|
name: str,
|
|
|
|
stringUndefined: str,
|
|
|
|
stringNull: str,
|
|
|
|
stringString: str,
|
|
|
|
numberEmptyString: number,
|
|
|
|
numberNull: number,
|
|
|
|
numberUndefined: number,
|
|
|
|
numberString: number,
|
|
|
|
numberNumber: number,
|
|
|
|
datetimeEmptyString: datetime,
|
|
|
|
datetimeNull: datetime,
|
|
|
|
datetimeUndefined: datetime,
|
|
|
|
datetimeString: datetime,
|
|
|
|
datetimeDate: datetime,
|
|
|
|
boolNull: bool,
|
|
|
|
boolEmpty: bool,
|
|
|
|
boolUndefined: bool,
|
|
|
|
boolString: bool,
|
|
|
|
boolBool: bool,
|
|
|
|
attachmentNull: attachment,
|
|
|
|
attachmentUndefined: attachment,
|
|
|
|
attachmentEmpty: attachment,
|
|
|
|
attachmentEmptyArrayStr: attachment,
|
|
|
|
arrayFieldEmptyArrayStr: arrayField,
|
|
|
|
arrayFieldArrayStrKnown: arrayField,
|
|
|
|
arrayFieldNull: arrayField,
|
|
|
|
arrayFieldUndefined: arrayField,
|
|
|
|
optsFieldEmptyStr: optsField,
|
|
|
|
optsFieldUndefined: optsField,
|
|
|
|
optsFieldNull: optsField,
|
|
|
|
optsFieldStrKnown: optsField,
|
|
|
|
},
|
|
|
|
})
|
2020-10-05 18:28:23 +02:00
|
|
|
|
2023-09-12 19:18:19 +02:00
|
|
|
const row = {
|
2023-09-12 11:02:44 +02:00
|
|
|
name: "Test Row",
|
|
|
|
stringUndefined: undefined,
|
|
|
|
stringNull: null,
|
|
|
|
stringString: "i am a string",
|
|
|
|
numberEmptyString: "",
|
|
|
|
numberNull: null,
|
|
|
|
numberUndefined: undefined,
|
|
|
|
numberString: "123",
|
|
|
|
numberNumber: 123,
|
|
|
|
datetimeEmptyString: "",
|
|
|
|
datetimeNull: null,
|
|
|
|
datetimeUndefined: undefined,
|
|
|
|
datetimeString: "1984-04-20T00:00:00.000Z",
|
|
|
|
datetimeDate: new Date("1984-04-20"),
|
|
|
|
boolNull: null,
|
|
|
|
boolEmpty: "",
|
|
|
|
boolUndefined: undefined,
|
|
|
|
boolString: "true",
|
|
|
|
boolBool: true,
|
|
|
|
tableId: table._id,
|
|
|
|
attachmentNull: null,
|
|
|
|
attachmentUndefined: undefined,
|
|
|
|
attachmentEmpty: "",
|
|
|
|
attachmentEmptyArrayStr: "[]",
|
|
|
|
arrayFieldEmptyArrayStr: "[]",
|
|
|
|
arrayFieldUndefined: undefined,
|
|
|
|
arrayFieldNull: null,
|
|
|
|
arrayFieldArrayStrKnown: "['One']",
|
|
|
|
optsFieldEmptyStr: "",
|
|
|
|
optsFieldUndefined: undefined,
|
|
|
|
optsFieldNull: null,
|
|
|
|
optsFieldStrKnown: "Alpha",
|
|
|
|
}
|
|
|
|
|
|
|
|
const createdRow = await config.createRow(row)
|
|
|
|
const id = createdRow._id!
|
|
|
|
|
|
|
|
const saved = (await loadRow(id, table._id!)).body
|
|
|
|
|
|
|
|
expect(saved.stringUndefined).toBe(undefined)
|
|
|
|
expect(saved.stringNull).toBe("")
|
|
|
|
expect(saved.stringString).toBe("i am a string")
|
|
|
|
expect(saved.numberEmptyString).toBe(null)
|
|
|
|
expect(saved.numberNull).toBe(null)
|
|
|
|
expect(saved.numberUndefined).toBe(undefined)
|
|
|
|
expect(saved.numberString).toBe(123)
|
|
|
|
expect(saved.numberNumber).toBe(123)
|
|
|
|
expect(saved.datetimeEmptyString).toBe(null)
|
|
|
|
expect(saved.datetimeNull).toBe(null)
|
|
|
|
expect(saved.datetimeUndefined).toBe(undefined)
|
|
|
|
expect(saved.datetimeString).toBe(
|
|
|
|
new Date(row.datetimeString).toISOString()
|
|
|
|
)
|
|
|
|
expect(saved.datetimeDate).toBe(row.datetimeDate.toISOString())
|
|
|
|
expect(saved.boolNull).toBe(null)
|
|
|
|
expect(saved.boolEmpty).toBe(null)
|
|
|
|
expect(saved.boolUndefined).toBe(undefined)
|
|
|
|
expect(saved.boolString).toBe(true)
|
|
|
|
expect(saved.boolBool).toBe(true)
|
|
|
|
expect(saved.attachmentNull).toEqual([])
|
|
|
|
expect(saved.attachmentUndefined).toBe(undefined)
|
|
|
|
expect(saved.attachmentEmpty).toEqual([])
|
|
|
|
expect(saved.attachmentEmptyArrayStr).toEqual([])
|
|
|
|
expect(saved.arrayFieldEmptyArrayStr).toEqual([])
|
|
|
|
expect(saved.arrayFieldNull).toEqual([])
|
|
|
|
expect(saved.arrayFieldUndefined).toEqual(undefined)
|
|
|
|
expect(saved.optsFieldEmptyStr).toEqual(null)
|
|
|
|
expect(saved.optsFieldUndefined).toEqual(undefined)
|
|
|
|
expect(saved.optsFieldNull).toEqual(null)
|
|
|
|
expect(saved.arrayFieldArrayStrKnown).toEqual(["One"])
|
|
|
|
expect(saved.optsFieldStrKnown).toEqual("Alpha")
|
|
|
|
})
|
2020-04-22 17:35:20 +02:00
|
|
|
})
|
2020-05-28 16:39:29 +02:00
|
|
|
|
2023-08-11 16:00:50 +02:00
|
|
|
describe("view save", () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
it("views have extra data trimmed", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable({
|
2023-10-26 14:19:09 +02:00
|
|
|
type: "table",
|
2023-08-11 16:00:50 +02:00
|
|
|
name: "orders",
|
2023-09-12 09:57:47 +02:00
|
|
|
primary: ["OrderID"],
|
2023-08-11 16:00:50 +02:00
|
|
|
schema: {
|
|
|
|
Country: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "Country",
|
|
|
|
},
|
|
|
|
OrderID: {
|
2023-09-12 09:57:47 +02:00
|
|
|
type: FieldType.NUMBER,
|
2023-08-11 16:00:50 +02:00
|
|
|
name: "OrderID",
|
|
|
|
},
|
|
|
|
Story: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "Story",
|
|
|
|
},
|
|
|
|
},
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
2023-08-11 16:00:50 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView({
|
2023-11-08 11:25:28 +01:00
|
|
|
name: uuid.v4(),
|
2023-08-11 16:00:50 +02:00
|
|
|
schema: {
|
2023-09-12 09:52:46 +02:00
|
|
|
Country: {
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
OrderID: {
|
|
|
|
visible: true,
|
|
|
|
},
|
2023-08-11 16:00:50 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-09-12 09:52:46 +02:00
|
|
|
const createRowResponse = await config.api.row.save(
|
|
|
|
createViewResponse.id,
|
|
|
|
{
|
|
|
|
OrderID: "1111",
|
|
|
|
Country: "Aussy",
|
|
|
|
Story: "aaaaa",
|
|
|
|
}
|
|
|
|
)
|
2023-08-11 16:00:50 +02:00
|
|
|
|
2023-09-12 09:52:46 +02:00
|
|
|
const row = await config.api.row.get(table._id!, createRowResponse._id!)
|
2023-08-11 16:00:50 +02:00
|
|
|
expect(row.body.Story).toBeUndefined()
|
2023-09-12 09:52:46 +02:00
|
|
|
expect(row.body).toEqual({
|
|
|
|
...defaultRowFields,
|
2023-09-12 09:57:47 +02:00
|
|
|
OrderID: 1111,
|
2023-09-12 09:52:46 +02:00
|
|
|
Country: "Aussy",
|
2023-09-12 09:57:47 +02:00
|
|
|
_id: createRowResponse._id,
|
2023-09-12 09:52:46 +02:00
|
|
|
_rev: createRowResponse._rev,
|
|
|
|
tableId: table._id,
|
|
|
|
})
|
2023-08-11 16:00:50 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-09-10 10:36:14 +02:00
|
|
|
describe("patch", () => {
|
2023-09-12 20:49:47 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2020-09-10 10:36:14 +02:00
|
|
|
it("should update only the fields that are supplied", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const existing = await config.createRow()
|
2020-09-10 10:36:14 +02:00
|
|
|
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-11-03 13:53:37 +01:00
|
|
|
const row = await config.api.row.patch(table._id!, {
|
2023-07-26 13:53:35 +02:00
|
|
|
_id: existing._id!,
|
|
|
|
_rev: existing._rev!,
|
|
|
|
tableId: table._id!,
|
2023-07-26 12:29:19 +02:00
|
|
|
name: "Updated Name",
|
|
|
|
})
|
2022-09-28 09:56:45 +02:00
|
|
|
|
2023-11-03 13:53:37 +01:00
|
|
|
expect(row.name).toEqual("Updated Name")
|
|
|
|
expect(row.description).toEqual(existing.description)
|
2020-09-10 10:36:14 +02:00
|
|
|
|
2023-11-03 13:53:37 +01:00
|
|
|
const savedRow = await loadRow(row._id!, table._id!)
|
2020-09-10 10:36:14 +02:00
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
expect(savedRow.body.description).toEqual(existing.description)
|
|
|
|
expect(savedRow.body.name).toEqual("Updated Name")
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage + 2) // account for the second load
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should throw an error when given improper types", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const existing = await config.createRow()
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-07-26 12:29:19 +02:00
|
|
|
await config.api.row.patch(
|
|
|
|
table._id!,
|
|
|
|
{
|
2023-07-26 13:53:35 +02:00
|
|
|
_id: existing._id!,
|
|
|
|
_rev: existing._rev!,
|
|
|
|
tableId: table._id!,
|
2021-03-10 18:55:42 +01:00
|
|
|
name: 1,
|
2023-07-26 12:29:19 +02:00
|
|
|
},
|
|
|
|
{ expectStatus: 400 }
|
|
|
|
)
|
2022-05-11 12:32:53 +02:00
|
|
|
|
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage)
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
2023-11-06 16:33:02 +01:00
|
|
|
|
|
|
|
it("should not overwrite links if those links are not set", async () => {
|
|
|
|
let linkField: FieldSchema = {
|
|
|
|
type: FieldType.LINK,
|
|
|
|
name: "",
|
|
|
|
fieldName: "",
|
|
|
|
constraints: {
|
|
|
|
type: "array",
|
|
|
|
presence: false,
|
|
|
|
},
|
|
|
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
|
|
tableId: InternalTable.USER_METADATA,
|
|
|
|
}
|
|
|
|
|
|
|
|
let table = await config.api.table.create({
|
|
|
|
name: "TestTable",
|
|
|
|
type: "table",
|
|
|
|
sourceType: TableSourceType.INTERNAL,
|
|
|
|
sourceId: INTERNAL_TABLE_SOURCE_ID,
|
|
|
|
schema: {
|
|
|
|
user1: { ...linkField, name: "user1", fieldName: "user1" },
|
|
|
|
user2: { ...linkField, name: "user2", fieldName: "user2" },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
let user1 = await config.createUser()
|
|
|
|
let user2 = await config.createUser()
|
|
|
|
|
|
|
|
let row = await config.api.row.save(table._id!, {
|
|
|
|
user1: [{ _id: user1._id }],
|
|
|
|
user2: [{ _id: user2._id }],
|
|
|
|
})
|
|
|
|
|
|
|
|
let getResp = await config.api.row.get(table._id!, row._id!)
|
|
|
|
expect(getResp.body.user1[0]._id).toEqual(user1._id)
|
|
|
|
expect(getResp.body.user2[0]._id).toEqual(user2._id)
|
|
|
|
|
|
|
|
let patchResp = await config.api.row.patch(table._id!, {
|
|
|
|
_id: row._id!,
|
|
|
|
_rev: row._rev!,
|
|
|
|
tableId: table._id!,
|
|
|
|
user1: [{ _id: user2._id }],
|
|
|
|
})
|
|
|
|
expect(patchResp.user1[0]._id).toEqual(user2._id)
|
|
|
|
expect(patchResp.user2[0]._id).toEqual(user2._id)
|
|
|
|
|
|
|
|
getResp = await config.api.row.get(table._id!, row._id!)
|
|
|
|
expect(getResp.body.user1[0]._id).toEqual(user2._id)
|
|
|
|
expect(getResp.body.user2[0]._id).toEqual(user2._id)
|
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("destroy", () => {
|
2023-09-12 20:49:47 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2021-03-10 18:55:42 +01:00
|
|
|
it("should be able to delete a row", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const createdRow = await config.createRow()
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.delete(table._id!, [createdRow])
|
2021-06-17 17:52:52 +02:00
|
|
|
expect(res.body[0]._id).toEqual(createdRow._id)
|
2022-09-28 09:56:45 +02:00
|
|
|
await assertRowUsage(rowUsage - 1)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
2020-09-10 10:36:14 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 16:39:29 +02:00
|
|
|
describe("validate", () => {
|
2023-09-12 20:49:47 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
it("should return no errors on valid row", async () => {
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.validate(table._id!, { name: "ivan" })
|
2022-09-28 09:56:45 +02:00
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
expect(res.valid).toBe(true)
|
|
|
|
expect(Object.keys(res.errors)).toEqual([])
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage)
|
2020-05-28 16:39:29 +02:00
|
|
|
})
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
it("should errors on invalid row", async () => {
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.validate(table._id!, { name: 1 })
|
2022-09-28 09:56:45 +02:00
|
|
|
|
2023-09-08 10:22:24 +02:00
|
|
|
if (isInternal) {
|
2023-09-13 09:57:41 +02:00
|
|
|
expect(res.valid).toBe(false)
|
|
|
|
expect(Object.keys(res.errors)).toEqual(["name"])
|
2023-09-08 10:22:24 +02:00
|
|
|
} else {
|
|
|
|
// Validation for external is not implemented, so it will always return valid
|
2023-09-13 09:57:41 +02:00
|
|
|
expect(res.valid).toBe(true)
|
|
|
|
expect(Object.keys(res.errors)).toEqual([])
|
2023-09-08 10:22:24 +02:00
|
|
|
}
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage)
|
2020-05-28 16:39:29 +02:00
|
|
|
})
|
|
|
|
})
|
2021-02-02 12:46:10 +01:00
|
|
|
|
2021-03-10 18:55:42 +01:00
|
|
|
describe("bulkDelete", () => {
|
2023-09-12 21:09:25 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2021-03-10 18:55:42 +01:00
|
|
|
it("should be able to delete a bulk set of rows", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const row1 = await config.createRow()
|
|
|
|
const row2 = await config.createRow()
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.delete(table._id!, [row1, row2])
|
2022-05-11 12:32:53 +02:00
|
|
|
|
2021-03-10 18:55:42 +01:00
|
|
|
expect(res.body.length).toEqual(2)
|
2023-07-13 12:17:24 +02:00
|
|
|
await loadRow(row1._id!, table._id!, 404)
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertRowUsage(rowUsage - 2)
|
2022-09-28 09:56:45 +02:00
|
|
|
await assertQueryUsage(queryUsage + 1)
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
2023-07-24 10:08:10 +02:00
|
|
|
|
|
|
|
it("should be able to delete a variety of row set types", async () => {
|
2023-09-13 09:37:11 +02:00
|
|
|
const [row1, row2, row3] = await Promise.all([
|
|
|
|
config.createRow(),
|
|
|
|
config.createRow(),
|
|
|
|
config.createRow(),
|
|
|
|
])
|
2023-07-24 10:08:10 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.delete(table._id!, [
|
|
|
|
row1,
|
|
|
|
row2._id,
|
|
|
|
{ _id: row3._id },
|
|
|
|
])
|
2023-07-24 10:08:10 +02:00
|
|
|
|
|
|
|
expect(res.body.length).toEqual(3)
|
|
|
|
await loadRow(row1._id!, table._id!, 404)
|
|
|
|
await assertRowUsage(rowUsage - 3)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should accept a valid row object and delete the row", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const row1 = await config.createRow()
|
2023-07-24 10:08:10 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.delete(table._id!, row1)
|
2023-07-24 10:08:10 +02:00
|
|
|
|
|
|
|
expect(res.body.id).toEqual(row1._id)
|
|
|
|
await loadRow(row1._id!, table._id!, 404)
|
|
|
|
await assertRowUsage(rowUsage - 1)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
|
|
|
})
|
2023-07-24 16:03:13 +02:00
|
|
|
|
|
|
|
it("Should ignore malformed/invalid delete requests", async () => {
|
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res = await config.api.row.delete(
|
|
|
|
table._id!,
|
|
|
|
{ not: "valid" },
|
|
|
|
{ expectStatus: 400 }
|
|
|
|
)
|
2023-07-24 16:03:13 +02:00
|
|
|
expect(res.body.message).toEqual("Invalid delete rows request")
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res2 = await config.api.row.delete(
|
|
|
|
table._id!,
|
|
|
|
{ rows: 123 },
|
|
|
|
{ expectStatus: 400 }
|
|
|
|
)
|
2023-07-24 16:03:13 +02:00
|
|
|
expect(res2.body.message).toEqual("Invalid delete rows request")
|
|
|
|
|
2023-09-13 09:57:41 +02:00
|
|
|
const res3 = await config.api.row.delete(table._id!, "invalid", {
|
|
|
|
expectStatus: 400,
|
|
|
|
})
|
2023-07-24 16:03:13 +02:00
|
|
|
expect(res3.body.message).toEqual("Invalid delete rows request")
|
|
|
|
|
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage)
|
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
|
|
|
|
2023-09-08 10:43:19 +02:00
|
|
|
// Legacy views are not available for external
|
|
|
|
isInternal &&
|
|
|
|
describe("fetchView", () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2023-09-08 10:43:19 +02:00
|
|
|
it("should be able to fetch tables contents via 'view'", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const row = await config.createRow()
|
2023-09-08 10:43:19 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
2022-05-11 12:32:53 +02:00
|
|
|
|
2023-09-13 09:59:17 +02:00
|
|
|
const res = await config.api.legacyView.get(table._id!)
|
2023-09-08 10:43:19 +02:00
|
|
|
expect(res.body.length).toEqual(1)
|
|
|
|
expect(res.body[0]._id).toEqual(row._id)
|
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
|
2023-09-08 10:43:19 +02:00
|
|
|
it("should throw an error if view doesn't exist", async () => {
|
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
2022-05-11 12:32:53 +02:00
|
|
|
|
2023-09-13 09:59:17 +02:00
|
|
|
await config.api.legacyView.get("derp", { expectStatus: 404 })
|
2022-05-11 12:32:53 +02:00
|
|
|
|
2023-09-08 10:43:19 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage)
|
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
|
2023-09-08 10:43:19 +02:00
|
|
|
it("should be able to run on a view", async () => {
|
|
|
|
const view = await config.createLegacyView({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: "ViewTest",
|
|
|
|
filters: [],
|
|
|
|
schema: {},
|
|
|
|
})
|
2023-09-12 19:31:58 +02:00
|
|
|
const row = await config.createRow()
|
2023-09-08 10:43:19 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
2022-05-11 12:32:53 +02:00
|
|
|
|
2023-09-13 09:59:17 +02:00
|
|
|
const res = await config.api.legacyView.get(view.name)
|
2023-09-08 10:43:19 +02:00
|
|
|
expect(res.body.length).toEqual(1)
|
|
|
|
expect(res.body[0]._id).toEqual(row._id)
|
2021-03-10 18:55:42 +01:00
|
|
|
|
2023-09-08 10:43:19 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
|
|
|
})
|
2022-05-11 12:32:53 +02:00
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
|
|
|
|
describe("fetchEnrichedRows", () => {
|
2023-09-12 21:09:25 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2021-02-02 12:46:10 +01:00
|
|
|
it("should allow enriching some linked rows", async () => {
|
2023-09-08 15:09:50 +02:00
|
|
|
const { linkedTable, firstRow, secondRow } = await tenancy.doInTenant(
|
2023-07-13 12:17:24 +02:00
|
|
|
config.getTenantId(),
|
2022-09-28 09:56:45 +02:00
|
|
|
async () => {
|
2023-09-13 14:09:48 +02:00
|
|
|
const linkedTable = await config.createLinkedTable(
|
|
|
|
RelationshipType.ONE_TO_MANY,
|
|
|
|
["link"],
|
|
|
|
{
|
2023-11-08 11:25:28 +01:00
|
|
|
// Making sure that the combined table name + column name is within postgres limits
|
|
|
|
name: uuid.v4().replace(/-/g, "").substring(0, 16),
|
2023-09-13 14:09:48 +02:00
|
|
|
type: "table",
|
|
|
|
primary: ["id"],
|
|
|
|
primaryDisplay: "id",
|
|
|
|
schema: {
|
|
|
|
id: {
|
|
|
|
type: FieldType.AUTO,
|
|
|
|
name: "id",
|
|
|
|
autocolumn: true,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
2023-09-08 15:09:50 +02:00
|
|
|
},
|
|
|
|
},
|
2023-09-13 14:09:48 +02:00
|
|
|
}
|
|
|
|
)
|
2023-09-12 19:31:58 +02:00
|
|
|
const firstRow = await config.createRow({
|
2022-09-28 09:56:45 +02:00
|
|
|
name: "Test Contact",
|
|
|
|
description: "original description",
|
|
|
|
tableId: table._id,
|
|
|
|
})
|
2023-09-12 19:31:58 +02:00
|
|
|
const secondRow = await config.createRow({
|
2022-09-28 09:56:45 +02:00
|
|
|
name: "Test 2",
|
|
|
|
description: "og desc",
|
|
|
|
link: [{ _id: firstRow._id }],
|
2023-09-08 15:09:50 +02:00
|
|
|
tableId: linkedTable._id,
|
2022-09-28 09:56:45 +02:00
|
|
|
})
|
2023-09-08 15:09:50 +02:00
|
|
|
return { linkedTable, firstRow, secondRow }
|
2022-09-28 09:56:45 +02:00
|
|
|
}
|
|
|
|
)
|
2022-05-11 12:32:53 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
2021-03-10 18:55:42 +01:00
|
|
|
|
|
|
|
// test basic enrichment
|
2023-09-13 10:13:54 +02:00
|
|
|
const resBasic = await config.api.row.get(
|
|
|
|
linkedTable._id!,
|
|
|
|
secondRow._id!
|
|
|
|
)
|
2023-09-08 15:09:50 +02:00
|
|
|
expect(resBasic.body.link.length).toBe(1)
|
2023-09-12 18:09:09 +02:00
|
|
|
expect(resBasic.body.link[0]).toEqual({
|
|
|
|
_id: firstRow._id,
|
|
|
|
primaryDisplay: firstRow.name,
|
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
|
|
|
|
// test full enrichment
|
2023-09-13 10:13:54 +02:00
|
|
|
const resEnriched = await config.api.row.getEnriched(
|
|
|
|
linkedTable._id!,
|
|
|
|
secondRow._id!
|
|
|
|
)
|
2021-03-10 18:55:42 +01:00
|
|
|
expect(resEnriched.body.link.length).toBe(1)
|
|
|
|
expect(resEnriched.body.link[0]._id).toBe(firstRow._id)
|
|
|
|
expect(resEnriched.body.link[0].name).toBe("Test Contact")
|
|
|
|
expect(resEnriched.body.link[0].description).toBe("original description")
|
2022-05-11 12:32:53 +02:00
|
|
|
await assertRowUsage(rowUsage)
|
2022-09-28 09:56:45 +02:00
|
|
|
await assertQueryUsage(queryUsage + 2)
|
2021-02-02 12:46:10 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-12 18:07:50 +02:00
|
|
|
isInternal &&
|
|
|
|
describe("attachments", () => {
|
2023-09-12 21:09:25 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2023-09-12 18:07:50 +02:00
|
|
|
it("should allow enriching attachment rows", async () => {
|
|
|
|
const table = await config.createAttachmentTable()
|
|
|
|
const attachmentId = `${structures.uuid()}.csv`
|
2023-09-12 19:31:58 +02:00
|
|
|
const row = await config.createRow({
|
2023-09-12 18:07:50 +02:00
|
|
|
name: "test",
|
|
|
|
description: "test",
|
|
|
|
attachment: [
|
|
|
|
{
|
|
|
|
key: `${config.getAppId()}/attachments/${attachmentId}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
tableId: table._id,
|
|
|
|
})
|
|
|
|
// the environment needs configured for this
|
|
|
|
await setup.switchToSelfHosted(async () => {
|
|
|
|
return context.doInAppContext(config.getAppId(), async () => {
|
|
|
|
const enriched = await outputProcessing(table, [row])
|
|
|
|
expect((enriched as Row[])[0].attachment[0].url).toBe(
|
|
|
|
`/files/signed/prod-budi-app-assets/${config.getProdAppId()}/attachments/${attachmentId}`
|
|
|
|
)
|
|
|
|
})
|
2022-01-31 18:00:22 +01:00
|
|
|
})
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
2021-03-04 14:07:33 +01:00
|
|
|
})
|
2022-09-28 09:56:45 +02:00
|
|
|
|
|
|
|
describe("exportData", () => {
|
2023-09-12 21:09:25 +02:00
|
|
|
beforeAll(async () => {
|
2023-09-12 20:47:06 +02:00
|
|
|
const tableConfig = generateTableConfig()
|
2023-10-26 19:21:06 +02:00
|
|
|
table = await createTable(tableConfig)
|
2023-09-12 20:47:06 +02:00
|
|
|
})
|
|
|
|
|
2022-09-28 09:56:45 +02:00
|
|
|
it("should allow exporting all columns", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const existing = await config.createRow()
|
2023-09-13 10:13:54 +02:00
|
|
|
const res = await config.api.row.exportRows(table._id!, {
|
|
|
|
rows: [existing._id!],
|
|
|
|
})
|
2022-09-28 09:56:45 +02:00
|
|
|
const results = JSON.parse(res.text)
|
|
|
|
expect(results.length).toEqual(1)
|
|
|
|
const row = results[0]
|
|
|
|
|
|
|
|
// Ensure all original columns were exported
|
|
|
|
expect(Object.keys(row).length).toBeGreaterThanOrEqual(
|
|
|
|
Object.keys(existing).length
|
|
|
|
)
|
|
|
|
Object.keys(existing).forEach(key => {
|
|
|
|
expect(row[key]).toEqual(existing[key])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow exporting only certain columns", async () => {
|
2023-09-12 19:31:58 +02:00
|
|
|
const existing = await config.createRow()
|
2023-09-13 10:13:54 +02:00
|
|
|
const res = await config.api.row.exportRows(table._id!, {
|
|
|
|
rows: [existing._id!],
|
|
|
|
columns: ["_id"],
|
|
|
|
})
|
2022-09-28 09:56:45 +02:00
|
|
|
const results = JSON.parse(res.text)
|
|
|
|
expect(results.length).toEqual(1)
|
|
|
|
const row = results[0]
|
|
|
|
|
|
|
|
// Ensure only the _id column was exported
|
|
|
|
expect(Object.keys(row).length).toEqual(1)
|
|
|
|
expect(row._id).toEqual(existing._id)
|
|
|
|
})
|
|
|
|
})
|
2023-07-14 10:26:22 +02:00
|
|
|
|
2023-07-31 12:03:03 +02:00
|
|
|
describe("view 2.0", () => {
|
2023-09-08 15:46:10 +02:00
|
|
|
async function userTable(): Promise<Table> {
|
2023-07-31 12:03:03 +02:00
|
|
|
return {
|
2023-11-08 11:25:28 +01:00
|
|
|
name: `users_${uuid.v4()}`,
|
2023-10-25 20:00:25 +02:00
|
|
|
sourceId: INTERNAL_TABLE_SOURCE_ID,
|
2023-10-26 14:19:09 +02:00
|
|
|
sourceType: TableSourceType.INTERNAL,
|
2023-09-08 15:29:05 +02:00
|
|
|
type: "table",
|
|
|
|
primary: ["id"],
|
2023-07-31 12:03:03 +02:00
|
|
|
schema: {
|
2023-09-08 15:29:05 +02:00
|
|
|
id: {
|
|
|
|
type: FieldType.AUTO,
|
|
|
|
name: "id",
|
|
|
|
autocolumn: true,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
|
|
|
},
|
2023-07-31 12:03:03 +02:00
|
|
|
name: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "name",
|
|
|
|
},
|
|
|
|
surname: {
|
|
|
|
type: FieldType.STRING,
|
2023-09-08 15:29:05 +02:00
|
|
|
name: "surname",
|
2023-07-31 12:03:03 +02:00
|
|
|
},
|
|
|
|
age: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "age",
|
|
|
|
},
|
|
|
|
address: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "address",
|
|
|
|
},
|
|
|
|
jobTitle: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "jobTitle",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const randomRowData = () => ({
|
|
|
|
name: generator.first(),
|
|
|
|
surname: generator.last(),
|
|
|
|
age: generator.age(),
|
|
|
|
address: generator.address(),
|
|
|
|
jobTitle: generator.word(),
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("create", () => {
|
|
|
|
it("should persist a new row with only the provided view fields", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-09-12 19:39:40 +02:00
|
|
|
const view = await config.createView({
|
2023-08-01 12:08:40 +02:00
|
|
|
schema: {
|
2023-07-31 12:03:03 +02:00
|
|
|
name: { visible: true },
|
|
|
|
surname: { visible: true },
|
|
|
|
address: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const data = randomRowData()
|
2023-08-08 14:19:22 +02:00
|
|
|
const newRow = await config.api.row.save(view.id, {
|
2023-09-12 19:03:24 +02:00
|
|
|
tableId: table!._id,
|
2023-07-31 12:03:03 +02:00
|
|
|
_viewId: view.id,
|
|
|
|
...data,
|
|
|
|
})
|
|
|
|
|
|
|
|
const row = await config.api.row.get(table._id!, newRow._id!)
|
|
|
|
expect(row.body).toEqual({
|
|
|
|
name: data.name,
|
|
|
|
surname: data.surname,
|
|
|
|
address: data.address,
|
2023-09-12 19:03:24 +02:00
|
|
|
tableId: table!._id,
|
2023-09-08 15:29:05 +02:00
|
|
|
_id: newRow._id,
|
|
|
|
_rev: newRow._rev,
|
|
|
|
id: newRow.id,
|
|
|
|
...defaultRowFields,
|
2023-07-31 12:03:03 +02:00
|
|
|
})
|
|
|
|
expect(row.body._viewId).toBeUndefined()
|
|
|
|
expect(row.body.age).toBeUndefined()
|
|
|
|
expect(row.body.jobTitle).toBeUndefined()
|
|
|
|
})
|
|
|
|
})
|
2023-07-31 15:41:13 +02:00
|
|
|
|
|
|
|
describe("patch", () => {
|
|
|
|
it("should update only the view fields for a row", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-07-31 16:14:14 +02:00
|
|
|
const tableId = table._id!
|
2023-09-12 19:39:40 +02:00
|
|
|
const view = await config.createView({
|
2023-08-01 12:08:40 +02:00
|
|
|
schema: {
|
2023-07-31 15:41:13 +02:00
|
|
|
name: { visible: true },
|
|
|
|
address: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-08-08 14:19:22 +02:00
|
|
|
const newRow = await config.api.row.save(view.id, {
|
2023-07-31 15:41:13 +02:00
|
|
|
tableId,
|
|
|
|
_viewId: view.id,
|
|
|
|
...randomRowData(),
|
|
|
|
})
|
|
|
|
const newData = randomRowData()
|
2023-08-08 14:19:22 +02:00
|
|
|
await config.api.row.patch(view.id, {
|
2023-07-31 15:41:13 +02:00
|
|
|
tableId,
|
|
|
|
_viewId: view.id,
|
|
|
|
_id: newRow._id!,
|
|
|
|
_rev: newRow._rev!,
|
|
|
|
...newData,
|
|
|
|
})
|
|
|
|
|
|
|
|
const row = await config.api.row.get(tableId, newRow._id!)
|
|
|
|
expect(row.body).toEqual({
|
|
|
|
...newRow,
|
|
|
|
name: newData.name,
|
|
|
|
address: newData.address,
|
2023-09-08 15:29:05 +02:00
|
|
|
_id: newRow._id,
|
2023-07-31 15:41:13 +02:00
|
|
|
_rev: expect.any(String),
|
2023-09-08 15:29:05 +02:00
|
|
|
id: newRow.id,
|
|
|
|
...defaultRowFields,
|
2023-07-31 15:41:13 +02:00
|
|
|
})
|
|
|
|
expect(row.body._viewId).toBeUndefined()
|
|
|
|
expect(row.body.age).toBeUndefined()
|
|
|
|
expect(row.body.jobTitle).toBeUndefined()
|
|
|
|
})
|
|
|
|
})
|
2023-07-31 16:14:14 +02:00
|
|
|
|
|
|
|
describe("destroy", () => {
|
|
|
|
it("should be able to delete a row", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-07-31 16:14:14 +02:00
|
|
|
const tableId = table._id!
|
2023-09-12 19:39:40 +02:00
|
|
|
const view = await config.createView({
|
2023-08-01 12:08:40 +02:00
|
|
|
schema: {
|
2023-07-31 16:14:14 +02:00
|
|
|
name: { visible: true },
|
|
|
|
address: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-09-12 19:31:58 +02:00
|
|
|
const createdRow = await config.createRow()
|
2023-07-31 16:14:14 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-08-08 14:19:22 +02:00
|
|
|
await config.api.row.delete(view.id, [createdRow])
|
2023-07-31 16:14:14 +02:00
|
|
|
|
|
|
|
await assertRowUsage(rowUsage - 1)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
|
|
|
|
|
|
|
await config.api.row.get(tableId, createdRow._id!, {
|
|
|
|
expectStatus: 404,
|
|
|
|
})
|
|
|
|
})
|
2023-07-31 16:37:24 +02:00
|
|
|
|
|
|
|
it("should be able to delete multiple rows", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-07-31 16:37:24 +02:00
|
|
|
const tableId = table._id!
|
2023-09-12 19:39:40 +02:00
|
|
|
const view = await config.createView({
|
2023-08-01 12:08:40 +02:00
|
|
|
schema: {
|
2023-07-31 16:37:24 +02:00
|
|
|
name: { visible: true },
|
|
|
|
address: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-09-12 19:31:58 +02:00
|
|
|
const rows = await Promise.all([
|
|
|
|
config.createRow(),
|
|
|
|
config.createRow(),
|
|
|
|
config.createRow(),
|
|
|
|
])
|
2023-07-31 16:37:24 +02:00
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
const queryUsage = await getQueryUsage()
|
|
|
|
|
2023-08-08 14:19:22 +02:00
|
|
|
await config.api.row.delete(view.id, [rows[0], rows[2]])
|
2023-07-31 16:37:24 +02:00
|
|
|
|
|
|
|
await assertRowUsage(rowUsage - 2)
|
|
|
|
await assertQueryUsage(queryUsage + 1)
|
|
|
|
|
|
|
|
await config.api.row.get(tableId, rows[0]._id!, {
|
|
|
|
expectStatus: 404,
|
|
|
|
})
|
|
|
|
await config.api.row.get(tableId, rows[2]._id!, {
|
|
|
|
expectStatus: 404,
|
|
|
|
})
|
|
|
|
await config.api.row.get(tableId, rows[1]._id!, { expectStatus: 200 })
|
|
|
|
})
|
2023-07-31 16:14:14 +02:00
|
|
|
})
|
2023-08-02 15:13:43 +02:00
|
|
|
|
|
|
|
describe("view search", () => {
|
2023-08-14 13:44:05 +02:00
|
|
|
const viewSchema = { age: { visible: true }, name: { visible: true } }
|
2023-09-08 15:54:27 +02:00
|
|
|
async function userTable(): Promise<Table> {
|
2023-08-02 15:13:43 +02:00
|
|
|
return {
|
2023-11-08 11:25:28 +01:00
|
|
|
name: `users_${uuid.v4()}`,
|
2023-10-25 20:00:25 +02:00
|
|
|
sourceId: INTERNAL_TABLE_SOURCE_ID,
|
2023-10-26 14:19:09 +02:00
|
|
|
sourceType: TableSourceType.INTERNAL,
|
2023-09-08 15:54:27 +02:00
|
|
|
type: "table",
|
|
|
|
primary: ["id"],
|
2023-08-02 15:13:43 +02:00
|
|
|
schema: {
|
2023-09-08 15:54:27 +02:00
|
|
|
id: {
|
|
|
|
type: FieldType.AUTO,
|
|
|
|
name: "id",
|
|
|
|
autocolumn: true,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
|
|
|
},
|
2023-08-02 15:13:43 +02:00
|
|
|
name: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "name",
|
|
|
|
constraints: { type: "string" },
|
|
|
|
},
|
|
|
|
age: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "age",
|
|
|
|
constraints: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 16:23:34 +02:00
|
|
|
it("returns empty rows from view when no schema is passed", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-09-13 09:37:11 +02:00
|
|
|
const rows = await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () =>
|
|
|
|
config.api.row.save(table._id!, { tableId: table._id })
|
2023-09-12 19:03:24 +02:00
|
|
|
)
|
2023-09-13 09:37:11 +02:00
|
|
|
)
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView()
|
2023-08-02 15:13:43 +02:00
|
|
|
const response = await config.api.viewV2.search(createViewResponse.id)
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(10)
|
|
|
|
expect(response.body).toEqual({
|
2023-09-08 16:23:34 +02:00
|
|
|
rows: expect.arrayContaining(
|
|
|
|
rows.map(r => ({
|
|
|
|
_viewId: createViewResponse.id,
|
|
|
|
tableId: table._id,
|
|
|
|
_id: r._id,
|
|
|
|
_rev: r._rev,
|
|
|
|
...defaultRowFields,
|
|
|
|
}))
|
|
|
|
),
|
2023-09-13 15:39:59 +02:00
|
|
|
...(isInternal
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
hasNextPage: false,
|
|
|
|
bookmark: null,
|
|
|
|
}),
|
2023-08-02 15:13:43 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("searching respects the view filters", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-13 09:37:11 +02:00
|
|
|
await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () =>
|
|
|
|
config.api.row.save(table._id!, {
|
|
|
|
tableId: table._id,
|
|
|
|
name: generator.name(),
|
|
|
|
age: generator.integer({ min: 10, max: 30 }),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
const expectedRows = await Promise.all(
|
|
|
|
Array.from({ length: 5 }, () =>
|
|
|
|
config.api.row.save(table._id!, {
|
2023-08-02 15:13:43 +02:00
|
|
|
tableId: table._id,
|
|
|
|
name: generator.name(),
|
|
|
|
age: 40,
|
|
|
|
})
|
|
|
|
)
|
2023-09-13 09:37:11 +02:00
|
|
|
)
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView({
|
2023-10-12 20:00:53 +02:00
|
|
|
query: [
|
|
|
|
{ operator: SearchQueryOperators.EQUAL, field: "age", value: 40 },
|
|
|
|
],
|
2023-08-14 13:44:05 +02:00
|
|
|
schema: viewSchema,
|
2023-08-02 15:13:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(createViewResponse.id)
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(5)
|
|
|
|
expect(response.body).toEqual({
|
|
|
|
rows: expect.arrayContaining(
|
2023-09-08 16:31:47 +02:00
|
|
|
expectedRows.map(r => ({
|
|
|
|
_viewId: createViewResponse.id,
|
|
|
|
tableId: table._id,
|
|
|
|
name: r.name,
|
|
|
|
age: r.age,
|
|
|
|
_id: r._id,
|
|
|
|
_rev: r._rev,
|
|
|
|
...defaultRowFields,
|
|
|
|
}))
|
2023-08-02 15:13:43 +02:00
|
|
|
),
|
2023-09-13 15:39:59 +02:00
|
|
|
...(isInternal
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
hasNextPage: false,
|
|
|
|
bookmark: null,
|
|
|
|
}),
|
2023-08-02 15:13:43 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const sortTestOptions: [
|
|
|
|
{
|
|
|
|
field: string
|
|
|
|
order?: SortOrder
|
|
|
|
type?: SortType
|
|
|
|
},
|
|
|
|
string[]
|
|
|
|
][] = [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "name",
|
|
|
|
order: SortOrder.ASCENDING,
|
|
|
|
type: SortType.STRING,
|
|
|
|
},
|
|
|
|
["Alice", "Bob", "Charly", "Danny"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "name",
|
|
|
|
},
|
|
|
|
["Alice", "Bob", "Charly", "Danny"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "name",
|
|
|
|
order: SortOrder.DESCENDING,
|
|
|
|
},
|
|
|
|
["Danny", "Charly", "Bob", "Alice"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "name",
|
|
|
|
order: SortOrder.DESCENDING,
|
|
|
|
type: SortType.STRING,
|
|
|
|
},
|
|
|
|
["Danny", "Charly", "Bob", "Alice"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "age",
|
|
|
|
order: SortOrder.ASCENDING,
|
|
|
|
type: SortType.number,
|
|
|
|
},
|
|
|
|
["Danny", "Alice", "Charly", "Bob"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "age",
|
|
|
|
order: SortOrder.ASCENDING,
|
|
|
|
},
|
|
|
|
["Danny", "Alice", "Charly", "Bob"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "age",
|
|
|
|
order: SortOrder.DESCENDING,
|
|
|
|
},
|
|
|
|
["Bob", "Charly", "Alice", "Danny"],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "age",
|
|
|
|
order: SortOrder.DESCENDING,
|
|
|
|
type: SortType.number,
|
|
|
|
},
|
|
|
|
["Bob", "Charly", "Alice", "Danny"],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
|
2023-09-13 09:37:11 +02:00
|
|
|
describe("sorting", () => {
|
|
|
|
beforeAll(async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-08-02 15:13:43 +02:00
|
|
|
const users = [
|
|
|
|
{ name: "Alice", age: 25 },
|
|
|
|
{ name: "Bob", age: 30 },
|
|
|
|
{ name: "Charly", age: 27 },
|
|
|
|
{ name: "Danny", age: 15 },
|
|
|
|
]
|
2023-09-13 09:37:11 +02:00
|
|
|
await Promise.all(
|
|
|
|
users.map(u =>
|
|
|
|
config.api.row.save(table._id!, {
|
|
|
|
tableId: table._id,
|
|
|
|
...u,
|
|
|
|
})
|
|
|
|
)
|
2023-09-08 16:31:47 +02:00
|
|
|
)
|
2023-09-13 09:37:11 +02:00
|
|
|
})
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-13 09:37:11 +02:00
|
|
|
it.each(sortTestOptions)(
|
|
|
|
"allow sorting (%s)",
|
|
|
|
async (sortParams, expected) => {
|
|
|
|
const createViewResponse = await config.createView({
|
|
|
|
sort: sortParams,
|
|
|
|
schema: viewSchema,
|
2023-08-02 15:13:43 +02:00
|
|
|
})
|
2023-09-13 09:37:11 +02:00
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(
|
|
|
|
createViewResponse.id
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(4)
|
|
|
|
expect(response.body.rows).toEqual(
|
|
|
|
expected.map(name => expect.objectContaining({ name }))
|
|
|
|
)
|
2023-08-02 15:13:43 +02:00
|
|
|
}
|
2023-09-13 09:37:11 +02:00
|
|
|
)
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-13 09:37:11 +02:00
|
|
|
it.each(sortTestOptions)(
|
|
|
|
"allow override the default view sorting (%s)",
|
|
|
|
async (sortParams, expected) => {
|
|
|
|
const createViewResponse = await config.createView({
|
|
|
|
sort: {
|
|
|
|
field: "name",
|
|
|
|
order: SortOrder.ASCENDING,
|
|
|
|
type: SortType.STRING,
|
|
|
|
},
|
|
|
|
schema: viewSchema,
|
|
|
|
})
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-13 09:37:11 +02:00
|
|
|
const response = await config.api.viewV2.search(
|
|
|
|
createViewResponse.id,
|
|
|
|
{
|
|
|
|
sort: sortParams.field,
|
|
|
|
sortOrder: sortParams.order,
|
|
|
|
sortType: sortParams.type,
|
|
|
|
query: {},
|
|
|
|
}
|
|
|
|
)
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-13 09:37:11 +02:00
|
|
|
expect(response.body.rows).toHaveLength(4)
|
|
|
|
expect(response.body.rows).toEqual(
|
|
|
|
expected.map(name => expect.objectContaining({ name }))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2023-08-02 15:13:43 +02:00
|
|
|
|
|
|
|
it("when schema is defined, defined columns and row attributes are returned", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-09-13 09:37:11 +02:00
|
|
|
const rows = await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () =>
|
|
|
|
config.api.row.save(table._id!, {
|
2023-08-02 15:13:43 +02:00
|
|
|
tableId: table._id,
|
|
|
|
name: generator.name(),
|
|
|
|
age: generator.age(),
|
|
|
|
})
|
|
|
|
)
|
2023-09-13 09:37:11 +02:00
|
|
|
)
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const view = await config.createView({
|
2023-08-14 13:44:05 +02:00
|
|
|
schema: { name: { visible: true } },
|
2023-08-02 15:13:43 +02:00
|
|
|
})
|
|
|
|
const response = await config.api.viewV2.search(view.id)
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(10)
|
|
|
|
expect(response.body.rows).toEqual(
|
|
|
|
expect.arrayContaining(
|
|
|
|
rows.map(r => ({
|
2023-09-08 16:31:47 +02:00
|
|
|
...(isInternal
|
|
|
|
? expectAnyInternalColsAttributes
|
|
|
|
: expectAnyExternalColsAttributes),
|
2023-08-02 15:13:43 +02:00
|
|
|
_viewId: view.id,
|
|
|
|
name: r.name,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("views without data can be returned", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
const table = await createTable(await userTable())
|
2023-08-02 15:13:43 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView()
|
2023-08-02 15:13:43 +02:00
|
|
|
const response = await config.api.viewV2.search(createViewResponse.id)
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(0)
|
|
|
|
})
|
2023-08-04 17:41:49 +02:00
|
|
|
|
2023-08-07 08:22:39 +02:00
|
|
|
it("respects the limit parameter", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
await createTable(await userTable())
|
2023-09-13 09:37:11 +02:00
|
|
|
await Promise.all(Array.from({ length: 10 }, () => config.createRow()))
|
|
|
|
|
2023-08-04 17:41:49 +02:00
|
|
|
const limit = generator.integer({ min: 1, max: 8 })
|
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView()
|
2023-08-04 17:41:49 +02:00
|
|
|
const response = await config.api.viewV2.search(createViewResponse.id, {
|
|
|
|
limit,
|
2023-08-25 16:28:29 +02:00
|
|
|
query: {},
|
2023-08-04 17:41:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(limit)
|
|
|
|
})
|
2023-08-07 09:49:04 +02:00
|
|
|
|
|
|
|
it("can handle pagination", async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
await createTable(await userTable())
|
2023-09-13 09:37:11 +02:00
|
|
|
await Promise.all(Array.from({ length: 10 }, () => config.createRow()))
|
2023-08-07 09:49:04 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView()
|
2023-08-07 09:49:04 +02:00
|
|
|
const allRows = (await config.api.viewV2.search(createViewResponse.id))
|
|
|
|
.body.rows
|
|
|
|
|
|
|
|
const firstPageResponse = await config.api.viewV2.search(
|
|
|
|
createViewResponse.id,
|
|
|
|
{
|
|
|
|
paginate: true,
|
|
|
|
limit: 4,
|
2023-08-25 16:28:29 +02:00
|
|
|
query: {},
|
2023-08-07 09:49:04 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(firstPageResponse.body).toEqual({
|
|
|
|
rows: expect.arrayContaining(allRows.slice(0, 4)),
|
2023-09-08 16:39:13 +02:00
|
|
|
totalRows: isInternal ? 10 : undefined,
|
2023-08-07 09:49:04 +02:00
|
|
|
hasNextPage: true,
|
2023-09-08 16:39:13 +02:00
|
|
|
bookmark: expect.anything(),
|
2023-08-07 09:49:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const secondPageResponse = await config.api.viewV2.search(
|
|
|
|
createViewResponse.id,
|
|
|
|
{
|
|
|
|
paginate: true,
|
|
|
|
limit: 4,
|
|
|
|
bookmark: firstPageResponse.body.bookmark,
|
2023-08-25 16:28:29 +02:00
|
|
|
|
|
|
|
query: {},
|
2023-08-07 09:49:04 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(secondPageResponse.body).toEqual({
|
|
|
|
rows: expect.arrayContaining(allRows.slice(4, 8)),
|
2023-09-08 16:39:13 +02:00
|
|
|
totalRows: isInternal ? 10 : undefined,
|
2023-08-07 09:49:04 +02:00
|
|
|
hasNextPage: true,
|
2023-09-08 16:39:13 +02:00
|
|
|
bookmark: expect.anything(),
|
2023-08-07 09:49:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const lastPageResponse = await config.api.viewV2.search(
|
|
|
|
createViewResponse.id,
|
|
|
|
{
|
|
|
|
paginate: true,
|
|
|
|
limit: 4,
|
|
|
|
bookmark: secondPageResponse.body.bookmark,
|
2023-08-25 16:28:29 +02:00
|
|
|
query: {},
|
2023-08-07 09:49:04 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(lastPageResponse.body).toEqual({
|
|
|
|
rows: expect.arrayContaining(allRows.slice(8)),
|
2023-09-08 16:39:13 +02:00
|
|
|
totalRows: isInternal ? 10 : undefined,
|
2023-08-07 09:49:04 +02:00
|
|
|
hasNextPage: false,
|
2023-09-08 16:39:13 +02:00
|
|
|
bookmark: expect.anything(),
|
2023-08-07 09:49:04 +02:00
|
|
|
})
|
|
|
|
})
|
2023-09-04 14:12:37 +02:00
|
|
|
|
2023-09-19 15:01:03 +02:00
|
|
|
isInternal &&
|
|
|
|
it("doesn't allow creating in user table", async () => {
|
|
|
|
const userTableId = InternalTable.USER_METADATA
|
|
|
|
const response = await config.api.row.save(
|
|
|
|
userTableId,
|
|
|
|
{
|
|
|
|
tableId: userTableId,
|
|
|
|
firstName: "Joe",
|
|
|
|
lastName: "Joe",
|
|
|
|
email: "joe@joe.com",
|
|
|
|
roles: {},
|
|
|
|
},
|
|
|
|
{ expectStatus: 400 }
|
|
|
|
)
|
|
|
|
expect(response.message).toBe("Cannot create new user entry.")
|
|
|
|
})
|
|
|
|
|
2023-09-04 14:12:37 +02:00
|
|
|
describe("permissions", () => {
|
|
|
|
let viewId: string
|
|
|
|
let tableId: string
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
await createTable(await userTable())
|
2023-09-13 09:37:11 +02:00
|
|
|
await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () => config.createRow())
|
|
|
|
)
|
2023-09-04 14:12:37 +02:00
|
|
|
|
2023-09-12 19:39:40 +02:00
|
|
|
const createViewResponse = await config.createView()
|
2023-09-04 14:12:37 +02:00
|
|
|
|
|
|
|
tableId = table._id!
|
|
|
|
viewId = createViewResponse.id
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mocks.licenses.useViewPermissions()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not allow public users to fetch by default", async () => {
|
|
|
|
await config.publish()
|
|
|
|
await config.api.viewV2.search(viewId, undefined, {
|
|
|
|
expectStatus: 403,
|
|
|
|
usePublicUser: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allow public users to fetch when permissions are explicit", async () => {
|
|
|
|
await config.api.permission.set({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: viewId,
|
|
|
|
})
|
|
|
|
await config.publish()
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(viewId, undefined, {
|
|
|
|
usePublicUser: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allow public users to fetch when permissions are inherited", async () => {
|
|
|
|
await config.api.permission.set({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: tableId,
|
|
|
|
})
|
|
|
|
await config.publish()
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(viewId, undefined, {
|
|
|
|
usePublicUser: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(response.body.rows).toHaveLength(10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("respects inherited permissions, not allowing not public views from public tables", async () => {
|
|
|
|
await config.api.permission.set({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: tableId,
|
|
|
|
})
|
|
|
|
await config.api.permission.set({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.POWER,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: viewId,
|
|
|
|
})
|
|
|
|
await config.publish()
|
|
|
|
|
|
|
|
await config.api.viewV2.search(viewId, undefined, {
|
|
|
|
usePublicUser: true,
|
|
|
|
expectStatus: 403,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-08-02 15:13:43 +02:00
|
|
|
})
|
2023-07-31 12:03:03 +02:00
|
|
|
})
|
2023-09-27 16:51:42 +02:00
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
let o2mTable: Table
|
|
|
|
let m2mTable: Table
|
|
|
|
beforeAll(async () => {
|
2023-10-26 19:21:06 +02:00
|
|
|
o2mTable = await createTable(
|
2023-09-29 12:48:55 +02:00
|
|
|
{ ...generateTableConfig(), name: "o2m" },
|
|
|
|
{
|
|
|
|
skipReassigning: true,
|
|
|
|
}
|
|
|
|
)
|
2023-10-26 19:21:06 +02:00
|
|
|
m2mTable = await createTable(
|
2023-09-29 12:48:55 +02:00
|
|
|
{ ...generateTableConfig(), name: "m2m" },
|
|
|
|
{
|
|
|
|
skipReassigning: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
[
|
|
|
|
"relationship fields",
|
2023-10-06 09:12:45 +02:00
|
|
|
(): Record<string, FieldSchema> => ({
|
2023-09-29 12:48:55 +02:00
|
|
|
user: {
|
|
|
|
name: "user",
|
|
|
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
|
|
type: FieldType.LINK,
|
|
|
|
tableId: o2mTable._id!,
|
|
|
|
fieldName: "fk_o2m",
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
name: "users",
|
|
|
|
relationshipType: RelationshipType.MANY_TO_MANY,
|
|
|
|
type: FieldType.LINK,
|
|
|
|
tableId: m2mTable._id!,
|
|
|
|
fieldName: "fk_m2m",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
(tableId: string) =>
|
|
|
|
config.api.row.save(tableId, {
|
2023-11-08 11:25:28 +01:00
|
|
|
name: uuid.v4(),
|
2023-09-29 12:48:55 +02:00
|
|
|
description: generator.paragraph(),
|
|
|
|
tableId,
|
|
|
|
}),
|
|
|
|
(row: Row) => ({
|
|
|
|
_id: row._id,
|
|
|
|
primaryDisplay: row.name,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"bb reference fields",
|
2023-10-06 09:12:45 +02:00
|
|
|
(): Record<string, FieldSchema> => ({
|
2023-09-29 12:48:55 +02:00
|
|
|
user: {
|
|
|
|
name: "user",
|
|
|
|
type: FieldType.BB_REFERENCE,
|
|
|
|
subtype: FieldTypeSubtypes.BB_REFERENCE.USER,
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
name: "users",
|
|
|
|
type: FieldType.BB_REFERENCE,
|
2023-10-04 17:53:46 +02:00
|
|
|
subtype: FieldTypeSubtypes.BB_REFERENCE.USERS,
|
2023-09-29 12:48:55 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
() => config.createUser(),
|
|
|
|
(row: Row) => ({
|
|
|
|
_id: row._id,
|
|
|
|
primaryDisplay: row.email,
|
2023-10-11 19:10:58 +02:00
|
|
|
email: row.email,
|
|
|
|
firstName: row.firstName,
|
|
|
|
lastName: row.lastName,
|
2023-09-29 12:48:55 +02:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
])("links - %s", (__, relSchema, dataGenerator, resultMapper) => {
|
2023-09-27 16:51:42 +02:00
|
|
|
let tableId: string
|
2023-09-29 12:48:55 +02:00
|
|
|
let o2mData: Row[]
|
|
|
|
let m2mData: Row[]
|
2023-09-27 18:32:18 +02:00
|
|
|
|
2023-09-27 16:51:42 +02:00
|
|
|
beforeAll(async () => {
|
|
|
|
const tableConfig = generateTableConfig()
|
2023-09-27 18:32:18 +02:00
|
|
|
|
|
|
|
if (config.datasource) {
|
2023-10-25 20:00:25 +02:00
|
|
|
tableConfig.sourceId = config.datasource._id!
|
2023-09-27 18:32:18 +02:00
|
|
|
if (config.datasource.plus) {
|
2023-10-26 14:19:09 +02:00
|
|
|
tableConfig.sourceType = TableSourceType.EXTERNAL
|
2023-09-27 18:32:18 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 16:51:42 +02:00
|
|
|
const table = await config.api.table.create({
|
|
|
|
...tableConfig,
|
|
|
|
schema: {
|
|
|
|
...tableConfig.schema,
|
2023-09-29 12:48:55 +02:00
|
|
|
...relSchema(),
|
2023-09-27 16:51:42 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
tableId = table._id!
|
2023-09-27 17:35:16 +02:00
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
o2mData = [
|
|
|
|
await dataGenerator(o2mTable._id!),
|
|
|
|
await dataGenerator(o2mTable._id!),
|
|
|
|
await dataGenerator(o2mTable._id!),
|
|
|
|
await dataGenerator(o2mTable._id!),
|
|
|
|
]
|
|
|
|
|
|
|
|
m2mData = [
|
|
|
|
await dataGenerator(m2mTable._id!),
|
|
|
|
await dataGenerator(m2mTable._id!),
|
|
|
|
await dataGenerator(m2mTable._id!),
|
|
|
|
await dataGenerator(m2mTable._id!),
|
2023-09-27 17:35:16 +02:00
|
|
|
]
|
2023-09-27 16:51:42 +02:00
|
|
|
})
|
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("can save a row when relationship fields are empty", async () => {
|
2023-09-27 16:51:42 +02:00
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
expect(row).toEqual({
|
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
|
|
|
_id: expect.any(String),
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
|
|
|
type: isInternal ? "row" : undefined,
|
2023-09-27 16:51:42 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("can save a row with a single relationship field", async () => {
|
|
|
|
const user = _.sample(o2mData)!
|
2023-09-27 16:51:42 +02:00
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
user: [user],
|
2023-09-27 16:51:42 +02:00
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
expect(row).toEqual({
|
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
2023-09-29 12:48:55 +02:00
|
|
|
user: [user].map(u => resultMapper(u)),
|
2023-09-27 16:51:42 +02:00
|
|
|
_id: expect.any(String),
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
|
|
|
type: isInternal ? "row" : undefined,
|
2023-09-29 12:48:55 +02:00
|
|
|
[`fk_${o2mTable.name}_fk_o2m`]: isInternal ? undefined : user.id,
|
2023-09-27 16:51:42 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("can save a row with a multiple relationship field", async () => {
|
|
|
|
const selectedUsers = _.sampleSize(m2mData, 2)
|
2023-09-27 16:51:42 +02:00
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-27 17:35:16 +02:00
|
|
|
users: selectedUsers,
|
2023-09-27 16:51:42 +02:00
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
expect(row).toEqual({
|
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
2023-09-29 12:48:55 +02:00
|
|
|
users: expect.arrayContaining(selectedUsers.map(u => resultMapper(u))),
|
2023-09-27 16:51:42 +02:00
|
|
|
_id: expect.any(String),
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
|
|
|
type: isInternal ? "row" : undefined,
|
2023-09-27 16:51:42 +02:00
|
|
|
})
|
2023-09-27 17:16:37 +02:00
|
|
|
})
|
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("can retrieve rows with no populated relationships", async () => {
|
2023-09-27 17:16:37 +02:00
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
const { body: retrieved } = await config.api.row.get(tableId, row._id!)
|
|
|
|
expect(retrieved).toEqual({
|
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
|
|
|
user: undefined,
|
|
|
|
users: undefined,
|
|
|
|
_id: row._id,
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
|
|
|
...defaultRowFields,
|
2023-09-27 17:16:37 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("can retrieve rows with populated relationships", async () => {
|
|
|
|
const user1 = _.sample(o2mData)!
|
|
|
|
const [user2, user3] = _.sampleSize(m2mData, 2)
|
2023-09-27 17:16:37 +02:00
|
|
|
|
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [user2, user3],
|
2023-09-27 17:16:37 +02:00
|
|
|
user: [user1],
|
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
const { body: retrieved } = await config.api.row.get(tableId, row._id!)
|
|
|
|
expect(retrieved).toEqual({
|
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
2023-09-29 12:48:55 +02:00
|
|
|
user: expect.arrayContaining([user1].map(u => resultMapper(u))),
|
|
|
|
users: expect.arrayContaining([user2, user3].map(u => resultMapper(u))),
|
2023-09-27 17:16:37 +02:00
|
|
|
_id: row._id,
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
2023-09-29 12:48:55 +02:00
|
|
|
[`fk_${o2mTable.name}_fk_o2m`]: isInternal ? undefined : user1.id,
|
2023-09-27 18:32:18 +02:00
|
|
|
...defaultRowFields,
|
2023-09-27 17:16:37 +02:00
|
|
|
})
|
2023-09-27 16:51:42 +02:00
|
|
|
})
|
2023-09-27 17:03:16 +02:00
|
|
|
|
|
|
|
it("can update an existing populated row", async () => {
|
2023-09-29 12:48:55 +02:00
|
|
|
const user = _.sample(o2mData)!
|
|
|
|
const [users1, users2, users3] = _.sampleSize(m2mData, 3)
|
2023-09-27 17:03:16 +02:00
|
|
|
|
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users1, users2],
|
2023-09-27 17:03:16 +02:00
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
const updatedRow = await config.api.row.save(tableId, {
|
|
|
|
...row,
|
2023-09-29 12:48:55 +02:00
|
|
|
user: [user],
|
|
|
|
users: [users3, users1],
|
2023-09-27 17:03:16 +02:00
|
|
|
})
|
|
|
|
expect(updatedRow).toEqual({
|
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
2023-09-29 12:48:55 +02:00
|
|
|
user: expect.arrayContaining([user].map(u => resultMapper(u))),
|
|
|
|
users: expect.arrayContaining(
|
|
|
|
[users3, users1].map(u => resultMapper(u))
|
|
|
|
),
|
2023-09-27 17:03:16 +02:00
|
|
|
_id: row._id,
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
|
|
|
type: isInternal ? "row" : undefined,
|
2023-09-29 12:48:55 +02:00
|
|
|
[`fk_${o2mTable.name}_fk_o2m`]: isInternal ? undefined : user.id,
|
2023-09-27 17:03:16 +02:00
|
|
|
})
|
|
|
|
})
|
2023-09-27 17:08:29 +02:00
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("can wipe an existing populated relationships in row", async () => {
|
|
|
|
const [user1, user2] = _.sampleSize(m2mData, 2)
|
2023-09-27 17:08:29 +02:00
|
|
|
|
|
|
|
const rowData = {
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
|
|
|
users: [user1, user2],
|
|
|
|
}
|
|
|
|
const row = await config.api.row.save(tableId, rowData)
|
|
|
|
|
|
|
|
const updatedRow = await config.api.row.save(tableId, {
|
|
|
|
...row,
|
|
|
|
user: null,
|
|
|
|
users: null,
|
|
|
|
})
|
2023-09-29 14:40:40 +02:00
|
|
|
expect(updatedRow).toEqual({
|
2023-09-27 17:08:29 +02:00
|
|
|
name: rowData.name,
|
|
|
|
description: rowData.description,
|
|
|
|
tableId,
|
|
|
|
_id: row._id,
|
|
|
|
_rev: expect.any(String),
|
2023-09-29 14:58:41 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
|
|
|
type: isInternal ? "row" : undefined,
|
2023-09-29 14:40:40 +02:00
|
|
|
})
|
2023-09-27 17:08:29 +02:00
|
|
|
})
|
2023-09-27 17:48:30 +02:00
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("fetch all will populate the relationships", async () => {
|
|
|
|
const [user1] = _.sampleSize(o2mData, 1)
|
|
|
|
const [users1, users2, users3] = _.sampleSize(m2mData, 3)
|
2023-09-27 17:48:30 +02:00
|
|
|
|
|
|
|
const rows: {
|
|
|
|
name: string
|
|
|
|
description: string
|
2023-09-29 12:48:55 +02:00
|
|
|
user?: Row[]
|
|
|
|
users?: Row[]
|
2023-09-27 17:48:30 +02:00
|
|
|
tableId: string
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users1, users2],
|
2023-09-27 17:48:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
|
|
|
user: [user1],
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users1, users3],
|
2023-09-27 17:48:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users3],
|
2023-09-27 17:48:30 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
await config.api.row.save(tableId, rows[0])
|
|
|
|
await config.api.row.save(tableId, rows[1])
|
|
|
|
await config.api.row.save(tableId, rows[2])
|
|
|
|
|
|
|
|
const res = await config.api.row.fetch(tableId)
|
|
|
|
|
|
|
|
expect(res).toEqual(
|
|
|
|
expect.arrayContaining(
|
|
|
|
rows.map(r => ({
|
|
|
|
name: r.name,
|
|
|
|
description: r.description,
|
|
|
|
tableId,
|
2023-09-29 12:48:55 +02:00
|
|
|
user: r.user?.map(u => resultMapper(u)),
|
2023-09-29 12:54:16 +02:00
|
|
|
users: r.users?.length
|
|
|
|
? expect.arrayContaining(r.users?.map(u => resultMapper(u)))
|
|
|
|
: undefined,
|
2023-09-27 17:48:30 +02:00
|
|
|
_id: expect.any(String),
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
2023-09-29 12:48:55 +02:00
|
|
|
[`fk_${o2mTable.name}_fk_o2m`]:
|
|
|
|
isInternal || !r.user?.length ? undefined : r.user[0].id,
|
2023-09-27 18:32:18 +02:00
|
|
|
...defaultRowFields,
|
2023-09-27 17:48:30 +02:00
|
|
|
}))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-09-29 12:48:55 +02:00
|
|
|
it("search all will populate the relationships", async () => {
|
|
|
|
const [user1] = _.sampleSize(o2mData, 1)
|
|
|
|
const [users1, users2, users3] = _.sampleSize(m2mData, 3)
|
2023-09-27 17:48:30 +02:00
|
|
|
|
|
|
|
const rows: {
|
|
|
|
name: string
|
|
|
|
description: string
|
2023-09-29 12:48:55 +02:00
|
|
|
user?: Row[]
|
|
|
|
users?: Row[]
|
2023-09-27 17:48:30 +02:00
|
|
|
tableId: string
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users1, users2],
|
2023-09-27 17:48:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
|
|
|
user: [user1],
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users1, users3],
|
2023-09-27 17:48:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
...basicRow(tableId),
|
|
|
|
name: generator.name(),
|
|
|
|
description: generator.name(),
|
2023-09-29 12:48:55 +02:00
|
|
|
users: [users3],
|
2023-09-27 17:48:30 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
await config.api.row.save(tableId, rows[0])
|
|
|
|
await config.api.row.save(tableId, rows[1])
|
|
|
|
await config.api.row.save(tableId, rows[2])
|
|
|
|
|
|
|
|
const res = await config.api.row.search(tableId)
|
|
|
|
|
|
|
|
expect(res).toEqual({
|
|
|
|
rows: expect.arrayContaining(
|
|
|
|
rows.map(r => ({
|
|
|
|
name: r.name,
|
|
|
|
description: r.description,
|
|
|
|
tableId,
|
2023-09-29 12:48:55 +02:00
|
|
|
user: r.user?.map(u => resultMapper(u)),
|
|
|
|
users: r.users?.length
|
|
|
|
? expect.arrayContaining(r.users?.map(u => resultMapper(u)))
|
|
|
|
: undefined,
|
2023-09-27 17:48:30 +02:00
|
|
|
_id: expect.any(String),
|
|
|
|
_rev: expect.any(String),
|
2023-09-27 18:32:18 +02:00
|
|
|
id: isInternal ? undefined : expect.any(Number),
|
2023-09-29 12:48:55 +02:00
|
|
|
[`fk_${o2mTable.name}_fk_o2m`]:
|
|
|
|
isInternal || !r.user?.length ? undefined : r.user[0].id,
|
2023-09-27 18:32:18 +02:00
|
|
|
...defaultRowFields,
|
2023-09-27 17:48:30 +02:00
|
|
|
}))
|
|
|
|
),
|
2023-09-27 18:32:18 +02:00
|
|
|
...(isInternal
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
hasNextPage: false,
|
|
|
|
bookmark: null,
|
|
|
|
}),
|
2023-09-27 17:48:30 +02:00
|
|
|
})
|
|
|
|
})
|
2023-09-27 16:51:42 +02:00
|
|
|
})
|
2023-11-07 19:34:51 +01:00
|
|
|
|
|
|
|
describe("Formula fields", () => {
|
|
|
|
let relationshipTable: Table, tableId: string, relatedRow: Row
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const otherTableId = config.table!._id!
|
|
|
|
const cfg = generateTableConfig()
|
|
|
|
relationshipTable = await config.createLinkedTable(
|
|
|
|
RelationshipType.ONE_TO_MANY,
|
|
|
|
["links"],
|
|
|
|
{
|
|
|
|
...cfg,
|
2023-11-08 15:57:30 +01:00
|
|
|
// needs to be a short name
|
|
|
|
name: "b",
|
2023-11-07 19:34:51 +01:00
|
|
|
schema: {
|
|
|
|
...cfg.schema,
|
|
|
|
formula: {
|
|
|
|
name: "formula",
|
|
|
|
type: FieldType.FORMULA,
|
|
|
|
formula: "{{ links.0.name }}",
|
|
|
|
formulaType: FormulaTypes.DYNAMIC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
tableId = relationshipTable._id!
|
|
|
|
|
|
|
|
relatedRow = await config.api.row.save(otherTableId, {
|
|
|
|
name: generator.word(),
|
|
|
|
description: generator.paragraph(),
|
|
|
|
})
|
|
|
|
await config.api.row.save(tableId, {
|
|
|
|
name: generator.word(),
|
|
|
|
description: generator.paragraph(),
|
|
|
|
tableId,
|
|
|
|
links: [relatedRow._id],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to search for rows containing formulas", async () => {
|
|
|
|
const { rows } = await config.api.row.search(tableId)
|
|
|
|
expect(rows.length).toBe(1)
|
|
|
|
expect(rows[0].links.length).toBe(1)
|
|
|
|
const row = rows[0]
|
|
|
|
expect(row.formula).toBe(relatedRow.name)
|
|
|
|
})
|
|
|
|
})
|
2021-09-30 13:55:21 +02:00
|
|
|
})
|