Merge Mike's changes.

This commit is contained in:
Sam Rose 2024-11-27 13:15:45 +00:00
commit e7f2dc840e
No known key found for this signature in database
3 changed files with 50 additions and 9 deletions

View File

@ -1,11 +1,13 @@
import { import {
Datasource, Datasource,
Operation, Operation,
QueryJson,
SourceName, SourceName,
SqlQuery, SqlQuery,
SqlClient, SqlClient,
EnrichedQueryJson, EnrichedQueryJson,
TableSchema,
Table,
TableSourceType,
} 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"
@ -13,8 +15,23 @@ import { generator } from "@budibase/backend-core/tests"
import sdk from "../../sdk" import sdk from "../../sdk"
const Sql = sql.Sql const Sql = sql.Sql
// this doesn't exist strictly
const TABLE = buildTable("tableName", {})
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, " ")
} }
@ -23,8 +40,22 @@ describe("Captures of real examples", () => {
const relationshipLimit = 500 const relationshipLimit = 500
const primaryLimit = 100 const primaryLimit = 100
function getJson(name: string) { function getJson(name: string): EnrichedQueryJson {
return require(join(__dirname, "sqlQueryJson", name)) as EnrichedQueryJson // 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", () => {
@ -51,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"`
) )
), ),
}) })
@ -82,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`
) )
@ -200,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],
}) })
@ -234,12 +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: { datasource: "", entityId: "", operation: op }, endpoint: { datasource: "", entityId: "", operation: op },
resource: { resource: {
fields, fields,
}, },
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"],

View File

@ -12635,6 +12635,11 @@ husky@^8.0.3:
resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184" resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184"
integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg== integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==
husky@^9.1.4:
version "9.1.7"
resolved "https://registry.yarnpkg.com/husky/-/husky-9.1.7.tgz#d46a38035d101b46a70456a850ff4201344c0b2d"
integrity sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==
ical-generator@4.1.0: ical-generator@4.1.0:
version "4.1.0" version "4.1.0"
resolved "https://registry.yarnpkg.com/ical-generator/-/ical-generator-4.1.0.tgz#2a336c951864c5583a2aa715d16f2edcdfd2d90b" resolved "https://registry.yarnpkg.com/ical-generator/-/ical-generator-4.1.0.tgz#2a336c951864c5583a2aa715d16f2edcdfd2d90b"