Skip to content

Commit

Permalink
refactor: removing globals usages
Browse files Browse the repository at this point in the history
  • Loading branch information
pleerock committed Apr 2, 2022
1 parent 2205a1a commit ec27803
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 184 deletions.
38 changes: 17 additions & 21 deletions test/functional/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,40 @@ import {
import { DataSource } from "../../../src/data-source/DataSource"
import { Repository } from "../../../src/repository/Repository"
import { TreeRepository } from "../../../src/repository/TreeRepository"
import { getConnectionManager } from "../../../src/index"
import { NoConnectionForRepositoryError } from "../../../src/error/NoConnectionForRepositoryError"
import { EntityManager } from "../../../src/entity-manager/EntityManager"
import { CannotGetEntityManagerNotConnectedError } from "../../../src/error/CannotGetEntityManagerNotConnectedError"
import { DataSourceOptions } from "../../../src/data-source/DataSourceOptions"
import { PostgresConnectionOptions } from "../../../src/driver/postgres/PostgresConnectionOptions"

describe("Connection", () => {
// const resourceDir = __dirname + "/../../../../../test/functional/connection/";

describe("before connection is established", function () {
let connection: DataSource
let dataSource: DataSource
before(async () => {
const options = setupSingleTestingConnection("mysql", {
name: "default",
entities: [],
})
if (!options) return

connection = getConnectionManager().create(options)
dataSource = new DataSource(options)
})
after(() => {
if (connection && connection.isInitialized)
return connection.close()
if (dataSource && dataSource.isInitialized)
return dataSource.destroy()

return Promise.resolve()
})

it("connection.isConnected should be false", () => {
if (!connection) return
if (!dataSource) return

connection.isInitialized.should.be.false
dataSource.isInitialized.should.be.false
})

it.skip("entity manager and reactive entity manager should not be accessible", () => {
expect(() => connection.manager).to.throw(
expect(() => dataSource.manager).to.throw(
CannotGetEntityManagerNotConnectedError,
)
// expect(() => connection.reactiveEntityManager).to.throw(CannotGetEntityManagerNotConnectedError);
Expand All @@ -72,46 +70,44 @@ describe("Connection", () => {
});*/

it("should not be able to close", () => {
if (!connection) return
return connection.close().should.be.rejected // CannotCloseNotConnectedError
if (!dataSource) return
return dataSource.close().should.be.rejected // CannotCloseNotConnectedError
})

it("should not be able to sync a schema", () => {
if (!connection) return
return connection.synchronize().should.be.rejected // CannotCloseNotConnectedError
if (!dataSource) return
return dataSource.synchronize().should.be.rejected // CannotCloseNotConnectedError
})

it.skip("should not be able to use repositories", () => {
if (!connection) return
if (!dataSource) return

expect(() => connection.getRepository(Post)).to.throw(
expect(() => dataSource.getRepository(Post)).to.throw(
NoConnectionForRepositoryError,
)
expect(() => connection.getTreeRepository(Category)).to.throw(
expect(() => dataSource.getTreeRepository(Category)).to.throw(
NoConnectionForRepositoryError,
)
// expect(() => connection.getReactiveRepository(Post)).to.throw(NoConnectionForRepositoryError);
// expect(() => connection.getReactiveTreeRepository(Category)).to.throw(NoConnectionForRepositoryError);
})

it("should be able to connect", () => {
if (!connection) return
return connection.connect().should.be.fulfilled
if (!dataSource) return
return dataSource.connect().should.be.fulfilled
})
})

describe.skip("establishing connection", function () {
it("should throw DriverOptionNotSetError when extra.socketPath and host is missing", function () {
expect(() => {
getConnectionManager().create(<DataSourceOptions>{
new DataSource({
type: "mysql",
username: "test",
password: "test",
database: "test",
entities: [],
dropSchema: false,
schemaCreate: false,
enabledDrivers: ["mysql"],
})
}).to.throw(Error)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from "chai"
import "reflect-metadata"
import { getConnectionManager } from "../../../../src"
import { DataSource } from "../../../../src/data-source/DataSource"
import { Repository } from "../../../../src/repository/Repository"
import { setupSingleTestingConnection } from "../../../utils/test-utils"
Expand All @@ -14,21 +13,21 @@ describe("persistence > custom-column-names", function () {
// -------------------------------------------------------------------------

// connect to db
let connection: DataSource
let dataSource: DataSource
before(async () => {
const options = setupSingleTestingConnection("mysql", {
entities: [Post, Category, CategoryMetadata],
})
if (!options) return

connection = getConnectionManager().create(options)
dataSource = new DataSource(options)
})
after(() => connection.close())
after(() => dataSource.close())

// clean up database before each test
function reloadDatabase() {
if (!connection) return
return connection.synchronize(true).catch((e) => {
if (!dataSource) return
return dataSource.synchronize(true).catch((e) => {
throw e
})
}
Expand All @@ -37,18 +36,18 @@ describe("persistence > custom-column-names", function () {
let categoryRepository: Repository<Category>
let metadataRepository: Repository<CategoryMetadata>
before(function () {
if (!connection) return
postRepository = connection.getRepository(Post)
categoryRepository = connection.getRepository(Category)
metadataRepository = connection.getRepository(CategoryMetadata)
if (!dataSource) return
postRepository = dataSource.getRepository(Post)
categoryRepository = dataSource.getRepository(Category)
metadataRepository = dataSource.getRepository(CategoryMetadata)
})

// -------------------------------------------------------------------------
// Specifications
// -------------------------------------------------------------------------

describe("attach exist entity to exist entity with many-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newCategory: Category, loadedPost: Post

before(reloadDatabase)
Expand Down Expand Up @@ -96,7 +95,7 @@ describe("persistence > custom-column-names", function () {
})

describe("attach new entity to exist entity with many-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newCategory: Category, loadedPost: Post

before(reloadDatabase)
Expand Down Expand Up @@ -139,7 +138,7 @@ describe("persistence > custom-column-names", function () {
})

describe("attach new entity to new entity with many-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newCategory: Category, loadedPost: Post

before(reloadDatabase)
Expand Down Expand Up @@ -177,7 +176,7 @@ describe("persistence > custom-column-names", function () {
})

describe("attach exist entity to exist entity with one-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post,
newCategory: Category,
newMetadata: CategoryMetadata,
Expand Down Expand Up @@ -241,7 +240,7 @@ describe("persistence > custom-column-names", function () {
})

describe("attach new entity to exist entity with one-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post,
newCategory: Category,
newMetadata: CategoryMetadata,
Expand Down
9 changes: 5 additions & 4 deletions test/github-issues/2096/issue-2096.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "reflect-metadata"
import { expect } from "chai"
import { createConnection } from "../../../src"
import { DataSource } from "../../../src"
import { getTypeOrmConfig } from "../../utils/test-utils"
import { MysqlConnectionOptions } from "../../../src/driver/mysql/MysqlConnectionOptions"

Expand All @@ -19,15 +19,16 @@ describe("github issues > #2096 [mysql] Database name isn't read from url", () =

const url = `mysql://${username}:${password}@${host}:${port}/${database}`

const connection = await createConnection({
const dataSource = new DataSource({
name: "#2096",
url,
entities: [__dirname + "/entity/*{.js,.ts}"],
synchronize: true,
type: "mysql",
})
expect(connection.isInitialized).to.eq(true)
await connection.close()
await dataSource.initialize()
expect(dataSource.isInitialized).to.eq(true)
await dataSource.destroy()
}
})
})
24 changes: 11 additions & 13 deletions test/github-issues/2871/issue-2871.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import "reflect-metadata"
import { expect } from "chai"

import {
closeTestingConnections,
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { createConnection, Repository } from "../../../src"

import { DataSource, Repository } from "../../../src"
import { Bar } from "./entity/Bar"
import { DocumentEnum } from "./documentEnum"

describe("github issues > #2871 Empty enum array is returned as array with single empty string", () => {
let connection: DataSource
let dataSource: DataSource
let repository: Repository<Bar>

before(async () => {
Expand All @@ -25,17 +22,18 @@ describe("github issues > #2871 Empty enum array is returned as array with singl

if (!options) return

connection = await createConnection(options)
dataSource = new DataSource(options)
await dataSource.initialize()
})
beforeEach(async () => {
if (!connection) return
await reloadTestingDatabases([connection])
repository = connection.getRepository(Bar)
if (!dataSource) return
await reloadTestingDatabases([dataSource])
repository = dataSource.getRepository(Bar)
})
after(() => closeTestingConnections([connection]))
after(() => closeTestingConnections([dataSource]))

it("should extract array with values from enum array values from 'postgres'", async () => {
if (!connection) return
if (!dataSource) return
const documents: DocumentEnum[] = [
DocumentEnum.DOCUMENT_A,
DocumentEnum.DOCUMENT_B,
Expand All @@ -51,7 +49,7 @@ describe("github issues > #2871 Empty enum array is returned as array with singl
})

it("should extract array with one value from enum array with one value from 'postgres'", async () => {
if (!connection) return
if (!dataSource) return
const documents: DocumentEnum[] = [DocumentEnum.DOCUMENT_D]

const barSaved = await repository.save({ documents })
Expand All @@ -64,7 +62,7 @@ describe("github issues > #2871 Empty enum array is returned as array with singl

// This `it` test that issue #2871 is fixed
it("should extract empty array from empty enum array from 'postgres'", async () => {
if (!connection) return
if (!dataSource) return
const documents: DocumentEnum[] = []

const barSaved = await repository.save({ documents })
Expand Down
5 changes: 3 additions & 2 deletions test/github-issues/2927/issue-2927.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/index"
import { DataSource } from "../../../src"
import { expect } from "chai"
import { Content } from "./entity/Content"
import { Photo } from "./entity/Photo"
Expand All @@ -19,6 +19,7 @@ describe("github issues > #2927 When using base class' custom repository, the di
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["postgres"],
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
Expand Down
14 changes: 6 additions & 8 deletions test/github-issues/4753/issue-4753.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getConnectionManager } from "../../../src"
import { DataSource } from "../../../src/data-source/DataSource"
import { MysqlConnectionOptions } from "../../../src/driver/mysql/MysqlConnectionOptions"
import {
Expand All @@ -13,8 +12,8 @@ function isMySql(v: TestingConnectionOptions): v is MysqlConnectionOptions {
}

describe("github issues > #4753 MySQL Replication Config broken", () => {
let connections: DataSource[] = []
after(() => closeTestingConnections(connections))
let dataSources: DataSource[] = []
after(() => closeTestingConnections(dataSources))

it("should connect without error when using replication", async () => {
const connectionOptions: MysqlConnectionOptions | undefined =
Expand All @@ -26,8 +25,7 @@ describe("github issues > #4753 MySQL Replication Config broken", () => {
// Skip if MySQL tests aren't enabled at all
return
}
const connectionManager = getConnectionManager()
const connection = connectionManager.create({
const dataSource = new DataSource({
type: "mysql",
replication: {
master: {
Expand All @@ -48,8 +46,8 @@ describe("github issues > #4753 MySQL Replication Config broken", () => {
entities: [User],
})

connections.push(connection)
await connection.connect()
connection.isInitialized.should.be.true
dataSources.push(dataSource)
await dataSource.connect()
dataSource.isInitialized.should.be.true
})
})
9 changes: 5 additions & 4 deletions test/github-issues/5119/issue-5119.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import { DataSource, createConnection } from "../../../src"
import { DataSource } from "../../../src"
import { fail } from "assert"

describe("github issues > #5119 migration with foreign key that changes target", () => {
Expand Down Expand Up @@ -36,9 +36,10 @@ describe("github issues > #5119 migration with foreign key that changes target",
fail()
return
}
const connection = await createConnection(options)
const dataSource = new DataSource(options)
await dataSource.initialize()
try {
const sqlInMemory = await connection.driver
const sqlInMemory = await dataSource.driver
.createSchemaBuilder()
.log()

Expand All @@ -61,7 +62,7 @@ describe("github issues > #5119 migration with foreign key that changes target",
`ALTER TABLE "post" DROP CONSTRAINT "FK_4490d00e1925ca046a1f52ddf04"`,
])
} finally {
connection.close()
dataSource.close()
}
}),
)
Expand Down

0 comments on commit ec27803

Please sign in to comment.