Document.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var assert = require("assert"),
  3. Document = require("../../../lib/pipeline/Document");
  4. // Mocha one-liner to make these tests self-hosted
  5. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  6. exports.Document = {
  7. "Json conversion": {
  8. "convert to Json": function toJson() {
  9. var aDocument = {"prop1":0},
  10. result = Document.toJson(aDocument);
  11. assert.equal(result, '{"prop1":0}');
  12. },
  13. "convert to Json with metadata": function toJsonWithMetaData() {
  14. var aDocument = {"prop1": 0,"metadata":"stuff"},
  15. result = Document.toJsonWithMetaData(aDocument);
  16. assert.equal(result, '{"prop1":0,"metadata":"stuff"}');
  17. },
  18. "convert from Json": function fromJsonWithMetaData() {
  19. var aDocumentString = '{\"prop1\":0,\"metadata\":1}',
  20. jsonDocument = {"prop1":0,"metadata":1},
  21. result = Document.fromJsonWithMetaData(aDocumentString);
  22. assert.deepEqual(result, jsonDocument);
  23. },
  24. },
  25. "compare 2 Documents": {
  26. "should return 0 if Documents are identical": function compareDocumentsIdentical() {
  27. var lDocument = {"prop1": 0},
  28. rDocument = {"prop1": 0},
  29. result = Document.compare(lDocument, rDocument);
  30. assert.equal(result, 0);
  31. },
  32. "should return -1 if left Document is shorter": function compareLeftDocumentShorter() {
  33. var lDocument = {"prop1": 0},
  34. rDocument = {"prop1": 0, "prop2": 0},
  35. result = Document.compare(lDocument, rDocument);
  36. assert.equal(result, -1);
  37. },
  38. "should return 1 if right Document is shorter": function compareRightDocumentShorter() {
  39. var lDocument = {"prop1": 0, "prop2": 0},
  40. rDocument = {"prop1": 0},
  41. result = Document.compare(lDocument, rDocument);
  42. assert.equal(result, 1);
  43. },
  44. "should return nameCmp result -1 if left Document field value is less": function compareLeftDocumentFieldLess() {
  45. var lDocument = {"prop1": 0},
  46. rDocument = {"prop1": 1},
  47. result = Document.compare(lDocument, rDocument);
  48. assert.equal(result, -1);
  49. },
  50. "should return nameCmp result 1 if right Document field value is less": function compareRightDocumentFieldLess() {
  51. var lDocument = {"prop1": 1},
  52. rDocument = {"prop1": 0},
  53. result = Document.compare(lDocument, rDocument);
  54. assert.equal(result, 1);
  55. },
  56. },
  57. "clone a Document": {
  58. "should return same field and value from cloned Document ": function clonedDocumentSingleFieldValue() {
  59. var doc = {"prop1": 17},
  60. res = Document.clone(doc);
  61. assert(res instanceof Object);
  62. assert.deepEqual(doc, res);
  63. assert.equal(res.prop1, 17);
  64. },
  65. "should return same fields and values from cloned Document ": function clonedDocumentMultiFieldValue() {
  66. var doc = {"prop1": 17, "prop2": "a string"},
  67. res = Document.clone(doc);
  68. assert.deepEqual(doc, res);
  69. assert(res instanceof Object);
  70. assert.equal(res.prop1, 17);
  71. assert.equal(res.prop2, "a string");
  72. },
  73. },
  74. "serialize and deserialize for sorter": {
  75. "should return a string": function serializeDocument() {
  76. var doc = {"prop1":1},
  77. res = Document.serializeForSorter(doc);
  78. assert.equal(res, "{\"prop1\":1}");
  79. },
  80. "should return a Document": function deserializeToDocument() {
  81. var str = "{\"prop1\":1}",
  82. doc = {"prop1":1},
  83. res = Document.deserializeForSorter(str);
  84. assert.deepEqual(res, doc);
  85. },
  86. },
  87. };