should generate a string of random numbers of specified length

This commit is contained in:
Simon Larsen 2024-08-12 22:57:36 +01:00
parent 01be73612d
commit c53584e9d5
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
5 changed files with 30 additions and 35 deletions

View File

@ -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(),

View File

@ -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<DataSource> {
public async connect(): Promise<DataSource> {
let retry: number = 0;
try {
@ -34,7 +34,7 @@ export default class Database {
async (): Promise<DataSource> => {
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<void> {
await dropDatabase({
options: dataSourceOptions,
options: this.getDatasourceOptions(),
});
}
public async createDatabase(): Promise<void> {
await createDatabase({
options: dataSourceOptions,
options: this.getDatasourceOptions(),
ifNotExist: true,
});
}
public async createAndConnect(): Promise<void> {
await this.createDatabase();
await this.connect();
}
public async disconnectAndDropDatabase(): Promise<void> {
// 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();

View File

@ -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<void> {
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<DatabaseSource> {
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<void> {
// Drop the database. Since this is the in-mem db, it will be destroyed.
return this.dataSourceOptions;
}
}
export const PostgresAppInstance: TestDatabase = new TestDatabase();

View File

@ -32,7 +32,8 @@ export default class Faker {
public static getRandomNumbers(count: number): number {
const randomNumbers: Array<number> = [];
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(""));
}

View File

@ -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",