token_store_test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. module('lunr.TokenStore')
  2. test('adding a token to the store', function () {
  3. var store = new lunr.TokenStore,
  4. doc = { ref: 123, tf: 1 },
  5. token = 'foo'
  6. store.add(token, doc)
  7. ok(store.root['f']['o']['o']['docs'][123] === doc)
  8. equal(store.length, 1)
  9. })
  10. test('adding another document to the token', function () {
  11. var store = new lunr.TokenStore,
  12. doc1 = { ref: 123, tf: 1 },
  13. doc2 = { ref: 456, tf: 1 },
  14. token = 'foo'
  15. store.add(token, doc1)
  16. store.add(token, doc2)
  17. ok(store.root['f']['o']['o']['docs'][123] === doc1)
  18. ok(store.root['f']['o']['o']['docs'][456] === doc2)
  19. })
  20. test('checking if a token exists in the store', function () {
  21. var store = new lunr.TokenStore,
  22. doc = { ref: 123, tf: 1 },
  23. token = 'foo'
  24. store.add(token, doc)
  25. ok(store.has(token))
  26. })
  27. test('checking if a token does not exist in the store', function () {
  28. var store = new lunr.TokenStore,
  29. doc = { ref: 123, tf: 1 },
  30. token = 'foo'
  31. ok(!store.has('bar'))
  32. store.add(token, doc)
  33. ok(!store.has('bar'))
  34. })
  35. test('retrieving items from the store', function () {
  36. var store = new lunr.TokenStore,
  37. doc = { ref: 123, tf: 1 },
  38. token = 'foo'
  39. store.add(token, doc)
  40. deepEqual(store.get(token), {
  41. '123': doc
  42. })
  43. deepEqual(store.get(''), {})
  44. })
  45. test('retrieving items that do not exist in the store', function () {
  46. var store = new lunr.TokenStore
  47. deepEqual(store.get('foo'), {})
  48. })
  49. test('counting items in the store', function () {
  50. var store = new lunr.TokenStore,
  51. doc1 = { ref: 123, tf: 1 },
  52. doc2 = { ref: 456, tf: 1 },
  53. doc3 = { ref: 789, tf: 1 }
  54. store.add('foo', doc1)
  55. store.add('foo', doc2)
  56. store.add('bar', doc3)
  57. equal(store.count('foo'), 2)
  58. equal(store.count('bar'), 1)
  59. equal(store.count('baz'), 0)
  60. })
  61. test('removing a document from the token store', function () {
  62. var store = new lunr.TokenStore,
  63. doc = { ref: 123, tf: 1 }
  64. deepEqual(store.get('foo'), {})
  65. store.add('foo', doc)
  66. deepEqual(store.get('foo'), {
  67. '123': doc
  68. })
  69. store.remove('foo', 123)
  70. deepEqual(store.get('foo'), {})
  71. })
  72. test('removing a document that is not in the store', function () {
  73. var store = new lunr.TokenStore,
  74. doc1 = { ref: 123, tf: 1 },
  75. doc2 = { ref: 567, tf: 1 }
  76. store.add('foo', doc1)
  77. store.add('bar', doc2)
  78. store.remove('foo', 456)
  79. deepEqual(store.get('foo'), { 123: doc1 })
  80. })
  81. test('removing a document from a key that does not exist', function () {
  82. var store = new lunr.TokenStore
  83. store.remove('foo', 123)
  84. ok(!store.has('foo'))
  85. })
  86. test('expand a token into all descendent tokens', function () {
  87. var store = new lunr.TokenStore,
  88. doc = { ref: 123, tf: 1 }
  89. store.add('hell', doc)
  90. store.add('hello', doc)
  91. store.add('help', doc)
  92. store.add('held', doc)
  93. store.add('foo', doc)
  94. store.add('bar', doc)
  95. var tokens = store.expand('hel')
  96. deepEqual(tokens, ['hell', 'hello', 'help', 'held'])
  97. })
  98. test('serialisation', function () {
  99. var store = new lunr.TokenStore
  100. deepEqual(store.toJSON(), { root: { docs: {} }, length: 0 })
  101. store.add('foo', { ref: 123, tf: 1 })
  102. deepEqual(store.toJSON(),
  103. {
  104. root: {
  105. docs: {},
  106. f: {
  107. docs: {},
  108. o: {
  109. docs: {},
  110. o: {
  111. docs: { 123: { ref: 123, tf: 1 } }
  112. }
  113. }
  114. }
  115. },
  116. length: 1
  117. }
  118. )
  119. })
  120. test('loading a serialised story', function () {
  121. var serialisedData = {
  122. root: {
  123. docs: {},
  124. f: {
  125. docs: {},
  126. o: {
  127. docs: {},
  128. o: {
  129. docs: { 123: { ref: 123, tf: 1 } }
  130. }
  131. }
  132. }
  133. },
  134. length: 1
  135. }
  136. var store = lunr.TokenStore.load(serialisedData),
  137. documents = store.get('foo')
  138. equal(store.length, 1)
  139. deepEqual(documents, { 123: { ref: 123, tf: 1 }})
  140. })