2023-07-12 16:13:00 +02:00
|
|
|
import * as setup from "./utilities"
|
2023-07-19 15:43:21 +02:00
|
|
|
import {
|
|
|
|
CreateViewRequest,
|
2024-03-14 18:11:09 +01:00
|
|
|
Datasource,
|
2023-08-01 12:08:40 +02:00
|
|
|
FieldSchema,
|
2023-07-19 15:43:21 +02:00
|
|
|
FieldType,
|
2023-10-26 14:19:09 +02:00
|
|
|
INTERNAL_TABLE_SOURCE_ID,
|
2024-03-15 18:03:47 +01:00
|
|
|
PermissionLevel,
|
|
|
|
QuotaUsageType,
|
2024-06-19 19:57:37 +02:00
|
|
|
Row,
|
2024-03-14 18:11:09 +01:00
|
|
|
SaveTableRequest,
|
2023-07-19 15:43:21 +02:00
|
|
|
SortOrder,
|
|
|
|
SortType,
|
2024-03-15 18:03:47 +01:00
|
|
|
StaticQuotaName,
|
2023-07-19 15:43:21 +02:00
|
|
|
Table,
|
2023-10-26 14:19:09 +02:00
|
|
|
TableSourceType,
|
2023-08-02 13:37:58 +02:00
|
|
|
UpdateViewRequest,
|
2023-07-19 15:43:21 +02:00
|
|
|
ViewV2,
|
2024-06-19 19:57:37 +02:00
|
|
|
SearchResponse,
|
2024-07-10 12:08:11 +02:00
|
|
|
BasicOperator,
|
2024-09-04 10:29:05 +02:00
|
|
|
CalculationType,
|
2024-09-02 17:32:21 +02:00
|
|
|
RelationshipType,
|
2024-09-03 14:44:55 +02:00
|
|
|
TableSchema,
|
|
|
|
RenameColumn,
|
2024-09-24 17:51:05 +02:00
|
|
|
BBReferenceFieldSubType,
|
2024-10-02 17:42:05 +02:00
|
|
|
NumericCalculationFieldMetadata,
|
2024-10-03 10:28:29 +02:00
|
|
|
ViewV2Schema,
|
2024-10-08 12:50:16 +02:00
|
|
|
ViewV2Type,
|
2024-10-08 16:34:34 +02:00
|
|
|
JsonTypes,
|
2023-07-19 15:43:21 +02:00
|
|
|
} from "@budibase/types"
|
2024-03-15 18:03:47 +01:00
|
|
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
2024-03-26 15:43:17 +01:00
|
|
|
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
2024-03-14 18:11:09 +01:00
|
|
|
import merge from "lodash/merge"
|
2024-03-15 18:03:47 +01:00
|
|
|
import { quotas } from "@budibase/pro"
|
2024-10-08 12:15:53 +02:00
|
|
|
import { db, roles, features } from "@budibase/backend-core"
|
2023-07-12 16:13:00 +02:00
|
|
|
|
2023-08-29 16:13:44 +02:00
|
|
|
describe.each([
|
2024-10-01 16:04:01 +02:00
|
|
|
["lucene", undefined],
|
|
|
|
["sqs", undefined],
|
2024-09-26 16:22:10 +02:00
|
|
|
[DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)],
|
2024-10-01 16:04:01 +02:00
|
|
|
[DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)],
|
|
|
|
[DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)],
|
|
|
|
[DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)],
|
|
|
|
[DatabaseName.ORACLE, getDatasource(DatabaseName.ORACLE)],
|
2024-06-19 19:57:37 +02:00
|
|
|
])("/v2/views (%s)", (name, dsProvider) => {
|
2024-03-14 18:11:09 +01:00
|
|
|
const config = setup.getConfig()
|
2024-06-19 19:57:37 +02:00
|
|
|
const isSqs = name === "sqs"
|
|
|
|
const isLucene = name === "lucene"
|
|
|
|
const isInternal = isSqs || isLucene
|
2023-07-12 16:13:00 +02:00
|
|
|
|
2023-08-29 16:13:44 +02:00
|
|
|
let table: Table
|
2024-03-14 18:11:09 +01:00
|
|
|
let datasource: Datasource
|
2024-06-19 19:57:37 +02:00
|
|
|
let envCleanup: (() => void) | undefined
|
2024-03-14 18:11:09 +01:00
|
|
|
|
|
|
|
function saveTableRequest(
|
2024-03-26 15:43:17 +01:00
|
|
|
...overrides: Partial<Omit<SaveTableRequest, "name">>[]
|
2024-03-14 18:11:09 +01:00
|
|
|
): SaveTableRequest {
|
|
|
|
const req: SaveTableRequest = {
|
2024-03-26 15:43:17 +01:00
|
|
|
name: generator.guid().replaceAll("-", "").substring(0, 16),
|
2024-03-14 18:11:09 +01:00
|
|
|
type: "table",
|
|
|
|
sourceType: datasource
|
|
|
|
? TableSourceType.EXTERNAL
|
|
|
|
: TableSourceType.INTERNAL,
|
|
|
|
sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID,
|
|
|
|
primary: ["id"],
|
|
|
|
schema: {
|
|
|
|
id: {
|
2024-07-30 18:44:00 +02:00
|
|
|
type: FieldType.NUMBER,
|
2024-03-14 18:11:09 +01:00
|
|
|
name: "id",
|
|
|
|
autocolumn: true,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return merge(req, ...overrides)
|
|
|
|
}
|
|
|
|
|
|
|
|
function priceTable(): SaveTableRequest {
|
|
|
|
return saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
Price: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "Price",
|
|
|
|
constraints: {},
|
|
|
|
},
|
|
|
|
Category: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "Category",
|
|
|
|
constraints: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-07-12 16:13:00 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2024-10-07 19:18:04 +02:00
|
|
|
await features.testutils.withFeatureFlags("*", { SQS: isSqs }, () =>
|
2024-08-09 11:30:27 +02:00
|
|
|
config.init()
|
2024-07-08 15:21:07 +02:00
|
|
|
)
|
2024-10-07 19:18:04 +02:00
|
|
|
|
|
|
|
envCleanup = features.testutils.setFeatureFlags("*", {
|
|
|
|
SQS: isSqs,
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
|
|
|
|
if (dsProvider) {
|
|
|
|
datasource = await config.createDatasource({
|
2024-03-26 15:43:17 +01:00
|
|
|
datasource: await dsProvider,
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
table = await config.api.table.save(priceTable())
|
2023-07-12 16:13:00 +02:00
|
|
|
})
|
|
|
|
|
2024-03-14 18:11:09 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
setup.afterAll()
|
2024-06-19 19:57:37 +02:00
|
|
|
if (envCleanup) {
|
|
|
|
envCleanup()
|
|
|
|
}
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
2023-08-29 16:13:44 +02:00
|
|
|
|
2024-05-27 14:31:45 +02:00
|
|
|
beforeEach(() => {
|
2024-08-09 14:29:19 +02:00
|
|
|
jest.clearAllMocks()
|
2024-05-27 14:31:45 +02:00
|
|
|
mocks.licenses.useCloudFree()
|
|
|
|
})
|
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
describe("view crud", () => {
|
2024-08-28 14:20:39 +02:00
|
|
|
describe("create", () => {
|
|
|
|
it("persist the view when the view is successfully created", async () => {
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const res = await config.api.viewV2.create(newView)
|
2023-07-12 16:13:00 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(res).toEqual({
|
|
|
|
...newView,
|
|
|
|
id: expect.stringMatching(new RegExp(`${table._id!}_`)),
|
|
|
|
version: 2,
|
|
|
|
})
|
2023-07-12 16:13:00 +02:00
|
|
|
})
|
2023-07-18 14:34:23 +02:00
|
|
|
|
2024-10-16 19:28:40 +02:00
|
|
|
it.only("can persist views with all fields", async () => {
|
2024-10-07 17:33:14 +02:00
|
|
|
const newView: Required<Omit<CreateViewRequest, "queryUI" | "type">> = {
|
2024-08-28 14:20:39 +02:00
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
primaryDisplay: "id",
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.EQUAL,
|
|
|
|
field: "field",
|
|
|
|
value: "value",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
sort: {
|
|
|
|
field: "fieldToSort",
|
|
|
|
order: SortOrder.DESCENDING,
|
|
|
|
type: SortType.STRING,
|
2023-10-12 20:00:53 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: {
|
|
|
|
visible: true,
|
|
|
|
},
|
2023-08-02 13:37:58 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
}
|
|
|
|
const res = await config.api.viewV2.create(newView)
|
2023-07-18 14:34:23 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(res).toEqual({
|
|
|
|
...newView,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: {
|
|
|
|
visible: true,
|
|
|
|
},
|
2024-05-24 16:07:07 +02:00
|
|
|
},
|
2024-10-16 19:28:40 +02:00
|
|
|
queryUI: {},
|
2024-08-28 14:20:39 +02:00
|
|
|
id: expect.any(String),
|
|
|
|
version: 2,
|
|
|
|
})
|
2023-07-18 14:34:23 +02:00
|
|
|
})
|
2023-08-01 10:45:00 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("persist only UI schema overrides", async () => {
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: {
|
|
|
|
name: "id",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
Price: {
|
|
|
|
name: "Price",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
visible: true,
|
|
|
|
order: 1,
|
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
Category: {
|
|
|
|
name: "Category",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
visible: false,
|
|
|
|
icon: "ic",
|
|
|
|
},
|
|
|
|
} as Record<string, FieldSchema>,
|
|
|
|
}
|
2023-08-01 10:45:00 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
const createdView = await config.api.viewV2.create(newView)
|
2023-08-01 10:45:00 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(createdView).toEqual({
|
|
|
|
...newView,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: {
|
|
|
|
visible: true,
|
|
|
|
order: 1,
|
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
Category: {
|
|
|
|
visible: false,
|
|
|
|
icon: "ic",
|
|
|
|
},
|
2024-07-02 13:58:16 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
id: createdView.id,
|
|
|
|
version: 2,
|
|
|
|
})
|
2023-08-01 10:45:00 +02:00
|
|
|
})
|
2023-08-01 11:31:58 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("will not throw an exception if the schema is 'deleting' non UI fields", async () => {
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: {
|
|
|
|
name: "id",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
autocolumn: true,
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
Price: {
|
|
|
|
name: "Price",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
Category: {
|
|
|
|
name: "Category",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
} as Record<string, FieldSchema>,
|
|
|
|
}
|
2023-08-01 11:31:58 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
await config.api.viewV2.create(newView, {
|
|
|
|
status: 201,
|
|
|
|
})
|
2023-08-01 11:31:58 +02:00
|
|
|
})
|
2024-05-24 16:15:24 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("does not persist non-visible fields", async () => {
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
primaryDisplay: "id",
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: { visible: true },
|
|
|
|
Category: { visible: false },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const res = await config.api.viewV2.create(newView)
|
2024-05-24 16:15:24 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(res).toEqual({
|
|
|
|
...newView,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: { visible: true },
|
|
|
|
Category: { visible: false },
|
2024-05-24 16:15:24 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
id: expect.any(String),
|
|
|
|
version: 2,
|
|
|
|
})
|
2024-05-24 16:15:24 +02:00
|
|
|
})
|
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("throws bad request when the schema fields are not valid", async () => {
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
nonExisting: {
|
|
|
|
visible: true,
|
|
|
|
},
|
2024-05-24 16:15:24 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
}
|
|
|
|
await config.api.viewV2.create(newView, {
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: 'Field "nonExisting" is not valid for the requested table',
|
|
|
|
},
|
|
|
|
})
|
2024-05-24 16:15:24 +02:00
|
|
|
})
|
2024-05-27 13:39:43 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
describe("readonly fields", () => {
|
|
|
|
it("readonly fields are persisted", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
2024-05-27 14:31:45 +02:00
|
|
|
schema: {
|
2024-08-28 14:20:39 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 14:31:45 +02:00
|
|
|
name: {
|
2024-08-28 14:20:39 +02:00
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
2024-05-27 14:31:45 +02:00
|
|
|
},
|
|
|
|
description: {
|
2024-08-28 14:20:39 +02:00
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
2024-05-27 14:31:45 +02:00
|
|
|
},
|
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
}
|
2024-05-27 14:31:45 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
const res = await config.api.viewV2.create(newView)
|
|
|
|
expect(res.schema).toEqual({
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 13:59:39 +02:00
|
|
|
name: {
|
2024-05-27 14:31:45 +02:00
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
2024-05-27 13:59:39 +02:00
|
|
|
},
|
|
|
|
description: {
|
2024-05-27 14:31:45 +02:00
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
2024-05-27 13:59:39 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2024-05-27 14:31:45 +02:00
|
|
|
})
|
2024-05-27 13:59:39 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("required fields cannot be marked as readonly", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: { presence: true },
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
2024-05-27 14:31:45 +02:00
|
|
|
schema: {
|
2024-08-28 14:20:39 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 14:31:45 +02:00
|
|
|
name: {
|
2024-08-28 14:20:39 +02:00
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
2024-05-27 14:31:45 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
await config.api.viewV2.create(newView, {
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
'You can\'t make "name" readonly because it is a required field.',
|
|
|
|
status: 400,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("readonly fields must be visible", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
2024-05-27 14:31:45 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
name: {
|
|
|
|
visible: false,
|
|
|
|
readonly: true,
|
2024-05-27 14:31:45 +02:00
|
|
|
},
|
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await config.api.viewV2.create(newView, {
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
'Field "name" must be visible if you want to make it readonly',
|
|
|
|
status: 400,
|
2024-05-27 14:31:45 +02:00
|
|
|
},
|
|
|
|
})
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2024-05-27 13:39:43 +02:00
|
|
|
|
2024-09-18 13:35:05 +02:00
|
|
|
it("readonly fields can be used on free license", async () => {
|
2024-08-28 14:20:39 +02:00
|
|
|
mocks.licenses.useCloudFree()
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
name: {
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
},
|
2024-05-27 13:39:43 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
}
|
2024-05-27 13:39:43 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
await config.api.viewV2.create(newView, {
|
2024-09-18 14:17:30 +02:00
|
|
|
status: 201,
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2024-05-27 14:31:45 +02:00
|
|
|
})
|
2024-05-27 13:39:43 +02:00
|
|
|
})
|
2024-05-27 14:16:03 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("display fields must be visible", async () => {
|
2024-05-27 14:31:45 +02:00
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
2024-08-28 14:20:39 +02:00
|
|
|
primaryDisplay: "name",
|
2024-05-27 14:16:03 +02:00
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 14:16:03 +02:00
|
|
|
name: {
|
2024-05-27 14:31:45 +02:00
|
|
|
visible: false,
|
2024-05-27 14:16:03 +02:00
|
|
|
},
|
|
|
|
},
|
2024-05-27 14:31:45 +02:00
|
|
|
}
|
2024-05-27 14:16:03 +02:00
|
|
|
|
2024-05-27 14:31:45 +02:00
|
|
|
await config.api.viewV2.create(newView, {
|
2024-05-27 14:16:03 +02:00
|
|
|
status: 400,
|
2024-05-27 14:31:45 +02:00
|
|
|
body: {
|
2024-08-28 14:20:39 +02:00
|
|
|
message: 'You can\'t hide "name" because it is the display column.',
|
2024-05-27 14:31:45 +02:00
|
|
|
status: 400,
|
|
|
|
},
|
|
|
|
})
|
2024-05-27 14:16:03 +02:00
|
|
|
})
|
2024-05-27 14:44:00 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("display fields can be readonly", async () => {
|
2024-05-27 14:44:00 +02:00
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
name: "description",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const newView: CreateViewRequest = {
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
2024-08-28 14:20:39 +02:00
|
|
|
primaryDisplay: "name",
|
2024-05-27 14:44:00 +02:00
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 14:44:00 +02:00
|
|
|
name: {
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
await config.api.viewV2.create(newView, {
|
2024-08-28 14:20:39 +02:00
|
|
|
status: 201,
|
2024-05-27 14:44:00 +02:00
|
|
|
})
|
|
|
|
})
|
2024-10-02 10:35:47 +02:00
|
|
|
|
2024-10-02 11:36:45 +02:00
|
|
|
it("can create a view with calculation fields", async () => {
|
2024-10-02 10:35:47 +02:00
|
|
|
let view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-02 10:35:47 +02:00
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-10-02 11:36:45 +02:00
|
|
|
expect(Object.keys(view.schema!)).toHaveLength(1)
|
|
|
|
|
2024-10-02 17:42:05 +02:00
|
|
|
let sum = view.schema!.sum as NumericCalculationFieldMetadata
|
2024-10-02 10:35:47 +02:00
|
|
|
expect(sum).toBeDefined()
|
|
|
|
expect(sum.calculationType).toEqual(CalculationType.SUM)
|
|
|
|
expect(sum.field).toEqual("Price")
|
|
|
|
|
|
|
|
view = await config.api.viewV2.get(view.id)
|
2024-10-02 17:42:05 +02:00
|
|
|
sum = view.schema!.sum as NumericCalculationFieldMetadata
|
2024-10-02 10:35:47 +02:00
|
|
|
expect(sum).toBeDefined()
|
|
|
|
expect(sum.calculationType).toEqual(CalculationType.SUM)
|
|
|
|
expect(sum.field).toEqual("Price")
|
|
|
|
})
|
2024-10-04 11:45:03 +02:00
|
|
|
|
2024-10-07 17:33:14 +02:00
|
|
|
it("cannot create a view with calculation fields unless it has the right type", async () => {
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
"Calculation fields are not allowed in non-calculation views",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-10-08 12:10:23 +02:00
|
|
|
|
2024-10-04 11:45:03 +02:00
|
|
|
it("cannot create a calculation view with more than 5 aggregations", async () => {
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-08 12:19:43 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-04 11:45:03 +02:00
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
min: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.MIN,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.MAX,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
avg: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.AVG,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
sum2: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: "Calculation views can only have a maximum of 5 fields",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-10-04 12:09:29 +02:00
|
|
|
|
|
|
|
it("cannot create a calculation view with duplicate calculations", async () => {
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-08 13:31:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-04 12:09:29 +02:00
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
sum2: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
'Duplicate calculation on field "Price", calculation type "sum"',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("finds duplicate counts", async () => {
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-08 13:31:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-04 12:09:29 +02:00
|
|
|
schema: {
|
|
|
|
count: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
},
|
|
|
|
count2: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
'Duplicate calculation on field "*", calculation type "count"',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("finds duplicate count distincts", async () => {
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-08 13:31:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-04 12:09:29 +02:00
|
|
|
schema: {
|
|
|
|
count: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
distinct: true,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
count2: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
distinct: true,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
'Duplicate calculation on field "Price", calculation type "count"',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not confuse counts and count distincts in the duplicate check", async () => {
|
|
|
|
await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-08 13:31:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-04 12:09:29 +02:00
|
|
|
schema: {
|
|
|
|
count: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
},
|
|
|
|
count2: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
distinct: true,
|
|
|
|
field: "Price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-10-08 12:50:16 +02:00
|
|
|
|
2024-10-08 18:04:45 +02:00
|
|
|
!isLucene &&
|
|
|
|
it("does not get confused when a calculation field shadows a basic one", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
age: {
|
|
|
|
name: "age",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [{ age: 1 }, { age: 2 }, { age: 3 }],
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
age: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "age",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(1)
|
|
|
|
expect(rows[0].age).toEqual(6)
|
|
|
|
})
|
|
|
|
|
2024-10-08 16:34:34 +02:00
|
|
|
// We don't allow the creation of tables with most JsonTypes when using
|
|
|
|
// external datasources.
|
|
|
|
isInternal &&
|
|
|
|
it("cannot use complex types as group-by fields", async () => {
|
|
|
|
for (const type of JsonTypes) {
|
|
|
|
const field = { name: "field", type } as FieldSchema
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({ schema: { field } })
|
|
|
|
)
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
field: { visible: true },
|
|
|
|
},
|
2024-10-08 12:50:16 +02:00
|
|
|
},
|
2024-10-08 16:34:34 +02:00
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: `Grouping by fields of type "${type}" is not supported`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2024-10-10 10:16:43 +02:00
|
|
|
|
|
|
|
isInternal &&
|
|
|
|
it("shouldn't trigger a complex type check on a group by field if field is invisible", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
field: {
|
|
|
|
name: "field",
|
|
|
|
type: FieldType.JSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
field: { visible: false },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 201,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-05-27 14:16:03 +02:00
|
|
|
})
|
2024-06-04 12:33:48 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
describe("update", () => {
|
|
|
|
let view: ViewV2
|
2024-06-04 12:33:48 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
table = await config.api.table.save(priceTable())
|
2024-06-04 12:33:48 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-06-04 12:33:48 +02:00
|
|
|
schema: {
|
2024-08-28 14:20:39 +02:00
|
|
|
id: { visible: true },
|
2024-06-04 12:33:48 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2023-07-25 15:34:25 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("can update an existing view data", async () => {
|
|
|
|
const tableId = table._id!
|
|
|
|
await config.api.viewV2.update({
|
2023-07-25 15:41:04 +02:00
|
|
|
...view,
|
2023-10-12 20:00:53 +02:00
|
|
|
query: [
|
|
|
|
{
|
2024-07-10 12:08:11 +02:00
|
|
|
operator: BasicOperator.EQUAL,
|
2023-10-12 20:00:53 +02:00
|
|
|
field: "newField",
|
|
|
|
value: "thatValue",
|
|
|
|
},
|
|
|
|
],
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2023-07-25 15:34:25 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect((await config.api.table.get(tableId)).views).toEqual({
|
|
|
|
[view.name]: {
|
|
|
|
...view,
|
|
|
|
query: [
|
|
|
|
{ operator: "equal", field: "newField", value: "thatValue" },
|
|
|
|
],
|
|
|
|
schema: expect.anything(),
|
2023-10-12 20:00:53 +02:00
|
|
|
},
|
2023-07-25 15:41:04 +02:00
|
|
|
})
|
2023-08-02 13:37:58 +02:00
|
|
|
})
|
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("can update all fields", async () => {
|
|
|
|
const tableId = table._id!
|
|
|
|
|
2024-10-07 17:33:14 +02:00
|
|
|
const updatedData: Required<
|
|
|
|
Omit<UpdateViewRequest, "queryUI" | "type">
|
|
|
|
> = {
|
2024-08-28 14:20:39 +02:00
|
|
|
version: view.version,
|
|
|
|
id: view.id,
|
|
|
|
tableId,
|
|
|
|
name: view.name,
|
|
|
|
primaryDisplay: "Price",
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.EQUAL,
|
|
|
|
field: generator.word(),
|
|
|
|
value: generator.word(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
sort: {
|
2023-08-07 13:16:23 +02:00
|
|
|
field: generator.word(),
|
2024-08-28 14:20:39 +02:00
|
|
|
order: SortOrder.DESCENDING,
|
|
|
|
type: SortType.STRING,
|
2024-05-27 14:11:50 +02:00
|
|
|
},
|
2023-08-29 16:39:19 +02:00
|
|
|
schema: {
|
2024-08-28 14:20:39 +02:00
|
|
|
id: { visible: true },
|
|
|
|
Category: {
|
2023-08-29 16:39:19 +02:00
|
|
|
visible: false,
|
2024-08-28 14:20:39 +02:00
|
|
|
},
|
|
|
|
Price: {
|
2024-05-27 14:11:50 +02:00
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
2023-07-25 15:41:04 +02:00
|
|
|
},
|
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
}
|
|
|
|
await config.api.viewV2.update(updatedData)
|
2023-07-25 15:41:04 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect((await config.api.table.get(tableId)).views).toEqual({
|
|
|
|
[view.name]: {
|
|
|
|
...updatedData,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
}),
|
|
|
|
Category: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
}),
|
|
|
|
Price: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
2023-07-25 15:41:04 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("can update an existing view name", async () => {
|
|
|
|
const tableId = table._id!
|
|
|
|
const newName = generator.guid()
|
|
|
|
await config.api.viewV2.update({ ...view, name: newName })
|
2023-07-25 19:46:46 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(await config.api.table.get(tableId)).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
views: {
|
|
|
|
[newName]: { ...view, name: newName, schema: expect.anything() },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
2024-08-28 14:20:39 +02:00
|
|
|
|
|
|
|
it("cannot update an unexisting views nor edit ids", async () => {
|
|
|
|
const tableId = table._id!
|
|
|
|
await config.api.viewV2.update(
|
|
|
|
{ ...view, id: generator.guid() },
|
|
|
|
{ status: 404 }
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(await config.api.table.get(tableId)).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
views: {
|
|
|
|
[view.name]: {
|
|
|
|
...view,
|
|
|
|
schema: expect.anything(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2023-07-25 19:46:46 +02:00
|
|
|
})
|
2023-08-01 11:38:36 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("cannot update views with the wrong tableId", async () => {
|
|
|
|
const tableId = table._id!
|
|
|
|
await config.api.viewV2.update(
|
|
|
|
{
|
|
|
|
...view,
|
|
|
|
tableId: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.EQUAL,
|
|
|
|
field: "newField",
|
|
|
|
value: "thatValue",
|
|
|
|
},
|
|
|
|
],
|
2023-08-01 11:38:36 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
{ status: 404 }
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(await config.api.table.get(tableId)).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
views: {
|
|
|
|
[view.name]: {
|
|
|
|
...view,
|
|
|
|
schema: expect.anything(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-09-25 12:44:30 +02:00
|
|
|
isInternal &&
|
|
|
|
it("cannot update views v1", async () => {
|
|
|
|
const viewV1 = await config.api.legacyView.save({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
filters: [],
|
|
|
|
schema: {},
|
|
|
|
})
|
2024-08-28 14:20:39 +02:00
|
|
|
|
2024-09-25 12:44:30 +02:00
|
|
|
await config.api.viewV2.update(viewV1 as unknown as ViewV2, {
|
2024-08-28 14:20:39 +02:00
|
|
|
status: 400,
|
2024-09-25 12:44:30 +02:00
|
|
|
body: {
|
|
|
|
message: "Only views V2 can be updated",
|
|
|
|
status: 400,
|
|
|
|
},
|
|
|
|
})
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2023-08-01 11:38:36 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("cannot update the a view with unmatching ids between url and body", async () => {
|
|
|
|
const anotherView = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
2023-08-01 11:38:36 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
|
|
|
const result = await config
|
|
|
|
.request!.put(`/api/v2/views/${anotherView.id}`)
|
|
|
|
.send(view)
|
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(400)
|
|
|
|
|
|
|
|
expect(result.body).toEqual({
|
|
|
|
message: "View id does not match between the body and the uri path",
|
|
|
|
status: 400,
|
|
|
|
})
|
2023-08-01 11:38:36 +02:00
|
|
|
})
|
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("updates only UI schema overrides", async () => {
|
|
|
|
const updatedView = await config.api.viewV2.update({
|
2023-08-01 11:38:36 +02:00
|
|
|
...view,
|
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
...view.schema,
|
2023-08-01 11:38:36 +02:00
|
|
|
Price: {
|
|
|
|
name: "Price",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
visible: true,
|
2024-08-28 14:20:39 +02:00
|
|
|
order: 1,
|
|
|
|
width: 100,
|
2023-08-01 11:38:36 +02:00
|
|
|
},
|
|
|
|
Category: {
|
|
|
|
name: "Category",
|
|
|
|
type: FieldType.STRING,
|
2024-08-28 14:20:39 +02:00
|
|
|
visible: false,
|
|
|
|
icon: "ic",
|
2023-08-01 11:38:36 +02:00
|
|
|
},
|
2023-08-01 12:08:40 +02:00
|
|
|
} as Record<string, FieldSchema>,
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2024-05-27 14:44:00 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(updatedView).toEqual({
|
|
|
|
...view,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: {
|
|
|
|
visible: true,
|
|
|
|
order: 1,
|
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
Category: { visible: false, icon: "ic" },
|
2024-05-27 14:44:00 +02:00
|
|
|
},
|
2024-08-28 14:20:39 +02:00
|
|
|
id: view.id,
|
|
|
|
version: 2,
|
|
|
|
})
|
2024-05-27 14:44:00 +02:00
|
|
|
})
|
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("will not throw an exception if the schema is 'deleting' non UI fields", async () => {
|
|
|
|
await config.api.viewV2.update(
|
|
|
|
{
|
|
|
|
...view,
|
|
|
|
schema: {
|
|
|
|
...view.schema,
|
|
|
|
Price: {
|
|
|
|
name: "Price",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
Category: {
|
|
|
|
name: "Category",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
} as Record<string, FieldSchema>,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
}
|
|
|
|
)
|
2024-05-27 14:44:00 +02:00
|
|
|
})
|
2024-05-27 14:53:26 +02:00
|
|
|
|
2024-10-07 17:33:14 +02:00
|
|
|
it("cannot update view type after creation", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: {
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.viewV2.update(
|
|
|
|
{
|
|
|
|
...view,
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: "Cannot update view type after creation",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
isInternal &&
|
|
|
|
it("updating schema will only validate modified field", async () => {
|
|
|
|
let view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Price: {
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
Category: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
2023-07-12 18:09:13 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
// Update the view to an invalid state
|
|
|
|
const tableToUpdate = await config.api.table.get(table._id!)
|
|
|
|
;(tableToUpdate.views![view.name] as ViewV2).schema!.id.visible =
|
|
|
|
false
|
|
|
|
await db.getDB(config.appId!).put(tableToUpdate)
|
|
|
|
|
|
|
|
view = await config.api.viewV2.get(view.id)
|
|
|
|
await config.api.viewV2.update(
|
|
|
|
{
|
|
|
|
...view,
|
|
|
|
schema: {
|
|
|
|
...view.schema,
|
|
|
|
Price: {
|
|
|
|
visible: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: 'You can\'t hide "id" because it is a required field.',
|
|
|
|
status: 400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-10-02 17:42:05 +02:00
|
|
|
|
|
|
|
!isLucene &&
|
|
|
|
describe("calculation views", () => {
|
|
|
|
let table: Table
|
|
|
|
let view: ViewV2
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
country: {
|
|
|
|
name: "country",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
age: {
|
|
|
|
name: "age",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-02 17:42:05 +02:00
|
|
|
schema: {
|
|
|
|
country: {
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
age: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "age",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
name: "Steve",
|
|
|
|
age: 30,
|
|
|
|
country: "UK",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Jane",
|
|
|
|
age: 31,
|
|
|
|
country: "UK",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Ruari",
|
|
|
|
age: 32,
|
|
|
|
country: "USA",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Alice",
|
|
|
|
age: 33,
|
|
|
|
country: "USA",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("returns the expected rows prior to modification", async () => {
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(2)
|
|
|
|
expect(rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
country: "USA",
|
|
|
|
age: 65,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
country: "UK",
|
|
|
|
age: 61,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can remove a group by field", async () => {
|
|
|
|
delete view.schema!.country
|
|
|
|
await config.api.viewV2.update(view)
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(1)
|
|
|
|
expect(rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
age: 126,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can remove a calculation field", async () => {
|
|
|
|
delete view.schema!.age
|
|
|
|
await config.api.viewV2.update(view)
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(4)
|
|
|
|
|
|
|
|
// Because the removal of the calculation field actually makes this
|
|
|
|
// no longer a calculation view, these rows will now have _id and
|
|
|
|
// _rev fields.
|
|
|
|
expect(rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({ country: "UK" }),
|
|
|
|
expect.objectContaining({ country: "UK" }),
|
|
|
|
expect.objectContaining({ country: "USA" }),
|
|
|
|
expect.objectContaining({ country: "USA" }),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can add a new group by field", async () => {
|
|
|
|
view.schema!.name = { visible: true }
|
|
|
|
await config.api.viewV2.update(view)
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(4)
|
|
|
|
expect(rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
name: "Steve",
|
|
|
|
age: 30,
|
|
|
|
country: "UK",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Jane",
|
|
|
|
age: 31,
|
|
|
|
country: "UK",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Ruari",
|
|
|
|
age: 32,
|
|
|
|
country: "USA",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Alice",
|
|
|
|
age: 33,
|
|
|
|
country: "USA",
|
|
|
|
},
|
|
|
|
])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-10-07 15:59:29 +02:00
|
|
|
it("can add a new group by field that is invisible, even if required on the table", async () => {
|
|
|
|
view.schema!.name = { visible: false }
|
|
|
|
await config.api.viewV2.update(view)
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(2)
|
|
|
|
expect(rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
country: "USA",
|
|
|
|
age: 65,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
country: "UK",
|
|
|
|
age: 61,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-10-02 17:42:05 +02:00
|
|
|
it("can add a new calculation field", async () => {
|
|
|
|
view.schema!.count = {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
}
|
|
|
|
await config.api.viewV2.update(view)
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(2)
|
|
|
|
expect(rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
country: "USA",
|
|
|
|
age: 65,
|
|
|
|
count: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
country: "UK",
|
|
|
|
age: 61,
|
|
|
|
count: 2,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2024-05-27 14:53:26 +02:00
|
|
|
})
|
2024-06-04 16:40:09 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
describe("delete", () => {
|
|
|
|
let view: ViewV2
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
view = await config.api.viewV2.create({
|
2024-06-04 16:46:50 +02:00
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
2024-06-04 16:40:09 +02:00
|
|
|
},
|
2024-06-04 16:46:50 +02:00
|
|
|
})
|
2024-08-28 14:20:39 +02:00
|
|
|
})
|
2024-06-04 16:40:09 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("can delete an existing view", async () => {
|
|
|
|
const tableId = table._id!
|
|
|
|
const getPersistedView = async () =>
|
|
|
|
(await config.api.table.get(tableId)).views![view.name]
|
2024-06-04 16:40:09 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(await getPersistedView()).toBeDefined()
|
2023-07-12 18:09:13 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
await config.api.viewV2.delete(view.id)
|
2023-07-19 15:47:45 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
expect(await getPersistedView()).toBeUndefined()
|
|
|
|
})
|
2023-07-12 18:09:13 +02:00
|
|
|
})
|
2023-08-11 16:16:33 +02:00
|
|
|
|
2024-08-28 14:07:59 +02:00
|
|
|
describe.each([
|
|
|
|
["from view api", (view: ViewV2) => config.api.viewV2.get(view.id)],
|
|
|
|
[
|
|
|
|
"from table",
|
|
|
|
async (view: ViewV2) => {
|
|
|
|
const table = await config.api.table.get(view.tableId)
|
2024-08-29 12:34:12 +02:00
|
|
|
return table.views![view.name] as ViewV2
|
2024-08-28 14:07:59 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
])("read (%s)", (_, getDelegate) => {
|
2024-08-28 13:36:40 +02:00
|
|
|
let table: Table
|
|
|
|
let tableId: string
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
2024-07-02 14:02:16 +02:00
|
|
|
schema: {
|
2024-08-28 13:36:40 +02:00
|
|
|
one: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "one",
|
|
|
|
},
|
|
|
|
two: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "two",
|
|
|
|
},
|
|
|
|
three: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "three",
|
2024-07-02 14:02:16 +02:00
|
|
|
},
|
2024-06-04 16:46:50 +02:00
|
|
|
},
|
2024-08-28 13:36:40 +02:00
|
|
|
})
|
2024-06-04 16:46:50 +02:00
|
|
|
)
|
2024-08-28 13:36:40 +02:00
|
|
|
tableId = table._id!
|
2024-06-04 16:46:50 +02:00
|
|
|
})
|
2023-07-25 15:34:25 +02:00
|
|
|
|
2024-08-28 13:36:40 +02:00
|
|
|
it("retrieves the view data with the enriched schema", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
2023-07-12 18:09:13 +02:00
|
|
|
|
2024-08-28 14:07:59 +02:00
|
|
|
expect(await getDelegate(view)).toEqual({
|
2024-08-28 14:20:39 +02:00
|
|
|
...view,
|
|
|
|
schema: {
|
|
|
|
id: { ...table.schema["id"], visible: true },
|
|
|
|
one: { ...table.schema["one"], visible: true },
|
|
|
|
two: { ...table.schema["two"], visible: true },
|
|
|
|
three: { ...table.schema["three"], visible: false },
|
|
|
|
},
|
2024-08-28 14:07:59 +02:00
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
2023-07-12 18:09:13 +02:00
|
|
|
|
2024-08-28 14:07:59 +02:00
|
|
|
it("does not include columns removed from the table", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const table = await config.api.table.get(tableId)
|
|
|
|
const { one: _, ...newSchema } = table.schema
|
|
|
|
await config.api.table.save({ ...table, schema: newSchema })
|
|
|
|
|
|
|
|
expect(await getDelegate(view)).toEqual({
|
|
|
|
...view,
|
|
|
|
schema: {
|
|
|
|
id: { ...table.schema["id"], visible: true },
|
|
|
|
two: { ...table.schema["two"], visible: true },
|
|
|
|
three: { ...table.schema["three"], visible: false },
|
2024-08-28 13:36:40 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2023-07-12 18:09:13 +02:00
|
|
|
|
2024-08-28 14:11:02 +02:00
|
|
|
it("does not include columns hidden from the table", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const table = await config.api.table.get(tableId)
|
|
|
|
await config.api.table.save({
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
two: { ...table.schema["two"], visible: false },
|
|
|
|
},
|
|
|
|
})
|
2023-07-12 18:09:13 +02:00
|
|
|
|
2024-08-28 14:11:02 +02:00
|
|
|
expect(await getDelegate(view)).toEqual({
|
|
|
|
...view,
|
|
|
|
schema: {
|
|
|
|
id: { ...table.schema["id"], visible: true },
|
|
|
|
one: { ...table.schema["one"], visible: true },
|
|
|
|
three: { ...table.schema["three"], visible: false },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2023-07-19 15:47:45 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
it("should be able to fetch readonly config after downgrades", async () => {
|
|
|
|
const res = await config.api.viewV2.create({
|
|
|
|
name: generator.name(),
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
2024-08-28 14:26:00 +02:00
|
|
|
one: { visible: true, readonly: true },
|
2024-08-28 14:20:39 +02:00
|
|
|
},
|
|
|
|
})
|
2023-08-11 16:16:33 +02:00
|
|
|
|
2024-08-28 14:20:39 +02:00
|
|
|
mocks.licenses.useCloudFree()
|
2024-08-29 12:34:12 +02:00
|
|
|
const view = await getDelegate(res)
|
2024-08-28 14:26:00 +02:00
|
|
|
expect(view.schema?.one).toEqual(
|
2024-08-28 14:20:39 +02:00
|
|
|
expect.objectContaining({ visible: true, readonly: true })
|
|
|
|
)
|
2024-04-08 16:48:27 +02:00
|
|
|
})
|
2024-05-27 14:53:26 +02:00
|
|
|
})
|
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
describe("updating table schema", () => {
|
|
|
|
describe("existing columns changed to required", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
id: {
|
|
|
|
name: "id",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
autocolumn: true,
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
2024-05-27 14:53:26 +02:00
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
it("allows updating when no views constrains the field", async () => {
|
|
|
|
await config.api.viewV2.create({
|
|
|
|
name: "view a",
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
name: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
table = await config.api.table.get(table._id!)
|
|
|
|
await config.api.table.save(
|
|
|
|
{
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: { presence: { allowEmpty: false } },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ status: 200 }
|
|
|
|
)
|
|
|
|
})
|
2024-05-27 14:44:00 +02:00
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
it("rejects if field is readonly in any view", async () => {
|
|
|
|
await config.api.viewV2.create({
|
|
|
|
name: "view a",
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
name: {
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
},
|
2024-03-14 18:11:09 +01:00
|
|
|
},
|
2024-08-28 13:28:45 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
table = await config.api.table.get(table._id!)
|
|
|
|
await config.api.table.save(
|
|
|
|
{
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: { presence: true },
|
|
|
|
},
|
|
|
|
},
|
2024-03-14 18:11:09 +01:00
|
|
|
},
|
2024-08-28 13:28:45 +02:00
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
status: 400,
|
|
|
|
message:
|
|
|
|
'To make field "name" required, this field must be present and writable in views: view a.',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
it("rejects if field is hidden in any view", async () => {
|
|
|
|
await config.api.viewV2.create({
|
|
|
|
name: "view a",
|
|
|
|
tableId: table._id!,
|
|
|
|
schema: { id: { visible: true } },
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
|
2024-08-28 13:28:45 +02:00
|
|
|
table = await config.api.table.get(table._id!)
|
|
|
|
await config.api.table.save(
|
|
|
|
{
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: { presence: true },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
status: 400,
|
|
|
|
message:
|
|
|
|
'To make field "name" required, this field must be present and writable in views: view a.',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
2024-09-02 17:32:21 +02:00
|
|
|
|
2024-09-03 10:42:27 +02:00
|
|
|
describe("foreign relationship columns", () => {
|
2024-09-03 14:44:55 +02:00
|
|
|
const createAuxTable = () =>
|
|
|
|
config.api.table.save(
|
2024-09-02 17:32:21 +02:00
|
|
|
saveTableRequest({
|
|
|
|
primaryDisplay: "name",
|
|
|
|
schema: {
|
2024-09-03 10:40:29 +02:00
|
|
|
name: { name: "name", type: FieldType.STRING },
|
|
|
|
age: { name: "age", type: FieldType.NUMBER },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const createMainTable = async (
|
|
|
|
links: {
|
|
|
|
name: string
|
|
|
|
tableId: string
|
|
|
|
fk: string
|
|
|
|
}[]
|
|
|
|
) => {
|
2024-09-03 10:40:29 +02:00
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
await config.api.table.save({
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
2024-09-03 14:44:55 +02:00
|
|
|
...links.reduce<TableSchema>((acc, c) => {
|
|
|
|
acc[c.name] = {
|
|
|
|
name: c.name,
|
|
|
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
|
|
type: FieldType.LINK,
|
|
|
|
tableId: c.tableId,
|
|
|
|
fieldName: c.fk,
|
|
|
|
constraints: { type: "array" },
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {}),
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
})
|
2024-09-03 14:44:55 +02:00
|
|
|
return table
|
|
|
|
}
|
2024-09-03 10:40:29 +02:00
|
|
|
|
2024-10-03 10:28:29 +02:00
|
|
|
const createView = async (tableId: string, schema: ViewV2Schema) =>
|
2024-09-03 14:44:55 +02:00
|
|
|
await config.api.viewV2.create({
|
|
|
|
name: generator.guid(),
|
|
|
|
tableId,
|
|
|
|
schema,
|
2024-09-03 10:40:29 +02:00
|
|
|
})
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const renameColumn = async (table: Table, renaming: RenameColumn) => {
|
|
|
|
const newSchema = { ...table.schema }
|
2024-09-03 16:15:33 +02:00
|
|
|
newSchema[renaming.updated] = {
|
2024-09-03 14:44:55 +02:00
|
|
|
...table.schema[renaming.old],
|
|
|
|
name: renaming.updated,
|
2024-09-03 16:15:33 +02:00
|
|
|
}
|
|
|
|
delete newSchema[renaming.old]
|
2024-09-03 14:44:55 +02:00
|
|
|
|
2024-09-03 10:40:29 +02:00
|
|
|
await config.api.table.save({
|
2024-09-03 14:44:55 +02:00
|
|
|
...table,
|
|
|
|
schema: newSchema,
|
|
|
|
_rename: renaming,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
it("updating a column will update link columns configuration", async () => {
|
|
|
|
let auxTable = await createAuxTable()
|
|
|
|
|
|
|
|
const table = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
|
|
|
])
|
|
|
|
// Refetch auxTable
|
|
|
|
auxTable = await config.api.table.get(auxTable._id!)
|
|
|
|
|
|
|
|
const view = await createView(table._id!, {
|
|
|
|
aux: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
|
|
|
age: { visible: true, readonly: true },
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
|
|
|
|
2024-09-03 10:40:29 +02:00
|
|
|
const updatedView = await config.api.viewV2.get(view.id)
|
|
|
|
expect(updatedView).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
schema: expect.objectContaining({
|
|
|
|
aux: expect.objectContaining({
|
|
|
|
columns: {
|
2024-09-09 16:42:41 +02:00
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
|
|
|
name: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
|
|
|
dob: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("handles multiple fields using the same table", async () => {
|
2024-09-03 14:44:55 +02:00
|
|
|
let auxTable = await createAuxTable()
|
2024-09-02 17:32:21 +02:00
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const table = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
|
|
|
{ name: "aux2", tableId: auxTable._id!, fk: "fk_aux2" },
|
|
|
|
])
|
2024-09-02 18:36:44 +02:00
|
|
|
// Refetch auxTable
|
|
|
|
auxTable = await config.api.table.get(auxTable._id!)
|
2024-09-02 17:32:21 +02:00
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const view = await createView(table._id!, {
|
|
|
|
aux: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
|
|
|
age: { visible: true, readonly: true },
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
2024-09-02 17:32:21 +02:00
|
|
|
},
|
2024-09-03 14:44:55 +02:00
|
|
|
aux2: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
|
|
|
age: { visible: true, readonly: true },
|
2024-09-02 17:32:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
|
|
|
|
2024-09-02 17:32:21 +02:00
|
|
|
const updatedView = await config.api.viewV2.get(view.id)
|
|
|
|
expect(updatedView).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
schema: expect.objectContaining({
|
|
|
|
aux: expect.objectContaining({
|
|
|
|
columns: {
|
2024-09-09 16:42:41 +02:00
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
|
|
|
name: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
|
|
|
dob: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
2024-09-02 17:32:21 +02:00
|
|
|
},
|
|
|
|
}),
|
2024-09-03 10:40:29 +02:00
|
|
|
aux2: expect.objectContaining({
|
|
|
|
columns: {
|
2024-09-09 16:42:41 +02:00
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
|
|
|
name: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
|
|
|
dob: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not rename columns with the same name but from other tables", async () => {
|
2024-09-03 14:44:55 +02:00
|
|
|
let auxTable = await createAuxTable()
|
|
|
|
let aux2Table = await createAuxTable()
|
|
|
|
|
|
|
|
const table = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
|
|
|
{ name: "aux2", tableId: aux2Table._id!, fk: "fk_aux2" },
|
|
|
|
])
|
2024-09-03 10:40:29 +02:00
|
|
|
|
|
|
|
// Refetch auxTable
|
|
|
|
auxTable = await config.api.table.get(auxTable._id!)
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const view = await createView(table._id!, {
|
|
|
|
aux: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
},
|
2024-09-03 14:44:55 +02:00
|
|
|
aux2: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
await renameColumn(auxTable, { old: "name", updated: "fullName" })
|
|
|
|
|
2024-09-03 10:40:29 +02:00
|
|
|
const updatedView = await config.api.viewV2.get(view.id)
|
|
|
|
expect(updatedView).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
schema: expect.objectContaining({
|
|
|
|
aux: expect.objectContaining({
|
|
|
|
columns: {
|
2024-09-09 16:42:41 +02:00
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
|
|
|
fullName: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
|
|
|
age: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
aux2: expect.objectContaining({
|
|
|
|
columns: {
|
2024-09-09 16:42:41 +02:00
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
|
|
|
name: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
|
|
|
age: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
2024-09-03 10:40:29 +02:00
|
|
|
},
|
|
|
|
}),
|
2024-09-02 17:32:21 +02:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
2024-09-03 14:23:08 +02:00
|
|
|
|
|
|
|
it("updates all views references", async () => {
|
2024-09-03 14:44:55 +02:00
|
|
|
let auxTable = await createAuxTable()
|
2024-09-03 14:23:08 +02:00
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const table1 = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux_table1" },
|
|
|
|
])
|
|
|
|
const table2 = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux_table2" },
|
|
|
|
])
|
2024-09-03 14:23:08 +02:00
|
|
|
|
|
|
|
// Refetch auxTable
|
|
|
|
auxTable = await config.api.table.get(auxTable._id!)
|
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
const viewSchema = {
|
|
|
|
aux: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
|
|
|
age: { visible: true, readonly: true },
|
2024-09-03 14:23:08 +02:00
|
|
|
},
|
2024-09-03 14:44:55 +02:00
|
|
|
},
|
2024-09-03 14:23:08 +02:00
|
|
|
}
|
2024-09-03 14:44:55 +02:00
|
|
|
const view1 = await createView(table1._id!, viewSchema)
|
|
|
|
const view2 = await createView(table1._id!, viewSchema)
|
|
|
|
const view3 = await createView(table2._id!, viewSchema)
|
2024-09-03 14:23:08 +02:00
|
|
|
|
2024-09-03 14:44:55 +02:00
|
|
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
2024-09-03 14:23:08 +02:00
|
|
|
|
|
|
|
for (const view of [view1, view2, view3]) {
|
|
|
|
const updatedView = await config.api.viewV2.get(view.id)
|
|
|
|
expect(updatedView).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
schema: expect.objectContaining({
|
|
|
|
aux: expect.objectContaining({
|
|
|
|
columns: {
|
2024-09-09 16:42:41 +02:00
|
|
|
id: expect.objectContaining({
|
|
|
|
visible: false,
|
|
|
|
readonly: false,
|
|
|
|
}),
|
|
|
|
name: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
|
|
|
dob: expect.objectContaining({
|
|
|
|
visible: true,
|
|
|
|
readonly: true,
|
|
|
|
}),
|
2024-09-03 14:23:08 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
|
|
|
})
|
2024-10-10 12:50:38 +02:00
|
|
|
|
|
|
|
describe("calculation views", () => {
|
|
|
|
it("should not remove calculation columns when modifying table schema", async () => {
|
|
|
|
let table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
age: {
|
|
|
|
name: "age",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
let view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "age",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
table = await config.api.table.get(table._id!)
|
|
|
|
await config.api.table.save({
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: { presence: true },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
view = await config.api.viewV2.get(view.id)
|
|
|
|
expect(Object.keys(view.schema!).sort()).toEqual([
|
|
|
|
"age",
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"sum",
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
2024-03-14 18:11:09 +01:00
|
|
|
})
|
2024-03-15 18:03:47 +01:00
|
|
|
|
2024-03-18 10:18:45 +01:00
|
|
|
describe("row operations", () => {
|
2024-03-15 18:03:47 +01:00
|
|
|
let table: Table, view: ViewV2
|
|
|
|
beforeEach(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
one: { type: FieldType.STRING, name: "one" },
|
|
|
|
two: { type: FieldType.STRING, name: "two" },
|
2024-07-15 17:44:43 +02:00
|
|
|
default: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "default",
|
|
|
|
default: "default",
|
|
|
|
},
|
2024-03-15 18:03:47 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-03-15 18:03:47 +01:00
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("create", () => {
|
|
|
|
it("should persist a new row with only the provided view fields", async () => {
|
|
|
|
const newRow = await config.api.row.save(view.id, {
|
|
|
|
tableId: table!._id,
|
|
|
|
_viewId: view.id,
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
2024-07-15 17:44:43 +02:00
|
|
|
default: "ohnoes",
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const row = await config.api.row.get(table._id!, newRow._id!)
|
|
|
|
expect(row.one).toBeUndefined()
|
|
|
|
expect(row.two).toEqual("bar")
|
2024-07-15 17:44:43 +02:00
|
|
|
expect(row.default).toEqual("default")
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
2024-05-27 16:36:45 +02:00
|
|
|
|
|
|
|
it("can't persist readonly columns", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 16:36:45 +02:00
|
|
|
one: { visible: true, readonly: true },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const row = await config.api.row.save(view.id, {
|
|
|
|
tableId: table!._id,
|
|
|
|
_viewId: view.id,
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(row.one).toBeUndefined()
|
|
|
|
expect(row.two).toEqual("bar")
|
|
|
|
})
|
2024-08-28 13:16:42 +02:00
|
|
|
|
|
|
|
it("should not return non-view view fields for a row", async () => {
|
|
|
|
const newRow = await config.api.row.save(view.id, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(newRow.one).toBeUndefined()
|
|
|
|
expect(newRow.two).toEqual("bar")
|
|
|
|
})
|
2024-10-08 18:55:42 +02:00
|
|
|
|
|
|
|
it("should not be possible to create a row in a calculation view", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.save(
|
|
|
|
view.id,
|
|
|
|
{ one: "foo" },
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: "Cannot insert rows through a calculation view",
|
|
|
|
status: 400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("patch", () => {
|
2024-08-28 13:16:42 +02:00
|
|
|
it("should not return non-view view fields for a row", async () => {
|
|
|
|
const newRow = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
const row = await config.api.row.patch(view.id, {
|
|
|
|
tableId: table._id!,
|
|
|
|
_id: newRow._id!,
|
|
|
|
_rev: newRow._rev!,
|
|
|
|
one: "newFoo",
|
|
|
|
two: "newBar",
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(row.one).toBeUndefined()
|
|
|
|
expect(row.two).toEqual("newBar")
|
|
|
|
})
|
|
|
|
|
2024-03-15 18:03:47 +01:00
|
|
|
it("should update only the view fields for a row", async () => {
|
|
|
|
const newRow = await config.api.row.save(table._id!, {
|
2024-05-27 16:36:45 +02:00
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
await config.api.row.patch(view.id, {
|
|
|
|
tableId: table._id!,
|
|
|
|
_id: newRow._id!,
|
|
|
|
_rev: newRow._rev!,
|
|
|
|
one: "newFoo",
|
|
|
|
two: "newBar",
|
|
|
|
})
|
|
|
|
|
|
|
|
const row = await config.api.row.get(table._id!, newRow._id!)
|
|
|
|
expect(row.one).toEqual("foo")
|
|
|
|
expect(row.two).toEqual("newBar")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can't update readonly columns", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-05-27 16:36:45 +02:00
|
|
|
one: { visible: true, readonly: true },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const newRow = await config.api.row.save(table._id!, {
|
2024-03-15 18:03:47 +01:00
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
await config.api.row.patch(view.id, {
|
|
|
|
tableId: table._id!,
|
|
|
|
_id: newRow._id!,
|
|
|
|
_rev: newRow._rev!,
|
|
|
|
one: "newFoo",
|
|
|
|
two: "newBar",
|
|
|
|
})
|
|
|
|
|
|
|
|
const row = await config.api.row.get(table._id!, newRow._id!)
|
|
|
|
expect(row.one).toEqual("foo")
|
|
|
|
expect(row.two).toEqual("newBar")
|
|
|
|
})
|
2024-10-08 18:55:42 +02:00
|
|
|
|
|
|
|
it("should not be possible to modify a row in a calculation view", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const newRow = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.patch(
|
|
|
|
view.id,
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
_id: newRow._id!,
|
|
|
|
_rev: newRow._rev!,
|
|
|
|
one: "newFoo",
|
|
|
|
two: "newBar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: "Cannot update rows through a calculation view",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("destroy", () => {
|
2024-08-28 13:28:45 +02:00
|
|
|
const getRowUsage = async () => {
|
|
|
|
const { total } = await config.doInContext(undefined, () =>
|
|
|
|
quotas.getCurrentUsageValues(
|
|
|
|
QuotaUsageType.STATIC,
|
|
|
|
StaticQuotaName.ROWS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
|
|
|
const assertRowUsage = async (expected: number) => {
|
|
|
|
const usage = await getRowUsage()
|
|
|
|
expect(usage).toBe(expected)
|
|
|
|
}
|
|
|
|
|
2024-03-15 18:03:47 +01:00
|
|
|
it("should be able to delete a row", async () => {
|
|
|
|
const createdRow = await config.api.row.save(table._id!, {})
|
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
await config.api.row.bulkDelete(view.id, { rows: [createdRow] })
|
2024-06-12 15:49:37 +02:00
|
|
|
await assertRowUsage(isInternal ? rowUsage - 1 : rowUsage)
|
2024-03-15 18:03:47 +01:00
|
|
|
await config.api.row.get(table._id!, createdRow._id!, {
|
|
|
|
status: 404,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to delete multiple rows", async () => {
|
|
|
|
const rows = await Promise.all([
|
|
|
|
config.api.row.save(table._id!, {}),
|
|
|
|
config.api.row.save(table._id!, {}),
|
|
|
|
config.api.row.save(table._id!, {}),
|
|
|
|
])
|
|
|
|
const rowUsage = await getRowUsage()
|
|
|
|
|
|
|
|
await config.api.row.bulkDelete(view.id, { rows: [rows[0], rows[2]] })
|
|
|
|
|
2024-06-12 15:49:37 +02:00
|
|
|
await assertRowUsage(isInternal ? rowUsage - 2 : rowUsage)
|
2024-03-15 18:03:47 +01:00
|
|
|
|
|
|
|
await config.api.row.get(table._id!, rows[0]._id!, {
|
|
|
|
status: 404,
|
|
|
|
})
|
|
|
|
await config.api.row.get(table._id!, rows[2]._id!, {
|
|
|
|
status: 404,
|
|
|
|
})
|
|
|
|
await config.api.row.get(table._id!, rows[1]._id!, { status: 200 })
|
|
|
|
})
|
2024-10-09 17:05:49 +02:00
|
|
|
|
|
|
|
it("should not be possible to delete a row in a calculation view", async () => {
|
|
|
|
const row = await config.api.row.save(table._id!, {})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.delete(
|
|
|
|
view.id,
|
|
|
|
{ _id: row._id! },
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message: "Cannot delete rows through a calculation view",
|
|
|
|
status: 400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
2024-08-28 13:26:23 +02:00
|
|
|
describe("read", () => {
|
|
|
|
let view: ViewV2
|
|
|
|
let table: Table
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
Country: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "Country",
|
|
|
|
},
|
|
|
|
Story: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "Story",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
Country: {
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("views have extra data trimmed", async () => {
|
|
|
|
let row = await config.api.row.save(view.id, {
|
|
|
|
Country: "Aussy",
|
|
|
|
Story: "aaaaa",
|
|
|
|
})
|
|
|
|
|
|
|
|
row = await config.api.row.get(table._id!, row._id!)
|
|
|
|
expect(row.Story).toBeUndefined()
|
|
|
|
expect(row.Country).toEqual("Aussy")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-03-15 18:03:47 +01:00
|
|
|
describe("search", () => {
|
2024-09-26 12:56:03 +02:00
|
|
|
it("returns empty rows from view when no schema is passed", async () => {
|
2024-03-15 18:03:47 +01:00
|
|
|
const rows = await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () => config.api.row.save(table._id!, {}))
|
|
|
|
)
|
|
|
|
const response = await config.api.viewV2.search(view.id)
|
|
|
|
expect(response.rows).toHaveLength(10)
|
|
|
|
expect(response).toEqual({
|
|
|
|
rows: expect.arrayContaining(
|
|
|
|
rows.map(r => ({
|
|
|
|
_viewId: view.id,
|
|
|
|
tableId: table._id,
|
2024-06-03 13:26:49 +02:00
|
|
|
id: r.id,
|
2024-03-15 18:03:47 +01:00
|
|
|
_id: r._id,
|
|
|
|
_rev: r._rev,
|
|
|
|
...(isInternal
|
|
|
|
? {
|
|
|
|
type: "row",
|
|
|
|
updatedAt: expect.any(String),
|
|
|
|
createdAt: expect.any(String),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
}))
|
|
|
|
),
|
|
|
|
...(isInternal
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
hasNextPage: false,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("searching respects the view filters", async () => {
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
const two = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
2024-07-10 12:08:11 +02:00
|
|
|
operator: BasicOperator.EQUAL,
|
2024-03-15 18:03:47 +01:00
|
|
|
field: "two",
|
|
|
|
value: "bar2",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
schema: {
|
2024-06-03 13:26:49 +02:00
|
|
|
id: { visible: true },
|
2024-07-02 14:02:16 +02:00
|
|
|
one: { visible: false },
|
2024-03-15 18:03:47 +01:00
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id)
|
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response).toEqual({
|
|
|
|
rows: expect.arrayContaining([
|
|
|
|
{
|
|
|
|
_viewId: view.id,
|
|
|
|
tableId: table._id,
|
2024-06-03 13:26:49 +02:00
|
|
|
id: two.id,
|
2024-03-15 18:03:47 +01:00
|
|
|
two: two.two,
|
|
|
|
_id: two._id,
|
|
|
|
_rev: two._rev,
|
|
|
|
...(isInternal
|
|
|
|
? {
|
|
|
|
type: "row",
|
|
|
|
createdAt: expect.any(String),
|
|
|
|
updatedAt: expect.any(String),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
...(isInternal
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
hasNextPage: false,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-10-01 10:55:25 +02:00
|
|
|
it("views filters are respected even if the column is hidden", async () => {
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
const two = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.EQUAL,
|
|
|
|
field: "two",
|
|
|
|
value: "bar2",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: false },
|
|
|
|
two: { visible: false },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id)
|
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response.rows).toEqual([
|
|
|
|
expect.objectContaining({ _id: two._id }),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2024-03-15 18:03:47 +01:00
|
|
|
it("views without data can be returned", async () => {
|
|
|
|
const response = await config.api.viewV2.search(view.id)
|
|
|
|
expect(response.rows).toHaveLength(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("respects the limit parameter", async () => {
|
|
|
|
await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () => config.api.row.save(table._id!, {}))
|
|
|
|
)
|
|
|
|
const limit = generator.integer({ min: 1, max: 8 })
|
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
limit,
|
|
|
|
query: {},
|
|
|
|
})
|
|
|
|
expect(response.rows).toHaveLength(limit)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("can handle pagination", async () => {
|
|
|
|
await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () => config.api.row.save(table._id!, {}))
|
|
|
|
)
|
|
|
|
const rows = (await config.api.viewV2.search(view.id)).rows
|
|
|
|
|
|
|
|
const page1 = await config.api.viewV2.search(view.id, {
|
|
|
|
paginate: true,
|
|
|
|
limit: 4,
|
|
|
|
query: {},
|
2024-06-19 19:57:37 +02:00
|
|
|
countRows: true,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
expect(page1).toEqual({
|
|
|
|
rows: expect.arrayContaining(rows.slice(0, 4)),
|
|
|
|
hasNextPage: true,
|
|
|
|
bookmark: expect.anything(),
|
2024-06-19 19:57:37 +02:00
|
|
|
totalRows: 10,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const page2 = await config.api.viewV2.search(view.id, {
|
|
|
|
paginate: true,
|
|
|
|
limit: 4,
|
|
|
|
bookmark: page1.bookmark,
|
|
|
|
query: {},
|
2024-06-19 19:57:37 +02:00
|
|
|
countRows: true,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
expect(page2).toEqual({
|
|
|
|
rows: expect.arrayContaining(rows.slice(4, 8)),
|
|
|
|
hasNextPage: true,
|
|
|
|
bookmark: expect.anything(),
|
2024-06-19 19:57:37 +02:00
|
|
|
totalRows: 10,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const page3 = await config.api.viewV2.search(view.id, {
|
|
|
|
paginate: true,
|
|
|
|
limit: 4,
|
|
|
|
bookmark: page2.bookmark,
|
|
|
|
query: {},
|
2024-06-19 19:57:37 +02:00
|
|
|
countRows: true,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
2024-06-19 19:57:37 +02:00
|
|
|
const expectation: SearchResponse<Row> = {
|
2024-03-15 18:03:47 +01:00
|
|
|
rows: expect.arrayContaining(rows.slice(8)),
|
|
|
|
hasNextPage: false,
|
2024-06-19 19:57:37 +02:00
|
|
|
totalRows: 10,
|
|
|
|
}
|
|
|
|
if (isLucene) {
|
|
|
|
expectation.bookmark = expect.anything()
|
|
|
|
}
|
|
|
|
expect(page3).toEqual(expectation)
|
2024-03-15 18:03:47 +01: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,
|
2024-03-25 20:00:08 +01:00
|
|
|
type: SortType.NUMBER,
|
2024-03-15 18:03:47 +01:00
|
|
|
},
|
|
|
|
["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,
|
2024-03-25 20:00:08 +01:00
|
|
|
type: SortType.NUMBER,
|
2024-03-15 18:03:47 +01:00
|
|
|
},
|
|
|
|
["Bob", "Charly", "Alice", "Danny"],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
|
|
|
|
describe("sorting", () => {
|
|
|
|
let table: Table
|
2024-06-03 13:26:49 +02:00
|
|
|
const viewSchema = {
|
|
|
|
id: { visible: true },
|
|
|
|
age: { visible: true },
|
|
|
|
name: { visible: true },
|
|
|
|
}
|
2024-03-15 18:03:47 +01:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
type: "table",
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "name",
|
|
|
|
},
|
|
|
|
surname: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "surname",
|
|
|
|
},
|
|
|
|
age: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "age",
|
|
|
|
},
|
|
|
|
address: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "address",
|
|
|
|
},
|
|
|
|
jobTitle: {
|
|
|
|
type: FieldType.STRING,
|
|
|
|
name: "jobTitle",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const users = [
|
|
|
|
{ name: "Alice", age: 25 },
|
|
|
|
{ name: "Bob", age: 30 },
|
|
|
|
{ name: "Charly", age: 27 },
|
|
|
|
{ name: "Danny", age: 15 },
|
|
|
|
]
|
|
|
|
await Promise.all(
|
|
|
|
users.map(u =>
|
|
|
|
config.api.row.save(table._id!, {
|
|
|
|
tableId: table._id,
|
|
|
|
...u,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it.each(sortTestOptions)(
|
|
|
|
"allow sorting (%s)",
|
|
|
|
async (sortParams, expected) => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
sort: sortParams,
|
|
|
|
schema: viewSchema,
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id)
|
|
|
|
|
|
|
|
expect(response.rows).toHaveLength(4)
|
|
|
|
expect(response.rows).toEqual(
|
|
|
|
expected.map(name => expect.objectContaining({ name }))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
it.each(sortTestOptions)(
|
|
|
|
"allow override the default view sorting (%s)",
|
|
|
|
async (sortParams, expected) => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
sort: {
|
|
|
|
field: "name",
|
|
|
|
order: SortOrder.ASCENDING,
|
|
|
|
type: SortType.STRING,
|
|
|
|
},
|
|
|
|
schema: viewSchema,
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
sort: sortParams.field,
|
|
|
|
sortOrder: sortParams.order,
|
|
|
|
sortType: sortParams.type,
|
|
|
|
query: {},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(response.rows).toHaveLength(4)
|
|
|
|
expect(response.rows).toEqual(
|
|
|
|
expected.map(name => expect.objectContaining({ name }))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-08-07 12:36:51 +02:00
|
|
|
|
2024-08-27 16:29:37 +02:00
|
|
|
it("can query on top of the view filters", async () => {
|
2024-08-21 13:40:17 +02:00
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
|
|
|
const three = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo3",
|
|
|
|
two: "bar3",
|
|
|
|
})
|
2024-08-07 13:15:30 +02:00
|
|
|
|
2024-08-21 13:40:17 +02:00
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.NOT_EQUAL,
|
|
|
|
field: "one",
|
|
|
|
value: "foo2",
|
2024-08-07 13:15:30 +02:00
|
|
|
},
|
2024-08-21 13:40:17 +02:00
|
|
|
],
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: true },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
2024-08-07 13:15:30 +02:00
|
|
|
|
2024-08-21 13:40:17 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
|
|
|
[BasicOperator.EQUAL]: {
|
|
|
|
two: "bar3",
|
2024-08-07 13:15:30 +02:00
|
|
|
},
|
2024-08-21 13:40:17 +02:00
|
|
|
[BasicOperator.NOT_EMPTY]: {
|
|
|
|
two: null,
|
|
|
|
},
|
|
|
|
},
|
2024-08-07 13:15:30 +02:00
|
|
|
})
|
2024-08-21 13:40:17 +02:00
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response.rows).toEqual(
|
|
|
|
expect.arrayContaining([expect.objectContaining({ _id: three._id })])
|
|
|
|
)
|
|
|
|
})
|
2024-08-07 13:15:30 +02:00
|
|
|
|
2024-08-21 13:40:17 +02:00
|
|
|
it("can query on top of the view filters (using or filters)", async () => {
|
|
|
|
const one = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
|
|
|
const three = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo3",
|
|
|
|
two: "bar3",
|
|
|
|
})
|
2024-08-07 12:40:03 +02:00
|
|
|
|
2024-08-21 13:40:17 +02:00
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.NOT_EQUAL,
|
|
|
|
field: "two",
|
|
|
|
value: "bar2",
|
2024-08-07 13:21:28 +02:00
|
|
|
},
|
2024-08-21 13:40:17 +02:00
|
|
|
],
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: false },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
2024-08-07 12:40:03 +02:00
|
|
|
|
2024-08-21 13:40:17 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
|
|
|
allOr: true,
|
|
|
|
[BasicOperator.NOT_EQUAL]: {
|
|
|
|
two: "bar",
|
2024-08-07 12:40:03 +02:00
|
|
|
},
|
2024-08-21 13:40:17 +02:00
|
|
|
[BasicOperator.NOT_EMPTY]: {
|
|
|
|
two: null,
|
|
|
|
},
|
|
|
|
},
|
2024-08-21 12:59:28 +02:00
|
|
|
})
|
2024-08-21 13:40:17 +02:00
|
|
|
expect(response.rows).toHaveLength(2)
|
|
|
|
expect(response.rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
2024-08-07 13:21:28 +02:00
|
|
|
expect.objectContaining({ _id: one._id }),
|
2024-08-21 13:40:17 +02:00
|
|
|
expect.objectContaining({ _id: three._id }),
|
2024-08-07 13:21:28 +02:00
|
|
|
])
|
2024-08-21 13:40:17 +02:00
|
|
|
)
|
|
|
|
})
|
2024-08-21 12:59:28 +02:00
|
|
|
|
2024-08-07 13:15:30 +02:00
|
|
|
isLucene &&
|
2024-08-21 12:14:40 +02:00
|
|
|
it.each([true, false])(
|
|
|
|
"in lucene, cannot override a view filter",
|
|
|
|
async allOr => {
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
const two = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
2024-08-07 13:15:30 +02:00
|
|
|
|
2024-08-21 12:14:40 +02:00
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.EQUAL,
|
|
|
|
field: "two",
|
|
|
|
value: "bar2",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: false },
|
|
|
|
two: { visible: true },
|
2024-08-07 13:15:30 +02:00
|
|
|
},
|
2024-08-21 12:14:40 +02:00
|
|
|
})
|
2024-08-07 13:15:30 +02:00
|
|
|
|
2024-08-21 12:14:40 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
|
|
|
allOr,
|
|
|
|
equal: {
|
|
|
|
two: "bar",
|
|
|
|
},
|
2024-08-07 13:15:30 +02:00
|
|
|
},
|
2024-08-21 12:14:40 +02:00
|
|
|
})
|
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response.rows).toEqual([
|
|
|
|
expect.objectContaining({ _id: two._id }),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
)
|
2024-08-07 12:40:03 +02:00
|
|
|
|
2024-08-07 13:15:30 +02:00
|
|
|
!isLucene &&
|
2024-08-21 12:14:40 +02:00
|
|
|
it.each([true, false])(
|
|
|
|
"can filter a view without a view filter",
|
|
|
|
async allOr => {
|
|
|
|
const one = await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
2024-08-07 12:40:03 +02:00
|
|
|
|
2024-08-21 12:14:40 +02:00
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: false },
|
|
|
|
two: { visible: true },
|
|
|
|
},
|
|
|
|
})
|
2024-08-07 12:40:03 +02:00
|
|
|
|
2024-08-21 12:14:40 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
|
|
|
allOr,
|
|
|
|
equal: {
|
|
|
|
two: "bar",
|
|
|
|
},
|
2024-08-07 13:21:28 +02:00
|
|
|
},
|
2024-08-21 12:14:40 +02:00
|
|
|
})
|
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response.rows).toEqual([
|
|
|
|
expect.objectContaining({ _id: one._id }),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
)
|
2024-08-07 12:40:03 +02:00
|
|
|
|
2024-08-07 13:15:30 +02:00
|
|
|
!isLucene &&
|
2024-08-21 12:14:40 +02:00
|
|
|
it.each([true, false])("cannot bypass a view filter", async allOr => {
|
2024-08-07 13:21:28 +02:00
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo",
|
|
|
|
two: "bar",
|
|
|
|
})
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
one: "foo2",
|
|
|
|
two: "bar2",
|
|
|
|
})
|
2024-08-07 12:36:51 +02:00
|
|
|
|
2024-08-07 13:21:28 +02:00
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
operator: BasicOperator.EQUAL,
|
|
|
|
field: "two",
|
|
|
|
value: "bar2",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
schema: {
|
|
|
|
id: { visible: true },
|
|
|
|
one: { visible: false },
|
|
|
|
two: { visible: true },
|
2024-08-07 12:36:51 +02:00
|
|
|
},
|
2024-08-07 13:21:28 +02:00
|
|
|
})
|
2024-08-07 12:36:51 +02:00
|
|
|
|
2024-08-07 13:21:28 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
2024-08-21 12:14:40 +02:00
|
|
|
allOr,
|
2024-08-07 13:21:28 +02:00
|
|
|
equal: {
|
|
|
|
two: "bar",
|
|
|
|
},
|
2024-08-07 12:36:51 +02:00
|
|
|
},
|
2024-08-07 13:21:28 +02:00
|
|
|
})
|
|
|
|
expect(response.rows).toHaveLength(0)
|
2024-08-07 12:36:51 +02:00
|
|
|
})
|
2024-08-09 11:39:58 +02:00
|
|
|
|
2024-09-24 17:42:25 +02:00
|
|
|
describe("foreign relationship columns", () => {
|
2024-09-25 12:44:30 +02:00
|
|
|
let envCleanup: () => void
|
|
|
|
beforeAll(() => {
|
2024-10-07 19:18:04 +02:00
|
|
|
envCleanup = features.testutils.setFeatureFlags("*", {
|
|
|
|
ENRICHED_RELATIONSHIPS: true,
|
2024-09-25 12:44:30 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
envCleanup?.()
|
|
|
|
})
|
|
|
|
|
2024-09-24 17:42:25 +02:00
|
|
|
const createMainTable = async (
|
|
|
|
links: {
|
|
|
|
name: string
|
|
|
|
tableId: string
|
|
|
|
fk: string
|
|
|
|
}[]
|
|
|
|
) => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: { title: { name: "title", type: FieldType.STRING } },
|
|
|
|
})
|
|
|
|
)
|
|
|
|
await config.api.table.save({
|
|
|
|
...table,
|
|
|
|
schema: {
|
|
|
|
...table.schema,
|
|
|
|
...links.reduce<TableSchema>((acc, c) => {
|
|
|
|
acc[c.name] = {
|
|
|
|
name: c.name,
|
|
|
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
|
|
|
type: FieldType.LINK,
|
|
|
|
tableId: c.tableId,
|
|
|
|
fieldName: c.fk,
|
|
|
|
constraints: { type: "array" },
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
const createAuxTable = (schema: TableSchema) =>
|
|
|
|
config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
primaryDisplay: "name",
|
|
|
|
schema: {
|
|
|
|
...schema,
|
|
|
|
name: { name: "name", type: FieldType.STRING },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
it("returns squashed fields respecting the view config", async () => {
|
|
|
|
const auxTable = await createAuxTable({
|
|
|
|
age: { name: "age", type: FieldType.NUMBER },
|
|
|
|
})
|
|
|
|
const auxRow = await config.api.row.save(auxTable._id!, {
|
|
|
|
name: generator.name(),
|
|
|
|
age: generator.age(),
|
|
|
|
})
|
2024-09-25 12:44:30 +02:00
|
|
|
|
|
|
|
const table = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
|
|
|
])
|
2024-09-24 17:51:05 +02:00
|
|
|
await config.api.row.save(table._id!, {
|
2024-09-24 17:42:25 +02:00
|
|
|
title: generator.word(),
|
|
|
|
aux: [auxRow],
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
title: { visible: true },
|
|
|
|
aux: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: false, readonly: false },
|
|
|
|
age: { visible: true, readonly: true },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-09-25 12:44:30 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id)
|
2024-09-24 17:42:25 +02:00
|
|
|
expect(response.rows).toEqual([
|
|
|
|
expect.objectContaining({
|
|
|
|
aux: [
|
|
|
|
{
|
|
|
|
_id: auxRow._id,
|
|
|
|
primaryDisplay: auxRow.name,
|
|
|
|
age: auxRow.age,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
})
|
2024-09-24 17:51:05 +02:00
|
|
|
|
|
|
|
it("enriches squashed fields", async () => {
|
|
|
|
const auxTable = await createAuxTable({
|
|
|
|
user: {
|
|
|
|
name: "user",
|
|
|
|
type: FieldType.BB_REFERENCE_SINGLE,
|
|
|
|
subtype: BBReferenceFieldSubType.USER,
|
|
|
|
constraints: { presence: true },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const table = await createMainTable([
|
|
|
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
|
|
|
])
|
|
|
|
|
|
|
|
const user = config.getUser()
|
|
|
|
const auxRow = await config.api.row.save(auxTable._id!, {
|
|
|
|
name: generator.name(),
|
|
|
|
user: user._id,
|
|
|
|
})
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
title: generator.word(),
|
|
|
|
aux: [auxRow],
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
schema: {
|
|
|
|
title: { visible: true },
|
|
|
|
aux: {
|
|
|
|
visible: true,
|
|
|
|
columns: {
|
|
|
|
name: { visible: true, readonly: true },
|
|
|
|
user: { visible: true, readonly: true },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-09-25 12:44:30 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id)
|
2024-09-24 17:51:05 +02:00
|
|
|
|
|
|
|
expect(response.rows).toEqual([
|
|
|
|
expect.objectContaining({
|
|
|
|
aux: [
|
|
|
|
{
|
|
|
|
_id: auxRow._id,
|
|
|
|
primaryDisplay: auxRow.name,
|
|
|
|
name: auxRow.name,
|
|
|
|
user: {
|
|
|
|
_id: user._id,
|
|
|
|
email: user.email,
|
|
|
|
firstName: user.firstName,
|
|
|
|
lastName: user.lastName,
|
|
|
|
primaryDisplay: user.email,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
})
|
2024-09-24 17:42:25 +02:00
|
|
|
})
|
2024-09-25 15:59:47 +02:00
|
|
|
|
2024-09-26 16:22:10 +02:00
|
|
|
!isLucene &&
|
|
|
|
describe("calculations", () => {
|
|
|
|
let table: Table
|
|
|
|
let rows: Row[]
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
quantity: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "quantity",
|
|
|
|
},
|
|
|
|
price: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2024-09-04 10:29:05 +02:00
|
|
|
|
2024-09-26 16:22:10 +02:00
|
|
|
rows = await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () =>
|
|
|
|
config.api.row.save(table._id!, {
|
|
|
|
quantity: generator.natural({ min: 1, max: 10 }),
|
|
|
|
price: generator.natural({ min: 1, max: 10 }),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to search by calculations", async () => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-09-26 16:22:10 +02:00
|
|
|
name: generator.guid(),
|
2024-09-04 10:29:05 +02:00
|
|
|
schema: {
|
2024-09-26 16:22:10 +02:00
|
|
|
"Quantity Sum": {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "quantity",
|
2024-09-04 10:29:05 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-09-26 16:22:10 +02:00
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
})
|
2024-09-04 10:29:05 +02:00
|
|
|
|
2024-09-26 16:22:10 +02:00
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response.rows).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
"Quantity Sum": rows.reduce((acc, r) => acc + r.quantity, 0),
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
)
|
2024-09-04 10:29:05 +02:00
|
|
|
|
2024-09-26 16:22:10 +02:00
|
|
|
// Calculation views do not return rows that can be linked back to
|
|
|
|
// the source table, and so should not have an _id field.
|
|
|
|
for (const row of response.rows) {
|
|
|
|
expect("_id" in row).toBe(false)
|
|
|
|
}
|
2024-09-04 10:29:05 +02:00
|
|
|
})
|
2024-10-01 12:48:14 +02:00
|
|
|
|
2024-10-01 16:04:01 +02:00
|
|
|
it("should be able to group by a basic field", async () => {
|
2024-10-01 12:48:14 +02:00
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-01 12:48:14 +02:00
|
|
|
schema: {
|
|
|
|
quantity: {
|
|
|
|
visible: true,
|
|
|
|
field: "quantity",
|
|
|
|
},
|
|
|
|
"Total Price": {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
})
|
|
|
|
|
2024-10-01 16:04:01 +02:00
|
|
|
const priceByQuantity: Record<number, number> = {}
|
|
|
|
for (const row of rows) {
|
|
|
|
priceByQuantity[row.quantity] ??= 0
|
|
|
|
priceByQuantity[row.quantity] += row.price
|
|
|
|
}
|
|
|
|
|
2024-10-01 12:48:14 +02:00
|
|
|
for (const row of response.rows) {
|
2024-10-01 16:04:01 +02:00
|
|
|
expect(row["Total Price"]).toEqual(priceByQuantity[row.quantity])
|
2024-10-01 12:48:14 +02:00
|
|
|
}
|
|
|
|
})
|
2024-10-01 17:06:40 +02:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
CalculationType.COUNT,
|
|
|
|
CalculationType.SUM,
|
|
|
|
CalculationType.AVG,
|
|
|
|
CalculationType.MIN,
|
|
|
|
CalculationType.MAX,
|
|
|
|
])("should be able to calculate $type", async type => {
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-01 17:06:40 +02:00
|
|
|
schema: {
|
|
|
|
aggregate: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: type,
|
|
|
|
field: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
})
|
|
|
|
|
|
|
|
function calculate(
|
|
|
|
type: CalculationType,
|
|
|
|
numbers: number[]
|
|
|
|
): number {
|
|
|
|
switch (type) {
|
|
|
|
case CalculationType.COUNT:
|
|
|
|
return numbers.length
|
|
|
|
case CalculationType.SUM:
|
|
|
|
return numbers.reduce((a, b) => a + b, 0)
|
|
|
|
case CalculationType.AVG:
|
|
|
|
return numbers.reduce((a, b) => a + b, 0) / numbers.length
|
|
|
|
case CalculationType.MIN:
|
|
|
|
return Math.min(...numbers)
|
|
|
|
case CalculationType.MAX:
|
|
|
|
return Math.max(...numbers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const prices = rows.map(row => row.price)
|
|
|
|
const expected = calculate(type, prices)
|
|
|
|
const actual = response.rows[0].aggregate
|
|
|
|
|
|
|
|
if (type === CalculationType.AVG) {
|
|
|
|
// The average calculation can introduce floating point rounding
|
|
|
|
// errors, so we need to compare to within a small margin of
|
|
|
|
// error.
|
|
|
|
expect(actual).toBeCloseTo(expected)
|
|
|
|
} else {
|
|
|
|
expect(actual).toEqual(expected)
|
|
|
|
}
|
|
|
|
})
|
2024-10-02 17:42:05 +02:00
|
|
|
|
|
|
|
it("should be able to do a COUNT(DISTINCT)", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-02 17:42:05 +02:00
|
|
|
schema: {
|
|
|
|
count: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
distinct: true,
|
|
|
|
field: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
name: "John",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "John",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sue",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.row.search(view.id)
|
|
|
|
expect(rows).toHaveLength(1)
|
|
|
|
expect(rows[0].count).toEqual(2)
|
|
|
|
})
|
2024-10-02 17:47:53 +02:00
|
|
|
|
|
|
|
it("should not be able to COUNT(DISTINCT ...) against a non-existent field", async () => {
|
|
|
|
await config.api.viewV2.create(
|
|
|
|
{
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-02 17:47:53 +02:00
|
|
|
schema: {
|
|
|
|
count: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.COUNT,
|
|
|
|
distinct: true,
|
|
|
|
field: "does not exist oh no",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400,
|
|
|
|
body: {
|
|
|
|
message:
|
|
|
|
'Calculation field "count" references field "does not exist oh no" which does not exist in the table schema',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-10-09 18:25:41 +02:00
|
|
|
|
2024-10-10 11:37:47 +02:00
|
|
|
it("should be able to filter rows on the view itself", async () => {
|
2024-10-09 18:25:41 +02:00
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
quantity: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "quantity",
|
|
|
|
},
|
|
|
|
price: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
query: {
|
|
|
|
equal: {
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 2,
|
|
|
|
price: 10,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
})
|
|
|
|
expect(rows).toHaveLength(1)
|
|
|
|
expect(rows[0].sum).toEqual(3)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to filter on group by fields", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
quantity: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "quantity",
|
|
|
|
},
|
|
|
|
price: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
quantity: { visible: true },
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 2,
|
|
|
|
price: 10,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
|
|
|
equal: {
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(rows).toHaveLength(1)
|
|
|
|
expect(rows[0].sum).toEqual(3)
|
|
|
|
})
|
2024-10-10 18:10:07 +02:00
|
|
|
|
|
|
|
it("should be able to sort by group by field", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
quantity: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "quantity",
|
|
|
|
},
|
|
|
|
price: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
quantity: { visible: true },
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 2,
|
|
|
|
price: 10,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
sort: "quantity",
|
|
|
|
sortOrder: SortOrder.DESCENDING,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(rows).toEqual([
|
|
|
|
expect.objectContaining({ quantity: 2, sum: 10 }),
|
|
|
|
expect.objectContaining({ quantity: 1, sum: 3 }),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to sort by a calculation", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
quantity: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "quantity",
|
|
|
|
},
|
|
|
|
price: {
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
name: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
await config.api.row.bulkImport(table._id!, {
|
|
|
|
rows: [
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 1,
|
|
|
|
price: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quantity: 2,
|
|
|
|
price: 10,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
type: ViewV2Type.CALCULATION,
|
|
|
|
schema: {
|
|
|
|
quantity: { visible: true },
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
sort: "sum",
|
|
|
|
sortOrder: SortOrder.DESCENDING,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(rows).toEqual([
|
|
|
|
expect.objectContaining({ quantity: 2, sum: 10 }),
|
|
|
|
expect.objectContaining({ quantity: 1, sum: 3 }),
|
|
|
|
])
|
|
|
|
})
|
2024-09-04 10:29:05 +02:00
|
|
|
})
|
2024-10-01 18:23:21 +02:00
|
|
|
|
|
|
|
!isLucene &&
|
|
|
|
it("should not need required fields to be present", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
name: {
|
|
|
|
name: "name",
|
|
|
|
type: FieldType.STRING,
|
|
|
|
constraints: {
|
|
|
|
presence: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
age: {
|
|
|
|
name: "age",
|
|
|
|
type: FieldType.NUMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
config.api.row.save(table._id!, { name: "Steve", age: 30 }),
|
|
|
|
config.api.row.save(table._id!, { name: "Jane", age: 31 }),
|
|
|
|
])
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
2024-10-07 17:33:14 +02:00
|
|
|
type: ViewV2Type.CALCULATION,
|
2024-10-01 18:23:21 +02:00
|
|
|
schema: {
|
|
|
|
sum: {
|
|
|
|
visible: true,
|
|
|
|
calculationType: CalculationType.SUM,
|
|
|
|
field: "age",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(response.rows).toHaveLength(1)
|
|
|
|
expect(response.rows[0].sum).toEqual(61)
|
|
|
|
})
|
2024-10-11 16:55:25 +02:00
|
|
|
|
|
|
|
it("should be able to filter on a single user field in both the view query and search query", async () => {
|
|
|
|
const table = await config.api.table.save(
|
|
|
|
saveTableRequest({
|
|
|
|
schema: {
|
|
|
|
user: {
|
|
|
|
name: "user",
|
|
|
|
type: FieldType.BB_REFERENCE_SINGLE,
|
|
|
|
subtype: BBReferenceFieldSubType.USER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
await config.api.row.save(table._id!, {
|
|
|
|
user: config.getUser()._id,
|
|
|
|
})
|
|
|
|
|
|
|
|
const view = await config.api.viewV2.create({
|
|
|
|
tableId: table._id!,
|
|
|
|
name: generator.guid(),
|
|
|
|
query: {
|
|
|
|
equal: {
|
|
|
|
user: "{{ [user].[_id] }}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
schema: {
|
|
|
|
user: {
|
|
|
|
visible: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const { rows } = await config.api.viewV2.search(view.id, {
|
|
|
|
query: {
|
|
|
|
equal: {
|
|
|
|
user: "{{ [user].[_id] }}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(rows).toHaveLength(1)
|
|
|
|
expect(rows[0].user._id).toEqual(config.getUser()._id)
|
|
|
|
})
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("permissions", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await Promise.all(
|
|
|
|
Array.from({ length: 10 }, () => config.api.row.save(table._id!, {}))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not allow public users to fetch by default", async () => {
|
|
|
|
await config.publish()
|
|
|
|
await config.api.viewV2.publicSearch(view.id, undefined, {
|
2024-07-10 13:36:07 +02:00
|
|
|
status: 401,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allow public users to fetch when permissions are explicit", async () => {
|
|
|
|
await config.api.permission.add({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: view.id,
|
|
|
|
})
|
|
|
|
await config.publish()
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.publicSearch(view.id)
|
|
|
|
|
|
|
|
expect(response.rows).toHaveLength(10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("allow public users to fetch when permissions are inherited", async () => {
|
|
|
|
await config.api.permission.add({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: table._id!,
|
|
|
|
})
|
2024-09-30 16:36:49 +02:00
|
|
|
await config.api.permission.revoke({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, // Don't think this matters since we are revoking the permission
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: view.id,
|
|
|
|
})
|
2024-03-15 18:03:47 +01:00
|
|
|
await config.publish()
|
|
|
|
|
|
|
|
const response = await config.api.viewV2.publicSearch(view.id)
|
|
|
|
|
|
|
|
expect(response.rows).toHaveLength(10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("respects inherited permissions, not allowing not public views from public tables", async () => {
|
|
|
|
await config.api.permission.add({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.PUBLIC,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: table._id!,
|
|
|
|
})
|
|
|
|
await config.api.permission.add({
|
|
|
|
roleId: roles.BUILTIN_ROLE_IDS.POWER,
|
|
|
|
level: PermissionLevel.READ,
|
|
|
|
resourceId: view.id,
|
|
|
|
})
|
|
|
|
await config.publish()
|
|
|
|
|
|
|
|
await config.api.viewV2.publicSearch(view.id, undefined, {
|
2024-07-10 13:36:07 +02:00
|
|
|
status: 401,
|
2024-03-15 18:03:47 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-07-12 16:13:00 +02:00
|
|
|
})
|