serialisation_test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. module('serialisation', {
  2. setup: function () {
  3. this.corpus = [{
  4. id: 'a',
  5. title: 'Mr. Green kills Colonel Mustard',
  6. body: 'Mr. Green killed Colonel Mustard in the study with the candlestick. Mr. Green is not a very nice fellow.'
  7. },{
  8. id: 'b',
  9. title: 'Plumb waters plant',
  10. body: 'Professor Plumb has a green plant in his study'
  11. },{
  12. id: 'c',
  13. title: 'Scarlett helps Professor',
  14. body: 'Miss Scarlett watered Professor Plumbs green plant while he was away from his office last week.'
  15. }]
  16. }
  17. })
  18. test('dumping and loading an index', function () {
  19. var idx = new lunr.Index
  20. idx.field('title', { boost: 10 })
  21. idx.field('body')
  22. this.corpus.forEach(function (doc) { idx.add(doc) })
  23. var dumpedIdx = JSON.stringify(idx),
  24. clonedIdx = lunr.Index.load(JSON.parse(dumpedIdx))
  25. deepEqual(idx.search('green plant'), clonedIdx.search('green plant'))
  26. })
  27. test('dumping and loading an index with a populated pipeline', function () {
  28. var idx = lunr(function () {
  29. this.field('title', { boost: 10 })
  30. this.field('body')
  31. })
  32. this.corpus.forEach(function (doc) { idx.add(doc) })
  33. var dumpedIdx = JSON.stringify(idx),
  34. clonedIdx = lunr.Index.load(JSON.parse(dumpedIdx))
  35. deepEqual(idx.pipeline._stack, clonedIdx.pipeline._stack)
  36. deepEqual(idx.search('water'), clonedIdx.search('water'))
  37. })
  38. test('dumping and loading an index with a custom tokenizer', function () {
  39. var customTokenizer = function () {},
  40. idx = lunr()
  41. lunr.tokenizer.registerFunction(customTokenizer, 'custom')
  42. idx.tokenizer(customTokenizer)
  43. var dumpedIdx = JSON.stringify(idx),
  44. clonedIndex = lunr.Index.load(JSON.parse(dumpedIdx))
  45. equal(clonedIndex.tokenizerFn, idx.tokenizerFn)
  46. })