search-service.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import mongoose from 'mongoose';
  2. import SearchService from '~/server/service/search';
  3. import NamedQuery from '~/server/models/named-query';
  4. const { getInstance } = require('../../setup-crowi');
  5. describe('SearchService test', () => {
  6. let crowi;
  7. let searchService;
  8. const DEFAULT = 'FullTextSearch';
  9. // let NamedQuery;
  10. let dummyAliasOf;
  11. let namedQuery1;
  12. let namedQuery2;
  13. const dummyFullTextSearchDelegator = {
  14. search() {
  15. return;
  16. },
  17. };
  18. beforeAll(async() => {
  19. crowi = await getInstance();
  20. searchService = new SearchService(crowi);
  21. searchService.nqDelegators = {
  22. ...searchService.nqDelegators,
  23. [DEFAULT]: dummyFullTextSearchDelegator, // override with dummy full-text search delegator
  24. };
  25. dummyAliasOf = 'match -notmatch "phrase" -"notphrase" prefix:/pre1 -prefix:/pre2 tag:Tag1 -tag:Tag2';
  26. await NamedQuery.insertMany([
  27. { name: 'named_query2', aliasOf: dummyAliasOf },
  28. ]);
  29. namedQuery1 = await NamedQuery.findOne({ name: 'named_query1' });
  30. namedQuery2 = await NamedQuery.findOne({ name: 'named_query2' });
  31. });
  32. describe('parseQueryString()', () => {
  33. test('should parse queryString', async() => {
  34. const queryString = 'match -notmatch "phrase" -"notphrase" prefix:/pre1 -prefix:/pre2 tag:Tag1 -tag:Tag2';
  35. const terms = await searchService.parseQueryString(queryString);
  36. const expected = { // QueryTerms
  37. match: ['match'],
  38. not_match: ['notmatch'],
  39. phrase: ['"phrase"'],
  40. not_phrase: ['"notphrase"'],
  41. prefix: ['/pre1'],
  42. not_prefix: ['/pre2'],
  43. tag: ['Tag1'],
  44. not_tag: ['Tag2'],
  45. };
  46. expect(terms).toStrictEqual(expected);
  47. });
  48. });
  49. describe('parseSearchQuery()', () => {
  50. test('should return result with expanded aliasOf value', async() => {
  51. const queryString = '[nq:named_query2]';
  52. const parsedQuery = await searchService.parseSearchQuery(queryString);
  53. const expected = {
  54. queryString: dummyAliasOf,
  55. terms: {
  56. match: ['match'],
  57. not_match: ['notmatch'],
  58. phrase: ['"phrase"'],
  59. not_phrase: ['"notphrase"'],
  60. prefix: ['/pre1'],
  61. not_prefix: ['/pre2'],
  62. tag: ['Tag1'],
  63. not_tag: ['Tag2'],
  64. },
  65. };
  66. expect(parsedQuery).toStrictEqual(expected);
  67. });
  68. });
  69. describe('resolve()', () => {
  70. test('should resolve as full-text search delegator', async() => {
  71. const parsedQuery = {
  72. queryString: dummyAliasOf,
  73. terms: {
  74. match: ['match'],
  75. not_match: ['notmatch'],
  76. phrase: ['"phrase"'],
  77. not_phrase: ['"notphrase"'],
  78. prefix: ['/pre1'],
  79. not_prefix: ['/pre2'],
  80. tag: ['Tag1'],
  81. not_tag: ['Tag2'],
  82. },
  83. };
  84. const [delegator, data] = await searchService.resolve(parsedQuery);
  85. const expectedData = parsedQuery;
  86. expect(data).toStrictEqual(expectedData);
  87. expect(typeof delegator.search).toBe('function');
  88. });
  89. });
  90. });