import "reflect-metadata"
import {
    createTestingConnections,
    closeTestingConnections,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { User } from "./entity/UserEntity"

import { expect } from "chai"

describe("github issues > #5365 Generated Identity for Postgres 10+", () => {
    let connections: DataSource[]
    before(
        async () =>
            (connections = await createTestingConnections({
                entities: [User],
                schemaCreate: false,
                dropSchema: true,
                enabledDrivers: ["postgres"],
            })),
    )
    after(() => closeTestingConnections(connections))
    it("should produce proper SQL for creating a table with identity column", () =>
        Promise.all(
            connections.map(async (connection) => {
                const sqlInMemory = await connection.driver
                    .createSchemaBuilder()
                    .log()
                expect(sqlInMemory)
                    .to.have.property("upQueries")
                    .that.is.an("array")
                    .and.has.length(1)
                expect(sqlInMemory.upQueries[0])
                    .to.have.property("query")
                    .that.eql(
                        `CREATE TABLE "user" ("id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL, CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`,
                    )
            }),
        ))
})
