Fixing an issue with to_char testing in sql.spec.ts.

This commit is contained in:
mike12345567 2024-07-30 12:29:16 +01:00
parent b6560d1d7b
commit ff2802873e
1 changed files with 30 additions and 9 deletions

View File

@ -1,12 +1,16 @@
import { import {
FieldType, FieldType,
Operation, Operation,
PaginationJson,
QueryJson, QueryJson,
SearchFilters,
SortJson,
SqlClient,
Table, Table,
TableSourceType, TableSourceType,
SqlClient,
} from "@budibase/types" } from "@budibase/types"
import { sql } from "@budibase/backend-core" import { sql } from "@budibase/backend-core"
import { merge } from "lodash"
const Sql = sql.Sql const Sql = sql.Sql
@ -25,7 +29,16 @@ const TABLE: Table = {
primary: ["id"], primary: ["id"],
} }
function endpoint(table: any, operation: any) { const ORACLE_TABLE: Partial<Table> = {
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
},
}
function endpoint(table: string, operation: Operation) {
return { return {
datasourceId: "Postgres", datasourceId: "Postgres",
operation: operation, operation: operation,
@ -39,19 +52,25 @@ function generateReadJson({
filters, filters,
sort, sort,
paginate, paginate,
}: any = {}): QueryJson { }: {
const tableObj = { ...TABLE } table?: Partial<Table>
fields?: string[]
filters?: SearchFilters
sort?: SortJson
paginate?: PaginationJson
} = {}): QueryJson {
let tableObj: Table = { ...TABLE }
if (table) { if (table) {
tableObj.name = table tableObj = merge(TABLE, table)
} }
return { return {
endpoint: endpoint(table || TABLE_NAME, "READ"), endpoint: endpoint(tableObj.name || TABLE_NAME, Operation.READ),
resource: { resource: {
fields: fields || [], fields: fields || [],
}, },
filters: filters || {}, filters: filters || {},
sort: sort || {}, sort: sort || {},
paginate: paginate || {}, paginate: paginate || undefined,
meta: { meta: {
table: tableObj, table: tableObj,
}, },
@ -212,6 +231,7 @@ describe("SQL query builder", () => {
it("should use an oracle compatible coalesce query for oracle when using the equals filter", () => { it("should use an oracle compatible coalesce query for oracle when using the equals filter", () => {
let query = new Sql(SqlClient.ORACLE, limit)._query( let query = new Sql(SqlClient.ORACLE, limit)._query(
generateReadJson({ generateReadJson({
table: ORACLE_TABLE,
filters: { filters: {
equal: { equal: {
name: "John", name: "John",
@ -222,13 +242,14 @@ describe("SQL query builder", () => {
expect(query).toEqual({ expect(query).toEqual({
bindings: ["John", limit, 5000], bindings: ["John", limit, 5000],
sql: `select * from (select * from (select * from (select * from "test" where COALESCE("test"."name", -1) = :1 order by "test"."id" asc) where rownum <= :2) "test" order by "test"."id" asc) where rownum <= :3`, sql: `select * from (select * from (select * from (select * from "test" where (to_char("test"."name") IS NOT NULL AND to_char("test"."name") = :1) order by "test"."id" asc) where rownum <= :2) "test" order by "test"."id" asc) where rownum <= :3`,
}) })
}) })
it("should use an oracle compatible coalesce query for oracle when using the not equals filter", () => { it("should use an oracle compatible coalesce query for oracle when using the not equals filter", () => {
let query = new Sql(SqlClient.ORACLE, limit)._query( let query = new Sql(SqlClient.ORACLE, limit)._query(
generateReadJson({ generateReadJson({
table: ORACLE_TABLE,
filters: { filters: {
notEqual: { notEqual: {
name: "John", name: "John",
@ -239,7 +260,7 @@ describe("SQL query builder", () => {
expect(query).toEqual({ expect(query).toEqual({
bindings: ["John", limit, 5000], bindings: ["John", limit, 5000],
sql: `select * from (select * from (select * from (select * from "test" where COALESCE("test"."name", -1) != :1 order by "test"."id" asc) where rownum <= :2) "test" order by "test"."id" asc) where rownum <= :3`, sql: `select * from (select * from (select * from (select * from "test" where (to_char("test"."name") IS NOT NULL AND to_char("test"."name") != :1) OR to_char("test"."name") IS NULL order by "test"."id" asc) where rownum <= :2) "test" order by "test"."id" asc) where rownum <= :3`,
}) })
}) })
}) })