1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/test/backend/DBTestHelper.ts

186 lines
6.3 KiB
TypeScript
Raw Normal View History

2019-12-10 17:44:35 +08:00
import {Config} from '../../src/common/config/private/Config';
import * as path from 'path';
2021-01-04 17:32:19 +08:00
import * as fs from 'fs';
import {SQLConnection} from '../../src/backend/model/database/sql/SQLConnection';
2020-01-29 01:36:52 +08:00
import {ServerConfig} from '../../src/common/config/private/PrivateConfig';
2020-12-29 05:08:57 +08:00
import {ProjectPath} from '../../src/backend/ProjectPath';
import {DirectoryDTO} 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-02 03:48:38 +08:00
import {Utils} from '../../src/common/Utils';
declare let describe: any;
const savedDescribe = describe;
class IndexingManagerTest extends IndexingManager {
public async saveToDB(scannedDirectory: DirectoryDTO): 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-02 03:48:38 +08:00
export class DBTestHelper {
static enable = {
2021-04-02 03:48:38 +08:00
memory: false,
sqlite: true,
mysql: process.env.TEST_MYSQL !== 'false'
};
public static readonly savedDescribe = savedDescribe;
tempDir: string;
constructor(public dbType: ServerConfig.DatabaseType) {
2019-02-23 06:39:01 +08:00
this.tempDir = path.join(__dirname, './tmp');
}
2021-04-02 03:48:38 +08:00
static describe(settingsOverride: {
memory?: boolean;
sqlite?: boolean;
mysql?: boolean;
} = {}) {
const settings = Utils.clone(DBTestHelper.enable);
for (const key of Object.keys(settingsOverride)) {
(<any>settings)[key] = (<any>settingsOverride)[key];
}
return (name: string, tests: (helper?: DBTestHelper) => void) => {
savedDescribe(name, async () => {
if (settings.sqlite) {
const helper = new DBTestHelper(ServerConfig.DatabaseType.sqlite);
savedDescribe('sqlite', () => {
return tests(helper);
});
}
if (settings.mysql) {
const helper = new DBTestHelper(ServerConfig.DatabaseType.mysql);
savedDescribe('mysql', function () {
this.timeout(99999999); // hint for the test environment
// @ts-ignore
return tests(helper);
});
}
if (settings.memory) {
const helper = new DBTestHelper(ServerConfig.DatabaseType.memory);
savedDescribe('memory', () => {
return tests(helper);
});
}
});
};
}
public static async persistTestDir(directory: DirectoryDTO): Promise<DirectoryEntity> {
await ObjectManagers.InitSQLManagers();
const connection = await SQLConnection.getConnection();
ObjectManagers.getInstance().IndexingManager.indexDirectory = () => Promise.resolve(null);
const im = new IndexingManagerTest();
await im.saveToDB(directory);
// 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);
const populateDir = async (d: DirectoryDTO) => {
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, <any>d.directories[i]);
await populateDir(d.directories[i]);
}
};
await populateDir(dir);
await ObjectManagers.reset();
return dir;
}
public async initDB() {
if (this.dbType === ServerConfig.DatabaseType.sqlite) {
await this.initSQLite();
2021-04-02 03:48:38 +08:00
} else if (this.dbType === ServerConfig.DatabaseType.mysql) {
await this.initMySQL();
}
}
public async clearDB() {
if (this.dbType === ServerConfig.DatabaseType.sqlite) {
await this.clearUpSQLite();
2021-04-02 03:48:38 +08:00
} else if (this.dbType === ServerConfig.DatabaseType.mysql) {
await this.clearUpMysql();
2021-04-02 03:48:38 +08:00
} else if (this.dbType === ServerConfig.DatabaseType.memory) {
await this.clearUpMemory();
}
}
private async initSQLite() {
await this.resetSQLite();
Config.Server.Database.type = ServerConfig.DatabaseType.sqlite;
2019-12-29 16:34:08 +08:00
Config.Server.Database.dbFolder = this.tempDir;
2020-12-29 05:08:57 +08:00
ProjectPath.reset();
}
private async initMySQL() {
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
Config.Server.Database.mysql.database = 'pigallery2_test';
await this.resetMySQL();
}
private async resetSQLite() {
2021-04-02 03:48:38 +08:00
await ObjectManagers.reset();
// await SQLConnection.close();
2021-01-04 17:32:19 +08:00
await fs.promises.rmdir(this.tempDir, {recursive: true});
}
private async resetMySQL() {
Config.Server.Database.type = ServerConfig.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();
}
private async clearUpMysql() {
Config.Server.Database.type = ServerConfig.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 clearUpSQLite() {
return this.resetSQLite();
}
2021-04-02 03:48:38 +08:00
private async clearUpMemory() {
return this.resetSQLite();
}
}