Updating sql alias test case.

This commit is contained in:
mike12345567 2024-11-26 18:15:08 +00:00
parent e4ae16cc39
commit 92e92c8b7f
3 changed files with 42 additions and 21 deletions

View File

@ -1208,7 +1208,7 @@ class InternalBuilder {
const schema = table.schema[baseName] const schema = table.schema[baseName]
let identifier = this.rawQuotedIdentifier(tableField) let identifier = this.rawQuotedIdentifier(tableField)
if (schema.type === FieldType.BIGINT) { if (schema?.type === FieldType.BIGINT) {
identifier = this.castIntToString(identifier) identifier = this.castIntToString(identifier)
} }
return [unaliased, identifier] return [unaliased, identifier]

View File

@ -1,12 +1,13 @@
import { import {
Datasource, Datasource,
Operation, Operation,
QueryJson,
SourceName, SourceName,
SqlQuery, SqlQuery,
Table, Table,
TableSourceType, TableSourceType,
SqlClient, SqlClient,
EnrichedQueryJson,
TableSchema,
} from "@budibase/types" } from "@budibase/types"
import { sql } from "@budibase/backend-core" import { sql } from "@budibase/backend-core"
import { join } from "path" import { join } from "path"
@ -16,17 +17,21 @@ import sdk from "../../sdk"
const Sql = sql.Sql const Sql = sql.Sql
// this doesn't exist strictly // this doesn't exist strictly
const TABLE: Table = { const TABLE = buildTable("tableName", {})
type: "table",
sourceType: TableSourceType.EXTERNAL,
sourceId: "SOURCE_ID",
schema: {},
name: "tableName",
primary: ["id"],
}
const AliasTables = sdk.rows.AliasTables const AliasTables = sdk.rows.AliasTables
function buildTable(name: string, schema: TableSchema): Table {
return {
type: "table",
sourceType: TableSourceType.EXTERNAL,
sourceId: "SOURCE_ID",
schema: schema,
name: name,
primary: ["id"],
}
}
function multiline(sql: string) { function multiline(sql: string) {
return sql.replace(/\n/g, "").replace(/ +/g, " ") return sql.replace(/\n/g, "").replace(/ +/g, " ")
} }
@ -35,8 +40,22 @@ describe("Captures of real examples", () => {
const relationshipLimit = 500 const relationshipLimit = 500
const primaryLimit = 100 const primaryLimit = 100
function getJson(name: string): QueryJson { function getJson(name: string): EnrichedQueryJson {
return require(join(__dirname, "sqlQueryJson", name)) as QueryJson // tables aren't fully specified in the test JSON
const base = require(join(__dirname, "sqlQueryJson", name)) as Omit<
EnrichedQueryJson,
"tables"
>
const tables: Record<string, Table> = { [base.table.name]: base.table }
if (base.relationships) {
for (let { tableName } of base.relationships) {
tables[tableName] = buildTable(tableName, {})
}
}
return {
...base,
tables: tables,
}
} }
describe("create", () => { describe("create", () => {
@ -63,7 +82,7 @@ describe("Captures of real examples", () => {
bindings: [primaryLimit, relationshipLimit, relationshipLimit], bindings: [primaryLimit, relationshipLimit, relationshipLimit],
sql: expect.stringContaining( sql: expect.stringContaining(
multiline( multiline(
`select json_agg(json_build_object('completed',"b"."completed",'completed',"b"."completed",'executorid',"b"."executorid",'executorid',"b"."executorid",'qaid',"b"."qaid",'qaid',"b"."qaid",'taskid',"b"."taskid",'taskid',"b"."taskid",'taskname',"b"."taskname",'taskname',"b"."taskname")` `select json_agg(json_build_object('executorid',"b"."executorid",'executorid',"b"."executorid",'qaid',"b"."qaid",'qaid',"b"."qaid",'taskid',"b"."taskid",'taskid',"b"."taskid",'completed',"b"."completed",'completed',"b"."completed",'taskname',"b"."taskname",'taskname',"b"."taskname"`
) )
), ),
}) })
@ -94,7 +113,7 @@ describe("Captures of real examples", () => {
sql: expect.stringContaining( sql: expect.stringContaining(
multiline( multiline(
`with "paginated" as (select "a".* from "products" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc limit $1) `with "paginated" as (select "a".* from "products" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc limit $1)
select "a".*, (select json_agg(json_build_object('completed',"b"."completed",'executorid',"b"."executorid",'qaid',"b"."qaid",'taskid',"b"."taskid",'taskname',"b"."taskname")) select "a".*, (select json_agg(json_build_object('executorid',"b"."executorid",'qaid',"b"."qaid",'taskid',"b"."taskid",'completed',"b"."completed",'taskname',"b"."taskname"))
from (select "b".* from "tasks" as "b" inner join "products_tasks" as "c" on "b"."taskid" = "c"."taskid" where "c"."productid" = "a"."productid" order by "b"."taskid" asc limit $2) as "b") as "tasks" from (select "b".* from "tasks" as "b" inner join "products_tasks" as "c" on "b"."taskid" = "c"."taskid" where "c"."productid" = "a"."productid" order by "b"."taskid" asc limit $2) as "b") as "tasks"
from "paginated" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc` from "paginated" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc`
) )
@ -212,8 +231,8 @@ describe("Captures of real examples", () => {
}, queryJson) }, queryJson)
expect(returningQuery).toEqual({ expect(returningQuery).toEqual({
sql: multiline( sql: multiline(
`select top (@p0) * from [people] where CASE WHEN [people].[name] = @p1 THEN 1 ELSE 0 END = 1 `select top (@p0) * from [people] as [a] where CASE WHEN [a].[name] = @p1 THEN 1 ELSE 0 END = 1 and
and CASE WHEN [people].[age] = @p2 THEN 1 ELSE 0 END = 1 order by [people].[name] asc` CASE WHEN [a].[age] = @p2 THEN 1 ELSE 0 END = 1 order by [a].[name] asc`
), ),
bindings: [1, "Test", 22], bindings: [1, "Test", 22],
}) })
@ -246,15 +265,17 @@ describe("Captures of real examples", () => {
} }
} }
function getQuery(op: Operation, fields: string[] = ["a"]): QueryJson { function getQuery(
op: Operation,
fields: string[] = ["a"]
): EnrichedQueryJson {
return { return {
endpoint: { datasourceId: "", entityId: "", operation: op }, endpoint: { datasourceId: "", entityId: "", operation: op },
resource: { resource: {
fields, fields,
}, },
meta: { table: TABLE,
table: TABLE, tables: { [TABLE.name]: TABLE },
},
} }
} }

View File

@ -51,7 +51,7 @@
"extra": { "extra": {
"idFilter": {} "idFilter": {}
}, },
"meta": { "table": {
"type": "table", "type": "table",
"_id": "datasource_plus_8066e56456784eb2a00129d31be5c3e7__persons", "_id": "datasource_plus_8066e56456784eb2a00129d31be5c3e7__persons",
"primary": ["personid"], "primary": ["personid"],