From e03975462e56a15dc280e90d9fc3a6e2a4609e6d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 19 Apr 2024 11:09:20 +0200 Subject: [PATCH] Improve tests --- .../src/api/routes/tests/datasource.spec.ts | 147 +++++++++++++++++- .../src/tests/utilities/api/datasource.ts | 10 ++ .../types/src/documents/app/datasource.ts | 4 +- 3 files changed, 153 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index f5e31b6a1a..1e463115c2 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.ts +++ b/packages/server/src/api/routes/tests/datasource.spec.ts @@ -5,9 +5,19 @@ import { context, events } from "@budibase/backend-core" import sdk from "../../../sdk" import tk from "timekeeper" -import { mocks } from "@budibase/backend-core/tests" -import { QueryPreview, SourceName } from "@budibase/types" +import { generator, mocks } from "@budibase/backend-core/tests" +import { + Datasource, + FieldSchema, + FieldType, + QueryPreview, + RelationshipType, + SourceName, + Table, + TableSchema, +} from "@budibase/types" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" +import { tableForDatasource } from "../../../tests/utilities/structures" tk.freeze(mocks.date.MOCK_DATE) @@ -225,7 +235,7 @@ describe("/datasources", () => { }) }) - describe.only.each([ + describe.each([ [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], @@ -235,13 +245,140 @@ describe("/datasources", () => { datasource = await config.api.datasource.create(await dsProvider) }) - it("aa", async () => { + it("fetching schema will not drop tables or columns", async () => { const datasourceId = datasource!._id! + + const simpleTable = await config.api.table.save( + tableForDatasource(datasource, { + name: generator.guid(), + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + }, + }) + ) + const fullSchema: { [type in FieldType]: FieldSchema & { type: type } } = + { + [FieldType.STRING]: { + name: "string", + type: FieldType.STRING, + }, + // [FieldType.LONGFORM]: { + // name: "longform", + // type: FieldType.LONGFORM, + // }, + // [FieldType.OPTIONS]: { + // name: "options", + // type: FieldType.OPTIONS, + // }, + [FieldType.NUMBER]: { + name: "number", + type: FieldType.NUMBER, + }, + // [FieldType.BOOLEAN]: { + // name: "boolean", + // type: FieldType.BOOLEAN, + // }, + // [FieldType.ARRAY]: { + // name: "array", + // type: FieldType.ARRAY, + // }, + // [FieldType.DATETIME]: { + // name: "datetime", + // type: FieldType.DATETIME, + // }, + // [FieldType.ATTACHMENTS]: { + // name: "attachments", + // type: FieldType.ATTACHMENTS, + // }, + // [FieldType.ATTACHMENT_SINGLE]: { + // name: "attachment_single", + // type: FieldType.ATTACHMENT_SINGLE, + // }, + // [FieldType.LINK]: { + // name: "link", + // type: FieldType.LINK, + // tableId: simpleTable._id!, + // relationshipType: RelationshipType.ONE_TO_MANY, + // fieldName: "link", + // foreignKey: "fk", + // }, + // [FieldType.FORMULA]: { + // name: "formula", + // type: FieldType.FORMULA, + // formula: "any formula", + // }, + // [FieldType.AUTO]: { + // name: "auto", + // type: FieldType.AUTO, + // }, + // [FieldType.JSON]: { + // name: "json", + // type: FieldType.JSON, + // }, + // [FieldType.INTERNAL]: { + // name: "internal", + // type: FieldType.INTERNAL, + // }, + // [FieldType.BARCODEQR]: { + // name: "barcodeqr", + // type: FieldType.BARCODEQR, + // }, + // [FieldType.BIGINT]: { + // name: "bigint", + // type: FieldType.BIGINT, + // }, + // [FieldType.BB_REFERENCE]: { + // name: "bb_reference", + // type: FieldType.BB_REFERENCE, + // }, + } + + const fullTable = await config.api.table.save( + tableForDatasource(datasource, { + name: generator.guid(), + schema: fullSchema, + }) + ) + const persisted = await config.api.datasource.get(datasourceId) await config.api.datasource.fetchSchema(datasourceId) const updated = await config.api.datasource.get(datasourceId) - expect(updated).toEqual(persisted) + const expected: Datasource = { + ...persisted, + entities: + persisted?.entities && + Object.entries(persisted.entities).reduce>( + (acc, [tableName, table]) => { + acc[tableName] = { + ...table, + primaryDisplay: expect.not.stringMatching( + new RegExp(`^${table.primaryDisplay || ""}$`) + ), + schema: Object.entries(table.schema).reduce( + (acc, [fieldName, field]) => { + acc[fieldName] = expect.objectContaining({ + ...field, + externalType: expect.not.stringMatching( + new RegExp(`^${field.externalType || ""}$`) + ), + }) + return acc + }, + {} + ), + } + return acc + }, + {} + ), + + _rev: expect.any(String), + } + expect(updated).toEqual(expected) }) }) }) diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index 6ac624f0db..bb4c74093c 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -5,6 +5,7 @@ import { UpdateDatasourceResponse, UpdateDatasourceRequest, QueryJson, + BuildSchemaFromSourceResponse, } from "@budibase/types" import { Expectations, TestAPI } from "./base" @@ -69,4 +70,13 @@ export class DatasourceAPI extends TestAPI { expectations, }) } + + fetchSchema = async (id: string, expectations?: Expectations) => { + return await this._post( + `/api/datasources/${id}/schema`, + { + expectations, + } + ) + } } diff --git a/packages/types/src/documents/app/datasource.ts b/packages/types/src/documents/app/datasource.ts index 8976e1cae3..32f5bbb132 100644 --- a/packages/types/src/documents/app/datasource.ts +++ b/packages/types/src/documents/app/datasource.ts @@ -13,9 +13,7 @@ export interface Datasource extends Document { config?: Record plus?: boolean isSQL?: boolean - entities?: { - [key: string]: Table - } + entities?: Record } export enum RestAuthType {