UNPKG

mongodb-expressions

Version:

MongoDB expressions for fire.js

470 lines (461 loc) 12.8 kB
var vows = require('vows') var assert = require('assert') var FireRuntime = require('fire').Runtime var configRuntime = require('./util').configRuntime var dropCollection = require('./util').dropCollection require('./runtimeExtensions.js') vows.describe('Mongo.Update input validation').addBatch({ "When I use Mongo.Update with no hint": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Update": null, } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) }, "it should return an error since an input is required": function(err, result) { assert.isNull(result) assert.isNotNull(err) assert.equal(err.error, "Mongo.Update requires a hint with the name of the collection"); } }, "When I use Mongo.Update with null input": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Update(locations)": null, } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) }, "it should return an error since an input is required": function(err, result) { assert.isNull(result) assert.isNotNull(err) assert.equal(err.error, "Mongo.Update input must be an object"); } }, "When I use Mongo.Update with a non-object conditions like a number": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Update(locations3)": { "conditions": 300 } } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) }, "it get an error because the input must be an object and not something else": function(err, result) { assert.equal(err.error, "Mongo.Update conditions must be an object") } }, "When I use Mongo.Update with a non-object changes like a number": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Update(locations3)": { "conditions": {}, "changes": 329 } } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) }, "it get an error because the input must be an object and not something else": function(err, result) { assert.equal(err.error, "Mongo.Update changes must be an object") } }, "When I use Mongo.Update with a non-object options like a number": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Update(locations3)": { "conditions": {}, "changes": {}, "options": 22 } } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) }, "it get an error because the input must be an object and not something else": function(err, result) { assert.equal(err.error, "Mongo.Update options must be an object") } } }).export(module); vows.describe('Mongo.Update').addBatch({ "When I use Mongo.Update with no matching documents": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Update(personsUpdate1)": { "conditions": { }, "changes": { } }, } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) },this,"personsUpdate1") // Drop database }, "it should return 0 documents updated": function(err, result) { assert.isNull(err) assert.isNotNull(result) assert.equal(result, 0) } }, "When I use Mongo.Update matching one document": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(personsUpdate2)": { name:"Johan", status: 0 }, "@set(count):": { "@Mongo.Update(personsUpdate2)": { "conditions": { name:"Johan", status: 0 }, "changes": { '$set': { name:"Super Johan" } } } }, "@set(doc)": { "@Mongo.FindOne(personsUpdate2)": { "conditions": { name:"Super Johan" } } }, "@return": { count: { "@get(count)": null }, doc: { "@get(doc)": null } } } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) },this,"personsUpdate2") // Drop database }, "it should return 1 document updated": function(err, result) { assert.isNull(err) assert.isNotNull(result.doc) assert.isNotNull(result.count) assert.equal(result.count, 1) assert.equal(result.doc.name, "Super Johan") assert.equal(result.doc.status, 0) } }, "When I use Mongo.Update matching multiple documents but with no additional options": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(personsUpdate3)": { name:"Disabled Person A", status: 0 }, " @Mongo.Insert(personsUpdate3)": { name:"Disabled Person B", status: 0 }, "@set(count):": { "@Mongo.Update(personsUpdate3)": { "conditions": { status: 0 }, "changes": { '$set': { status:1 } } } }, "@set(docs)": { "@Mongo.Find(personsUpdate3)": { "conditions": { status: 1 } } }, "@return": { count: { "@get(count)": null }, docs: { "@get(docs)": null } } } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) },this,"personsUpdate3") // Drop database }, "it should return 1 document updated": function(err, result) { assert.isNull(err) assert.equal(result.count, 1) assert.equal(result.docs.length, 1) assert.equal(result.docs[0].name, "Disabled Person A") assert.equal(result.docs[0].status, 1) } }, "When I use Mongo.Update matching multiple documents with multi options": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(personsUpdate4)": { name:"Disabled Person A", status: 0 }, " @Mongo.Insert(personsUpdate4)": { name:"Disabled Person B", status: 0 }, "@set(count):": { "@Mongo.Update(personsUpdate4)": { "conditions": { status: 0 }, "changes": { '$set': { status:1 } }, "options": { "multi": true } } }, "@set(docs)": { "@Mongo.Find(personsUpdate4)": { "conditions": { status: 1 } } }, "@return": { count: { "@get(count)": null }, docs: { "@get(docs)": null } } } }); configRuntime(runtime, function(initError) { if(initError) { self.callback(initError, null) } else { var contextBase = { _resultCallback: function(res) { self.callback(null, res) }, _loopCallback: function() {}, _inputExpression: function() {}, _variables: {}, _errorCallback: function(err) { self.callback(err, null) } }; runtime._testOnly_runExpressionByName(testExpressionName, contextBase ,null) } }) },this,"personsUpdate4") // Drop database }, "it should return 2 documents updated": function(err, result) { assert.isNull(err) assert.equal(result.count, 2) assert.equal(result.docs.length, 2) assert.equal(result.docs[0].name, "Disabled Person A") assert.equal(result.docs[0].status, 1) assert.equal(result.docs[1].name, "Disabled Person B") assert.equal(result.docs[1].status, 1) } } }).export(module);