refactor: Update TestDatabase to use pg-mem for in-memory database in tests

This commit is contained in:
Simon Larsen 2024-08-09 09:53:22 -06:00
parent 99a80ab9b6
commit 01be73612d
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA

View File

@ -2,22 +2,24 @@ import PostgresDatabase, {
DatabaseSource,
DatabaseSourceOptions,
} from "../../../Server/Infrastructure/PostgresDatabase";
import { newDb } from "pg-mem";
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<void> {
const testDatasourceOptions = getTestDataSourceOptions();
const testDatasourceOptions: DatabaseSourceOptions = getTestDataSourceOptions();
await this.connect(testDatasourceOptions);
}
public override async connect(
dataSourceOptions: DatabaseSourceOptions,
): Promise<DatabaseSource> {
const db = newDb();
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;