diff options
author | Robert Guo <robert.guo@10gen.com> | 2016-10-06 16:34:21 -0400 |
---|---|---|
committer | Robert Guo <robert.guo@10gen.com> | 2016-10-11 16:58:34 -0400 |
commit | 1126f5411b19c87b3447ada413f11bfb0b466908 (patch) | |
tree | 20720308827e8b92dad89a8da1a2d49de9890fb0 | |
parent | 7727ad87b0ba8f1a55aa5e974ab90bbb5a6c1b4b (diff) | |
download | mongo-1126f5411b19c87b3447ada413f11bfb0b466908.tar.gz |
SERVER-26503 fix incorrect uses of assert.throws()
-rw-r--r-- | jstests/aggregation/bugs/server6556.js | 4 | ||||
-rw-r--r-- | jstests/core/fts_mix.js | 6 | ||||
-rw-r--r-- | jstests/core/fts_partition1.js | 4 | ||||
-rw-r--r-- | jstests/core/indexes_on_indexes.js | 8 | ||||
-rw-r--r-- | jstests/core/ord.js | 10 | ||||
-rw-r--r-- | jstests/core/push_sort.js | 48 | ||||
-rw-r--r-- | jstests/noPassthroughWithMongod/log_component_helpers.js | 8 | ||||
-rw-r--r-- | jstests/sharding/authwhere.js | 12 | ||||
-rw-r--r-- | jstests/ssl/x509_client.js | 40 |
9 files changed, 79 insertions, 61 deletions
diff --git a/jstests/aggregation/bugs/server6556.js b/jstests/aggregation/bugs/server6556.js index 261dc5a35b5..a6d1a0c4837 100644 --- a/jstests/aggregation/bugs/server6556.js +++ b/jstests/aggregation/bugs/server6556.js @@ -20,4 +20,6 @@ assert.eq(c.aggregate({$project: {_id: 0, matches: {$eq: ["as", "$foo"]}}}).toAr assert.eq(c.aggregate({$project: {_id: 0, matches: {$eq: ["as\0de", "$foo"]}}}).toArray(), [{matches: false}]); // should assert on fieldpaths with a null -assert.throws(c.aggregate, {$project: {_id: 0, matches: {$eq: ["as\0df", "$f\0oo"]}}}); +assert.throws(function() { + c.aggregate({$project: {_id: 0, matches: {$eq: ["as\0df", "$f\0oo"]}}}); +}); diff --git a/jstests/core/fts_mix.js b/jstests/core/fts_mix.js index 5142497fb41..2b1262db5eb 100644 --- a/jstests/core/fts_mix.js +++ b/jstests/core/fts_mix.js @@ -120,9 +120,11 @@ assert.eq(getIDS(res), getIDS(res2)); // -------------------------------------------- LANGUAGE ------------------------------------------- -assert.throws(tc.find({"$text": {"$search": "member", $language: "spanglish"}})); +assert.throws(function() { + tc.find({"$text": {"$search": "member", $language: "spanglish"}}).hasNext(); +}); assert.doesNotThrow(function() { - tc.find({"$text": {"$search": "member", $language: "english"}}); + tc.find({"$text": {"$search": "member", $language: "english"}}).hasNext(); }); // -------------------------------------------- LIMIT RESULTS -------------------------------------- diff --git a/jstests/core/fts_partition1.js b/jstests/core/fts_partition1.js index fc32507f430..47eaee44778 100644 --- a/jstests/core/fts_partition1.js +++ b/jstests/core/fts_partition1.js @@ -10,7 +10,9 @@ t.insert({_id: 4, x: 2, y: "bar"}); t.ensureIndex({x: 1, y: "text"}); -assert.throws(t.find({"$text": {"$search": "foo"}})); +assert.throws(function() { + t.find({"$text": {"$search": "foo"}}).hasNext(); +}); assert.eq([1], queryIDS(t, "foo", {x: 1})); diff --git a/jstests/core/indexes_on_indexes.js b/jstests/core/indexes_on_indexes.js index 3ebbf1d2f9c..bcf6b860c7c 100644 --- a/jstests/core/indexes_on_indexes.js +++ b/jstests/core/indexes_on_indexes.js @@ -13,13 +13,15 @@ assert.eq(t.system.indexes.getIndexes().length, 0); print("trying via createIndex"); - assert.throws(t.system.indexes.createIndex({_id: 1})); + assert.commandFailed(t.system.indexes.createIndex({_id: 1})); printjson(t.system.indexes.getIndexes()); assert.eq(t.system.indexes.getIndexes().length, 0); print("trying via direct insertion"); - assert.throws(t.system.indexes.insert( - {v: 1, key: {_id: 1}, ns: "indexes_on_indexes.system.indexes", name: "wontwork"})); + assert.writeErrorWithCode( + t.system.indexes.insert( + {v: 1, key: {_id: 1}, ns: "indexes_on_indexes.system.indexes", name: "wontwork"}), + ErrorCodes.CannotCreateIndex); printjson(t.system.indexes.getIndexes()); assert.eq(t.system.indexes.getIndexes().length, 0); }()); diff --git a/jstests/core/ord.js b/jstests/core/ord.js index 46607b539e8..6b37ebb6d50 100644 --- a/jstests/core/ord.js +++ b/jstests/core/ord.js @@ -5,7 +5,7 @@ // the $or implementation may need minor adjustments (memory ownership) if this // behavior is changed. -t = db.jstests_ord; +const t = db.jstests_ord; t.drop(); t.ensureIndex({a: 1}); @@ -19,8 +19,8 @@ for (i = 0; i < 100; ++i) { t.save({b: 1}); } -c = t.find({$or: [{a: 1}, {b: 1}]}).batchSize(100); -for (i = 0; i < 90; ++i) { +const c = t.find({$or: [{a: 1}, {b: 1}]}).batchSize(100); +for (i = 0; i < 100; ++i) { c.next(); } // At this point, our initial query has ended and there is a client cursor waiting @@ -31,4 +31,6 @@ t.dropIndex({a: 1}); // Dropping an index kills all cursors on the indexed namespace, not just those // cursors using the dropped index. -assert.throws(c.next()); +assert.throws(function() { + c.next(); +}); diff --git a/jstests/core/push_sort.js b/jstests/core/push_sort.js index b9676f6c963..2a518020248 100644 --- a/jstests/core/push_sort.js +++ b/jstests/core/push_sort.js @@ -56,46 +56,56 @@ assert.eq([{a: {b: 2}}, {a: {b: 3}}], t.findOne({_id: 7}).x); var doc8 = {_id: 8, x: [{a: 1}, {a: 2}]}; t.save(doc8); var res = t.update({_id: 8}, {$push: {x: {$sort: {a: -1}}}}); -assert.writeError(res); +assert.writeErrorWithCode(res, ErrorCodes.DollarPrefixedFieldName); assert.docEq(t.findOne({_id: 8}), doc8); // ensure doc was not changed t.save({_id: 100, x: [{a: 1}]}); -// For now, elements of the $each vector need to be objects. In here, '2' is an invalide $each. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [2], $slice: -2, $sort: {a: 1}}}})); +// Elements of the $each vector can be integers. In here, '2' is a valid $each. +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [2], $slice: -2, $sort: {a: 1}}}})); -// For the same reason as above, '1' is an invalid $each element. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}, 1], $slice: -2, $sort: {a: 1}}}})); +// For the same reason as above, '1' is an valid $each element. +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}, 1], $slice: -2, $sort: {a: 1}}}})); // The sort key pattern cannot be empty. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {}}}}), + ErrorCodes.BadValue); -// For now, we do not support positive $slice's (ie, trimming from the array's front). -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: 2, $sort: {a: 1}}}})); +// Support positive $slice's (ie, trimming from the array's front). +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: 2, $sort: {a: 1}}}})); // A $slice cannot be a fractional value. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2.1, $sort: {a: 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2.1, $sort: {a: 1}}}}), + ErrorCodes.BadValue); // The sort key pattern's value must be either 1 or -1. In here, {a:-2} is an invalid value. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: -2}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: -2}}}}), + ErrorCodes.BadValue); -// For now, we are not supporting sorting of basic elements (non-object, non-arrays). In here, -// the $sort clause would need to have a key pattern value rather than 1. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: 1}}})); +// Support sorting array alements that are not documents. +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: 1}}})); // The key pattern 'a.' is an invalid value for $sort. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'a.': 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'a.': 1}}}}), + ErrorCodes.BadValue); // An empty key pattern is not a valid $sort value. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'': 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {'': 1}}}}), + ErrorCodes.BadValue); // If a $slice is used, the only other $sort clause that's accepted is $sort. In here, $xxx // is not a valid clause. -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $xxx: {s: 1}}}})); +assert.writeErrorWithCode( + t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $xxx: {s: 1}}}}), + ErrorCodes.BadValue); t.remove({}); -// Ensure that existing values are validated in the array as objects during a $sort with $each, -// not only the elements in the $each array. +// Existing values are validated in the array do not have to be objects during a $sort with $each. t.save({_id: 100, x: [1, "foo"]}); -assert.throws(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: 1}}}})); +assert.writeOK(t.update({_id: 100}, {$push: {x: {$each: [{a: 2}], $slice: -2, $sort: {a: 1}}}})); diff --git a/jstests/noPassthroughWithMongod/log_component_helpers.js b/jstests/noPassthroughWithMongod/log_component_helpers.js index dfe6523ac35..405b2bdaca3 100644 --- a/jstests/noPassthroughWithMongod/log_component_helpers.js +++ b/jstests/noPassthroughWithMongod/log_component_helpers.js @@ -32,8 +32,12 @@ assert.eq(mongo.getLogComponents().storage.journal.verbosity, 1); // setLogLevel - invalid argument - assert.throws(mongo.setLogLevel, [2, 24]); - assert.throws(db.setLogLevel, [2, ["array", "not.allowed"]]); + assert.throws(function() { + mongo.setLogLevel(2, 24); + }); + assert.throws(function() { + db.setLogLevel(2, ["array", "not.allowed"]); + }); // Restore originalSettings assert.commandWorked( diff --git a/jstests/sharding/authwhere.js b/jstests/sharding/authwhere.js index 7443a91005a..a47dc5fb601 100644 --- a/jstests/sharding/authwhere.js +++ b/jstests/sharding/authwhere.js @@ -62,13 +62,19 @@ // Sanity check. test1Reader can count (read) test1, but not test2. assert.eq(test1DB.foo.count(), 1); - assert.throws(test2DB.foo.count); + assert.throws(function() { + test2DB.foo.count(); + }); // Cannot examine second database from a where clause. - assert.throws(test1DB.foo.count, ["db.getSiblingDB('test2').foo.count() == 1"]); + assert.throws(function() { + test1DB.foo.count("db.getSiblingDB('test2').foo.count() == 1"); + }); // Cannot write test1 via tricky where clause. - assert.throws(test1DB.foo.count, ["db.foo.insert({b: 1})"]); + assert.throws(function() { + test1DB.foo.count("db.foo.insert({b: 1})"); + }); assert.eq(test1DB.foo.count(), 1); } finally { adminDB.logout(); diff --git a/jstests/ssl/x509_client.js b/jstests/ssl/x509_client.js index 5bcc83edef9..5842438b98a 100644 --- a/jstests/ssl/x509_client.js +++ b/jstests/ssl/x509_client.js @@ -29,22 +29,16 @@ function authAndTest(mongo) { test = mongo.getDB("test"); // It should be impossible to create users with the same name as the server's subject - assert.throws( - function() { - external.createUser( - {user: SERVER_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); - }, - {}, - "Created user with same name as the server's x.509 subject"); + assert.throws(function() { + external.createUser( + {user: SERVER_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); + }, [], "Created user with same name as the server's x.509 subject"); // It should be impossible to create users with names recognized as cluster members - assert.throws( - function() { - external.createUser( - {user: INTERNAL_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); - }, - {}, - "Created user which would be recognized as a cluster member"); + assert.throws(function() { + external.createUser( + {user: INTERNAL_USER, roles: [{'role': 'userAdminAnyDatabase', 'db': 'admin'}]}); + }, [], "Created user which would be recognized as a cluster member"); // Add user using localhost exception external.createUser({ @@ -62,12 +56,9 @@ function authAndTest(mongo) { }); // Localhost exception should not be in place anymore - assert.throws( - function() { - test.foo.findOne(); - }, - {}, - "read without login"); + assert.throws(function() { + test.foo.findOne(); + }, [], "read without login"); assert(!external.auth({user: INVALID_CLIENT_USER, mechanism: 'MONGODB-X509'}), "authentication with invalid user should fail"); @@ -86,12 +77,9 @@ function authAndTest(mongo) { test.foo.findOne(); external.logout(); - assert.throws( - function() { - test.foo.findOne(); - }, - {}, - "read after logout"); + assert.throws(function() { + test.foo.findOne(); + }, [], "read after logout"); } print("1. Testing x.509 auth to mongod"); |