2019-12-10 11:44:35 +02:00
|
|
|
import {Config} from '../../src/common/config/private/Config';
|
2019-01-27 21:36:42 +02:00
|
|
|
import * as path from 'path';
|
2021-01-04 11:32:19 +02:00
|
|
|
import * as fs from 'fs';
|
2019-12-14 18:27:01 +02:00
|
|
|
import {SQLConnection} from '../../src/backend/model/database/sql/SQLConnection';
|
2020-01-28 19:36:52 +02:00
|
|
|
import {ServerConfig} from '../../src/common/config/private/PrivateConfig';
|
2020-12-28 23:08:57 +02:00
|
|
|
import {ProjectPath} from '../../src/backend/ProjectPath';
|
2021-01-17 11:56:33 +02:00
|
|
|
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';
|
2019-01-27 21:36:42 +02:00
|
|
|
|
|
|
|
declare let describe: any;
|
|
|
|
const savedDescribe = describe;
|
|
|
|
|
2021-01-17 11:56:33 +02:00
|
|
|
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-01-16 17:59:59 +02:00
|
|
|
|
2019-01-27 21:36:42 +02:00
|
|
|
export class SQLTestHelper {
|
|
|
|
|
|
|
|
static enable = {
|
|
|
|
sqlite: true,
|
2021-01-16 17:59:59 +02:00
|
|
|
mysql: process.env.TEST_MYSQL !== 'false'
|
2019-01-27 21:36:42 +02:00
|
|
|
};
|
|
|
|
public static readonly savedDescribe = savedDescribe;
|
|
|
|
tempDir: string;
|
|
|
|
|
2019-12-10 10:36:14 +02:00
|
|
|
constructor(public dbType: ServerConfig.DatabaseType) {
|
2019-02-23 00:39:01 +02:00
|
|
|
this.tempDir = path.join(__dirname, './tmp');
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static describe(name: string, tests: (helper?: SQLTestHelper) => void) {
|
|
|
|
savedDescribe(name, async () => {
|
|
|
|
if (SQLTestHelper.enable.sqlite) {
|
2019-12-10 10:36:14 +02:00
|
|
|
const helper = new SQLTestHelper(ServerConfig.DatabaseType.sqlite);
|
2019-01-27 21:36:42 +02:00
|
|
|
savedDescribe('sqlite', () => {
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (SQLTestHelper.enable.mysql) {
|
2019-12-10 10:36:14 +02:00
|
|
|
const helper = new SQLTestHelper(ServerConfig.DatabaseType.mysql);
|
2019-01-27 21:36:42 +02:00
|
|
|
savedDescribe('mysql', function () {
|
|
|
|
this.timeout(99999999);
|
|
|
|
// @ts-ignore
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-17 11:56:33 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-01-16 17:59:59 +02:00
|
|
|
|
2019-01-27 21:36:42 +02:00
|
|
|
public async initDB() {
|
2019-12-10 10:36:14 +02:00
|
|
|
if (this.dbType === ServerConfig.DatabaseType.sqlite) {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.initSQLite();
|
|
|
|
} else {
|
|
|
|
await this.initMySQL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async clearDB() {
|
2019-12-10 10:36:14 +02:00
|
|
|
if (this.dbType === ServerConfig.DatabaseType.sqlite) {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.clearUpSQLite();
|
|
|
|
} else {
|
|
|
|
await this.clearUpMysql();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async initSQLite() {
|
|
|
|
await this.resetSQLite();
|
|
|
|
|
2019-12-10 10:36:14 +02:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.sqlite;
|
2019-12-29 10:34:08 +02:00
|
|
|
Config.Server.Database.dbFolder = this.tempDir;
|
2020-12-28 23:08:57 +02:00
|
|
|
ProjectPath.reset();
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async initMySQL() {
|
2019-12-10 10:36:14 +02:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-27 21:36:42 +02:00
|
|
|
|
|
|
|
await this.resetMySQL();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async resetSQLite() {
|
|
|
|
await SQLConnection.close();
|
2021-01-04 11:32:19 +02:00
|
|
|
await fs.promises.rmdir(this.tempDir, {recursive: true});
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async resetMySQL() {
|
2019-12-10 10:36:14 +02:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-27 21:36:42 +02:00
|
|
|
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() {
|
2019-12-10 10:36:14 +02:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-27 21:36:42 +02:00
|
|
|
const conn = await SQLConnection.getConnection();
|
|
|
|
await conn.query('DROP DATABASE IF EXISTS ' + conn.options.database);
|
|
|
|
await SQLConnection.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async clearUpSQLite() {
|
|
|
|
return this.resetSQLite();
|
|
|
|
}
|
|
|
|
}
|