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';
|
2023-01-06 00:11:58 +02:00
|
|
|
import {SQLConnection} from '../../src/backend/model/database/SQLConnection';
|
2023-11-17 00:41:05 +02:00
|
|
|
import {DatabaseType} from '../../src/common/config/private/PrivateConfig';
|
2020-12-28 23:08:57 +02:00
|
|
|
import {ProjectPath} from '../../src/backend/ProjectPath';
|
2022-01-14 21:57:20 +02:00
|
|
|
import {DirectoryBaseDTO, ParentDirectoryDTO, SubDirectoryDTO} from '../../src/common/entities/DirectoryDTO';
|
2021-01-17 11:56:33 +02:00
|
|
|
import {ObjectManagers} from '../../src/backend/model/ObjectManagers';
|
2023-10-14 17:54:21 +02:00
|
|
|
import {DiskManager} from '../../src/backend/model/fileaccess/DiskManager';
|
2023-01-06 00:11:58 +02:00
|
|
|
import {IndexingManager} from '../../src/backend/model/database/IndexingManager';
|
|
|
|
import {GalleryManager} from '../../src/backend/model/database/GalleryManager';
|
2021-01-17 11:56:33 +02:00
|
|
|
import {Connection} from 'typeorm';
|
2021-04-01 21:48:38 +02:00
|
|
|
import {Utils} from '../../src/common/Utils';
|
2022-06-24 18:05:45 +02:00
|
|
|
import {TestHelper} from '../TestHelper';
|
2022-01-14 21:57:20 +02:00
|
|
|
import {VideoDTO} from '../../src/common/entities/VideoDTO';
|
|
|
|
import {PhotoDTO} from '../../src/common/entities/PhotoDTO';
|
2022-12-04 23:23:51 +02:00
|
|
|
import {Logger} from '../../src/backend/Logger';
|
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 {
|
|
|
|
|
2021-06-27 19:33:37 +02:00
|
|
|
public async saveToDB(scannedDirectory: ParentDirectoryDTO): Promise<void> {
|
2021-01-17 11:56:33 +02:00
|
|
|
return super.saveToDB(scannedDirectory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GalleryManagerTest extends GalleryManager {
|
|
|
|
|
2022-12-04 23:23:51 +02:00
|
|
|
public async getDirIdAndTime(connection: Connection, directoryName: string, directoryParent: string) {
|
|
|
|
return super.getDirIdAndTime(connection, directoryName, directoryParent);
|
2021-01-17 11:56:33 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 23:23:51 +02:00
|
|
|
public async getParentDirFromId(connection: Connection, dir: number): Promise<ParentDirectoryDTO> {
|
|
|
|
return super.getParentDirFromId(connection, dir);
|
2021-01-17 11:56:33 +02:00
|
|
|
}
|
2022-12-04 23:23:51 +02:00
|
|
|
|
2021-01-17 11:56:33 +02:00
|
|
|
}
|
2021-01-16 17:59:59 +02:00
|
|
|
|
2022-12-04 23:23:51 +02:00
|
|
|
const LOG_TAG = 'DBTestHelper';
|
|
|
|
|
2021-04-01 21:48:38 +02:00
|
|
|
export class DBTestHelper {
|
2019-01-27 21:36:42 +02:00
|
|
|
|
|
|
|
static enable = {
|
2021-05-28 23:28:12 +02:00
|
|
|
sqlite: process.env.TEST_SQLITE !== 'false',
|
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;
|
2022-12-04 23:23:51 +02:00
|
|
|
public tempDir: string;
|
2022-01-14 21:57:20 +02:00
|
|
|
public readonly testGalleyEntities: {
|
|
|
|
dir: ParentDirectoryDTO,
|
|
|
|
subDir: SubDirectoryDTO,
|
|
|
|
subDir2: SubDirectoryDTO,
|
|
|
|
v: VideoDTO,
|
|
|
|
p: PhotoDTO,
|
|
|
|
p2: PhotoDTO,
|
|
|
|
p3: PhotoDTO,
|
|
|
|
p4: PhotoDTO
|
|
|
|
} = {
|
|
|
|
/**
|
|
|
|
* dir
|
|
|
|
* |- v
|
|
|
|
* |- p
|
|
|
|
* |- p2
|
|
|
|
* |-> subDir
|
|
|
|
* |- p3
|
|
|
|
* |-> subDir2
|
|
|
|
* |- p4
|
|
|
|
*/
|
|
|
|
|
|
|
|
dir: null,
|
|
|
|
subDir: null,
|
|
|
|
subDir2: null,
|
|
|
|
v: null,
|
|
|
|
p: null,
|
|
|
|
p2: null,
|
|
|
|
p3: null,
|
|
|
|
p4: null
|
|
|
|
};
|
2019-01-27 21:36:42 +02:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
constructor(public dbType: DatabaseType) {
|
2019-02-23 00:39:01 +02:00
|
|
|
this.tempDir = path.join(__dirname, './tmp');
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
2021-04-01 21:48:38 +02:00
|
|
|
static describe(settingsOverride: {
|
|
|
|
sqlite?: boolean;
|
|
|
|
mysql?: boolean;
|
2021-04-18 15:48:35 +02:00
|
|
|
} = {}): (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)) {
|
2021-04-18 15:48:35 +02:00
|
|
|
(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) {
|
2021-04-18 15:48:35 +02:00
|
|
|
const helper = new DBTestHelper(DatabaseType.sqlite);
|
2021-04-01 21:48:38 +02:00
|
|
|
savedDescribe('sqlite', () => {
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (settings.mysql) {
|
2021-04-18 15:48:35 +02:00
|
|
|
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
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 21:37:19 +02:00
|
|
|
public static async persistTestDir(directory: DirectoryBaseDTO): Promise<ParentDirectoryDTO> {
|
2023-11-17 00:41:05 +02:00
|
|
|
await ObjectManagers.getInstance().init();
|
2021-01-17 11:56:33 +02:00
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
ObjectManagers.getInstance().IndexingManager.indexDirectory = () => Promise.resolve(null);
|
|
|
|
|
|
|
|
|
2021-08-07 18:09:46 +02:00
|
|
|
await ObjectManagers.getInstance().IndexingManager.saveToDB(directory as ParentDirectoryDTO);
|
2021-01-17 11:56:33 +02:00
|
|
|
// not saving subdirs. saveToDB destroys data
|
|
|
|
// await im.saveToDB(subDir);
|
|
|
|
// await im.saveToDB(subDir2);
|
|
|
|
|
|
|
|
if (ObjectManagers.getInstance().IndexingManager &&
|
2023-11-17 00:41:05 +02:00
|
|
|
ObjectManagers.getInstance().IndexingManager.IsSavingInProgress) {
|
2021-01-17 11:56:33 +02:00
|
|
|
await ObjectManagers.getInstance().IndexingManager.SavingReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gm = new GalleryManagerTest();
|
2022-12-04 23:23:51 +02:00
|
|
|
|
|
|
|
const dir = await gm.getParentDirFromId(connection,
|
2023-11-17 00:41:05 +02:00
|
|
|
(await gm.getDirIdAndTime(connection, directory.name, path.join(directory.path, path.sep))).id);
|
2021-01-17 11:56:33 +02:00
|
|
|
|
2021-06-27 19:33:37 +02:00
|
|
|
const populateDir = async (d: DirectoryBaseDTO) => {
|
2021-01-17 11:56:33 +02:00
|
|
|
for (let i = 0; i < d.directories.length; i++) {
|
2022-12-04 23:23:51 +02:00
|
|
|
d.directories[i] = await gm.getParentDirFromId(connection,
|
2023-11-17 00:41:05 +02:00
|
|
|
(await gm.getDirIdAndTime(connection, d.directories[i].name,
|
|
|
|
path.join(DiskManager.pathFromParent(d), path.sep))).id);
|
2021-01-17 11:56:33 +02:00
|
|
|
await populateDir(d.directories[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await populateDir(dir);
|
|
|
|
|
|
|
|
await ObjectManagers.reset();
|
|
|
|
return dir;
|
|
|
|
}
|
2021-01-16 17:59:59 +02:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public async initDB(): Promise<void> {
|
2021-06-03 21:19:21 +02:00
|
|
|
await Config.load();
|
2023-11-17 00:41:05 +02:00
|
|
|
Config.Extensions.enabled = false; // make all tests clean
|
2021-04-18 15:48:35 +02:00
|
|
|
if (this.dbType === DatabaseType.sqlite) {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.initSQLite();
|
2021-04-18 15:48:35 +02:00
|
|
|
} else if (this.dbType === DatabaseType.mysql) {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.initMySQL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public async clearDB(): Promise<void> {
|
|
|
|
if (this.dbType === DatabaseType.sqlite) {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.clearUpSQLite();
|
2021-04-18 15:48:35 +02:00
|
|
|
} else if (this.dbType === DatabaseType.mysql) {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.clearUpMysql();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:57:20 +02:00
|
|
|
public async setUpTestGallery(): Promise<void> {
|
|
|
|
const directory: ParentDirectoryDTO = TestHelper.getDirectoryEntry();
|
|
|
|
this.testGalleyEntities.subDir = TestHelper.getDirectoryEntry(directory, 'The Phantom Menace');
|
|
|
|
this.testGalleyEntities.subDir2 = TestHelper.getDirectoryEntry(directory, 'Return of the Jedi');
|
|
|
|
this.testGalleyEntities.p = TestHelper.getRandomizedPhotoEntry(directory, 'Photo1');
|
|
|
|
this.testGalleyEntities.p2 = TestHelper.getRandomizedPhotoEntry(directory, 'Photo2');
|
|
|
|
this.testGalleyEntities.p3 = TestHelper.getRandomizedPhotoEntry(this.testGalleyEntities.subDir, 'Photo3');
|
|
|
|
this.testGalleyEntities.p4 = TestHelper.getRandomizedPhotoEntry(this.testGalleyEntities.subDir2, 'Photo4');
|
|
|
|
this.testGalleyEntities.v = TestHelper.getVideoEntry1(directory);
|
|
|
|
|
|
|
|
this.testGalleyEntities.dir = await DBTestHelper.persistTestDir(directory);
|
|
|
|
this.testGalleyEntities.subDir = this.testGalleyEntities.dir.directories[0];
|
|
|
|
this.testGalleyEntities.subDir2 = this.testGalleyEntities.dir.directories[1];
|
|
|
|
this.testGalleyEntities.p = (this.testGalleyEntities.dir.media.filter(m => m.name === this.testGalleyEntities.p.name)[0] as any);
|
2022-12-04 23:23:51 +02:00
|
|
|
this.testGalleyEntities.p.directory = this.testGalleyEntities.dir;
|
2022-01-14 21:57:20 +02:00
|
|
|
this.testGalleyEntities.p2 = (this.testGalleyEntities.dir.media.filter(m => m.name === this.testGalleyEntities.p2.name)[0] as any);
|
2022-12-04 23:23:51 +02:00
|
|
|
this.testGalleyEntities.p2.directory = this.testGalleyEntities.dir;
|
2022-01-14 21:57:20 +02:00
|
|
|
this.testGalleyEntities.v = (this.testGalleyEntities.dir.media.filter(m => m.name === this.testGalleyEntities.v.name)[0] as any);
|
2022-12-04 23:23:51 +02:00
|
|
|
this.testGalleyEntities.v.directory = this.testGalleyEntities.dir;
|
2022-01-14 21:57:20 +02:00
|
|
|
this.testGalleyEntities.p3 = (this.testGalleyEntities.dir.directories[0].media[0] as any);
|
2022-12-04 23:23:51 +02:00
|
|
|
this.testGalleyEntities.p3.directory = this.testGalleyEntities.dir.directories[0];
|
2022-01-14 21:57:20 +02:00
|
|
|
this.testGalleyEntities.p4 = (this.testGalleyEntities.dir.directories[1].media[0] as any);
|
2022-12-04 23:23:51 +02:00
|
|
|
this.testGalleyEntities.p2.directory = this.testGalleyEntities.dir.directories[1];
|
2022-01-14 21:57:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
private async initMySQL(): Promise<void> {
|
2019-01-27 21:36:42 +02:00
|
|
|
await this.resetMySQL();
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
private async resetMySQL(): Promise<void> {
|
2022-12-04 23:23:51 +02:00
|
|
|
Logger.debug(LOG_TAG, 'resetting up mysql');
|
|
|
|
await this.clearUpMysql();
|
2019-01-27 21:36:42 +02:00
|
|
|
const conn = await SQLConnection.getConnection();
|
|
|
|
await conn.query('CREATE DATABASE IF NOT EXISTS ' + conn.options.database);
|
|
|
|
await SQLConnection.close();
|
2023-11-17 00:41:05 +02:00
|
|
|
await ObjectManagers.getInstance().init();
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
private async clearUpMysql(): Promise<void> {
|
2022-12-04 23:23:51 +02:00
|
|
|
Logger.debug(LOG_TAG, 'clearing up mysql');
|
2021-05-23 22:26:27 +02:00
|
|
|
await ObjectManagers.reset();
|
2022-12-28 20:12:18 +02:00
|
|
|
Config.Database.type = DatabaseType.mysql;
|
|
|
|
Config.Database.mysql.database = 'pigallery2_test';
|
2022-12-04 23:23:51 +02:00
|
|
|
await fs.promises.rm(this.tempDir, {recursive: true, force: true});
|
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();
|
|
|
|
}
|
|
|
|
|
2021-05-23 22:26:27 +02:00
|
|
|
private async initSQLite(): Promise<void> {
|
|
|
|
await this.resetSQLite();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async resetSQLite(): Promise<void> {
|
2022-12-04 23:23:51 +02:00
|
|
|
Logger.debug(LOG_TAG, 'resetting sqlite');
|
|
|
|
await this.clearUpSQLite();
|
2023-11-17 00:41:05 +02:00
|
|
|
await ObjectManagers.getInstance().init();
|
2021-05-23 22:26:27 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
private async clearUpSQLite(): Promise<void> {
|
2022-12-04 23:23:51 +02:00
|
|
|
Logger.debug(LOG_TAG, 'clearing up sqlite');
|
2022-12-28 20:12:18 +02:00
|
|
|
Config.Database.type = DatabaseType.sqlite;
|
|
|
|
Config.Database.dbFolder = this.tempDir;
|
2021-05-23 22:26:27 +02:00
|
|
|
ProjectPath.reset();
|
|
|
|
await ObjectManagers.reset();
|
2022-01-14 17:27:08 +02:00
|
|
|
await fs.promises.rm(this.tempDir, {recursive: true, force: true});
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
2021-04-01 21:48:38 +02:00
|
|
|
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|