diff --git a/Common/Server/Infrastructure/Postgres/TestDataSourceOptions.ts b/Common/Server/Infrastructure/Postgres/TestDataSourceOptions.ts index 974070bd2c..15dc1a3e71 100644 --- a/Common/Server/Infrastructure/Postgres/TestDataSourceOptions.ts +++ b/Common/Server/Infrastructure/Postgres/TestDataSourceOptions.ts @@ -10,8 +10,8 @@ const getTestDataSourceOptions: GetTestDataSourceOptions = // we use process.env values directly here because it can change during test runs and we need to get the latest values. return { ...ProdDataSourceOptions, - host: process.env["DATABASE_HOST"] || "localhost", - port: parseInt(process.env["DATABASE_PORT"]?.toString() || "5432"), + host: "localhost", + port: 5400, username: process.env["DATABASE_USERNAME"] || "postgres", password: process.env["DATABASE_PASSWORD"] || "password", database: DatabaseName + Faker.getRandomNumbers(16).toString(), diff --git a/Common/Server/Infrastructure/PostgresDatabase.ts b/Common/Server/Infrastructure/PostgresDatabase.ts index 86b6fcfc2b..aba961bb3c 100644 --- a/Common/Server/Infrastructure/PostgresDatabase.ts +++ b/Common/Server/Infrastructure/PostgresDatabase.ts @@ -1,5 +1,5 @@ import logger from "../Utils/Logger"; -import dataSourceOptions from "./Postgres/DataSourceOptions"; +import DatabaseDataSourceOptions from "./Postgres/DataSourceOptions"; import Sleep from "Common/Types/Sleep"; import { DataSource, DataSourceOptions } from "typeorm"; import { createDatabase, dropDatabase } from "typeorm-extension"; @@ -8,10 +8,12 @@ export type DatabaseSourceOptions = DataSourceOptions; export type DatabaseSource = DataSource; export default class Database { + protected dataSourceOptions!: DataSourceOptions; protected dataSource!: DataSource | null; public getDatasourceOptions(): DataSourceOptions { - return dataSourceOptions; + this.dataSourceOptions = DatabaseDataSourceOptions; + return this.dataSourceOptions; } public getDataSource(): DataSource | null { @@ -22,9 +24,7 @@ export default class Database { return Boolean(this.dataSource); } - public async connect( - dataSourceOptions: DataSourceOptions, - ): Promise { + public async connect(): Promise { let retry: number = 0; try { @@ -34,7 +34,7 @@ export default class Database { async (): Promise => { try { const PostgresDataSource: DataSource = new DataSource( - dataSourceOptions, + this.getDatasourceOptions(), ); const dataSource: DataSource = await PostgresDataSource.initialize(); @@ -94,16 +94,27 @@ export default class Database { public async dropDatabase(): Promise { await dropDatabase({ - options: dataSourceOptions, + options: this.getDatasourceOptions(), }); } public async createDatabase(): Promise { await createDatabase({ - options: dataSourceOptions, + options: this.getDatasourceOptions(), ifNotExist: true, }); } + + public async createAndConnect(): Promise { + await this.createDatabase(); + await this.connect(); + } + + public async disconnectAndDropDatabase(): Promise { + // Drop the database. Since this is the in-mem db, it will be destroyed. + await this.disconnect(); + await this.dropDatabase(); + } } export const PostgresAppInstance: Database = new Database(); diff --git a/Common/Tests/Server/TestingUtils/Database.ts b/Common/Tests/Server/TestingUtils/Database.ts index 0e5cd74d0d..de702953c0 100644 --- a/Common/Tests/Server/TestingUtils/Database.ts +++ b/Common/Tests/Server/TestingUtils/Database.ts @@ -1,33 +1,16 @@ import PostgresDatabase, { - DatabaseSource, DatabaseSourceOptions, } from "../../../Server/Infrastructure/PostgresDatabase"; -import { IMemoryDb, newDb } from "pg-mem"; -import logger from "../../../Server/Utils/Logger"; import getTestDataSourceOptions from "../../../Server/Infrastructure/Postgres/TestDataSourceOptions"; export default class TestDatabase extends PostgresDatabase { - public async createAndConnect(): Promise { - const testDatasourceOptions: DatabaseSourceOptions = getTestDataSourceOptions(); - await this.connect(testDatasourceOptions); - } + public override getDatasourceOptions(): DatabaseSourceOptions { + if (this.dataSourceOptions) { + return this.dataSourceOptions; + } - public override async connect( - dataSourceOptions: DatabaseSourceOptions, - ): Promise { - const db: IMemoryDb = newDb(); - const dataSource: DatabaseSource = - db.adapters.createTypeormDataSource(dataSourceOptions); - await dataSource.initialize(); - await dataSource.synchronize(); - logger.debug("Postgres Database Connected"); - this.dataSource = dataSource; - return dataSource; - } + this.dataSourceOptions = getTestDataSourceOptions(); - public async disconnectAndDropDatabase(): Promise { - // Drop the database. Since this is the in-mem db, it will be destroyed. + return this.dataSourceOptions; } } - -export const PostgresAppInstance: TestDatabase = new TestDatabase(); diff --git a/Common/Utils/Faker.ts b/Common/Utils/Faker.ts index 335b6c94ef..63beca6fb9 100644 --- a/Common/Utils/Faker.ts +++ b/Common/Utils/Faker.ts @@ -32,7 +32,8 @@ export default class Faker { public static getRandomNumbers(count: number): number { const randomNumbers: Array = []; for (let i: number = 0; i < count; i++) { - randomNumbers.push(Math.floor(Math.random() * 10)); // You can adjust the range as needed + // pick a random number between 1 and 9 + randomNumbers.push(Math.floor(Math.random() * 9) + 1); } return parseInt(randomNumbers.join("")); } diff --git a/Common/package.json b/Common/package.json index 203049cbf5..1ba920e955 100644 --- a/Common/package.json +++ b/Common/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "test": "jest --detectOpenHandles", + "test": "cd .. && export $(grep -v '^#' config.env | xargs) && cd Common && node --inspect node_modules/.bin/jest --runInBand ./Tests --detectOpenHandles", "coverage": "jest --detectOpenHandles --coverage", "compile": "tsc", "clear-modules": "rm -rf node_modules && rm package-lock.json && npm install",