1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-23 01:27:14 +02:00
pigallery2/test/backend/DBTestHelper.ts

192 lines
6.7 KiB
TypeScript
Raw Normal View History

2019-12-10 11:44:35 +02:00
import {Config} from '../../src/common/config/private/Config';
import * as path from 'path';
2021-01-04 11:32:19 +02:00
import * as fs from 'fs';
import {SQLConnection} from '../../src/backend/model/database/sql/SQLConnection';
2021-06-03 21:19:21 +02:00
import {DatabaseType} from '../../src/common/config/private/PrivateConfig';
2020-12-28 23:08:57 +02:00
import {ProjectPath} from '../../src/backend/ProjectPath';
2021-06-27 19:33:37 +02:00
import {DirectoryBaseDTO, ParentDirectoryDTO} from '../../src/common/entities/DirectoryDTO';
import {DirectoryEntity} from '../../src/backend/model/database/sql/enitites/DirectoryEntity';
import {ObjectManagers} from '../../src/backend/model/ObjectManagers';
import {DiskMangerWorker} from '../../src/backend/model/threading/DiskMangerWorker';
import {IndexingManager} from '../../src/backend/model/database/sql/IndexingManager';
import {GalleryManager} from '../../src/backend/model/database/sql/GalleryManager';
import {Connection} from 'typeorm';
2021-04-01 21:48:38 +02:00
import {Utils} from '../../src/common/Utils';
declare let describe: any;
const savedDescribe = describe;
class IndexingManagerTest extends IndexingManager {
2021-06-27 19:33:37 +02:00
public async saveToDB(scannedDirectory: ParentDirectoryDTO): Promise<void> {
return super.saveToDB(scannedDirectory);
}
}
class GalleryManagerTest extends GalleryManager {
public async selectParentDir(connection: Connection, directoryName: string, directoryParent: string): Promise<DirectoryEntity> {
return super.selectParentDir(connection, directoryName, directoryParent);
}
public async fillParentDir(connection: Connection, dir: DirectoryEntity): Promise<void> {
return super.fillParentDir(connection, dir);
}
}
2021-04-01 21:48:38 +02:00
export class DBTestHelper {
static enable = {
2021-04-01 21:48:38 +02:00
memory: false,
2021-05-28 23:28:12 +02:00
sqlite: process.env.TEST_SQLITE !== 'false',
mysql: process.env.TEST_MYSQL !== 'false'
};
public static readonly savedDescribe = savedDescribe;
tempDir: string;
constructor(public dbType: DatabaseType) {
2019-02-23 00:39:01 +02:00
this.tempDir = path.join(__dirname, './tmp');
}
2021-04-01 21:48:38 +02:00
static describe(settingsOverride: {
memory?: boolean;
sqlite?: boolean;
mysql?: boolean;
} = {}): (name: string, tests: (helper?: DBTestHelper) => void) => void {
2021-04-01 21:48:38 +02:00
const settings = Utils.clone(DBTestHelper.enable);
for (const key of Object.keys(settingsOverride)) {
(settings as any)[key] = (settingsOverride as any)[key];
2021-04-01 21:48:38 +02:00
}
return (name: string, tests: (helper?: DBTestHelper) => void) => {
savedDescribe(name, async () => {
if (settings.sqlite) {
const helper = new DBTestHelper(DatabaseType.sqlite);
2021-04-01 21:48:38 +02:00
savedDescribe('sqlite', () => {
return tests(helper);
});
}
if (settings.mysql) {
const helper = new DBTestHelper(DatabaseType.mysql);
savedDescribe('mysql', function(): void {
2021-04-01 21:48:38 +02:00
this.timeout(99999999); // hint for the test environment
// @ts-ignore
return tests(helper);
});
}
if (settings.memory) {
const helper = new DBTestHelper(DatabaseType.memory);
2021-04-01 21:48:38 +02:00
savedDescribe('memory', () => {
return tests(helper);
});
}
});
};
}
2021-06-27 19:33:37 +02:00
public static async persistTestDir(directory: DirectoryBaseDTO): Promise<DirectoryEntity> {
await ObjectManagers.InitSQLManagers();
const connection = await SQLConnection.getConnection();
ObjectManagers.getInstance().IndexingManager.indexDirectory = () => Promise.resolve(null);
const im = new IndexingManagerTest();
2021-06-27 19:33:37 +02:00
await im.saveToDB(directory as ParentDirectoryDTO);
// not saving subdirs. saveToDB destroys data
// await im.saveToDB(subDir);
// await im.saveToDB(subDir2);
if (ObjectManagers.getInstance().IndexingManager &&
ObjectManagers.getInstance().IndexingManager.IsSavingInProgress) {
await ObjectManagers.getInstance().IndexingManager.SavingReady;
}
const gm = new GalleryManagerTest();
const dir = await gm.selectParentDir(connection, directory.name, path.join(path.dirname('.'), path.sep));
await gm.fillParentDir(connection, dir);
2021-06-27 19:33:37 +02:00
const populateDir = async (d: DirectoryBaseDTO) => {
for (let i = 0; i < d.directories.length; i++) {
d.directories[i] = await gm.selectParentDir(connection, d.directories[i].name,
path.join(DiskMangerWorker.pathFromParent(d), path.sep));
await gm.fillParentDir(connection, d.directories[i] as any);
await populateDir(d.directories[i]);
}
};
await populateDir(dir);
await ObjectManagers.reset();
return dir;
}
public async initDB(): Promise<void> {
2021-06-03 21:19:21 +02:00
await Config.load();
if (this.dbType === DatabaseType.sqlite) {
await this.initSQLite();
} else if (this.dbType === DatabaseType.mysql) {
await this.initMySQL();
2021-05-11 15:20:36 +02:00
} else if (this.dbType === DatabaseType.memory) {
Config.Server.Database.type = DatabaseType.memory;
}
}
public async clearDB(): Promise<void> {
if (this.dbType === DatabaseType.sqlite) {
await this.clearUpSQLite();
} else if (this.dbType === DatabaseType.mysql) {
await this.clearUpMysql();
} else if (this.dbType === DatabaseType.memory) {
2021-04-01 21:48:38 +02:00
await this.clearUpMemory();
}
}
private async initMySQL(): Promise<void> {
await this.resetMySQL();
}
private async resetMySQL(): Promise<void> {
2021-05-28 23:28:12 +02:00
await ObjectManagers.reset();
Config.Server.Database.type = DatabaseType.mysql;
Config.Server.Database.mysql.database = 'pigallery2_test';
const conn = await SQLConnection.getConnection();
await conn.query('DROP DATABASE IF EXISTS ' + conn.options.database);
await conn.query('CREATE DATABASE IF NOT EXISTS ' + conn.options.database);
await SQLConnection.close();
await ObjectManagers.InitSQLManagers();
}
private async clearUpMysql(): Promise<void> {
await ObjectManagers.reset();
Config.Server.Database.type = DatabaseType.mysql;
Config.Server.Database.mysql.database = 'pigallery2_test';
const conn = await SQLConnection.getConnection();
await conn.query('DROP DATABASE IF EXISTS ' + conn.options.database);
await SQLConnection.close();
}
private async initSQLite(): Promise<void> {
await this.resetSQLite();
}
private async resetSQLite(): Promise<void> {
Config.Server.Database.type = DatabaseType.sqlite;
Config.Server.Database.dbFolder = this.tempDir;
ProjectPath.reset();
await ObjectManagers.reset();
await fs.promises.rmdir(this.tempDir, {recursive: true});
await ObjectManagers.InitSQLManagers();
}
private async clearUpSQLite(): Promise<void> {
Config.Server.Database.type = DatabaseType.sqlite;
Config.Server.Database.dbFolder = this.tempDir;
ProjectPath.reset();
await ObjectManagers.reset();
await fs.promises.rmdir(this.tempDir, {recursive: true});
}
2021-04-01 21:48:38 +02:00
private async clearUpMemory(): Promise<void> {
2021-04-01 21:48:38 +02:00
return this.resetSQLite();
}
}