import {expect} from 'chai'; import { ANDSearchQuery, DistanceSearch, FromDateSearch, MaxRatingSearch, MaxResolutionSearch, MinRatingSearch, MinResolutionSearch, OrientationSearch, ORSearchQuery, SearchQueryDTO, SearchQueryTypes, SomeOfSearchQuery, TextSearch, TextSearchQueryMatchTypes, ToDateSearch } from '../../../src/common/entities/SearchQueryDTO'; import {QueryKeywords, SearchQueryParser} from '../../../src/common/SearchQueryParser'; describe('SearchQueryParser', () => { const keywords: QueryKeywords = { NSomeOf: '-of', and: 'and', caption: 'caption', directory: 'directory', file_name: 'file-name', from: 'from', keyword: 'keyword', landscape: 'landscape', maxRating: 'max-rating', maxResolution: 'max-resolution', minRating: 'min-rating', minResolution: 'min-resolution', or: 'or', orientation: 'orientation', person: 'person', portrait: 'portrait', position: 'position', someOf: 'some-of', to: 'to', kmFrom: 'km-from' }; const check = (query: SearchQueryDTO) => { const parser = new SearchQueryParser(keywords); expect(parser.parse(parser.stringify(query))).to.deep.equals(query, parser.stringify(query)); }; describe('should serialize and deserialize', () => { it('Text search', () => { check({type: SearchQueryTypes.any_text, text: 'test'}); check({type: SearchQueryTypes.person, text: 'person_test'}); check({type: SearchQueryTypes.directory, text: 'directory'}); check({type: SearchQueryTypes.keyword, text: 'big boom'}); check({type: SearchQueryTypes.caption, text: 'caption'}); check({type: SearchQueryTypes.file_name, text: 'filename'}); check({type: SearchQueryTypes.position, text: 'New York'}); check({ type: SearchQueryTypes.position, matchType: TextSearchQueryMatchTypes.exact_match, text: 'New York' }); }); it('Date search', () => { check({type: SearchQueryTypes.from_date, value: (new Date(2020, 1, 1)).getTime()}); check({type: SearchQueryTypes.to_date, value: (new Date(2020, 1, 2)).getTime()}); }); it('Rating search', () => { check({type: SearchQueryTypes.min_rating, value: 10}); check({type: SearchQueryTypes.max_rating, value: 1}); }); it('Resolution search', () => { check({type: SearchQueryTypes.min_resolution, value: 10}); check({type: SearchQueryTypes.max_resolution, value: 5}); }); it('Distance search', () => { check({type: SearchQueryTypes.distance, distance: 10, from: {text: 'New York'}}); }); it('OrientationSearch search', () => { check({type: SearchQueryTypes.orientation, landscape: true}); check({type: SearchQueryTypes.orientation, landscape: false}); }); it('And search', () => { check({ type: SearchQueryTypes.AND, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.position, text: 'New York'} ] }); check({ type: SearchQueryTypes.AND, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, { type: SearchQueryTypes.position, matchType: TextSearchQueryMatchTypes.exact_match, text: 'New York' } ] }); check({ type: SearchQueryTypes.AND, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, { type: SearchQueryTypes.AND, list: [ {type: SearchQueryTypes.caption, text: 'caption'}, {type: SearchQueryTypes.position, text: 'New York'} ] } ] }); check({ type: SearchQueryTypes.AND, list: [ { type: SearchQueryTypes.SOME_OF, min: 2, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.position, text: 'New York'}, {type: SearchQueryTypes.caption, text: 'caption test'} ] }, {type: SearchQueryTypes.position, text: 'New York'} ] }); }); it('Or search', () => { check({ type: SearchQueryTypes.OR, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.position, text: 'New York'} ] }); check({ type: SearchQueryTypes.OR, list: [ { type: SearchQueryTypes.OR, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.person, text: 'person_test'} ] }, {type: SearchQueryTypes.position, text: 'New York'} ] }); }); it('Some of search', () => { check({ type: SearchQueryTypes.SOME_OF, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.position, text: 'New York'} ] }); check({ type: SearchQueryTypes.SOME_OF, list: [ { type: SearchQueryTypes.keyword, matchType: TextSearchQueryMatchTypes.exact_match, text: 'big boom' }, { type: SearchQueryTypes.position, matchType: TextSearchQueryMatchTypes.exact_match, text: 'New York' }, ] }); check({ type: SearchQueryTypes.SOME_OF, min: 2, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.position, text: 'New York'}, {type: SearchQueryTypes.caption, text: 'caption test'} ] }); check({ type: SearchQueryTypes.SOME_OF, min: 2, list: [ {type: SearchQueryTypes.keyword, text: 'big boom'}, {type: SearchQueryTypes.person, text: 'person_test'}, { type: SearchQueryTypes.AND, list: [ {type: SearchQueryTypes.caption, text: 'caption'}, {type: SearchQueryTypes.position, text: 'New York'} ] } ] }); }); }); });