From 404b82e12beea22cbbdf2c27322a8115a7ba376d Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Sun, 30 May 2021 15:09:47 +0200 Subject: [PATCH] Implementing .saved_searches.pg2conf saving tests --- .../model/database/sql/IndexingManager.ts | 21 ++++++--- .../model/database/sql/SQLConnection.ts | 9 ++-- src/common/Utils.ts | 8 ++++ src/common/config/private/PrivateConfig.ts | 2 +- .../search/search.gallery.component.ts | 1 - test/backend/assets/.saved_searches.pg2conf | 10 +++++ .../{AlbumManager.ts => AlbumManager.spec.ts} | 0 ...lleryManager.ts => GalleryManager.spec.ts} | 0 ...xingManager.ts => IndexingManager.spec.ts} | 45 ++++++++++++++++--- ...PersonManager.ts => PersonManager.spec.ts} | 0 ...SearchManager.ts => SearchManager.spec.ts} | 0 ...aringManager.ts => SharingManager.spec.ts} | 0 12 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 test/backend/assets/.saved_searches.pg2conf rename test/backend/unit/model/sql/{AlbumManager.ts => AlbumManager.spec.ts} (100%) rename test/backend/unit/model/sql/{GalleryManager.ts => GalleryManager.spec.ts} (100%) rename test/backend/unit/model/sql/{IndexingManager.ts => IndexingManager.spec.ts} (93%) rename test/backend/unit/model/sql/{PersonManager.ts => PersonManager.spec.ts} (100%) rename test/backend/unit/model/sql/{SearchManager.ts => SearchManager.spec.ts} (100%) rename test/backend/unit/model/sql/{SharingManager.ts => SharingManager.spec.ts} (100%) diff --git a/src/backend/model/database/sql/IndexingManager.ts b/src/backend/model/database/sql/IndexingManager.ts index fd020651..1433f545 100644 --- a/src/backend/model/database/sql/IndexingManager.ts +++ b/src/backend/model/database/sql/IndexingManager.ts @@ -51,6 +51,10 @@ export class IndexingManager implements IIndexingManager { } } + /** + * Indexes a dir, but returns early with the scanned version, + * does not wait for the DB to be saved + */ public indexDirectory(relativeDirectoryName: string): Promise { return new Promise(async (resolve, reject): Promise => { try { @@ -62,14 +66,12 @@ export class IndexingManager implements IIndexingManager { } scannedDirectory.media.forEach((p): any[] => p.readyThumbnails = []); - // filter server side pg2conf - const serverSideConfs = scannedDirectory.metaFile.filter(m => ServerPG2ConfMap[m.name]); - scannedDirectory.metaFile = scannedDirectory.metaFile.filter(m => !ServerPG2ConfMap[m.name]); - resolve(scannedDirectory); + const dirClone = Utils.shallowClone(scannedDirectory); + // filter server side only config from returning + dirClone.metaFile = dirClone.metaFile.filter(m => !ServerPG2ConfMap[m.name]); - // process server side pg2conf - await IndexingManager.processServerSidePG2Conf(serverSideConfs); + resolve(dirClone); // save directory to DB this.queueForSave(scannedDirectory).catch(console.error); @@ -94,7 +96,11 @@ export class IndexingManager implements IIndexingManager { } // Todo fix it, once typeorm support connection pools for sqlite + /** + * Queues up a directory to save to the DB. + */ protected async queueForSave(scannedDirectory: DirectoryDTO): Promise { + // Is this dir already queued for saving? if (this.savingQueue.findIndex((dir): boolean => dir.name === scannedDirectory.name && dir.path === scannedDirectory.path && dir.lastModified === scannedDirectory.lastModified && @@ -357,12 +363,15 @@ export class IndexingManager implements IIndexingManager { this.isSaving = true; try { const connection = await SQLConnection.getConnection(); + const serverSideConfigs = scannedDirectory.metaFile.filter(m => ServerPG2ConfMap[m.name]); + scannedDirectory.metaFile = scannedDirectory.metaFile.filter(m => !ServerPG2ConfMap[m.name]); const currentDirId: number = await this.saveParentDir(connection, scannedDirectory); await this.saveChildDirs(connection, currentDirId, scannedDirectory); await this.saveMedia(connection, currentDirId, scannedDirectory.media); await this.saveMetaFiles(connection, currentDirId, scannedDirectory); await ObjectManagers.getInstance().PersonManager.onGalleryIndexUpdate(); await ObjectManagers.getInstance().VersionManager.updateDataVersion(); + await IndexingManager.processServerSidePG2Conf(serverSideConfigs); } catch (e) { throw e; } finally { diff --git a/src/backend/model/database/sql/SQLConnection.ts b/src/backend/model/database/sql/SQLConnection.ts index d7e9ad36..6ae63f28 100644 --- a/src/backend/model/database/sql/SQLConnection.ts +++ b/src/backend/model/database/sql/SQLConnection.ts @@ -22,6 +22,7 @@ import {DatabaseType, ServerDataBaseConfig, SQLLogLevel} from '../../../../commo import {AlbumBaseEntity} from './enitites/album/AlbumBaseEntity'; import {SavedSearchEntity} from './enitites/album/SavedSearchEntity'; +const LOG_TAG = '[SQLConnection]'; export class SQLConnection { @@ -53,7 +54,7 @@ export class SQLConnection { if (Config.Server.Log.sqlLevel !== SQLLogLevel.none) { options.logging = SQLLogLevel[Config.Server.Log.sqlLevel]; } - + Logger.debug(LOG_TAG, 'Creating connection: ' + DatabaseType[Config.Server.Database.type]); this.connection = await this.createConnection(options); await SQLConnection.schemeSync(this.connection); } @@ -131,7 +132,7 @@ export class SQLConnection { return await createConnection(options); } catch (e) { if (e.sqlMessage === 'Unknown database \'' + options.database + '\'') { - Logger.debug('creating database: ' + options.database); + Logger.debug(LOG_TAG, 'creating database: ' + options.database); const tmpOption = Utils.clone(options); // @ts-ignore delete tmpOption.database; @@ -153,7 +154,7 @@ export class SQLConnection { if (version && version.version === DataStructureVersion) { return; } - Logger.info('Updating database scheme'); + Logger.info(LOG_TAG, 'Updating database scheme'); if (!version) { version = new VersionEntity(); } @@ -173,7 +174,7 @@ export class SQLConnection { await connection.dropDatabase(); await connection.synchronize(); await connection.getRepository(VersionEntity).save(version); - Logger.warn('Could not move users to the new db scheme, deleting them. Details:' + e.toString()); + Logger.warn(LOG_TAG, 'Could not move users to the new db scheme, deleting them. Details:' + e.toString()); } } diff --git a/src/common/Utils.ts b/src/common/Utils.ts index b42bacb7..e96598ac 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -46,6 +46,14 @@ export class Utils { return JSON.parse(JSON.stringify(object)); } + static shallowClone(object: T): T { + const c: any = {}; + for (const e of Object.entries(object)) { + c[e[0]] = [1]; + } + return c; + } + static zeroPrefix(value: string | number, length: number): string { const ret = '00000' + value; return ret.substr(ret.length - length); diff --git a/src/common/config/private/PrivateConfig.ts b/src/common/config/private/PrivateConfig.ts index 2c445f8c..fc3a8ca0 100644 --- a/src/common/config/private/PrivateConfig.ts +++ b/src/common/config/private/PrivateConfig.ts @@ -299,7 +299,7 @@ export class ServerPhotoConfig { export class ServerMediaConfig { @ConfigProperty({description: 'Images are loaded from this folder (read permission required)'}) folder: string = 'demo/images'; - @ConfigProperty({description: 'Thumbnails, coverted photos, videos will be stored here (write permission required)'}) + @ConfigProperty({description: 'Thumbnails, converted photos, videos will be stored here (write permission required)'}) tempFolder: string = 'demo/tmp'; @ConfigProperty() Video: ServerVideoConfig = new ServerVideoConfig(); diff --git a/src/frontend/app/ui/gallery/search/search.gallery.component.ts b/src/frontend/app/ui/gallery/search/search.gallery.component.ts index 5ade3f28..59b4a00e 100644 --- a/src/frontend/app/ui/gallery/search/search.gallery.component.ts +++ b/src/frontend/app/ui/gallery/search/search.gallery.component.ts @@ -96,7 +96,6 @@ export class GallerySearchComponent implements OnDestroy { onQueryChange(): void { - console.log('cahnge', this.searchQueryDTO); this.rawSearchText = this.searchQueryParserService.stringify(this.searchQueryDTO); // this.validateRawSearchText(); } diff --git a/test/backend/assets/.saved_searches.pg2conf b/test/backend/assets/.saved_searches.pg2conf new file mode 100644 index 00000000..9cc0570c --- /dev/null +++ b/test/backend/assets/.saved_searches.pg2conf @@ -0,0 +1,10 @@ +[ +{ + "name": "Alvin", + "searchQuery": { + "type": 105, + "text": "Alvin", + "matchType": 2 + } +} +] diff --git a/test/backend/unit/model/sql/AlbumManager.ts b/test/backend/unit/model/sql/AlbumManager.spec.ts similarity index 100% rename from test/backend/unit/model/sql/AlbumManager.ts rename to test/backend/unit/model/sql/AlbumManager.spec.ts diff --git a/test/backend/unit/model/sql/GalleryManager.ts b/test/backend/unit/model/sql/GalleryManager.spec.ts similarity index 100% rename from test/backend/unit/model/sql/GalleryManager.ts rename to test/backend/unit/model/sql/GalleryManager.spec.ts diff --git a/test/backend/unit/model/sql/IndexingManager.ts b/test/backend/unit/model/sql/IndexingManager.spec.ts similarity index 93% rename from test/backend/unit/model/sql/IndexingManager.ts rename to test/backend/unit/model/sql/IndexingManager.spec.ts index 64fbf083..a089f16d 100644 --- a/test/backend/unit/model/sql/IndexingManager.ts +++ b/test/backend/unit/model/sql/IndexingManager.spec.ts @@ -11,11 +11,14 @@ import {MediaDTO} from '../../../../../src/common/entities/MediaDTO'; import {FileDTO} from '../../../../../src/common/entities/FileDTO'; import {IndexingManager} from '../../../../../src/backend/model/database/sql/IndexingManager'; import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers'; -import {PersonManager} from '../../../../../src/backend/model/database/sql/PersonManager'; import {DBTestHelper} from '../../../DBTestHelper'; -import {VersionManager} from '../../../../../src/backend/model/database/sql/VersionManager'; import {DiskMangerWorker} from '../../../../../src/backend/model/threading/DiskMangerWorker'; import {ReIndexingSensitivity} from '../../../../../src/common/config/private/PrivateConfig'; +import {AlbumManager} from '../../../../../src/backend/model/database/sql/AlbumManager'; +import {SearchQueryTypes, TextSearch, TextSearchQueryMatchTypes} from '../../../../../src/common/entities/SearchQueryDTO'; +import {ProjectPath} from '../../../../../src/backend/ProjectPath'; +import * as path from 'path'; +import {DiskManager} from '../../../../../src/backend/model/DiskManger'; const deepEqualInAnyOrder = require('deep-equal-in-any-order'); const chai = require('chai'); @@ -59,12 +62,13 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => { beforeEach(async () => { await sqlHelper.initDB(); - ObjectManagers.getInstance().PersonManager = new PersonManager(); - ObjectManagers.getInstance().VersionManager = new VersionManager(); + // ObjectManagers.getInstance().PersonManager = new PersonManager(); + // ObjectManagers.getInstance().VersionManager = new VersionManager(); }); - after(async () => { + afterEach(async () => { + Config.loadSync(); await sqlHelper.clearDB(); }); @@ -472,7 +476,6 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => { .to.deep.equalInAnyOrder(Utils.clone(Utils.removeNullOrEmptyObj(parent))); }); - it('should reset DB', async () => { const gm = new GalleryManagerTest(); const im = new IndexingManagerTest(); @@ -574,4 +577,34 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => { }); }); + + DBTestHelper.savedDescribe('should index .pg2conf', () => { + + + it('.saved_searches.pg2conf', async () => { + Config.Server.Threading.enabled = false; + + Config.Server.Media.folder = path.join(__dirname, '/../../../assets'); + ProjectPath.ImageFolder = path.join(__dirname, '/../../../assets'); + const im = new IndexingManagerTest(); + const am = new AlbumManager(); + const dir = await DiskManager.scanDirectory('/'); + await im.saveToDB(dir); + const albums = await am.getAlbums(); + expect(albums[0].preview).to.be.an('object'); + delete albums[0].preview; + expect(albums).to.be.equalInAnyOrder([ + { + id: 1, + name: 'Alvin', + locked: true, + searchQuery: { + type: SearchQueryTypes.person, + text: 'Alvin', + matchType: TextSearchQueryMatchTypes.like + } as TextSearch + } + ]); + }); + }); }); diff --git a/test/backend/unit/model/sql/PersonManager.ts b/test/backend/unit/model/sql/PersonManager.spec.ts similarity index 100% rename from test/backend/unit/model/sql/PersonManager.ts rename to test/backend/unit/model/sql/PersonManager.spec.ts diff --git a/test/backend/unit/model/sql/SearchManager.ts b/test/backend/unit/model/sql/SearchManager.spec.ts similarity index 100% rename from test/backend/unit/model/sql/SearchManager.ts rename to test/backend/unit/model/sql/SearchManager.spec.ts diff --git a/test/backend/unit/model/sql/SharingManager.ts b/test/backend/unit/model/sql/SharingManager.spec.ts similarity index 100% rename from test/backend/unit/model/sql/SharingManager.ts rename to test/backend/unit/model/sql/SharingManager.spec.ts