diff options
author | Benjamin Murphy <benjamin_murphy@me.com> | 2016-04-11 15:43:29 -0400 |
---|---|---|
committer | Benjamin Murphy <benjamin_murphy@me.com> | 2016-04-15 13:03:12 -0400 |
commit | 77aaa5419340185ad1744f0b25f8543c6add2abc (patch) | |
tree | e4cc0776b626e9d8c401971eab6c8302b1fbc291 /jstests/aggregation/expressions | |
parent | 63d021f4107f4e48aa3c76629dd6dbd6abecb8e3 (diff) | |
download | mongo-77aaa5419340185ad1744f0b25f8543c6add2abc.tar.gz |
SERVER-10689 Aggregation now supports the switch expression.
Diffstat (limited to 'jstests/aggregation/expressions')
-rw-r--r-- | jstests/aggregation/expressions/switch.js | 150 | ||||
-rw-r--r-- | jstests/aggregation/expressions/switch_errors.js | 71 |
2 files changed, 221 insertions, 0 deletions
diff --git a/jstests/aggregation/expressions/switch.js b/jstests/aggregation/expressions/switch.js new file mode 100644 index 00000000000..9a6dbbb529d --- /dev/null +++ b/jstests/aggregation/expressions/switch.js @@ -0,0 +1,150 @@ +// In SERVER-10689, the $switch expression was introduced. In this file, we test the functionality +// of the expression. + +(function() { + "use strict"; + + var coll = db.switch; + coll.drop(); + + // Insert an empty document so that something can flow through the pipeline. + coll.insert({}); + + // Ensure that a branch is correctly evaluated. + var pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": { + "branches": [{"case": {"$eq": [1, 1]}, "then": "one is equal to one!"}], + } + } + } + }; + var res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": "one is equal to one!"}); + + // Ensure that the first branch which matches is chosen. + pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": { + "branches": [ + {"case": {"$eq": [1, 1]}, "then": "one is equal to one!"}, + {"case": {"$eq": [2, 2]}, "then": "two is equal to two!"} + ], + } + } + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": "one is equal to one!"}); + + // Ensure that the default is chosen if no case matches. + pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": { + "branches": [{"case": {"$eq": [1, 2]}, "then": "one is equal to two!"}], + "default": "no case matched." + } + } + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": "no case matched."}); + + // Ensure that nullish values are treated as false when they are a "case", and are null + // otherwise. + pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": { + "branches": [{"case": null, "then": "Null was true!"}], + "default": "No case matched." + } + } + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": "No case matched."}); + + pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": { + "branches": [{"case": "$missingField", "then": "Null was true!"}], + "default": "No case matched." + } + } + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": "No case matched."}); + + pipeline = { + "$project": { + "_id": 0, + "output": + {"$switch": {"branches": [{"case": true, "then": null}], "default": false}} + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": null}); + + pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": + {"branches": [{"case": true, "then": "$missingField"}], "default": false} + } + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {}); + + pipeline = { + "$project": { + "_id": 0, + "output": + {"$switch": {"branches": [{"case": null, "then": false}], "default": null}} + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {"output": null}); + + pipeline = { + "$project": { + "_id": 0, + "output": { + "$switch": + {"branches": [{"case": null, "then": false}], "default": "$missingField"} + } + } + }; + res = coll.aggregate(pipeline).toArray(); + + assert.eq(res.length, 1); + assert.eq(res[0], {}); +}()); diff --git a/jstests/aggregation/expressions/switch_errors.js b/jstests/aggregation/expressions/switch_errors.js new file mode 100644 index 00000000000..cf6dc0f4f93 --- /dev/null +++ b/jstests/aggregation/expressions/switch_errors.js @@ -0,0 +1,71 @@ +// SERVER-10689 introduced the $switch expression. In this file, we test the error cases of the +// expression. +load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. + +(function() { + "use strict"; + + var coll = db.switch; + coll.drop(); + + var pipeline = { + "$project": {"output": {"$switch": "not an object"}} + }; + assertErrorCode(coll, pipeline, 40060, "$switch requires an object as an argument."); + + pipeline = { + "$project": {"output": {"$switch": {"branches": "not an array"}}} + }; + assertErrorCode(coll, pipeline, 40061, "$switch requires 'branches' to be an array."); + + pipeline = { + "$project": {"output": {"$switch": {"branches": ["not an object"]}}} + }; + assertErrorCode(coll, pipeline, 40062, "$switch requires each branch to be an object."); + + pipeline = { + "$project": {"output": {"$switch": {"branches": [{}]}}} + }; + assertErrorCode(coll, pipeline, 40064, "$switch requires each branch have a 'case'."); + + pipeline = { + "$project": { + "output": { + "$switch": { + "branches": [{ + "case": 1, + }] + } + } + } + }; + assertErrorCode(coll, pipeline, 40065, "$switch requires each branch have a 'then'."); + + pipeline = { + "$project": + {"output": {"$switch": {"branches": [{"case": true, "then": false, "badKey": 1}]}}} + }; + assertErrorCode(coll, pipeline, 40063, "$switch found a branch with an unknown argument"); + + pipeline = { + "$project": {"output": {"$switch": {"notAnArgument": 1}}} + }; + assertErrorCode(coll, pipeline, 40067, "$switch found an unknown argument"); + + pipeline = { + "$project": {"output": {"$switch": {"branches": []}}} + }; + assertErrorCode(coll, pipeline, 40068, "$switch requires at least one branch"); + + pipeline = { + "$project": {"output": {"$switch": {}}} + }; + assertErrorCode(coll, pipeline, 40068, "$switch requires at least one branch"); + + coll.insert({x: 1}); + pipeline = { + "$project": + {"output": {"$switch": {"branches": [{"case": {"$eq": ["$x", 0]}, "then": 1}]}}} + }; + assertErrorCode(coll, pipeline, 40066, "$switch has no default and an input matched no case"); +}()); |