Adding tests to confirm when aliasing should be used.

This commit is contained in:
Michael Drury 2024-03-05 17:27:35 +00:00
parent a332c058ce
commit 0520c0c540
2 changed files with 84 additions and 19 deletions

View File

@ -1,17 +1,17 @@
import {
Datasource,
DatasourcePlusQueryResponse,
Operation,
QueryJson,
Row,
SearchFilters,
} from "@budibase/types"
import { Datasource, DatasourcePlusQueryResponse, Operation, QueryJson, Row, SearchFilters } from "@budibase/types"
import { getSQLClient } from "../../../sdk/app/rows/utils"
import { cloneDeep } from "lodash"
import sdk from "../../../sdk"
import { makeExternalQuery } from "../../../integrations/base/query"
import { SqlClient } from "../../../integrations/utils"
const WRITE_OPERATIONS: Operation[] = [
Operation.CREATE,
Operation.UPDATE,
Operation.DELETE,
]
const DISABLED_WRITE_CLIENTS: SqlClient[] = [SqlClient.MY_SQL, SqlClient.MS_SQL, SqlClient.ORACLE]
class CharSequence {
static alphabet = "abcdefghijklmnopqrstuvwxyz"
counters: number[]
@ -52,18 +52,11 @@ export default class AliasTables {
if (!fieldLength || fieldLength <= 0) {
return false
}
const writeOperations = [
Operation.CREATE,
Operation.UPDATE,
Operation.DELETE,
]
try {
const sqlClient = getSQLClient(datasource)
const isWrite = writeOperations.includes(json.endpoint.operation)
if (
isWrite &&
(sqlClient === SqlClient.MY_SQL || sqlClient === SqlClient.MS_SQL)
) {
const isWrite = WRITE_OPERATIONS.includes(json.endpoint.operation)
const isDisabledClient = DISABLED_WRITE_CLIENTS.includes(sqlClient)
if (isWrite && isDisabledClient) {
return false
}
} catch (err) {

View File

@ -1,4 +1,4 @@
import { QueryJson } from "@budibase/types"
import { Datasource, Operation, QueryJson, SourceName } from "@budibase/types"
import { join } from "path"
import Sql from "../base/sql"
import { SqlClient } from "../utils"
@ -198,6 +198,78 @@ describe("Captures of real examples", () => {
})
})
describe("check aliasing is disabled/enabled", () => {
const tables = ["tableA", "tableB"]
function getDatasource(source: SourceName): Datasource {
return {
source,
type: "datasource",
isSQL: true,
}
}
function getQuery(op: Operation, fields: string[] = ["a"]): QueryJson {
return {
endpoint: { datasourceId: "", entityId: "", operation: op },
resource: {
fields,
}
}
}
it("should check for Postgres aliased status", () => {
const aliasing = new AliasTables(tables)
const datasource = getDatasource(SourceName.POSTGRES)
expect(aliasing.isAliasingEnabled(getQuery(Operation.CREATE), datasource)).toEqual(true)
expect(aliasing.isAliasingEnabled(getQuery(Operation.READ), datasource)).toEqual(true)
expect(aliasing.isAliasingEnabled(getQuery(Operation.UPDATE), datasource)).toEqual(true)
expect(aliasing.isAliasingEnabled(getQuery(Operation.DELETE), datasource)).toEqual(true)
})
it("should check for MS-SQL aliased status", () => {
const aliasing = new AliasTables(tables)
const datasource = getDatasource(SourceName.SQL_SERVER)
expect(aliasing.isAliasingEnabled(getQuery(Operation.CREATE), datasource)).toEqual(false)
expect(aliasing.isAliasingEnabled(getQuery(Operation.READ), datasource)).toEqual(true)
expect(aliasing.isAliasingEnabled(getQuery(Operation.UPDATE), datasource)).toEqual(false)
expect(aliasing.isAliasingEnabled(getQuery(Operation.DELETE), datasource)).toEqual(false)
})
it("should check for MySQL aliased status", () => {
const aliasing = new AliasTables(tables)
const datasource = getDatasource(SourceName.MYSQL)
expect(aliasing.isAliasingEnabled(getQuery(Operation.CREATE), datasource)).toEqual(false)
expect(aliasing.isAliasingEnabled(getQuery(Operation.READ), datasource)).toEqual(true)
expect(aliasing.isAliasingEnabled(getQuery(Operation.UPDATE), datasource)).toEqual(false)
expect(aliasing.isAliasingEnabled(getQuery(Operation.DELETE), datasource)).toEqual(false)
})
it("should check for Oracle aliased status", () => {
const aliasing = new AliasTables(tables)
const datasource = getDatasource(SourceName.ORACLE)
expect(aliasing.isAliasingEnabled(getQuery(Operation.CREATE), datasource)).toEqual(false)
expect(aliasing.isAliasingEnabled(getQuery(Operation.READ), datasource)).toEqual(true)
expect(aliasing.isAliasingEnabled(getQuery(Operation.UPDATE), datasource)).toEqual(false)
expect(aliasing.isAliasingEnabled(getQuery(Operation.DELETE), datasource)).toEqual(false)
})
it("should disable aliasing for non-SQL datasources", () => {
const aliasing = new AliasTables(tables)
expect(aliasing.isAliasingEnabled(getQuery(Operation.READ), {
source: SourceName.GOOGLE_SHEETS,
type: "datasource",
isSQL: false,
}))
})
it("should disable when no fields", () => {
const aliasing = new AliasTables(tables)
const datasource = getDatasource(SourceName.POSTGRES)
expect(aliasing.isAliasingEnabled(getQuery(Operation.READ, []), datasource)).toEqual(false)
})
})
describe("check some edge cases", () => {
const tableNames = ["hello", "world"]