diff options
Diffstat (limited to 'jstests/aggregation/sources/graphLookup/basic.js')
-rw-r--r-- | jstests/aggregation/sources/graphLookup/basic.js | 133 |
1 files changed, 66 insertions, 67 deletions
diff --git a/jstests/aggregation/sources/graphLookup/basic.js b/jstests/aggregation/sources/graphLookup/basic.js index c0bcb1a8a53..ef44b9b60bb 100644 --- a/jstests/aggregation/sources/graphLookup/basic.js +++ b/jstests/aggregation/sources/graphLookup/basic.js @@ -6,16 +6,16 @@ // of the stage. (function() { - "use strict"; +"use strict"; - var local = db.local; - var foreign = db.foreign; +var local = db.local; +var foreign = db.foreign; - local.drop(); - foreign.drop(); +local.drop(); +foreign.drop(); - // Ensure a $graphLookup works even if one of the involved collections doesn't exist. - const basicGraphLookup = { +// Ensure a $graphLookup works even if one of the involved collections doesn't exist. +const basicGraphLookup = { $graphLookup: { from: "foreign", startWith: "$starting", @@ -25,40 +25,39 @@ } }; - assert.eq( - local.aggregate([basicGraphLookup]).toArray().length, - 0, - "expected an empty result set for a $graphLookup with non-existent local and foreign " + - "collections"); +assert.eq(local.aggregate([basicGraphLookup]).toArray().length, + 0, + "expected an empty result set for a $graphLookup with non-existent local and foreign " + + "collections"); - assert.writeOK(foreign.insert({})); +assert.writeOK(foreign.insert({})); - assert.eq(local.aggregate([basicGraphLookup]).toArray().length, - 0, - "expected an empty result set for a $graphLookup on a non-existent local collection"); +assert.eq(local.aggregate([basicGraphLookup]).toArray().length, + 0, + "expected an empty result set for a $graphLookup on a non-existent local collection"); - local.drop(); - foreign.drop(); +local.drop(); +foreign.drop(); - assert.writeOK(local.insert({_id: 0})); +assert.writeOK(local.insert({_id: 0})); - assert.eq(local.aggregate([basicGraphLookup]).toArray(), - [{_id: 0, results: []}], - "expected $graphLookup to succeed with a non-existent foreign collection"); +assert.eq(local.aggregate([basicGraphLookup]).toArray(), + [{_id: 0, results: []}], + "expected $graphLookup to succeed with a non-existent foreign collection"); - local.drop(); - foreign.drop(); +local.drop(); +foreign.drop(); - var bulk = foreign.initializeUnorderedBulkOp(); - for (var i = 0; i < 100; i++) { - bulk.insert({_id: i, neighbors: [i - 1, i + 1]}); - } - assert.writeOK(bulk.execute()); +var bulk = foreign.initializeUnorderedBulkOp(); +for (var i = 0; i < 100; i++) { + bulk.insert({_id: i, neighbors: [i - 1, i + 1]}); +} +assert.writeOK(bulk.execute()); - assert.writeOK(local.insert({starting: 50})); +assert.writeOK(local.insert({starting: 50})); - // Perform a simple $graphLookup and ensure it retrieves every result. - var res = local +// Perform a simple $graphLookup and ensure it retrieves every result. +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -70,10 +69,10 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - // Perform a $graphLookup and ensure it respects "maxDepth". - res = local +// Perform a $graphLookup and ensure it respects "maxDepth". +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -86,11 +85,11 @@ }) .toArray()[0]; - // At depth zero, we retrieve one integer, and two for every depth thereafter. - assert.eq(res.integers.length, 11); +// At depth zero, we retrieve one integer, and two for every depth thereafter. +assert.eq(res.integers.length, 11); - // Perform a $graphLookup and ensure it properly evaluates "startWith". - res = local +// Perform a $graphLookup and ensure it properly evaluates "startWith". +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -103,11 +102,11 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 1); - assert.eq(res.integers[0]._id, 53); +assert.eq(res.integers.length, 1); +assert.eq(res.integers[0]._id, 53); - // Perform a $graphLookup and ensure it properly expands "startWith". - res = local +// Perform a $graphLookup and ensure it properly expands "startWith". +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -120,17 +119,17 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 3); +assert.eq(res.integers.length, 3); - // $graphLookup should not recurse when the 'connectFromField' is missing. However, if it - // mistakenly does, then it would look for a 'connectToField' value of null. In order to prevent - // regressions, we insert a document with a 'connectToField' value of null, then perform a - // $graphLookup, and ensure that we do not find the erroneous document. - assert.writeOK(foreign.remove({_id: 51})); - assert.writeOK(foreign.insert({_id: 51})); - assert.writeOK(foreign.insert({_id: null, neighbors: [50, 52]})); +// $graphLookup should not recurse when the 'connectFromField' is missing. However, if it +// mistakenly does, then it would look for a 'connectToField' value of null. In order to prevent +// regressions, we insert a document with a 'connectToField' value of null, then perform a +// $graphLookup, and ensure that we do not find the erroneous document. +assert.writeOK(foreign.remove({_id: 51})); +assert.writeOK(foreign.insert({_id: 51})); +assert.writeOK(foreign.insert({_id: null, neighbors: [50, 52]})); - res = local +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -142,17 +141,17 @@ }) .toArray()[0]; - // Our result should be missing the values with _id from 52 to 99. - assert.eq(res.integers.length, 52); +// Our result should be missing the values with _id from 52 to 99. +assert.eq(res.integers.length, 52); - // Perform a $graphLookup and ensure we don't go into an infinite loop when our graph is cyclic. - assert.writeOK(foreign.remove({_id: {$in: [null, 51]}})); - assert.writeOK(foreign.insert({_id: 51, neighbors: [50, 52]})); +// Perform a $graphLookup and ensure we don't go into an infinite loop when our graph is cyclic. +assert.writeOK(foreign.remove({_id: {$in: [null, 51]}})); +assert.writeOK(foreign.insert({_id: 51, neighbors: [50, 52]})); - assert.writeOK(foreign.update({_id: 99}, {$set: {neighbors: [98, 0]}})); - assert.writeOK(foreign.update({_id: 0}, {$set: {neighbors: [99, 1]}})); +assert.writeOK(foreign.update({_id: 99}, {$set: {neighbors: [98, 0]}})); +assert.writeOK(foreign.update({_id: 0}, {$set: {neighbors: [99, 1]}})); - res = local +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -164,10 +163,10 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - // Perform a $graphLookup and ensure that "depthField" is properly populated. - res = local +// Perform a $graphLookup and ensure that "depthField" is properly populated. +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -180,9 +179,9 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - res.integers.forEach(function(n) { - assert.eq(n.distance, Math.abs(50 - n._id)); - }); +res.integers.forEach(function(n) { + assert.eq(n.distance, Math.abs(50 - n._id)); +}); }()); |