summaryrefslogtreecommitdiff
path: root/jstests/core/create_indexes.js
diff options
context:
space:
mode:
authorclang-format-7.0.1 <adam.martin@10gen.com>2019-07-26 18:20:35 -0400
committerADAM David Alan Martin <adam.martin@10gen.com>2019-07-27 11:02:23 -0400
commit134a4083953270e8a11430395357fb70a29047ad (patch)
treedd428e1230e31d92b20b393dfdc17ffe7fa79cb6 /jstests/core/create_indexes.js
parent1e46b5049003f427047e723ea5fab15b5a9253ca (diff)
downloadmongo-134a4083953270e8a11430395357fb70a29047ad.tar.gz
SERVER-41772 Apply clang-format 7.0.1 to the codebase
Diffstat (limited to 'jstests/core/create_indexes.js')
-rw-r--r--jstests/core/create_indexes.js360
1 files changed, 179 insertions, 181 deletions
diff --git a/jstests/core/create_indexes.js b/jstests/core/create_indexes.js
index 9d9f7b536bf..c3f2e0bfdd2 100644
--- a/jstests/core/create_indexes.js
+++ b/jstests/core/create_indexes.js
@@ -4,185 +4,183 @@
* ]
*/
(function() {
- 'use strict';
-
- var isMongos = ("isdbgrid" == db.runCommand("ismaster").msg);
-
- var extractResult = function(obj) {
- if (!isMongos)
- return obj;
-
- // Sample mongos format:
- // {
- // raw: {
- // "localhost:30000": {
- // createdCollectionAutomatically: false,
- // numIndexesBefore: 3,
- // numIndexesAfter: 5,
- // ok: 1
- // }
- // },
- // ok: 1
- // }
-
- var numFields = 0;
- var result = null;
- for (var field in obj.raw) {
- result = obj.raw[field];
- numFields++;
- }
-
- assert.neq(null, result);
- assert.eq(1, numFields);
- return result;
- };
-
- var checkImplicitCreate = function(createIndexResult, isMongos) {
- let allowImplicit = !isMongos;
- assert.eq(allowImplicit, createIndexResult.createdCollectionAutomatically);
- };
-
- var dbTest = db.getSisterDB('create_indexes_db');
- dbTest.dropDatabase();
-
- // Database does not exist
- var collDbNotExist = dbTest.create_indexes_no_db;
- var res = assert.commandWorked(
- collDbNotExist.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
- res = extractResult(res);
- checkImplicitCreate(res, isMongos);
- assert.eq(1, res.numIndexesBefore);
- assert.eq(2, res.numIndexesAfter);
- assert.isnull(res.note,
- 'createIndexes.note should not be present in results when adding a new index: ' +
- tojson(res));
-
- // Collection does not exist, but database does
- var t = dbTest.create_indexes;
- var res = assert.commandWorked(
- t.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
- res = extractResult(res);
- checkImplicitCreate(res, isMongos);
- assert.eq(1, res.numIndexesBefore);
- assert.eq(2, res.numIndexesAfter);
- assert.isnull(res.note,
- 'createIndexes.note should not be present in results when adding a new index: ' +
- tojson(res));
-
- // Both database and collection exist
- res = assert.commandWorked(
- t.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
- res = extractResult(res);
- assert(!res.createdCollectionAutomatically);
- assert.eq(2, res.numIndexesBefore);
- assert.eq(2,
- res.numIndexesAfter,
- 'numIndexesAfter missing from createIndexes result when adding a duplicate index: ' +
- tojson(res));
- assert(res.note,
- 'createIndexes.note should be present in results when adding a duplicate index: ' +
- tojson(res));
-
- res = t.runCommand("createIndexes",
- {indexes: [{key: {"x": 1}, name: "x_1"}, {key: {"y": 1}, name: "y_1"}]});
- res = extractResult(res);
- assert(!res.createdCollectionAutomatically);
- assert.eq(2, res.numIndexesBefore);
- assert.eq(3, res.numIndexesAfter);
-
- res = assert.commandWorked(t.runCommand(
- 'createIndexes', {indexes: [{key: {a: 1}, name: 'a_1'}, {key: {b: 1}, name: 'b_1'}]}));
- res = extractResult(res);
- assert(!res.createdCollectionAutomatically);
- assert.eq(3, res.numIndexesBefore);
- assert.eq(5, res.numIndexesAfter);
- assert.isnull(res.note,
- 'createIndexes.note should not be present in results when adding new indexes: ' +
- tojson(res));
-
- res = assert.commandWorked(t.runCommand(
- 'createIndexes', {indexes: [{key: {a: 1}, name: 'a_1'}, {key: {b: 1}, name: 'b_1'}]}));
-
- res = extractResult(res);
- assert.eq(5, res.numIndexesBefore);
- assert.eq(5,
- res.numIndexesAfter,
- 'numIndexesAfter missing from createIndexes result when adding duplicate indexes: ' +
- tojson(res));
- assert(res.note,
- 'createIndexes.note should be present in results when adding a duplicate index: ' +
- tojson(res));
-
- res = t.runCommand("createIndexes", {indexes: [{}]});
- assert(!res.ok);
-
- res = t.runCommand("createIndexes", {indexes: [{}, {key: {m: 1}, name: "asd"}]});
- assert(!res.ok);
-
- assert.eq(5, t.getIndexes().length);
-
- res = t.runCommand("createIndexes", {indexes: [{key: {"c": 1}, sparse: true, name: "c_1"}]});
- assert.eq(6, t.getIndexes().length);
- assert.eq(1,
- t.getIndexes()
- .filter(function(z) {
- return z.sparse;
- })
- .length);
-
- res = t.runCommand("createIndexes", {indexes: [{key: {"x": "foo"}, name: "x_1"}]});
- assert(!res.ok);
-
- assert.eq(6, t.getIndexes().length);
-
- res = t.runCommand("createIndexes", {indexes: [{key: {"x": 1}, name: ""}]});
- assert(!res.ok);
-
- assert.eq(6, t.getIndexes().length);
-
- // Test that v0 indexes cannot be created.
- res = t.runCommand('createIndexes', {indexes: [{key: {d: 1}, name: 'd_1', v: 0}]});
- assert.commandFailed(res, 'v0 index creation should fail');
-
- // Test that v1 indexes can be created explicitly.
- res = t.runCommand('createIndexes', {indexes: [{key: {d: 1}, name: 'd_1', v: 1}]});
- assert.commandWorked(res, 'v1 index creation should succeed');
-
- // Test that index creation fails with an invalid top-level field.
- res = t.runCommand('createIndexes', {indexes: [{key: {e: 1}, name: 'e_1'}], 'invalidField': 1});
- assert.commandFailedWithCode(res, ErrorCodes.BadValue);
-
- // Test that index creation fails with an invalid field in the index spec for index version V2.
- res = t.runCommand('createIndexes',
- {indexes: [{key: {e: 1}, name: 'e_1', 'v': 2, 'invalidField': 1}]});
- assert.commandFailedWithCode(res, ErrorCodes.InvalidIndexSpecificationOption);
-
- // Test that index creation fails with an invalid field in the index spec for index version V1.
- res = t.runCommand('createIndexes',
- {indexes: [{key: {e: 1}, name: 'e_1', 'v': 1, 'invalidField': 1}]});
- assert.commandFailedWithCode(res, ErrorCodes.InvalidIndexSpecificationOption);
-
- // Test that index creation fails with an index named '*'.
- res = t.runCommand('createIndexes', {indexes: [{key: {star: 1}, name: '*'}]});
- assert.commandFailedWithCode(res, ErrorCodes.BadValue);
-
- // Test that index creation fails with an index value of empty string.
- res = t.runCommand('createIndexes', {indexes: [{key: {f: ""}, name: 'f_1'}]});
- assert.commandFailedWithCode(res, ErrorCodes.CannotCreateIndex);
-
- // Test that index creation fails with duplicate index names in the index specs.
- res = t.runCommand('createIndexes', {
- indexes: [
- {key: {g: 1}, name: 'myidx'},
- {key: {h: 1}, name: 'myidx'},
- ],
- });
- assert.commandFailedWithCode(res, ErrorCodes.IndexKeySpecsConflict);
-
- // Test that user is not allowed to create indexes in config.transactions.
- var configDB = db.getSiblingDB('config');
- res = configDB.runCommand(
- {createIndexes: 'transactions', indexes: [{key: {star: 1}, name: 'star'}]});
- assert.commandFailedWithCode(res, ErrorCodes.IllegalOperation);
-
+'use strict';
+
+var isMongos = ("isdbgrid" == db.runCommand("ismaster").msg);
+
+var extractResult = function(obj) {
+ if (!isMongos)
+ return obj;
+
+ // Sample mongos format:
+ // {
+ // raw: {
+ // "localhost:30000": {
+ // createdCollectionAutomatically: false,
+ // numIndexesBefore: 3,
+ // numIndexesAfter: 5,
+ // ok: 1
+ // }
+ // },
+ // ok: 1
+ // }
+
+ var numFields = 0;
+ var result = null;
+ for (var field in obj.raw) {
+ result = obj.raw[field];
+ numFields++;
+ }
+
+ assert.neq(null, result);
+ assert.eq(1, numFields);
+ return result;
+};
+
+var checkImplicitCreate = function(createIndexResult, isMongos) {
+ let allowImplicit = !isMongos;
+ assert.eq(allowImplicit, createIndexResult.createdCollectionAutomatically);
+};
+
+var dbTest = db.getSisterDB('create_indexes_db');
+dbTest.dropDatabase();
+
+// Database does not exist
+var collDbNotExist = dbTest.create_indexes_no_db;
+var res = assert.commandWorked(
+ collDbNotExist.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
+res = extractResult(res);
+checkImplicitCreate(res, isMongos);
+assert.eq(1, res.numIndexesBefore);
+assert.eq(2, res.numIndexesAfter);
+assert.isnull(
+ res.note,
+ 'createIndexes.note should not be present in results when adding a new index: ' + tojson(res));
+
+// Collection does not exist, but database does
+var t = dbTest.create_indexes;
+var res =
+ assert.commandWorked(t.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
+res = extractResult(res);
+checkImplicitCreate(res, isMongos);
+assert.eq(1, res.numIndexesBefore);
+assert.eq(2, res.numIndexesAfter);
+assert.isnull(
+ res.note,
+ 'createIndexes.note should not be present in results when adding a new index: ' + tojson(res));
+
+// Both database and collection exist
+res = assert.commandWorked(t.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
+res = extractResult(res);
+assert(!res.createdCollectionAutomatically);
+assert.eq(2, res.numIndexesBefore);
+assert.eq(2,
+ res.numIndexesAfter,
+ 'numIndexesAfter missing from createIndexes result when adding a duplicate index: ' +
+ tojson(res));
+assert(res.note,
+ 'createIndexes.note should be present in results when adding a duplicate index: ' +
+ tojson(res));
+
+res = t.runCommand("createIndexes",
+ {indexes: [{key: {"x": 1}, name: "x_1"}, {key: {"y": 1}, name: "y_1"}]});
+res = extractResult(res);
+assert(!res.createdCollectionAutomatically);
+assert.eq(2, res.numIndexesBefore);
+assert.eq(3, res.numIndexesAfter);
+
+res = assert.commandWorked(t.runCommand(
+ 'createIndexes', {indexes: [{key: {a: 1}, name: 'a_1'}, {key: {b: 1}, name: 'b_1'}]}));
+res = extractResult(res);
+assert(!res.createdCollectionAutomatically);
+assert.eq(3, res.numIndexesBefore);
+assert.eq(5, res.numIndexesAfter);
+assert.isnull(
+ res.note,
+ 'createIndexes.note should not be present in results when adding new indexes: ' + tojson(res));
+
+res = assert.commandWorked(t.runCommand(
+ 'createIndexes', {indexes: [{key: {a: 1}, name: 'a_1'}, {key: {b: 1}, name: 'b_1'}]}));
+
+res = extractResult(res);
+assert.eq(5, res.numIndexesBefore);
+assert.eq(5,
+ res.numIndexesAfter,
+ 'numIndexesAfter missing from createIndexes result when adding duplicate indexes: ' +
+ tojson(res));
+assert(res.note,
+ 'createIndexes.note should be present in results when adding a duplicate index: ' +
+ tojson(res));
+
+res = t.runCommand("createIndexes", {indexes: [{}]});
+assert(!res.ok);
+
+res = t.runCommand("createIndexes", {indexes: [{}, {key: {m: 1}, name: "asd"}]});
+assert(!res.ok);
+
+assert.eq(5, t.getIndexes().length);
+
+res = t.runCommand("createIndexes", {indexes: [{key: {"c": 1}, sparse: true, name: "c_1"}]});
+assert.eq(6, t.getIndexes().length);
+assert.eq(1,
+ t.getIndexes()
+ .filter(function(z) {
+ return z.sparse;
+ })
+ .length);
+
+res = t.runCommand("createIndexes", {indexes: [{key: {"x": "foo"}, name: "x_1"}]});
+assert(!res.ok);
+
+assert.eq(6, t.getIndexes().length);
+
+res = t.runCommand("createIndexes", {indexes: [{key: {"x": 1}, name: ""}]});
+assert(!res.ok);
+
+assert.eq(6, t.getIndexes().length);
+
+// Test that v0 indexes cannot be created.
+res = t.runCommand('createIndexes', {indexes: [{key: {d: 1}, name: 'd_1', v: 0}]});
+assert.commandFailed(res, 'v0 index creation should fail');
+
+// Test that v1 indexes can be created explicitly.
+res = t.runCommand('createIndexes', {indexes: [{key: {d: 1}, name: 'd_1', v: 1}]});
+assert.commandWorked(res, 'v1 index creation should succeed');
+
+// Test that index creation fails with an invalid top-level field.
+res = t.runCommand('createIndexes', {indexes: [{key: {e: 1}, name: 'e_1'}], 'invalidField': 1});
+assert.commandFailedWithCode(res, ErrorCodes.BadValue);
+
+// Test that index creation fails with an invalid field in the index spec for index version V2.
+res = t.runCommand('createIndexes',
+ {indexes: [{key: {e: 1}, name: 'e_1', 'v': 2, 'invalidField': 1}]});
+assert.commandFailedWithCode(res, ErrorCodes.InvalidIndexSpecificationOption);
+
+// Test that index creation fails with an invalid field in the index spec for index version V1.
+res = t.runCommand('createIndexes',
+ {indexes: [{key: {e: 1}, name: 'e_1', 'v': 1, 'invalidField': 1}]});
+assert.commandFailedWithCode(res, ErrorCodes.InvalidIndexSpecificationOption);
+
+// Test that index creation fails with an index named '*'.
+res = t.runCommand('createIndexes', {indexes: [{key: {star: 1}, name: '*'}]});
+assert.commandFailedWithCode(res, ErrorCodes.BadValue);
+
+// Test that index creation fails with an index value of empty string.
+res = t.runCommand('createIndexes', {indexes: [{key: {f: ""}, name: 'f_1'}]});
+assert.commandFailedWithCode(res, ErrorCodes.CannotCreateIndex);
+
+// Test that index creation fails with duplicate index names in the index specs.
+res = t.runCommand('createIndexes', {
+ indexes: [
+ {key: {g: 1}, name: 'myidx'},
+ {key: {h: 1}, name: 'myidx'},
+ ],
+});
+assert.commandFailedWithCode(res, ErrorCodes.IndexKeySpecsConflict);
+
+// Test that user is not allowed to create indexes in config.transactions.
+var configDB = db.getSiblingDB('config');
+res =
+ configDB.runCommand({createIndexes: 'transactions', indexes: [{key: {star: 1}, name: 'star'}]});
+assert.commandFailedWithCode(res, ErrorCodes.IllegalOperation);
}());