UNPKG

mongodb-expressions

Version:

MongoDB expressions for fire.js

257 lines (243 loc) 6.91 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.Remove input validation').addBatch({ "When I use Mongo.Remove with no hint": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Remove": 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.Remove requires a hint with the name of the collection"); } }, "When I use Mongo.Remove with null input": { topic: function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Remove(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.Remove input must be an object"); } }, "When I use Mongo.Remove 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.Remove(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.Remove conditions must be an object") } } }).export(module); vows.describe('Mongo.Remove').addBatch({ "When I use Mongo.Remove with empty conditions": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(locationsRemove3)": { x:1, y:2 }," @Mongo.Insert(locationsRemove3)": { x:1, y:2 }, " @Mongo.Insert(locationsRemove3)": { x:1, y:2 }, "@Mongo.Remove(locationsRemove3)": { "conditions": { } }, } }); 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,"locationsRemove3") // Drop database }, "it should return all the documents in the collection": function(err, result) { assert.isNull(err) assert.isNotNull(result) assert.equal(result, 3) } }, "When I use Mongo.Remove matching multiple documents": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Update" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(personsRemove6)": { name:"Disabled Person A", status: 0 }, " @Mongo.Insert(personsRemove6)": { name:"Disabled Person B", status: 1 }, " @Mongo.Insert(personsRemove6)": { name:"Disabled Person C", status: 0 }, "@set(count):": { "@Mongo.Remove(personsRemove6)": { "conditions": { status: 0 } } }, "@set(docs)": { "@Mongo.Find(personsRemove6)": { "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,"personsRemove6") // Drop database }, "it should return 1 document removed": function(err, result) { assert.isNull(err) assert.equal(result.count, 2) assert.equal(result.docs.length, 1) assert.equal(result.docs[0].name, "Disabled Person B") assert.equal(result.docs[0].status, 1) } } }).export(module);