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

206 lines
6.6 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {expect} from 'chai';
import * as fs from 'fs';
import * as path from 'path';
import {Config} from '../../../../../common/config/private/Config';
import {DatabaseType} from '../../../../../common/config/private/IPrivateConfig';
import {SQLConnection} from '../../../../../backend/model/sql/SQLConnection';
2018-01-31 09:01:16 +08:00
import {
CameraMetadataEntity,
GPSMetadataEntity,
PhotoEntity,
PhotoMetadataEntity,
PositionMetaDataEntity
2018-03-31 03:30:30 +08:00
} from '../../../../../backend/model/sql/enitites/PhotoEntity';
import {SearchManager} from '../../../../../backend/model/sql/SearchManager';
import {AutoCompleteItem, SearchTypes} from '../../../../../common/entities/AutoCompleteItem';
import {SearchResultDTO} from '../../../../../common/entities/SearchResultDTO';
import {DirectoryEntity} from '../../../../../backend/model/sql/enitites/DirectoryEntity';
2018-11-18 05:46:34 +08:00
import {Utils} from '../../../../../common/Utils';
import {TestHelper} from './TestHelper';
2018-11-24 21:51:47 +08:00
import {VideoEntity} from '../../../../../backend/model/sql/enitites/VideoEntity';
2018-01-31 09:01:16 +08:00
describe('SearchManager', () => {
2018-03-31 03:30:30 +08:00
const tempDir = path.join(__dirname, '../../tmp');
const dbPath = path.join(tempDir, 'test.db');
2018-01-31 09:01:16 +08:00
const dir = TestHelper.getDirectoryEntry();
const p = TestHelper.getPhotoEntry1(dir);
const p2 = TestHelper.getPhotoEntry2(dir);
2018-11-24 21:51:47 +08:00
const v = TestHelper.getVideoEntry1(dir);
2018-01-31 09:01:16 +08:00
const setUpSqlDB = async () => {
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
Config.Server.database.type = DatabaseType.sqlite;
Config.Server.database.sqlite.storage = dbPath;
const conn = await SQLConnection.getConnection();
const pr = conn.getRepository(PhotoEntity);
await conn.getRepository(DirectoryEntity).save(p.directory);
await pr.save(p);
await pr.save(p2);
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();
};
2018-03-30 08:30:23 +08:00
const tearDownSqlDB = async () => {
2018-01-31 09:01:16 +08:00
await SQLConnection.close();
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
if (fs.existsSync(tempDir)) {
fs.rmdirSync(tempDir);
}
};
beforeEach(async () => {
await setUpSqlDB();
});
afterEach(async () => {
2018-03-30 08:30:23 +08:00
await tearDownSqlDB();
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) => {
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)]);
expect((await sm.autocomplete('a')).sort(cmp)).eql([
new AutoCompleteItem('Boba Fett', SearchTypes.keyword),
new AutoCompleteItem('star wars', SearchTypes.keyword),
new AutoCompleteItem('Anakin', SearchTypes.keyword),
new AutoCompleteItem('death star', SearchTypes.keyword),
new AutoCompleteItem('Padmé Amidala', SearchTypes.keyword),
new AutoCompleteItem('Natalie Portman', SearchTypes.keyword),
new AutoCompleteItem('Kamino', SearchTypes.position),
new AutoCompleteItem('Tatooine', SearchTypes.position),
new AutoCompleteItem('wars dir', SearchTypes.directory),
new AutoCompleteItem('Research City', SearchTypes.position)].sort(cmp));
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
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: [],
2018-11-05 02:28:32 +08:00
media: [p2],
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: [],
2018-11-05 02:28:32 +08:00
media: [p2],
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],
2018-11-05 02:28:32 +08:00
media: [p, p2],
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
});
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: [],
2018-11-18 05:46:34 +08:00
media: [p2],
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: [],
2018-11-18 05:46:34 +08:00
media: [p2],
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],
2018-11-18 05:46:34 +08:00
media: [p, p2],
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
});
});