From 6c6d77ad34a28471242757afa7cd05790618a64d Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Wed, 2 Feb 2022 00:24:16 +0100 Subject: [PATCH] fixing preview manager ordering --- .../model/database/sql/PreviewManager.ts | 10 +++++----- .../unit/model/sql/PreviewManager.spec.ts | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/backend/model/database/sql/PreviewManager.ts b/src/backend/model/database/sql/PreviewManager.ts index e4ea0d0c..7f828745 100644 --- a/src/backend/model/database/sql/PreviewManager.ts +++ b/src/backend/model/database/sql/PreviewManager.ts @@ -36,7 +36,7 @@ export class PreviewManager implements IPreviewManager { query.addOrderBy('media.rating', 'ASC'); break; case SortingMethods.descName: - query.addOrderBy('media.name', 'ASC'); + query.addOrderBy('media.name', 'DESC'); break; case SortingMethods.ascName: query.addOrderBy('media.name', 'ASC'); @@ -94,10 +94,10 @@ export class PreviewManager implements IPreviewManager { public async getAlbumPreview(album: { searchQuery: SearchQueryDTO }): Promise { - const albumQuery = await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager).prepareAndBuildWhereQuery(album.searchQuery); + const albumQuery: Brackets = await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager).prepareAndBuildWhereQuery(album.searchQuery); const connection = await SQLConnection.getConnection(); - const previewQuery = async (): Promise> => { + const previewQuery = (): SelectQueryBuilder => { const query = connection .getRepository(MediaEntity) .createQueryBuilder('media') @@ -111,7 +111,7 @@ export class PreviewManager implements IPreviewManager { let previewMedia = null; if (Config.Server.Preview.SearchQuery && !Utils.equalsFilter(Config.Server.Preview.SearchQuery, {type: SearchQueryTypes.any_text, text: ''} as TextSearch)) { - previewMedia = await (await previewQuery()) + previewMedia = await previewQuery() .andWhere(await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager) .prepareAndBuildWhereQuery(Config.Server.Preview.SearchQuery)) .limit(1) @@ -119,7 +119,7 @@ export class PreviewManager implements IPreviewManager { } if (!previewMedia) { - previewMedia = await (await previewQuery()) + previewMedia = await previewQuery() .limit(1) .getOne(); } diff --git a/test/backend/unit/model/sql/PreviewManager.spec.ts b/test/backend/unit/model/sql/PreviewManager.spec.ts index 4f252796..d88037ca 100644 --- a/test/backend/unit/model/sql/PreviewManager.spec.ts +++ b/test/backend/unit/model/sql/PreviewManager.spec.ts @@ -154,6 +154,9 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => { after(async () => { await sqlHelper.clearDB(); + }); + + afterEach(() => { Config.Server.Preview.SearchQuery = null; Config.Server.Preview.Sorting = [SortingMethods.descRating, SortingMethods.descDate]; }); @@ -218,10 +221,16 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => { text: 'sw' } as TextSearch }))).to.deep.equalInAnyOrder(previewifyMedia(p2)); - + // Having a preview search query that does not return valid result + Config.Server.Preview.SearchQuery = {type: SearchQueryTypes.any_text, text: 'wont find it'} as TextSearch; + expect(Utils.clone(await pm.getAlbumPreview({ + searchQuery: { + type: SearchQueryTypes.any_text, + text: 'Derem' + } as TextSearch + }))).to.deep.equalInAnyOrder(previewifyMedia(p2)); }); - it('should invalidate and update preview', async () => { const gm = new GalleryManagerTest(); const pm = new PreviewManager(); @@ -240,14 +249,14 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => { let subdir = await selectDir(); expect(subdir.validPreview).to.equal(true); - expect(subdir.preview.id).to.equal(2); + expect(subdir.preview.id).to.equal(p2.id); // new version should invalidate await pm.onNewDataVersion(subDir as ParentDirectoryDTO); subdir = await selectDir(); expect(subdir.validPreview).to.equal(false); // during invalidation, we do not remove the previous preview (it's good to show at least some photo) - expect(subdir.preview.id).to.equal(2); + expect(subdir.preview.id).to.equal(p2.id); await conn.createQueryBuilder() .update(DirectoryEntity) @@ -258,7 +267,7 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => { await gm.fillParentDir(conn, res); subdir = await selectDir(); expect(subdir.validPreview).to.equal(true); - expect(subdir.preview.id).to.equal(2); + expect(subdir.preview.id).to.equal(p2.id); });