summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shell_write_assertions.js
diff options
context:
space:
mode:
authorDavid Bradford <david.bradford@mongodb.com>2018-02-15 15:23:13 -0500
committerDavid Bradford <david.bradford@mongodb.com>2018-02-15 15:23:13 -0500
commitaa8388487e3bea0737237b48a06e6b20b243e791 (patch)
treec0773560c7d6acb2b45c8ccb6f1e38e2cff7f605 /jstests/noPassthrough/shell_write_assertions.js
parentcaf6f27ec26a5b0d325e2fcbad39d0239f798a43 (diff)
downloadmongo-aa8388487e3bea0737237b48a06e6b20b243e791.tar.gz
SERVER-33034: Update jstest asserts to accept a function for messages
Diffstat (limited to 'jstests/noPassthrough/shell_write_assertions.js')
-rw-r--r--jstests/noPassthrough/shell_write_assertions.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/jstests/noPassthrough/shell_write_assertions.js b/jstests/noPassthrough/shell_write_assertions.js
new file mode 100644
index 00000000000..e3473bd445e
--- /dev/null
+++ b/jstests/noPassthrough/shell_write_assertions.js
@@ -0,0 +1,89 @@
+/**
+ * Tests for the write assertion functions in mongo/shell/assert.js.
+ */
+(() => {
+ "use strict";
+ const conn = MongoRunner.runMongod();
+ const db = conn.getDB("writeAssertions");
+ assert.neq(null, conn, "mongodb was unable to start up");
+ const tests = [];
+
+ function setup() {
+ db.coll.drop();
+ }
+
+ function _doFailedWrite(collection) {
+ const duplicateId = 42;
+
+ const res = collection.insert({_id: duplicateId});
+ assert.writeOK(res, "write to collection should have been successful");
+ const failedRes = collection.insert({_id: duplicateId});
+ assert.writeError(failedRes, "duplicate key write should have failed");
+ return failedRes;
+ }
+
+ /* writeOK tests */
+ tests.push(function writeOKSuccessfulWriteDoesNotCallMsgFunction() {
+ var msgFunctionCalled = false;
+
+ const result = db.coll.insert({data: "hello world"});
+ assert.doesNotThrow(() => {
+ assert.writeOK(result, () => {
+ msgFunctionCalled = true;
+ });
+ });
+
+ assert.eq(false, msgFunctionCalled, "message function should not have been called");
+ });
+
+ tests.push(function writeOKUnsuccessfulWriteDoesCallMsgFunction() {
+ var msgFunctionCalled = false;
+
+ const failedResult = _doFailedWrite(db.coll);
+ assert.throws(() => {
+ assert.writeOK(failedResult, () => {
+ msgFunctionCalled = true;
+ });
+ });
+
+ assert.eq(true, msgFunctionCalled, "message function should have been called");
+ });
+
+ /* writeError tests */
+ tests.push(function writeErrorSuccessfulWriteDoesCallMsgFunction() {
+ var msgFunctionCalled = false;
+
+ const result = db.coll.insert({data: "hello world"});
+ assert.throws(() => {
+ assert.writeError(result, () => {
+ msgFunctionCalled = true;
+ });
+ });
+
+ assert.eq(true, msgFunctionCalled, "message function should have been called");
+ });
+
+ tests.push(function writeErrorUnsuccessfulWriteDoesNotCallMsgFunction() {
+ var msgFunctionCalled = false;
+
+ const failedResult = _doFailedWrite(db.coll);
+ assert.doesNotThrow(() => {
+ assert.writeError(failedResult, () => {
+ msgFunctionCalled = true;
+ });
+ });
+
+ assert.eq(false, msgFunctionCalled, "message function should not have been called");
+ });
+
+ /* main */
+
+ tests.forEach((test) => {
+ jsTest.log(`Starting tests '${test.name}'`);
+ setup();
+ test();
+ });
+
+ /* cleanup */
+ MongoRunner.stopMongod(conn);
+})();