query_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. suite('lunr.Query', function () {
  2. var allFields = ['title', 'body']
  3. suite('#term', function () {
  4. setup(function () {
  5. this.query = new lunr.Query (allFields)
  6. })
  7. suite('single string term', function () {
  8. setup(function () {
  9. this.query.term('foo')
  10. })
  11. test('adds a single clause', function () {
  12. assert.equal(this.query.clauses.length, 1)
  13. })
  14. test('clause has the correct term', function () {
  15. assert.equal(this.query.clauses[0].term, 'foo')
  16. })
  17. })
  18. suite('single token term', function () {
  19. setup(function () {
  20. this.query.term(new lunr.Token('foo'))
  21. })
  22. test('adds a single clause', function () {
  23. assert.equal(this.query.clauses.length, 1)
  24. })
  25. test('clause has the correct term', function () {
  26. assert.equal(this.query.clauses[0].term, 'foo')
  27. })
  28. })
  29. suite('multiple string terms', function () {
  30. setup(function () {
  31. this.query.term(['foo', 'bar'])
  32. })
  33. test('adds a single clause', function () {
  34. assert.equal(this.query.clauses.length, 2)
  35. })
  36. test('clause has the correct term', function () {
  37. var terms = this.query.clauses.map(function (c) { return c.term })
  38. assert.sameMembers(terms, ['foo', 'bar'])
  39. })
  40. })
  41. suite('multiple string terms with options', function () {
  42. setup(function () {
  43. this.query.term(['foo', 'bar'], { usePipeline: false })
  44. })
  45. test('clause has the correct term', function () {
  46. var terms = this.query.clauses.map(function (c) { return c.term })
  47. assert.sameMembers(terms, ['foo', 'bar'])
  48. })
  49. })
  50. suite('multiple token terms', function () {
  51. setup(function () {
  52. this.query.term(lunr.tokenizer('foo bar'))
  53. })
  54. test('adds a single clause', function () {
  55. assert.equal(this.query.clauses.length, 2)
  56. })
  57. test('clause has the correct term', function () {
  58. var terms = this.query.clauses.map(function (c) { return c.term })
  59. assert.sameMembers(terms, ['foo', 'bar'])
  60. })
  61. })
  62. })
  63. suite('#clause', function () {
  64. setup(function () {
  65. this.query = new lunr.Query (allFields)
  66. })
  67. suite('defaults', function () {
  68. setup(function () {
  69. this.query.clause({term: 'foo'})
  70. this.clause = this.query.clauses[0]
  71. })
  72. test('fields', function () {
  73. assert.sameMembers(this.clause.fields, allFields)
  74. })
  75. test('boost', function () {
  76. assert.equal(this.clause.boost, 1)
  77. })
  78. test('usePipeline', function () {
  79. assert.isTrue(this.clause.usePipeline)
  80. })
  81. })
  82. suite('specified', function () {
  83. setup(function () {
  84. this.query.clause({
  85. term: 'foo',
  86. boost: 10,
  87. fields: ['title'],
  88. usePipeline: false
  89. })
  90. this.clause = this.query.clauses[0]
  91. })
  92. test('fields', function () {
  93. assert.sameMembers(this.clause.fields, ['title'])
  94. })
  95. test('boost', function () {
  96. assert.equal(this.clause.boost, 10)
  97. })
  98. test('usePipeline', function () {
  99. assert.isFalse(this.clause.usePipeline)
  100. })
  101. })
  102. suite('wildcards', function () {
  103. suite('none', function () {
  104. setup(function () {
  105. this.query.clause({
  106. term: 'foo',
  107. wildcard: lunr.Query.wildcard.NONE
  108. })
  109. this.clause = this.query.clauses[0]
  110. })
  111. test('no wildcard', function () {
  112. assert.equal(this.clause.term, 'foo')
  113. })
  114. })
  115. suite('leading', function () {
  116. setup(function () {
  117. this.query.clause({
  118. term: 'foo',
  119. wildcard: lunr.Query.wildcard.LEADING
  120. })
  121. this.clause = this.query.clauses[0]
  122. })
  123. test('adds wildcard', function () {
  124. assert.equal(this.clause.term, '*foo')
  125. })
  126. })
  127. suite('trailing', function () {
  128. setup(function () {
  129. this.query.clause({
  130. term: 'foo',
  131. wildcard: lunr.Query.wildcard.TRAILING
  132. })
  133. this.clause = this.query.clauses[0]
  134. })
  135. test('adds wildcard', function () {
  136. assert.equal(this.clause.term, 'foo*')
  137. })
  138. })
  139. suite('leading and trailing', function () {
  140. setup(function () {
  141. this.query.clause({
  142. term: 'foo',
  143. wildcard: lunr.Query.wildcard.TRAILING | lunr.Query.wildcard.LEADING
  144. })
  145. this.clause = this.query.clauses[0]
  146. })
  147. test('adds wildcards', function () {
  148. assert.equal(this.clause.term, '*foo*')
  149. })
  150. })
  151. suite('existing', function () {
  152. setup(function () {
  153. this.query.clause({
  154. term: '*foo*',
  155. wildcard: lunr.Query.wildcard.TRAILING | lunr.Query.wildcard.LEADING
  156. })
  157. this.clause = this.query.clauses[0]
  158. })
  159. test('no additional wildcards', function () {
  160. assert.equal(this.clause.term, '*foo*')
  161. })
  162. })
  163. })
  164. })
  165. suite('#isNegated', function () {
  166. setup(function () {
  167. this.query = new lunr.Query (allFields)
  168. })
  169. suite('all prohibited', function () {
  170. setup(function () {
  171. this.query.term('foo', { presence: lunr.Query.presence.PROHIBITED })
  172. this.query.term('bar', { presence: lunr.Query.presence.PROHIBITED })
  173. })
  174. test('is negated', function () {
  175. assert.isTrue(this.query.isNegated())
  176. })
  177. })
  178. suite('some prohibited', function () {
  179. setup(function () {
  180. this.query.term('foo', { presence: lunr.Query.presence.PROHIBITED })
  181. this.query.term('bar', { presence: lunr.Query.presence.REQUIRED })
  182. })
  183. test('is negated', function () {
  184. assert.isFalse(this.query.isNegated())
  185. })
  186. })
  187. suite('none prohibited', function () {
  188. setup(function () {
  189. this.query.term('foo', { presence: lunr.Query.presence.OPTIONAL })
  190. this.query.term('bar', { presence: lunr.Query.presence.REQUIRED })
  191. })
  192. test('is negated', function () {
  193. assert.isFalse(this.query.isNegated())
  194. })
  195. })
  196. })
  197. })