mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-21 22:59:07 +00:00
should generate a string of random numbers of specified length
This commit is contained in:
parent
01be73612d
commit
c53584e9d5
@ -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.
|
// we use process.env values directly here because it can change during test runs and we need to get the latest values.
|
||||||
return {
|
return {
|
||||||
...ProdDataSourceOptions,
|
...ProdDataSourceOptions,
|
||||||
host: process.env["DATABASE_HOST"] || "localhost",
|
host: "localhost",
|
||||||
port: parseInt(process.env["DATABASE_PORT"]?.toString() || "5432"),
|
port: 5400,
|
||||||
username: process.env["DATABASE_USERNAME"] || "postgres",
|
username: process.env["DATABASE_USERNAME"] || "postgres",
|
||||||
password: process.env["DATABASE_PASSWORD"] || "password",
|
password: process.env["DATABASE_PASSWORD"] || "password",
|
||||||
database: DatabaseName + Faker.getRandomNumbers(16).toString(),
|
database: DatabaseName + Faker.getRandomNumbers(16).toString(),
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import logger from "../Utils/Logger";
|
import logger from "../Utils/Logger";
|
||||||
import dataSourceOptions from "./Postgres/DataSourceOptions";
|
import DatabaseDataSourceOptions from "./Postgres/DataSourceOptions";
|
||||||
import Sleep from "Common/Types/Sleep";
|
import Sleep from "Common/Types/Sleep";
|
||||||
import { DataSource, DataSourceOptions } from "typeorm";
|
import { DataSource, DataSourceOptions } from "typeorm";
|
||||||
import { createDatabase, dropDatabase } from "typeorm-extension";
|
import { createDatabase, dropDatabase } from "typeorm-extension";
|
||||||
@ -8,10 +8,12 @@ export type DatabaseSourceOptions = DataSourceOptions;
|
|||||||
export type DatabaseSource = DataSource;
|
export type DatabaseSource = DataSource;
|
||||||
|
|
||||||
export default class Database {
|
export default class Database {
|
||||||
|
protected dataSourceOptions!: DataSourceOptions;
|
||||||
protected dataSource!: DataSource | null;
|
protected dataSource!: DataSource | null;
|
||||||
|
|
||||||
public getDatasourceOptions(): DataSourceOptions {
|
public getDatasourceOptions(): DataSourceOptions {
|
||||||
return dataSourceOptions;
|
this.dataSourceOptions = DatabaseDataSourceOptions;
|
||||||
|
return this.dataSourceOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getDataSource(): DataSource | null {
|
public getDataSource(): DataSource | null {
|
||||||
@ -22,9 +24,7 @@ export default class Database {
|
|||||||
return Boolean(this.dataSource);
|
return Boolean(this.dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connect(
|
public async connect(): Promise<DataSource> {
|
||||||
dataSourceOptions: DataSourceOptions,
|
|
||||||
): Promise<DataSource> {
|
|
||||||
let retry: number = 0;
|
let retry: number = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -34,7 +34,7 @@ export default class Database {
|
|||||||
async (): Promise<DataSource> => {
|
async (): Promise<DataSource> => {
|
||||||
try {
|
try {
|
||||||
const PostgresDataSource: DataSource = new DataSource(
|
const PostgresDataSource: DataSource = new DataSource(
|
||||||
dataSourceOptions,
|
this.getDatasourceOptions(),
|
||||||
);
|
);
|
||||||
const dataSource: DataSource =
|
const dataSource: DataSource =
|
||||||
await PostgresDataSource.initialize();
|
await PostgresDataSource.initialize();
|
||||||
@ -94,16 +94,27 @@ export default class Database {
|
|||||||
|
|
||||||
public async dropDatabase(): Promise<void> {
|
public async dropDatabase(): Promise<void> {
|
||||||
await dropDatabase({
|
await dropDatabase({
|
||||||
options: dataSourceOptions,
|
options: this.getDatasourceOptions(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createDatabase(): Promise<void> {
|
public async createDatabase(): Promise<void> {
|
||||||
await createDatabase({
|
await createDatabase({
|
||||||
options: dataSourceOptions,
|
options: this.getDatasourceOptions(),
|
||||||
ifNotExist: true,
|
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();
|
export const PostgresAppInstance: Database = new Database();
|
||||||
|
@ -1,33 +1,16 @@
|
|||||||
import PostgresDatabase, {
|
import PostgresDatabase, {
|
||||||
DatabaseSource,
|
|
||||||
DatabaseSourceOptions,
|
DatabaseSourceOptions,
|
||||||
} from "../../../Server/Infrastructure/PostgresDatabase";
|
} from "../../../Server/Infrastructure/PostgresDatabase";
|
||||||
import { IMemoryDb, newDb } from "pg-mem";
|
|
||||||
import logger from "../../../Server/Utils/Logger";
|
|
||||||
import getTestDataSourceOptions from "../../../Server/Infrastructure/Postgres/TestDataSourceOptions";
|
import getTestDataSourceOptions from "../../../Server/Infrastructure/Postgres/TestDataSourceOptions";
|
||||||
|
|
||||||
export default class TestDatabase extends PostgresDatabase {
|
export default class TestDatabase extends PostgresDatabase {
|
||||||
public async createAndConnect(): Promise<void> {
|
public override getDatasourceOptions(): DatabaseSourceOptions {
|
||||||
const testDatasourceOptions: DatabaseSourceOptions = getTestDataSourceOptions();
|
if (this.dataSourceOptions) {
|
||||||
await this.connect(testDatasourceOptions);
|
return this.dataSourceOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async connect(
|
this.dataSourceOptions = getTestDataSourceOptions();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async disconnectAndDropDatabase(): Promise<void> {
|
return this.dataSourceOptions;
|
||||||
// Drop the database. Since this is the in-mem db, it will be destroyed.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PostgresAppInstance: TestDatabase = new TestDatabase();
|
|
||||||
|
@ -32,7 +32,8 @@ export default class Faker {
|
|||||||
public static getRandomNumbers(count: number): number {
|
public static getRandomNumbers(count: number): number {
|
||||||
const randomNumbers: Array<number> = [];
|
const randomNumbers: Array<number> = [];
|
||||||
for (let i: number = 0; i < count; i++) {
|
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(""));
|
return parseInt(randomNumbers.join(""));
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"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",
|
"coverage": "jest --detectOpenHandles --coverage",
|
||||||
"compile": "tsc",
|
"compile": "tsc",
|
||||||
"clear-modules": "rm -rf node_modules && rm package-lock.json && npm install",
|
"clear-modules": "rm -rf node_modules && rm package-lock.json && npm install",
|
||||||
|
Loading…
Reference in New Issue
Block a user