2019-12-10 17:44:35 +08:00
|
|
|
import {Config} from '../../src/common/config/private/Config';
|
2019-01-28 03:36:42 +08:00
|
|
|
import * as path from 'path';
|
2021-01-04 17:32:19 +08:00
|
|
|
import * as fs from 'fs';
|
2019-12-15 00:27:01 +08:00
|
|
|
import {SQLConnection} from '../../src/backend/model/database/sql/SQLConnection';
|
2021-06-04 03:19:21 +08:00
|
|
|
import {DatabaseType} from '../../src/common/config/private/PrivateConfig';
|
2020-12-29 05:08:57 +08:00
|
|
|
import {ProjectPath} from '../../src/backend/ProjectPath';
|
2022-01-15 03:57:20 +08:00
|
|
|
import {DirectoryBaseDTO, ParentDirectoryDTO, SubDirectoryDTO} from '../../src/common/entities/DirectoryDTO';
|
2021-01-17 17:56:33 +08:00
|
|
|
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';
|
2022-01-15 03:57:20 +08:00
|
|
|
import {TestHelper} from './unit/model/sql/TestHelper';
|
|
|
|
import {VideoDTO} from '../../src/common/entities/VideoDTO';
|
|
|
|
import {PhotoDTO} from '../../src/common/entities/PhotoDTO';
|
2019-01-28 03:36:42 +08:00
|
|
|
|
|
|
|
declare let describe: any;
|
|
|
|
const savedDescribe = describe;
|
|
|
|
|
2021-01-17 17:56:33 +08:00
|
|
|
class IndexingManagerTest extends IndexingManager {
|
|
|
|
|
2021-06-28 01:33:37 +08:00
|
|
|
public async saveToDB(scannedDirectory: ParentDirectoryDTO): Promise<void> {
|
2021-01-17 17:56:33 +08:00
|
|
|
return super.saveToDB(scannedDirectory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GalleryManagerTest extends GalleryManager {
|
|
|
|
|
2021-07-07 03:37:19 +08:00
|
|
|
public async selectParentDir(connection: Connection, directoryName: string, directoryParent: string): Promise<ParentDirectoryDTO> {
|
2021-01-17 17:56:33 +08:00
|
|
|
return super.selectParentDir(connection, directoryName, directoryParent);
|
|
|
|
}
|
|
|
|
|
2021-07-07 03:37:19 +08:00
|
|
|
public async fillParentDir(connection: Connection, dir: ParentDirectoryDTO): Promise<void> {
|
2021-01-17 17:56:33 +08:00
|
|
|
return super.fillParentDir(connection, dir);
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 23:59:59 +08:00
|
|
|
|
2021-04-02 03:48:38 +08:00
|
|
|
export class DBTestHelper {
|
2019-01-28 03:36:42 +08:00
|
|
|
|
|
|
|
static enable = {
|
2021-04-02 03:48:38 +08:00
|
|
|
memory: false,
|
2021-05-29 05:28:12 +08:00
|
|
|
sqlite: process.env.TEST_SQLITE !== 'false',
|
2021-01-16 23:59:59 +08:00
|
|
|
mysql: process.env.TEST_MYSQL !== 'false'
|
2019-01-28 03:36:42 +08:00
|
|
|
};
|
|
|
|
public static readonly savedDescribe = savedDescribe;
|
|
|
|
tempDir: string;
|
2022-01-15 03:57:20 +08: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-28 03:36:42 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
constructor(public dbType: DatabaseType) {
|
2019-02-23 06:39:01 +08:00
|
|
|
this.tempDir = path.join(__dirname, './tmp');
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-02 03:48:38 +08:00
|
|
|
static describe(settingsOverride: {
|
|
|
|
memory?: boolean;
|
|
|
|
sqlite?: boolean;
|
|
|
|
mysql?: boolean;
|
2021-04-18 21:48:35 +08:00
|
|
|
} = {}): (name: string, tests: (helper?: DBTestHelper) => void) => void {
|
2021-04-02 03:48:38 +08:00
|
|
|
const settings = Utils.clone(DBTestHelper.enable);
|
|
|
|
for (const key of Object.keys(settingsOverride)) {
|
2021-04-18 21:48:35 +08:00
|
|
|
(settings as any)[key] = (settingsOverride as any)[key];
|
2021-04-02 03:48:38 +08:00
|
|
|
}
|
|
|
|
return (name: string, tests: (helper?: DBTestHelper) => void) => {
|
|
|
|
savedDescribe(name, async () => {
|
|
|
|
if (settings.sqlite) {
|
2021-04-18 21:48:35 +08:00
|
|
|
const helper = new DBTestHelper(DatabaseType.sqlite);
|
2021-04-02 03:48:38 +08:00
|
|
|
savedDescribe('sqlite', () => {
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (settings.mysql) {
|
2021-04-18 21:48:35 +08:00
|
|
|
const helper = new DBTestHelper(DatabaseType.mysql);
|
|
|
|
savedDescribe('mysql', function(): void {
|
2021-04-02 03:48:38 +08:00
|
|
|
this.timeout(99999999); // hint for the test environment
|
|
|
|
// @ts-ignore
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (settings.memory) {
|
2021-04-18 21:48:35 +08:00
|
|
|
const helper = new DBTestHelper(DatabaseType.memory);
|
2021-04-02 03:48:38 +08:00
|
|
|
savedDescribe('memory', () => {
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 03:37:19 +08:00
|
|
|
public static async persistTestDir(directory: DirectoryBaseDTO): Promise<ParentDirectoryDTO> {
|
2021-01-17 17:56:33 +08:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
ObjectManagers.getInstance().IndexingManager.indexDirectory = () => Promise.resolve(null);
|
|
|
|
|
|
|
|
|
2021-08-08 00:09:46 +08:00
|
|
|
await ObjectManagers.getInstance().IndexingManager.saveToDB(directory as ParentDirectoryDTO);
|
2021-01-17 17:56:33 +08:00
|
|
|
// 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-28 01:33:37 +08:00
|
|
|
const populateDir = async (d: DirectoryBaseDTO) => {
|
2021-01-17 17:56:33 +08:00
|
|
|
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));
|
2021-04-18 21:48:35 +08:00
|
|
|
await gm.fillParentDir(connection, d.directories[i] as any);
|
2021-01-17 17:56:33 +08:00
|
|
|
await populateDir(d.directories[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await populateDir(dir);
|
|
|
|
|
|
|
|
await ObjectManagers.reset();
|
|
|
|
return dir;
|
|
|
|
}
|
2021-01-16 23:59:59 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public async initDB(): Promise<void> {
|
2021-06-04 03:19:21 +08:00
|
|
|
await Config.load();
|
2021-04-18 21:48:35 +08:00
|
|
|
if (this.dbType === DatabaseType.sqlite) {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.initSQLite();
|
2021-04-18 21:48:35 +08:00
|
|
|
} else if (this.dbType === DatabaseType.mysql) {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.initMySQL();
|
2021-05-11 21:20:36 +08:00
|
|
|
} else if (this.dbType === DatabaseType.memory) {
|
|
|
|
Config.Server.Database.type = DatabaseType.memory;
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public async clearDB(): Promise<void> {
|
|
|
|
if (this.dbType === DatabaseType.sqlite) {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.clearUpSQLite();
|
2021-04-18 21:48:35 +08:00
|
|
|
} else if (this.dbType === DatabaseType.mysql) {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.clearUpMysql();
|
2021-04-18 21:48:35 +08:00
|
|
|
} else if (this.dbType === DatabaseType.memory) {
|
2021-04-02 03:48:38 +08:00
|
|
|
await this.clearUpMemory();
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 03:57:20 +08: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);
|
|
|
|
this.testGalleyEntities.p2 = (this.testGalleyEntities.dir.media.filter(m => m.name === this.testGalleyEntities.p2.name)[0] as any);
|
|
|
|
this.testGalleyEntities.v = (this.testGalleyEntities.dir.media.filter(m => m.name === this.testGalleyEntities.v.name)[0] as any);
|
|
|
|
this.testGalleyEntities.p3 = (this.testGalleyEntities.dir.directories[0].media[0] as any);
|
|
|
|
this.testGalleyEntities.p4 = (this.testGalleyEntities.dir.directories[1].media[0] as any);
|
|
|
|
}
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
private async initMySQL(): Promise<void> {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.resetMySQL();
|
|
|
|
}
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
private async resetMySQL(): Promise<void> {
|
2021-05-29 05:28:12 +08:00
|
|
|
await ObjectManagers.reset();
|
2021-04-18 21:48:35 +08:00
|
|
|
Config.Server.Database.type = DatabaseType.mysql;
|
2019-12-09 21:05:06 +08:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-28 03:36:42 +08: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();
|
2021-05-24 04:26:27 +08:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
private async clearUpMysql(): Promise<void> {
|
2021-05-24 04:26:27 +08:00
|
|
|
await ObjectManagers.reset();
|
2021-04-18 21:48:35 +08:00
|
|
|
Config.Server.Database.type = DatabaseType.mysql;
|
2019-12-09 21:05:06 +08:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-28 03:36:42 +08:00
|
|
|
const conn = await SQLConnection.getConnection();
|
|
|
|
await conn.query('DROP DATABASE IF EXISTS ' + conn.options.database);
|
|
|
|
await SQLConnection.close();
|
|
|
|
}
|
|
|
|
|
2021-05-24 04:26:27 +08:00
|
|
|
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();
|
2022-01-14 23:27:08 +08:00
|
|
|
await fs.promises.rm(this.tempDir, {recursive: true, force: true});
|
2021-05-24 04:26:27 +08:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
|
|
|
}
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
private async clearUpSQLite(): Promise<void> {
|
2021-05-24 04:26:27 +08:00
|
|
|
Config.Server.Database.type = DatabaseType.sqlite;
|
|
|
|
Config.Server.Database.dbFolder = this.tempDir;
|
|
|
|
ProjectPath.reset();
|
|
|
|
await ObjectManagers.reset();
|
2022-01-14 23:27:08 +08:00
|
|
|
await fs.promises.rm(this.tempDir, {recursive: true, force: true});
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
2021-04-02 03:48:38 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
private async clearUpMemory(): Promise<void> {
|
2021-04-02 03:48:38 +08:00
|
|
|
return this.resetSQLite();
|
|
|
|
}
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|