Fixing aliasing test cases.

This commit is contained in:
mike12345567 2024-09-05 17:57:15 +01:00
parent e30469ce16
commit 7cdf8137c5
1 changed files with 48 additions and 43 deletions

View File

@ -32,8 +32,8 @@ function multiline(sql: string) {
} }
describe("Captures of real examples", () => { describe("Captures of real examples", () => {
const limit = 5000 const baseLimit = 5000
const relationshipLimit = 100 const primaryLimit = 100
function getJson(name: string): QueryJson { function getJson(name: string): QueryJson {
return require(join(__dirname, "sqlQueryJson", name)) as QueryJson return require(join(__dirname, "sqlQueryJson", name)) as QueryJson
@ -42,7 +42,7 @@ describe("Captures of real examples", () => {
describe("create", () => { describe("create", () => {
it("should create a row with relationships", () => { it("should create a row with relationships", () => {
const queryJson = getJson("createWithRelationships.json") const queryJson = getJson("createWithRelationships.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: ["A Street", 34, "London", "A", "B", "designer", 1990], bindings: ["A Street", 34, "London", "A", "B", "designer", 1990],
sql: multiline(`insert into "persons" ("address", "age", "city", "firstname", "lastname", "type", "year") sql: multiline(`insert into "persons" ("address", "age", "city", "firstname", "lastname", "type", "year")
@ -54,40 +54,42 @@ describe("Captures of real examples", () => {
describe("read", () => { describe("read", () => {
it("should handle basic retrieval with relationships", () => { it("should handle basic retrieval with relationships", () => {
const queryJson = getJson("basicFetchWithRelationships.json") const queryJson = getJson("basicFetchWithRelationships.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: [relationshipLimit, limit], bindings: [baseLimit, baseLimit, primaryLimit],
sql: expect.stringContaining( sql: expect.stringContaining(
multiline(`select "a"."year" as "a.year", "a"."firstname" as "a.firstname", "a"."personid" as "a.personid", multiline(
"a"."address" as "a.address", "a"."age" as "a.age", "a"."type" as "a.type", "a"."city" as "a.city", `select json_agg(json_build_object('executorid',"b"."executorid",'taskname',"b"."taskname",'taskid',"b"."taskid",'completed',"b"."completed",'qaid',"b"."qaid",'executorid',"b"."executorid",'taskname',"b"."taskname",'taskid',"b"."taskid",'completed',"b"."completed",'qaid',"b"."qaid")`
"a"."lastname" as "a.lastname", "b"."executorid" as "b.executorid", "b"."taskname" as "b.taskname", )
"b"."taskid" as "b.taskid", "b"."completed" as "b.completed", "b"."qaid" as "b.qaid",
"b"."executorid" as "b.executorid", "b"."taskname" as "b.taskname", "b"."taskid" as "b.taskid",
"b"."completed" as "b.completed", "b"."qaid" as "b.qaid"`)
), ),
}) })
}) })
it("should handle filtering by relationship", () => { it("should handle filtering by relationship", () => {
const queryJson = getJson("filterByRelationship.json") const queryJson = getJson("filterByRelationship.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: [relationshipLimit, "assembling", limit], bindings: [baseLimit, "assembling", primaryLimit],
sql: expect.stringContaining( sql: expect.stringContaining(
multiline(`where COALESCE("b"."taskname" = $2, FALSE) multiline(
order by "a"."productname" asc nulls first, "a"."productid" asc limit $3`) `where exists (select 1 from "tasks" as "b" inner join "products_tasks" as "c" on "b"."taskid" = "c"."taskid"
where "c"."productid" = "a"."productid" and COALESCE("b"."taskname" = $2, FALSE)`
)
), ),
}) })
}) })
it("should handle fetching many to many relationships", () => { it("should handle fetching many to many relationships", () => {
const queryJson = getJson("fetchManyToMany.json") const queryJson = getJson("fetchManyToMany.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: [relationshipLimit, limit], bindings: [baseLimit, primaryLimit],
sql: expect.stringContaining( sql: expect.stringContaining(
multiline(`left join "products_tasks" as "c" on "a"."productid" = "c"."productid" multiline(
left join "tasks" as "b" on "b"."taskid" = "c"."taskid" `) `select json_agg(json_build_object('executorid',"b"."executorid",'taskname',"b"."taskname",'taskid',"b"."taskid",'completed',"b"."completed",'qaid',"b"."qaid"))
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 $1`
)
), ),
}) })
}) })
@ -95,22 +97,21 @@ describe("Captures of real examples", () => {
it("should handle enrichment of rows", () => { it("should handle enrichment of rows", () => {
const queryJson = getJson("enrichRelationship.json") const queryJson = getJson("enrichRelationship.json")
const filters = queryJson.filters?.oneOf?.taskid as number[] const filters = queryJson.filters?.oneOf?.taskid as number[]
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: [...filters, limit, ...filters, limit], bindings: [baseLimit, ...filters, baseLimit],
sql: multiline( sql: multiline(
`select "a"."executorid" as "a.executorid", "a"."taskname" as "a.taskname", "a"."taskid" as "a.taskid", `select "a".*, (select json_agg(json_build_object('productname',"b"."productname",'productid',"b"."productid"))
"a"."completed" as "a.completed", "a"."qaid" as "a.qaid", "b"."productname" as "b.productname", "b"."productid" as "b.productid" from (select "b".* from "products" as "b" inner join "products_tasks" as "c" on "b"."productid" = "c"."productid"
from (select * from "tasks" as "a" where "a"."taskid" in ($1, $2) order by "a"."taskid" asc limit $3) as "a" where "c"."taskid" = "a"."taskid" order by "b"."productid" asc limit $1) as "b") as "products"
left join "products_tasks" as "c" on "a"."taskid" = "c"."taskid" left join "products" as "b" on "b"."productid" = "c"."productid" from "tasks" as "a" where "a"."taskid" in ($2, $3) order by "a"."taskid" asc limit $4`
where "a"."taskid" in ($4, $5) order by "a"."taskid" asc limit $6`
), ),
}) })
}) })
it("should manage query with many relationship filters", () => { it("should manage query with many relationship filters", () => {
const queryJson = getJson("manyRelationshipFilters.json") const queryJson = getJson("manyRelationshipFilters.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
const filters = queryJson.filters const filters = queryJson.filters
const notEqualsValue = Object.values(filters?.notEqual!)[0] const notEqualsValue = Object.values(filters?.notEqual!)[0]
const rangeValue: { high?: string | number; low?: string | number } = const rangeValue: { high?: string | number; low?: string | number } =
@ -119,17 +120,18 @@ describe("Captures of real examples", () => {
expect(query).toEqual({ expect(query).toEqual({
bindings: [ bindings: [
notEqualsValue, baseLimit,
relationshipLimit, baseLimit,
baseLimit,
rangeValue.low, rangeValue.low,
rangeValue.high, rangeValue.high,
equalValue, equalValue,
true, notEqualsValue,
limit, primaryLimit,
], ],
sql: expect.stringContaining( sql: expect.stringContaining(
multiline( multiline(
`where "c"."year" between $3 and $4 and COALESCE("b"."productname" = $5, FALSE)` `where exists (select 1 from "persons" as "c" where "c"."personid" = "a"."executorid" and "c"."year" between $4 and $5)`
) )
), ),
}) })
@ -139,17 +141,19 @@ describe("Captures of real examples", () => {
describe("update", () => { describe("update", () => {
it("should handle performing a simple update", () => { it("should handle performing a simple update", () => {
const queryJson = getJson("updateSimple.json") const queryJson = getJson("updateSimple.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: [1990, "C", "A Street", 34, "designer", "London", "B", 5], bindings: [1990, "C", "A Street", 34, "designer", "London", "B", 5],
sql: multiline(`update "persons" as "a" set "year" = $1, "firstname" = $2, "address" = $3, "age" = $4, sql: multiline(
"type" = $5, "city" = $6, "lastname" = $7 where COALESCE("a"."personid" = $8, FALSE) returning *`), `update "persons" as "a" set "year" = $1, "firstname" = $2, "address" = $3, "age" = $4,
"type" = $5, "city" = $6, "lastname" = $7 where COALESCE("a"."personid" = $8, FALSE) returning *`
),
}) })
}) })
it("should handle performing an update of relationships", () => { it("should handle performing an update of relationships", () => {
const queryJson = getJson("updateRelationship.json") const queryJson = getJson("updateRelationship.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: [1990, "C", "A Street", 34, "designer", "London", "B", 5], bindings: [1990, "C", "A Street", 34, "designer", "London", "B", 5],
sql: multiline(`update "persons" as "a" set "year" = $1, "firstname" = $2, "address" = $3, "age" = $4, sql: multiline(`update "persons" as "a" set "year" = $1, "firstname" = $2, "address" = $3, "age" = $4,
@ -161,12 +165,12 @@ describe("Captures of real examples", () => {
describe("delete", () => { describe("delete", () => {
it("should handle deleting with relationships", () => { it("should handle deleting with relationships", () => {
const queryJson = getJson("deleteSimple.json") const queryJson = getJson("deleteSimple.json")
let query = new Sql(SqlClient.POSTGRES, limit)._query(queryJson) let query = new Sql(SqlClient.POSTGRES, baseLimit)._query(queryJson)
expect(query).toEqual({ expect(query).toEqual({
bindings: ["ddd", ""], bindings: ["ddd", ""],
sql: multiline(`delete from "compositetable" as "a" sql: multiline(`delete from "compositetable" as "a"
where COALESCE("a"."keypartone" = $1, FALSE) and COALESCE("a"."keyparttwo" = $2, FALSE) where COALESCE("a"."keypartone" = $1, FALSE) and COALESCE("a"."keyparttwo" = $2, FALSE)
returning "a"."keyparttwo" as "a.keyparttwo", "a"."keypartone" as "a.keypartone", "a"."name" as "a.name"`), returning "a".*`),
}) })
}) })
}) })
@ -174,7 +178,7 @@ describe("Captures of real examples", () => {
describe("returning (everything bar Postgres)", () => { describe("returning (everything bar Postgres)", () => {
it("should be able to handle row returning", () => { it("should be able to handle row returning", () => {
const queryJson = getJson("createSimple.json") const queryJson = getJson("createSimple.json")
const SQL = new Sql(SqlClient.MS_SQL, limit) const SQL = new Sql(SqlClient.MS_SQL, baseLimit)
let query = SQL._query(queryJson, { disableReturning: true }) let query = SQL._query(queryJson, { disableReturning: true })
expect(query).toEqual({ expect(query).toEqual({
sql: "insert into [people] ([age], [name]) values (@p0, @p1)", sql: "insert into [people] ([age], [name]) values (@p0, @p1)",
@ -187,10 +191,11 @@ describe("Captures of real examples", () => {
returningQuery = input returningQuery = input
}, queryJson) }, queryJson)
expect(returningQuery).toEqual({ expect(returningQuery).toEqual({
sql: multiline(`select top (@p0) * from (select top (@p1) * from [people] where CASE WHEN [people].[name] = @p2 sql: multiline(
THEN 1 ELSE 0 END = 1 and CASE WHEN [people].[age] = @p3 THEN 1 ELSE 0 END = 1 order by [people].[name] asc) as [people] `select top (@p0) * from [people] where CASE WHEN [people].[name] = @p1 THEN 1 ELSE 0 END = 1
where CASE WHEN [people].[name] = @p4 THEN 1 ELSE 0 END = 1 and CASE WHEN [people].[age] = @p5 THEN 1 ELSE 0 END = 1`), and CASE WHEN [people].[age] = @p2 THEN 1 ELSE 0 END = 1 order by [people].[name] asc`
bindings: [5000, 1, "Test", 22, "Test", 22], ),
bindings: [1, "Test", 22],
}) })
}) })
}) })