Document_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  3. var assert = require("assert"),
  4. Document = require("../../../lib/pipeline/Document"),
  5. FieldPath = require("../../../lib/pipeline/FieldPath");
  6. exports.Document = {
  7. //SKIPPED: Create -- ours is a static class so we have no constructor
  8. //SKIPPED: CreateFromBsonObj -- no ctor again, would just use JSON.parse
  9. //SKIPPED: AddField - no need because we use: obj[key] = val
  10. //SKIPPED: GetValue - no need because we use: val = obj[key]
  11. //SKIPPED: SetField - no need because we usually use: obj[key] = val though setNestedField *does* is implemented now
  12. ".compare()": {
  13. "should work": function testCompare() { //jshint latedef:false
  14. assertComparison(0, {}, {});
  15. assertComparison(0, {a:1}, {a:1});
  16. assertComparison(-1, {}, {a:1});
  17. assertComparison(-1, {a:1}, {c:1});
  18. assertComparison(0, {a:1,r:2}, {a:1,r:2});
  19. assertComparison(-1, {a:1}, {a:1,r:2});
  20. assertComparison(0, {a:2}, {a:2});
  21. assertComparison(-1, {a:1}, {a:2});
  22. assertComparison(-1, {a:1,b:1}, {a:1,b:2});
  23. // numbers sort before strings
  24. assertComparison(-1, {a:1}, {a:"foo"});
  25. // helpers for the above
  26. function cmp(a, b) {
  27. var result = Document.compare(a, b);
  28. return result < 0 ? -1 : // sign
  29. result > 0 ? 1 :
  30. 0;
  31. }
  32. function assertComparison(expectedResult, a, b) {
  33. assert.strictEqual(expectedResult, cmp(a, b));
  34. assert.strictEqual(-expectedResult, cmp(b, a));
  35. if (expectedResult === 0) {
  36. var hash = JSON.stringify; // approximating real hash
  37. assert.strictEqual(hash(a), hash(b));
  38. }
  39. }
  40. },
  41. "should work for a null": function testCompareNamedNull(){
  42. var obj1 = {z:null},
  43. obj2 = {a:1};
  44. //// Comparsion with type precedence.
  45. // assert(obj1.woCompare(obj2) < 0); //NOTE: probably will not need this
  46. // Comparison with field name precedence.
  47. assert(Document.compare(obj1, obj2) > 0);
  48. },
  49. "should return 0 if Documents are identical": function() {
  50. var lDocument = {prop1: 0},
  51. rDocument = {prop1: 0},
  52. result = Document.compare(lDocument, rDocument);
  53. assert.equal(result, 0);
  54. },
  55. "should return -1 if left Document is shorter": function() {
  56. var lDocument = {prop1: 0},
  57. rDocument = {prop1: 0, prop2: 0},
  58. result = Document.compare(lDocument, rDocument);
  59. assert.equal(result, -1);
  60. },
  61. "should return 1 if right Document is shorter": function() {
  62. var lDocument = {prop1: 0, prop2: 0},
  63. rDocument = {prop1: 0},
  64. result = Document.compare(lDocument, rDocument);
  65. assert.equal(result, 1);
  66. },
  67. "should return nameCmp result -1 if left Document field value is less": function() {
  68. var lDocument = {prop1: 0},
  69. rDocument = {prop1: 1},
  70. result = Document.compare(lDocument, rDocument);
  71. assert.equal(result, -1);
  72. },
  73. "should return nameCmp result 1 if right Document field value is less": function() {
  74. var lDocument = {prop1: 1},
  75. rDocument = {prop1: 0},
  76. result = Document.compare(lDocument, rDocument);
  77. assert.equal(result, 1);
  78. },
  79. },
  80. ".clone()": {
  81. "should shallow clone a single field document": function testClone() {
  82. var doc = {a:{b:1}},
  83. clone = doc;
  84. //NOTE: silly since we use static helpers but here for consistency
  85. // Check equality
  86. assert.strictEqual(clone, doc);
  87. // Check pointer equality of sub document
  88. assert.strictEqual(clone.a, doc.a);
  89. // Change field in clone and ensure the original document's field is unchanged.
  90. clone = Document.clone(doc);
  91. clone.a = 2;
  92. assert.strictEqual(Document.getNestedField(doc, new FieldPath("a.b")), 1);
  93. // setNestedField and ensure the original document is unchanged.
  94. clone = Document.cloneDeep(doc);
  95. assert.strictEqual(Document.getNestedField(doc, "a.b"), 1);
  96. Document.setNestedField(clone, "a.b", 2);
  97. assert.strictEqual(Document.getNestedField(doc, "a.b"), 1);
  98. assert.strictEqual(Document.getNestedField(clone, "a.b"), 2);
  99. assert.deepEqual(doc, {a:{b:1}});
  100. assert.deepEqual(clone, {a:{b:2}});
  101. },
  102. "should shallow clone a multi field document": function testCloneMultipleFields() {
  103. var doc = {a:1,b:["ra",4],c:{z:1},d:"lal"},
  104. clone = Document.clone(doc);
  105. assert.deepEqual(doc, clone);
  106. },
  107. },
  108. //SKIPPED: FieldIteratorEmpty
  109. //SKIPPED: FieldIteratorSingle
  110. //SKIPPED: FieldIteratorMultiple
  111. ".toJson()": {
  112. "should convert to JSON Object": function() {
  113. var doc = {prop1:0};
  114. assert.deepEqual(Document.toJson(doc), {prop1:0});
  115. },
  116. },
  117. "serialize and deserialize for sorter": {
  118. "should return a string": function serializeDocument() {
  119. var doc = {prop1:1},
  120. res = Document.serializeForSorter(doc);
  121. assert.equal(res, "{\"prop1\":1}");
  122. },
  123. "should return a Document": function deserializeToDocument() {
  124. var str = "{\"prop1\":1}",
  125. doc = {prop1:1},
  126. res = Document.deserializeForSorter(str);
  127. assert.deepEqual(res, doc);
  128. },
  129. },
  130. };