Add test for relationship creation for MySQL

This commit is contained in:
Pedro Silva 2023-05-22 17:44:21 +01:00
parent 6e23d78677
commit 0498ad6e54
5 changed files with 91 additions and 3 deletions

View File

@ -12,7 +12,7 @@ export interface Datasource extends Document {
}
plus?: boolean
entities?: {
[key: string]: Table
[key: string]: any
}
}

View File

@ -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",

View File

@ -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
}

View File

@ -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)
})
})

View File

@ -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
)