From 95a50b49369a5167c1be388b8200a87b057cbed2 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Sun, 29 Dec 2019 09:34:08 +0100 Subject: [PATCH] fixing test issues --- .gitignore | 1 + benchmark/Benchmarks.ts | 14 ++++---- benchmark/index.ts | 4 +-- src/common/config/private/ConfigClass.ts | 34 ++++++++----------- .../private/PrivateConfigDefaultsClass.ts | 6 ++-- test/backend/SQLTestHelper.ts | 17 +++------- test/backend/integration/model/sql/typeorm.ts | 29 ++++++---------- 7 files changed, 44 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 86e76cc9..15c37a81 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ out-tsc/ ffmpeg/ locale.source.xlf test.* +/db/ diff --git a/benchmark/Benchmarks.ts b/benchmark/Benchmarks.ts index 45b96c87..46359d19 100644 --- a/benchmark/Benchmarks.ts +++ b/benchmark/Benchmarks.ts @@ -4,14 +4,16 @@ 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 {SearchManager} from '../src/backend/model/database/sql/SearchManager'; -import * as fs from 'fs'; -import * as path from 'path'; +import * as util from 'util'; +import * as rimraf from 'rimraf'; import {SearchTypes} from '../src/common/entities/AutoCompleteItem'; import {Utils} from '../src/common/Utils'; import {GalleryManager} from '../src/backend/model/database/sql/GalleryManager'; import {DirectoryDTO} from '../src/common/entities/DirectoryDTO'; import {ServerConfig} from '../src/common/config/private/IPrivateConfig'; +const rimrafPR = util.promisify(rimraf); + export interface BenchmarkResult { duration: number; directories?: number; @@ -28,7 +30,7 @@ export class BMIndexingManager extends IndexingManager { export class Benchmarks { - constructor(public RUNS: number, public dbPath: string) { + constructor(public RUNS: number, public dbFolder: string) { } @@ -120,11 +122,9 @@ export class Benchmarks { private resetDB = async () => { await SQLConnection.close(); - if (fs.existsSync(this.dbPath)) { - fs.unlinkSync(this.dbPath); - } + await rimrafPR(this.dbFolder); Config.Server.Database.type = ServerConfig.DatabaseType.sqlite; - Config.Server.Database.dbFolder = path.dirname(this.dbPath); + Config.Server.Database.dbFolder = this.dbFolder; await ObjectManagers.InitSQLManagers(); }; diff --git a/benchmark/index.ts b/benchmark/index.ts index fcaafa1c..cad14bab 100644 --- a/benchmark/index.ts +++ b/benchmark/index.ts @@ -8,7 +8,7 @@ import {DiskMangerWorker} from '../src/backend/model/threading/DiskMangerWorker' const config: { path: string, system: string } = require(path.join(__dirname, 'config.json')); Config.Server.Media.folder = config.path; -const dbPath = path.join(__dirname, 'test.db'); +const dbFolder = __dirname; ProjectPath.reset(); const RUNS = 50; @@ -52,7 +52,7 @@ const printResult = (result: BenchmarkResult, action: string, actionDetails: str const run = async () => { const start = Date.now(); - const bm = new Benchmarks(RUNS, dbPath); + const bm = new Benchmarks(RUNS, dbFolder); // header await printHeader(); diff --git a/src/common/config/private/ConfigClass.ts b/src/common/config/private/ConfigClass.ts index f0a0ace1..78a2f106 100644 --- a/src/common/config/private/ConfigClass.ts +++ b/src/common/config/private/ConfigClass.ts @@ -7,28 +7,24 @@ import {PrivateConfigDefaultsClass} from './PrivateConfigDefaultsClass'; declare var process: NodeJS.Process; +let CONFIG_PATH = path.join(__dirname, './../../../../config.json'); +// TODO: refactor this +let configPath = process.argv.find(s => s.startsWith('--config-path=')); +if (configPath) { + configPath = configPath.replace('--config-path=', ''); + if (path.isAbsolute(configPath)) { + CONFIG_PATH = configPath; + } else { + CONFIG_PATH = path.join(__dirname, './../../../../', configPath); + } + console.log('using config path:' + CONFIG_PATH); +} + /** * This configuration will be only at backend */ export class ConfigClass extends PrivateConfigDefaultsClass implements IPrivateConfig { - private static CONFIG_PATH = path.join(__dirname, './../../../../config.json'); - - constructor() { - super(); - - // TODO: refactor this - let configPath = process.argv.find(s => s.startsWith('--config-path=')); - if (configPath) { - configPath = configPath.replace('--config-path=', ''); - if (path.isAbsolute(configPath)) { - ConfigClass.CONFIG_PATH = configPath; - } else { - ConfigClass.CONFIG_PATH = path.join(__dirname, './../../../../', configPath); - } - console.log('using config path:' + ConfigClass.CONFIG_PATH); - } - } public setDatabaseType(type: ServerConfig.DatabaseType) { this.Server.Database.type = type; @@ -40,7 +36,7 @@ export class ConfigClass extends PrivateConfigDefaultsClass implements IPrivateC public load() { this.addComment(); - ConfigLoader.loadBackendConfig(this, ConfigClass.CONFIG_PATH, + ConfigLoader.loadBackendConfig(this, CONFIG_PATH, [['PORT', 'Server-port'], ['MYSQL_HOST', 'Server-Database-mysql-host'], ['MYSQL_PASSWORD', 'Server-Database-mysql-password'], @@ -69,7 +65,7 @@ export class ConfigClass extends PrivateConfigDefaultsClass implements IPrivateC public save() { try { this.addComment(); - ConfigLoader.saveConfigFile(ConfigClass.CONFIG_PATH, this); + ConfigLoader.saveConfigFile(CONFIG_PATH, this); this.removeComment(); } catch (e) { throw new Error('Error during saving config: ' + e.toString()); diff --git a/src/common/config/private/PrivateConfigDefaultsClass.ts b/src/common/config/private/PrivateConfigDefaultsClass.ts index b506a918..de86f2bb 100644 --- a/src/common/config/private/PrivateConfigDefaultsClass.ts +++ b/src/common/config/private/PrivateConfigDefaultsClass.ts @@ -87,7 +87,7 @@ export class PrivateConfigDefaultsClass extends PublicConfigClass implements IPr afterScheduleName: DefaultsJobs[DefaultsJobs.Indexing] } }, - { + /* { name: DefaultsJobs[DefaultsJobs['Photo Converting']], jobName: DefaultsJobs[DefaultsJobs['Photo Converting']], config: {}, @@ -95,14 +95,14 @@ export class PrivateConfigDefaultsClass extends PublicConfigClass implements IPr type: JobTriggerType.after, afterScheduleName: DefaultsJobs[DefaultsJobs['Thumbnail Generation']] } - }, + },*/ { name: DefaultsJobs[DefaultsJobs['Video Converting']], jobName: DefaultsJobs[DefaultsJobs['Video Converting']], config: {}, trigger: { type: JobTriggerType.after, - afterScheduleName: DefaultsJobs[DefaultsJobs['Photo Converting']] + afterScheduleName: DefaultsJobs[DefaultsJobs['Thumbnail Generation']] } }, { diff --git a/test/backend/SQLTestHelper.ts b/test/backend/SQLTestHelper.ts index 3f1aa4ce..d5f014ec 100644 --- a/test/backend/SQLTestHelper.ts +++ b/test/backend/SQLTestHelper.ts @@ -1,11 +1,13 @@ import {Config} from '../../src/common/config/private/Config'; -import * as fs from 'fs'; import * as path from 'path'; +import * as util from 'util'; +import * as rimraf from 'rimraf'; import {SQLConnection} from '../../src/backend/model/database/sql/SQLConnection'; import {ServerConfig} from '../../src/common/config/private/IPrivateConfig'; declare let describe: any; const savedDescribe = describe; +const rimrafPR = util.promisify(rimraf); export class SQLTestHelper { @@ -15,12 +17,9 @@ export class SQLTestHelper { }; public static readonly savedDescribe = savedDescribe; tempDir: string; - dbPath: string; constructor(public dbType: ServerConfig.DatabaseType) { this.tempDir = path.join(__dirname, './tmp'); - this.dbPath = path.join(__dirname, './tmp', 'test.db'); - } static describe(name: string, tests: (helper?: SQLTestHelper) => void) { @@ -63,7 +62,7 @@ export class SQLTestHelper { await this.resetSQLite(); Config.Server.Database.type = ServerConfig.DatabaseType.sqlite; - Config.Server.Database.dbFolder = path.dirname(this.dbPath); + Config.Server.Database.dbFolder = this.tempDir; } private async initMySQL() { @@ -75,13 +74,7 @@ export class SQLTestHelper { private async resetSQLite() { await SQLConnection.close(); - - if (fs.existsSync(this.dbPath)) { - fs.unlinkSync(this.dbPath); - } - if (fs.existsSync(this.tempDir)) { - fs.rmdirSync(this.tempDir); - } + await rimrafPR(this.tempDir); } private async resetMySQL() { diff --git a/test/backend/integration/model/sql/typeorm.ts b/test/backend/integration/model/sql/typeorm.ts index 9675b623..2c27bc98 100644 --- a/test/backend/integration/model/sql/typeorm.ts +++ b/test/backend/integration/model/sql/typeorm.ts @@ -1,6 +1,7 @@ import {expect} from 'chai'; -import * as fs from 'fs'; import * as path from 'path'; +import * as util from 'util'; +import * as rimraf from 'rimraf'; import {Config} from '../../../../../src/common/config/private/Config'; import {SQLConnection} from '../../../../../src/backend/model/database/sql/SQLConnection'; import {UserEntity} from '../../../../../src/backend/model/database/sql/enitites/UserEntity'; @@ -18,36 +19,28 @@ import {MediaDimensionEntity} from '../../../../../src/backend/model/database/sq import {VersionEntity} from '../../../../../src/backend/model/database/sql/enitites/VersionEntity'; import {ServerConfig} from '../../../../../src/common/config/private/IPrivateConfig'; + +const rimrafPR = util.promisify(rimraf); + describe('Typeorm integration', () => { const tempDir = path.join(__dirname, '../../tmp'); - const dbPath = path.join(tempDir, 'test.db'); - const setUpSqlDB = () => { - if (fs.existsSync(dbPath)) { - fs.unlinkSync(dbPath); - } - if (!fs.existsSync(tempDir)) { - fs.mkdirSync(tempDir); - } + const setUpSqlDB = async () => { + await rimrafPR(tempDir); Config.Server.Database.type = ServerConfig.DatabaseType.sqlite; - Config.Server.Database.dbFolder = path.dirname(dbPath); + Config.Server.Database.dbFolder = tempDir; }; const teardownUpSqlDB = async () => { await SQLConnection.close(); - if (fs.existsSync(dbPath)) { - fs.unlinkSync(dbPath); - } - if (fs.existsSync(tempDir)) { - fs.rmdirSync(tempDir); - } + await rimrafPR(tempDir); }; - beforeEach(() => { - setUpSqlDB(); + beforeEach(async () => { + await setUpSqlDB(); }); afterEach(async () => {