Skip to content

Commit

Permalink
refactor: Update Database class to initialize dataSourceOptions and d…
Browse files Browse the repository at this point in the history
…ataSource as null
  • Loading branch information
simlarsen committed Aug 13, 2024
1 parent 14cf7f0 commit f36ea78
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
8 changes: 6 additions & 2 deletions Common/Server/Infrastructure/PostgresDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export type DatabaseSourceOptions = DataSourceOptions;
export type DatabaseSource = DataSource;

export default class Database {
protected dataSourceOptions!: DataSourceOptions;
protected dataSource!: DataSource | null;
protected dataSourceOptions: DataSourceOptions | null = null;
protected dataSource: DataSource | null = null;

public getDatasourceOptions(): DataSourceOptions {
this.dataSourceOptions = DatabaseDataSourceOptions;
Expand Down Expand Up @@ -96,6 +96,8 @@ export default class Database {
await dropDatabase({
options: this.getDatasourceOptions(),
});
this.dataSource = null;
this.dataSourceOptions = null;
}

public async createDatabase(): Promise<void> {
Expand All @@ -118,3 +120,5 @@ export default class Database {
}

export const PostgresAppInstance: Database = new Database();


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ProjectService from "../../../Server/Services/ProjectService";
import ScheduledMaintenanceStateService from "../../../Server/Services/ScheduledMaintenanceStateService";
import UserServiceHelper from "../TestingUtils/Services/UserServiceHelper";
import UserService from "../../../Server/Services/UserService";
import User from "../../../Models/DatabaseModels/User";

describe("ScheduledMaintenanceService", () => {
let testDatabase: Database;
Expand All @@ -31,7 +32,7 @@ describe("ScheduledMaintenanceService", () => {
it("should trigger workflows only once", async () => {
// Prepare scheduled maintenance

let user = UserServiceHelper.generateRandomUser();
let user: User = UserServiceHelper.generateRandomUser();

user = await UserService.create({
data: user,
Expand Down
2 changes: 2 additions & 0 deletions Common/Tests/Server/TestingUtils/TestDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export default class TestDatabase extends PostgresDatabase {
return this.dataSourceOptions;
}
}

export const TestPostgresAppInstance: TestDatabase = new TestDatabase();
22 changes: 2 additions & 20 deletions Common/Tests/Server/TestingUtils/__mocks__/TestDatabase.mock.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import TestDatabase from "../TestDatabase";
import TestDatabase, { TestPostgresAppInstance } from "../TestDatabase";

export class TestDatabaseMock {
public static async getDbMock(): Promise<TestDatabase> {
const testDatabase: TestDatabase = new TestDatabase();

jest.mock("../../../../Server/Infrastructure/PostgresDatabase", () => {
const actualModule: any = jest.requireActual(
"../../../Server/Infrastructure/PostgresDatabase",
);
return {
__esModule: true,
default: actualModule.default,
PostgresAppInstance: {
getDataSource: () => {
return testDatabase.getDataSource();
},
isConnected: () => {
return testDatabase.isConnected();
},
},
};
});
const testDatabase: TestDatabase = TestPostgresAppInstance;

await testDatabase.createAndConnect();

Expand Down

0 comments on commit f36ea78

Please sign in to comment.