Document.js 5.1 KB

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