|
| 1 | +import "reflect-metadata" |
| 2 | +import { |
| 3 | + createTestingConnections, |
| 4 | + closeTestingConnections, |
| 5 | + reloadTestingDatabases, |
| 6 | +} from "../../utils/test-utils" |
| 7 | +import { DataSource } from "../../../src/data-source/DataSource" |
| 8 | +import { Weather } from "./entity/weather" |
| 9 | +import { expect } from "chai" |
| 10 | + |
| 11 | +describe("github issues > #7308 queryBuilder makes different parameter identifiers for same parameter, causing problems with groupby", () => { |
| 12 | + describe("Postgres & cockroachdb", () => { |
| 13 | + let dataSources: DataSource[] |
| 14 | + before( |
| 15 | + async () => |
| 16 | + (dataSources = await createTestingConnections({ |
| 17 | + entities: [Weather], |
| 18 | + enabledDrivers: [ |
| 19 | + "postgres", |
| 20 | + "cockroachdb", |
| 21 | + "spanner", |
| 22 | + "mssql", |
| 23 | + "oracle", |
| 24 | + ], |
| 25 | + schemaCreate: true, |
| 26 | + dropSchema: true, |
| 27 | + })), |
| 28 | + ) |
| 29 | + |
| 30 | + beforeEach(() => reloadTestingDatabases(dataSources)) |
| 31 | + after(() => closeTestingConnections(dataSources)) |
| 32 | + |
| 33 | + it("should not create different parameters identifiers for the same parameter", () => |
| 34 | + Promise.all( |
| 35 | + dataSources.map(async (dataSource) => { |
| 36 | + const [query, parameters] = dataSource |
| 37 | + .getRepository(Weather) |
| 38 | + .createQueryBuilder() |
| 39 | + .select("round(temperature, :floatNumber)") |
| 40 | + .addSelect("count(*)", "count") |
| 41 | + .groupBy("round(temperature, :floatNumber)") |
| 42 | + .setParameters({ floatNumber: 2.4 }) |
| 43 | + .getQueryAndParameters() |
| 44 | + query.should.not.be.undefined |
| 45 | + |
| 46 | + if ( |
| 47 | + dataSource.driver.options.type === "postgres" || |
| 48 | + dataSource.driver.options.type === "cockroachdb" |
| 49 | + ) { |
| 50 | + expect(query).to.equal( |
| 51 | + 'SELECT round(temperature, $1), count(*) AS "count" FROM "weather" "Weather" GROUP BY round(temperature, $1)', |
| 52 | + ) |
| 53 | + } else if (dataSource.driver.options.type === "spanner") { |
| 54 | + expect(query).to.equal( |
| 55 | + 'SELECT round(temperature, @param0), count(*) AS "count" FROM "weather" "Weather" GROUP BY round(temperature, @param0)', |
| 56 | + ) |
| 57 | + } else if (dataSource.driver.options.type === "oracle") { |
| 58 | + expect(query).to.equal( |
| 59 | + 'SELECT round(temperature, :1), count(*) AS "count" FROM "weather" "Weather" GROUP BY round(temperature, :1)', |
| 60 | + ) |
| 61 | + } else if (dataSource.driver.options.type === "mssql") { |
| 62 | + expect(query).to.equal( |
| 63 | + 'SELECT round(temperature, @0), count(*) AS "count" FROM "weather" "Weather" GROUP BY round(temperature, @0)', |
| 64 | + ) |
| 65 | + } |
| 66 | + return parameters.length.should.eql(1) |
| 67 | + }), |
| 68 | + )) |
| 69 | + }) |
| 70 | +}) |
0 commit comments