1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/test/backend/unit/model/sql/SearchManager.ts

280 lines
9.9 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {expect} from 'chai';
import {SQLConnection} from '../../../../../src/backend/model/database/sql/SQLConnection';
import {PhotoEntity} from '../../../../../src/backend/model/database/sql/enitites/PhotoEntity';
import {SearchManager} from '../../../../../src/backend/model/database/sql/SearchManager';
2019-12-10 17:44:35 +08:00
import {AutoCompleteItem, SearchTypes} from '../../../../../src/common/entities/AutoCompleteItem';
import {SearchResultDTO} from '../../../../../src/common/entities/SearchResultDTO';
import {DirectoryEntity} from '../../../../../src/backend/model/database/sql/enitites/DirectoryEntity';
2019-12-10 17:44:35 +08:00
import {Utils} from '../../../../../src/common/Utils';
import {TestHelper} from './TestHelper';
import {VideoEntity} from '../../../../../src/backend/model/database/sql/enitites/VideoEntity';
import {PersonEntry} from '../../../../../src/backend/model/database/sql/enitites/PersonEntry';
import {FaceRegionEntry} from '../../../../../src/backend/model/database/sql/enitites/FaceRegionEntry';
2019-12-10 17:44:35 +08:00
import {PhotoDTO} from '../../../../../src/common/entities/PhotoDTO';
import {SQLTestHelper} from '../../../SQLTestHelper';
2019-12-10 17:44:35 +08:00
import {Config} from '../../../../../src/common/config/private/Config';
2018-01-31 09:01:16 +08:00
// to help WebStorm to handle the test cases
declare let describe: any;
declare const after: any;
describe = SQLTestHelper.describe;
2018-01-31 09:01:16 +08:00
describe('SearchManager', (sqlHelper: SQLTestHelper) => {
2018-01-31 09:01:16 +08:00
const dir = TestHelper.getDirectoryEntry();
const p = TestHelper.getPhotoEntry1(dir);
const p2 = TestHelper.getPhotoEntry2(dir);
2019-01-27 07:03:40 +08:00
const p_faceLess = TestHelper.getPhotoEntry2(dir);
delete p_faceLess.metadata.faces;
p_faceLess.name = 'fl';
2018-11-24 21:51:47 +08:00
const v = TestHelper.getVideoEntry1(dir);
2018-01-31 09:01:16 +08:00
const setUpSqlDB = async () => {
await sqlHelper.initDB();
2018-01-31 09:01:16 +08:00
2019-01-27 07:03:40 +08:00
const savePhoto = async (photo: PhotoDTO) => {
const savedPhoto = await pr.save(photo);
if (!photo.metadata.faces) {
return;
}
for (let i = 0; i < photo.metadata.faces.length; i++) {
const face = photo.metadata.faces[i];
const person = await conn.getRepository(PersonEntry).save({name: face.name});
await conn.getRepository(FaceRegionEntry).save({box: face.box, person: person, media: savedPhoto});
}
};
2018-01-31 09:01:16 +08:00
const conn = await SQLConnection.getConnection();
const pr = conn.getRepository(PhotoEntity);
await conn.getRepository(DirectoryEntity).save(p.directory);
2019-01-27 07:03:40 +08:00
await savePhoto(p);
await savePhoto(p2);
await savePhoto(p_faceLess);
2018-11-24 21:51:47 +08:00
await conn.getRepository(VideoEntity).save(v);
2018-01-31 09:01:16 +08:00
await SQLConnection.close();
};
beforeEach(async () => {
await setUpSqlDB();
});
after(async () => {
await sqlHelper.clearDB();
});
2018-01-31 09:01:16 +08:00
it('should get autocomplete', async () => {
2018-11-02 18:22:18 +08:00
const sm = new SearchManager();
2018-01-31 09:01:16 +08:00
const cmp = (a: AutoCompleteItem, b: AutoCompleteItem) => {
2019-01-27 07:03:40 +08:00
if (a.text === b.text) {
return a.type - b.type;
}
2018-01-31 09:01:16 +08:00
return a.text.localeCompare(b.text);
};
2018-03-31 03:30:30 +08:00
expect((await sm.autocomplete('tat'))).to.deep.equal([new AutoCompleteItem('Tatooine', SearchTypes.position)]);
expect((await sm.autocomplete('star'))).to.deep.equal([new AutoCompleteItem('star wars', SearchTypes.keyword),
new AutoCompleteItem('death star', SearchTypes.keyword)]);
expect((await sm.autocomplete('wars'))).to.deep.equal([new AutoCompleteItem('star wars', SearchTypes.keyword),
new AutoCompleteItem('wars dir', SearchTypes.directory)]);
expect((await sm.autocomplete('arch'))).eql([new AutoCompleteItem('Research City', SearchTypes.position)]);
Config.Client.Search.AutoComplete.maxItemsPerCategory = 99999;
2018-03-31 03:30:30 +08:00
expect((await sm.autocomplete('a')).sort(cmp)).eql([
new AutoCompleteItem('Boba Fett', SearchTypes.keyword),
2019-01-27 07:03:40 +08:00
new AutoCompleteItem('Boba Fett', SearchTypes.person),
2018-03-31 03:30:30 +08:00
new AutoCompleteItem('star wars', SearchTypes.keyword),
new AutoCompleteItem('Anakin', SearchTypes.keyword),
2019-01-27 07:03:40 +08:00
new AutoCompleteItem('Anakin Skywalker', SearchTypes.person),
new AutoCompleteItem('Luke Skywalker', SearchTypes.person),
new AutoCompleteItem('Han Solo', SearchTypes.person),
2018-03-31 03:30:30 +08:00
new AutoCompleteItem('death star', SearchTypes.keyword),
2019-01-27 07:03:40 +08:00
new AutoCompleteItem('Padmé Amidala', SearchTypes.person),
new AutoCompleteItem('Obivan Kenobi', SearchTypes.person),
2018-03-31 03:30:30 +08:00
new AutoCompleteItem('Padmé Amidala', SearchTypes.keyword),
new AutoCompleteItem('Natalie Portman', SearchTypes.keyword),
2018-12-06 02:15:24 +08:00
new AutoCompleteItem('Han Solo\'s dice', SearchTypes.photo),
2018-03-31 03:30:30 +08:00
new AutoCompleteItem('Kamino', SearchTypes.position),
new AutoCompleteItem('Tatooine', SearchTypes.position),
new AutoCompleteItem('wars dir', SearchTypes.directory),
new AutoCompleteItem('Research City', SearchTypes.position)].sort(cmp));
Config.Client.Search.AutoComplete.maxItemsPerCategory = 1;
expect((await sm.autocomplete('a')).sort(cmp)).eql([
new AutoCompleteItem('Anakin', SearchTypes.keyword),
new AutoCompleteItem('star wars', SearchTypes.keyword),
new AutoCompleteItem('death star', SearchTypes.keyword),
new AutoCompleteItem('Anakin Skywalker', SearchTypes.person),
new AutoCompleteItem('Han Solo\'s dice', SearchTypes.photo),
new AutoCompleteItem('Kamino', SearchTypes.position),
new AutoCompleteItem('Research City', SearchTypes.position),
new AutoCompleteItem('wars dir', SearchTypes.directory),
new AutoCompleteItem('Boba Fett', SearchTypes.keyword)].sort(cmp));
Config.Client.Search.AutoComplete.maxItemsPerCategory = 5;
2018-11-24 21:51:47 +08:00
expect((await sm.autocomplete('sw')).sort(cmp)).to.deep.equal([new AutoCompleteItem('sw1', SearchTypes.photo),
new AutoCompleteItem('sw2', SearchTypes.photo), new AutoCompleteItem(v.name, SearchTypes.video)].sort(cmp));
expect((await sm.autocomplete(v.name)).sort(cmp)).to.deep.equal([new AutoCompleteItem(v.name, SearchTypes.video)]);
2018-01-31 09:01:16 +08:00
});
it('should search', async () => {
2018-11-02 18:22:18 +08:00
const sm = new SearchManager();
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.search('sw', null))).to.deep.equal(Utils.clone(<SearchResultDTO>{
2018-03-31 03:30:30 +08:00
searchText: 'sw',
2018-01-31 09:01:16 +08:00
searchType: null,
directories: [],
2018-11-26 07:26:29 +08:00
media: [p, p2, v],
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2019-01-27 07:03:40 +08:00
expect(Utils.clone(await sm.search('Boba', null))).to.deep.equal(Utils.clone(<SearchResultDTO>{
searchText: 'Boba',
searchType: null,
directories: [],
media: [p],
metaFile: [],
resultOverflow: false
}));
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.search('Tatooine', SearchTypes.position))).to.deep.equal(Utils.clone(<SearchResultDTO>{
2018-03-31 03:30:30 +08:00
searchText: 'Tatooine',
2018-01-31 09:01:16 +08:00
searchType: SearchTypes.position,
directories: [],
2018-11-05 02:28:32 +08:00
media: [p],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.search('ortm', SearchTypes.keyword))).to.deep.equal(Utils.clone(<SearchResultDTO>{
2018-03-31 03:30:30 +08:00
searchText: 'ortm',
2018-01-31 09:01:16 +08:00
searchType: SearchTypes.keyword,
directories: [],
2019-01-27 07:03:40 +08:00
media: [p2, p_faceLess],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.search('ortm', SearchTypes.keyword))).to.deep.equal(Utils.clone(<SearchResultDTO>{
2018-03-31 03:30:30 +08:00
searchText: 'ortm',
2018-01-31 09:01:16 +08:00
searchType: SearchTypes.keyword,
directories: [],
2019-01-27 07:03:40 +08:00
media: [p2, p_faceLess],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.search('wa', SearchTypes.keyword))).to.deep.equal(Utils.clone(<SearchResultDTO>{
2018-03-31 03:30:30 +08:00
searchText: 'wa',
2018-01-31 09:01:16 +08:00
searchType: SearchTypes.keyword,
directories: [dir],
2019-01-27 07:03:40 +08:00
media: [p, p2, p_faceLess],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-12-06 02:15:24 +08:00
expect(Utils.clone(await sm.search('han', SearchTypes.photo))).to.deep.equal(Utils.clone(<SearchResultDTO>{
searchText: 'han',
searchType: SearchTypes.photo,
directories: [],
media: [p],
metaFile: [],
resultOverflow: false
}));
2019-01-27 07:03:40 +08:00
expect(Utils.clone(await sm.search('sw', SearchTypes.video))).to.deep.equal(Utils.clone(<SearchResultDTO>{
searchText: 'sw',
searchType: SearchTypes.video,
directories: [],
media: [v],
metaFile: [],
resultOverflow: false
}));
2018-12-06 02:15:24 +08:00
expect(Utils.clone(await sm.search('han', SearchTypes.keyword))).to.deep.equal(Utils.clone(<SearchResultDTO>{
searchText: 'han',
searchType: SearchTypes.keyword,
directories: [],
media: [],
metaFile: [],
resultOverflow: false
}));
2019-01-27 07:03:40 +08:00
expect(Utils.clone(await sm.search('Boba', SearchTypes.person))).to.deep.equal(Utils.clone(<SearchResultDTO>{
searchText: 'Boba',
searchType: SearchTypes.person,
directories: [],
media: [p],
metaFile: [],
resultOverflow: false
}));
2018-01-31 09:01:16 +08:00
});
it('should instant search', async () => {
2018-11-02 18:22:18 +08:00
const sm = new SearchManager();
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.instantSearch('sw'))).to.deep.equal(Utils.clone({
2018-03-31 03:30:30 +08:00
searchText: 'sw',
2018-01-31 09:01:16 +08:00
directories: [],
2018-11-26 07:26:29 +08:00
media: [p, p2, v],
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.instantSearch('Tatooine'))).to.deep.equal(Utils.clone({
2018-03-31 03:30:30 +08:00
searchText: 'Tatooine',
2018-01-31 09:01:16 +08:00
directories: [],
2018-11-18 05:46:34 +08:00
media: [p],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.instantSearch('ortm'))).to.deep.equal(Utils.clone({
2018-03-31 03:30:30 +08:00
searchText: 'ortm',
2018-01-31 09:01:16 +08:00
directories: [],
2019-01-27 07:03:40 +08:00
media: [p2, p_faceLess],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-01-31 09:01:16 +08:00
2018-11-18 05:46:34 +08:00
expect(Utils.clone(await sm.instantSearch('wa'))).to.deep.equal(Utils.clone({
2018-03-31 03:30:30 +08:00
searchText: 'wa',
2018-01-31 09:01:16 +08:00
directories: [dir],
2019-01-27 07:03:40 +08:00
media: [p, p2, p_faceLess],
2018-11-26 07:26:29 +08:00
metaFile: [],
2018-01-31 09:01:16 +08:00
resultOverflow: false
2018-11-18 05:46:34 +08:00
}));
2018-12-06 02:15:24 +08:00
expect(Utils.clone(await sm.instantSearch('han'))).to.deep.equal(Utils.clone({
searchText: 'han',
directories: [],
media: [p],
metaFile: [],
resultOverflow: false
}));
2019-01-27 07:03:40 +08:00
expect(Utils.clone(await sm.instantSearch('Boba'))).to.deep.equal(Utils.clone({
searchText: 'Boba',
directories: [],
media: [p],
metaFile: [],
resultOverflow: false
}));
2018-01-31 09:01:16 +08:00
});
});