summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
Diffstat (limited to 'jstests')
-rw-r--r--jstests/core/map_reduce_validation.js90
-rw-r--r--jstests/sharding/map_reduce_validation.js57
2 files changed, 90 insertions, 57 deletions
diff --git a/jstests/core/map_reduce_validation.js b/jstests/core/map_reduce_validation.js
new file mode 100644
index 00000000000..051c5ef11f8
--- /dev/null
+++ b/jstests/core/map_reduce_validation.js
@@ -0,0 +1,90 @@
+// Tests that invalid options to the mapReduce command are rejected.
+// @tags: [
+// uses_map_reduce_with_temp_collections,
+// does_not_support_stepdowns,
+// ]
+(function() {
+"use strict";
+
+const source = db.mr_validation;
+source.drop();
+assert.commandWorked(source.insert({x: 1}));
+
+function mapFunc() {
+ emit(this.x, 1);
+}
+function reduceFunc(key, values) {
+ return Array.sum(values);
+}
+
+// Test that you can't specify sharded and inline.
+assert.commandFailed(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {inline: 1, sharded: true}
+}));
+
+// Test that you can't output to the admin or config databases.
+assert.commandFailedWithCode(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {merge: "foo", db: "admin"}
+}),
+ ErrorCodes.CommandNotSupported);
+
+assert.commandFailedWithCode(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {merge: "foo", db: "config"}
+}),
+ ErrorCodes.CommandNotSupported);
+
+// Test that you can output to a different database.
+assert.commandWorked(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {merge: "foo", db: "mr_validation_other"}
+}));
+assert.eq(db.getSiblingDB("mr_validation_other").foo.find().toArray(), [{_id: 1, value: 1}]);
+
+// Test that you can't use a regex as the namespace.
+// TODO SERVER-42677 We should be able to expect a single error code here once the parsing logic is
+// shared across mongos and mongod. For each of the remaining assertions it looks like there are two
+// possible error codes: one for mongos and one for mongod, and each of these are likely different
+// than the code we will use once we switch over to the IDL parser, so we'll avoid asserting on the
+// error code.
+assert.commandFailed(db.runCommand(
+ {mapReduce: /bar/, map: mapFunc, reduce: reduceFunc, out: {replace: "foo", db: "test"}}));
+
+assert.commandFailed(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {replace: /foo/, db: "test"}
+}));
+
+assert.commandFailed(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {merge: /foo/, db: "test"}
+}));
+
+assert.commandFailed(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {replace: "foo", db: /test/}
+}));
+
+assert.commandFailed(db.runCommand({
+ mapReduce: source.getName(),
+ map: mapFunc,
+ reduce: reduceFunc,
+ out: {merge: "foo", db: /test/}
+}));
+}());
diff --git a/jstests/sharding/map_reduce_validation.js b/jstests/sharding/map_reduce_validation.js
deleted file mode 100644
index 717c4937ded..00000000000
--- a/jstests/sharding/map_reduce_validation.js
+++ /dev/null
@@ -1,57 +0,0 @@
-var st = new ShardingTest({shards: 1});
-
-var testDB = st.s.getDB('test');
-
-var mapFunc = function() {
- emit(this.x, 1);
-};
-var reduceFunc = function(key, values) {
- return values.length;
-};
-
-assert.commandFailed(testDB.runCommand(
- {mapReduce: 'user', map: mapFunc, reduce: reduceFunc, out: {inline: 1, sharded: true}}));
-
-testDB.bar.insert({i: 1});
-assert.commandFailedWithCode(testDB.runCommand({
- mapReduce: 'bar',
- map: function() {
- emit(this.i, this.i * 3);
- },
- reduce: function(key, values) {
- return Array.sum(values);
- },
- out: {replace: "foo", db: "admin"}
-}),
- ErrorCodes.CommandNotSupported);
-
-assert.commandFailedWithCode(testDB.runCommand({
- mapReduce: 'bar',
- map: function() {
- emit(this.i, this.i * 3);
- },
- reduce: function(key, values) {
- return Array.sum(values);
- },
- out: {replace: "foo", db: "config"}
-}),
- ErrorCodes.CommandNotSupported);
-
-assert.commandWorked(testDB.runCommand({
- mapReduce: 'bar',
- map: function() {
- emit(this.i, this.i * 3);
- },
- reduce: function(key, values) {
- return Array.sum(values);
- },
- out: {replace: "foo", db: "test"}
-}));
-
-// Test invalid namespace.
-assert.commandFailedWithCode(
- testDB.runCommand(
- {mapReduce: /bar/, map: mapFunc, reduce: reduceFunc, out: {replace: "foo", db: "test"}}),
- ErrorCodes.InvalidNamespace);
-
-st.stop();