From 0498ad6e54dd16b35a26cf54fe8e2ecdfa40f165 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Mon, 22 May 2023 17:44:21 +0100 Subject: [PATCH 1/4] Add test for relationship creation for MySQL --- .../types/src/documents/app/datasource.ts | 2 +- qa-core/package.json | 3 +- .../src/internal-api/fixtures/datasources.ts | 50 +++++++++++++++++++ .../dataSources/mariaDB.integration.spec.ts | 37 ++++++++++++++ .../postgresSQL.integration.spec.ts | 2 +- 5 files changed, 91 insertions(+), 3 deletions(-) diff --git a/packages/types/src/documents/app/datasource.ts b/packages/types/src/documents/app/datasource.ts index 8dfdfe6d0f..cd378ab46c 100644 --- a/packages/types/src/documents/app/datasource.ts +++ b/packages/types/src/documents/app/datasource.ts @@ -12,7 +12,7 @@ export interface Datasource extends Document { } plus?: boolean entities?: { - [key: string]: Table + [key: string]: any } } diff --git a/qa-core/package.json b/qa-core/package.json index 987fe36d7c..c5208b5d32 100644 --- a/qa-core/package.json +++ b/qa-core/package.json @@ -17,7 +17,8 @@ "test:smoke": "yarn run test --testPathIgnorePatterns=/.+\\.integration\\.spec\\.ts", "test:ci": "start-server-and-test dev:built http://localhost:4001/health test:smoke", "serve": "start-server-and-test dev:built http://localhost:4001/health", - "dev:built": "cd ../ && yarn dev:built" + "dev:built": "cd ../ && yarn dev:built", + "test:datasources": "jest --runInBand --json --outputFile=testResults.json --testPathPattern='datasources/.*\\.spec\\.(ts|js)$'" }, "devDependencies": { "@budibase/types": "^2.3.17", diff --git a/qa-core/src/internal-api/fixtures/datasources.ts b/qa-core/src/internal-api/fixtures/datasources.ts index 4797f86631..c7f969a98c 100644 --- a/qa-core/src/internal-api/fixtures/datasources.ts +++ b/qa-core/src/internal-api/fixtures/datasources.ts @@ -1,4 +1,7 @@ +import { Datasource } from "@budibase/types" import { DatasourceRequest } from "../../types" +import { generator } from "../../shared" + // Add information about the data source to the fixtures file from 1password export const mongoDB = (): DatasourceRequest => { return { @@ -70,3 +73,50 @@ export const restAPI = (): DatasourceRequest => { fetchSchema: false, } } + +export const generateRelationshipForMySQL = ( + updatedDataSourceJson: any +): Datasource => { + const entities = updatedDataSourceJson!.datasource!.entities! + const datasourceId = updatedDataSourceJson!.datasource!._id! + const relationShipBody = { + ...updatedDataSourceJson.datasource, + entities: { + ...updatedDataSourceJson.datasource.entities, + employees: { + ...entities.employees, + schema: { + ...entities.employees.schema, + salaries: { + tableId: `${datasourceId}__salaries`, + name: "salaries", + relationshipType: "many-to-one", + fieldName: "salary", + type: "link", + main: true, + _id: generator.string(), + foreignKey: "emp_no", + }, + }, + }, + titles: { + ...entities.titles, + schema: { + ...entities.titles.schema, + employees: { + tableId: `${datasourceId}__employees`, + name: "employees", + relationshipType: "one-to-many", + fieldName: "emp_no", + type: "link", + main: true, + _id: generator.string(), + foreignKey: "emp_no", + }, + }, + }, + }, + } + + return relationShipBody +} diff --git a/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts b/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts index 3f479136f6..e68b35c2b5 100644 --- a/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts +++ b/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts @@ -66,4 +66,41 @@ describe("Internal API - Data Sources: MariaDB", () => { updatedDataSourceJson.datasource._rev! ) }) + + it("Create a relationship", async () => { + // Create app + await config.createApp() + + // Get all integrations + await config.api.integrations.getAll() + + // Add data source + const [dataSourceResponse, dataSourceJson] = + await config.api.datasources.add(fixtures.datasources.mariaDB()) + + // Update data source + const newDataSourceInfo = { + ...dataSourceJson.datasource, + name: "MariaDB2", + } + const [updatedDataSourceResponse, updatedDataSourceJson] = + await config.api.datasources.update(newDataSourceInfo) + + // Query data source + const [queryResponse, queryJson] = await config.api.queries.preview( + fixtures.queries.mariaDB(updatedDataSourceJson.datasource._id!) + ) + + expect(queryJson.rows.length).toEqual(10) + expect(queryJson.schemaFields).toEqual( + fixtures.queries.expectedSchemaFields.mariaDB + ) + + // Add relationship + const relationShipBody = fixtures.datasources.generateRelationshipForMySQL( + updatedDataSourceJson + ) + const [relationshipResponse, relationshipJson] = + await config.api.datasources.update(relationShipBody) + }) }) diff --git a/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts b/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts index ccfcce6f55..f59124e0ee 100644 --- a/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts +++ b/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts @@ -37,7 +37,7 @@ describe("Internal API - Data Sources: PostgresSQL", () => { fixtures.queries.postgres(updatedDataSourceJson.datasource._id!) ) - expect(queryJson.rows.length).toEqual(91) + expect(queryJson.rows.length).toEqual(92) expect(queryJson.schemaFields).toEqual( fixtures.queries.expectedSchemaFields.postgres ) From 4fd944dc35a6ac92088f0b21df93040fa620703e Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 30 May 2023 18:30:49 +0100 Subject: [PATCH 2/4] Replace any with Table --- packages/types/src/documents/app/datasource.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/documents/app/datasource.ts b/packages/types/src/documents/app/datasource.ts index cd378ab46c..8dfdfe6d0f 100644 --- a/packages/types/src/documents/app/datasource.ts +++ b/packages/types/src/documents/app/datasource.ts @@ -12,7 +12,7 @@ export interface Datasource extends Document { } plus?: boolean entities?: { - [key: string]: any + [key: string]: Table } } From 8b2737baa9d4f2502c1a9f0a8674857db834369d Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 30 May 2023 18:31:45 +0100 Subject: [PATCH 3/4] Remove temp command to only run datasource tests --- qa-core/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qa-core/package.json b/qa-core/package.json index c5208b5d32..987fe36d7c 100644 --- a/qa-core/package.json +++ b/qa-core/package.json @@ -17,8 +17,7 @@ "test:smoke": "yarn run test --testPathIgnorePatterns=/.+\\.integration\\.spec\\.ts", "test:ci": "start-server-and-test dev:built http://localhost:4001/health test:smoke", "serve": "start-server-and-test dev:built http://localhost:4001/health", - "dev:built": "cd ../ && yarn dev:built", - "test:datasources": "jest --runInBand --json --outputFile=testResults.json --testPathPattern='datasources/.*\\.spec\\.(ts|js)$'" + "dev:built": "cd ../ && yarn dev:built" }, "devDependencies": { "@budibase/types": "^2.3.17", From 8f09c07f46167a4a64c0df53cb467a35feb0c0f8 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 30 May 2023 18:48:33 +0100 Subject: [PATCH 4/4] Fix assertion --- .../internal-api/tests/dataSources/mariaDB.integration.spec.ts | 2 +- .../tests/dataSources/postgresSQL.integration.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts b/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts index e68b35c2b5..24c9cea94b 100644 --- a/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts +++ b/qa-core/src/internal-api/tests/dataSources/mariaDB.integration.spec.ts @@ -91,7 +91,7 @@ describe("Internal API - Data Sources: MariaDB", () => { fixtures.queries.mariaDB(updatedDataSourceJson.datasource._id!) ) - expect(queryJson.rows.length).toEqual(10) + expect(queryJson.rows.length).toBeGreaterThan(9) expect(queryJson.schemaFields).toEqual( fixtures.queries.expectedSchemaFields.mariaDB ) diff --git a/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts b/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts index f59124e0ee..2aabd608bc 100644 --- a/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts +++ b/qa-core/src/internal-api/tests/dataSources/postgresSQL.integration.spec.ts @@ -37,7 +37,7 @@ describe("Internal API - Data Sources: PostgresSQL", () => { fixtures.queries.postgres(updatedDataSourceJson.datasource._id!) ) - expect(queryJson.rows.length).toEqual(92) + expect(queryJson.rows.length).toBeGreaterThan(10) expect(queryJson.schemaFields).toEqual( fixtures.queries.expectedSchemaFields.postgres )