Document.js 3.3 KB

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