2021-05-29 01:21:18 +08:00
|
|
|
import {DBTestHelper} from '../../../DBTestHelper';
|
2021-07-07 03:37:19 +08:00
|
|
|
import {ParentDirectoryDTO, SubDirectoryDTO} from '../../../../../src/common/entities/DirectoryDTO';
|
2021-05-29 01:21:18 +08:00
|
|
|
import {TestHelper} from './TestHelper';
|
|
|
|
import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers';
|
2021-07-07 03:37:19 +08:00
|
|
|
import {PhotoDTO} from '../../../../../src/common/entities/PhotoDTO';
|
2021-05-29 01:21:18 +08:00
|
|
|
import {VideoDTO} from '../../../../../src/common/entities/VideoDTO';
|
|
|
|
import {AlbumManager} from '../../../../../src/backend/model/database/sql/AlbumManager';
|
|
|
|
import {SearchQueryTypes, TextSearch} from '../../../../../src/common/entities/SearchQueryDTO';
|
|
|
|
import {SQLConnection} from '../../../../../src/backend/model/database/sql/SQLConnection';
|
|
|
|
import {AlbumBaseEntity} from '../../../../../src/backend/model/database/sql/enitites/album/AlbumBaseEntity';
|
|
|
|
import {Utils} from '../../../../../src/common/Utils';
|
|
|
|
import {MediaDTO} from '../../../../../src/common/entities/MediaDTO';
|
2021-05-30 05:27:52 +08:00
|
|
|
import {SavedSearchDTO} from '../../../../../src/common/entities/album/SavedSearchDTO';
|
2021-05-29 01:21:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
|
|
|
|
const chai = require('chai');
|
|
|
|
|
|
|
|
chai.use(deepEqualInAnyOrder);
|
|
|
|
const {expect} = chai;
|
|
|
|
|
|
|
|
// to help WebStorm to handle the test cases
|
|
|
|
declare let describe: any;
|
|
|
|
declare const after: any;
|
|
|
|
declare const before: any;
|
|
|
|
const tmpDescribe = describe;
|
|
|
|
describe = DBTestHelper.describe(); // fake it os IDE plays nicely (recognize the test)
|
|
|
|
|
|
|
|
|
|
|
|
describe('AlbumManager', (sqlHelper: DBTestHelper) => {
|
|
|
|
describe = tmpDescribe;
|
2022-01-15 03:57:20 +08:00
|
|
|
|
|
|
|
|
2021-05-29 01:21:18 +08:00
|
|
|
|
|
|
|
const setUpSqlDB = async () => {
|
|
|
|
await sqlHelper.initDB();
|
2022-01-15 03:57:20 +08:00
|
|
|
await sqlHelper.setUpTestGallery();
|
2021-05-29 01:21:18 +08:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const toAlbumPreview = (m: MediaDTO): MediaDTO => {
|
2021-06-28 01:33:37 +08:00
|
|
|
// generated dirs for test contain everything, not like return values from the server.
|
|
|
|
const tmpDir: ParentDirectoryDTO = m.directory as ParentDirectoryDTO;
|
|
|
|
const tmpM = tmpDir.media;
|
|
|
|
const tmpD = tmpDir.directories;
|
|
|
|
const tmpP = tmpDir.preview;
|
|
|
|
const tmpMT = tmpDir.metaFile;
|
|
|
|
delete tmpDir.directories;
|
|
|
|
delete tmpDir.media;
|
|
|
|
delete tmpDir.preview;
|
|
|
|
delete tmpDir.metaFile;
|
2021-05-29 01:21:18 +08:00
|
|
|
const ret = Utils.clone(m);
|
2021-07-07 03:37:19 +08:00
|
|
|
delete ret.id;
|
|
|
|
ret.directory = {path: ret.directory.path, name: ret.directory.name};
|
|
|
|
delete ret.metadata;
|
2021-06-28 01:33:37 +08:00
|
|
|
tmpDir.directories = tmpD;
|
|
|
|
tmpDir.media = tmpM;
|
|
|
|
tmpDir.preview = tmpP;
|
|
|
|
tmpDir.metaFile = tmpMT;
|
2021-05-29 01:21:18 +08:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
before(setUpSqlDB);
|
|
|
|
after(sqlHelper.clearDB);
|
|
|
|
|
|
|
|
describe('Saved search', () => {
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(setUpSqlDB);
|
|
|
|
afterEach(sqlHelper.clearDB);
|
|
|
|
|
|
|
|
it('should add album', async () => {
|
|
|
|
const am = new AlbumManager();
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
|
|
|
|
const query: TextSearch = {text: 'test', type: SearchQueryTypes.any_text};
|
|
|
|
|
|
|
|
expect(await connection.getRepository(AlbumBaseEntity).find()).to.deep.equalInAnyOrder([]);
|
|
|
|
|
|
|
|
await am.addSavedSearch('Test Album', Utils.clone(query));
|
|
|
|
|
|
|
|
expect(await connection.getRepository(AlbumBaseEntity).find()).to.deep.equalInAnyOrder([{
|
|
|
|
id: 1,
|
|
|
|
name: 'Test Album',
|
2021-05-30 05:27:52 +08:00
|
|
|
locked: false,
|
2021-06-01 01:55:27 +08:00
|
|
|
count: 0,
|
2021-05-29 01:21:18 +08:00
|
|
|
searchQuery: query
|
2021-05-30 05:27:52 +08:00
|
|
|
} as SavedSearchDTO]);
|
2021-05-29 01:21:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete album', async () => {
|
|
|
|
const am = new AlbumManager();
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
|
|
|
|
const query: TextSearch = {text: 'test', type: SearchQueryTypes.any_text};
|
|
|
|
|
|
|
|
|
|
|
|
await am.addSavedSearch('Test Album', Utils.clone(query));
|
2021-05-30 05:27:52 +08:00
|
|
|
await am.addSavedSearch('Test Album2', Utils.clone(query), true);
|
2021-05-29 01:21:18 +08:00
|
|
|
|
|
|
|
expect(await connection.getRepository(AlbumBaseEntity).find()).to.deep.equalInAnyOrder([
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'Test Album',
|
2021-05-30 05:27:52 +08:00
|
|
|
locked: false,
|
2021-06-01 01:55:27 +08:00
|
|
|
count: 0,
|
2021-05-29 01:21:18 +08:00
|
|
|
searchQuery: query
|
2021-05-30 05:27:52 +08:00
|
|
|
} as SavedSearchDTO,
|
2021-05-29 01:21:18 +08:00
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
name: 'Test Album2',
|
2021-05-30 05:27:52 +08:00
|
|
|
locked: true,
|
2021-06-01 01:55:27 +08:00
|
|
|
count: 0,
|
2021-05-29 01:21:18 +08:00
|
|
|
searchQuery: query
|
2021-05-30 05:27:52 +08:00
|
|
|
} as SavedSearchDTO]);
|
2021-05-29 01:21:18 +08:00
|
|
|
|
|
|
|
await am.deleteAlbum(1);
|
|
|
|
expect(await connection.getRepository(AlbumBaseEntity).find()).to.deep.equalInAnyOrder([{
|
|
|
|
id: 2,
|
|
|
|
name: 'Test Album2',
|
2021-05-30 05:27:52 +08:00
|
|
|
locked: true,
|
2021-06-01 01:55:27 +08:00
|
|
|
count: 0,
|
2021-05-29 01:21:18 +08:00
|
|
|
searchQuery: query
|
2021-05-30 05:27:52 +08:00
|
|
|
} as SavedSearchDTO]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await am.deleteAlbum(2);
|
|
|
|
expect(false).to.be.equal(true); // should not reach
|
|
|
|
} catch (e) {
|
|
|
|
expect(e.message).to.equal('Could not delete album, id:2');
|
|
|
|
}
|
|
|
|
expect(await connection.getRepository(AlbumBaseEntity).find()).to.deep.equalInAnyOrder([{
|
|
|
|
id: 2,
|
|
|
|
name: 'Test Album2',
|
|
|
|
locked: true,
|
2021-06-01 01:55:27 +08:00
|
|
|
count: 0,
|
2021-05-30 05:27:52 +08:00
|
|
|
searchQuery: query
|
|
|
|
} as SavedSearchDTO]);
|
2021-05-29 01:21:18 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should list album', async () => {
|
|
|
|
const am = new AlbumManager();
|
|
|
|
|
|
|
|
const query: TextSearch = {text: 'photo1', type: SearchQueryTypes.any_text};
|
|
|
|
|
|
|
|
await am.addSavedSearch('Test Album', Utils.clone(query));
|
|
|
|
|
|
|
|
expect(await am.getAlbums()).to.deep.equalInAnyOrder(([{
|
|
|
|
id: 1,
|
|
|
|
name: 'Test Album',
|
|
|
|
searchQuery: query,
|
2021-05-30 05:27:52 +08:00
|
|
|
locked: false,
|
2021-06-01 01:55:27 +08:00
|
|
|
count: 1,
|
2022-01-15 03:57:20 +08:00
|
|
|
preview: toAlbumPreview(sqlHelper.testGalleyEntities.p)
|
2021-05-30 05:27:52 +08:00
|
|
|
} as SavedSearchDTO]));
|
2021-05-29 01:21:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|