UNPKG

mongodb-expressions

Version:

MongoDB expressions for fire.js

734 lines (692 loc) 20 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.Find').addBatch({ "When I use Mongo.Find with null input": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(locations)": { "lat":100, "long":200 }, " @Mongo.Insert(locations)": { "lat":300, "long":400 }, " @Mongo.Insert(locations)": { "lat":500, "long":600 }, "@Mongo.Find(locations)": null, "@each": { "lat": { "@get(CurrentItem.lat)": null }, "long": { "@get(CurrentItem.long)": 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,"locations") // Drop database }, "it should retrieve all the inserted documents in the collection": function(err, result) { assert.isNotNull(result) assert.isNull(err) assert.equal(result.length, 3) assert.equal(result[0].lat, 100) assert.equal(result[0].long, 200) assert.equal(result[1].lat, 300) assert.equal(result[1].long, 400) assert.equal(result[2].lat, 500) assert.equal(result[2].long, 600) } }, "When I use Mongo.Find with an array input": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Find(locations2)": [], } }); 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,"locations2") // Drop database }, "it get an error because the input must be an object and not an array": function(err, result) { assert.equal(err.error, "Mongo.Find input must be an object") } }, "When I use Mongo.Find with an non-object input like a number": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Find(locations3)": 2, } }); 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,"locations3") // Drop database }, "it get an error because the input must be an object and not something else": function(err, result) { assert.equal(err.error, "Mongo.Find input must be an object") } }, "When I use Mongo.Find with an array as a condition": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Find(locations4)": { "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, "locations4") // Drop database }, "it should return an error since an array is not a valid condition": function(err, result) { assert.equal(err.error, "Mongo.Find conditions must be an object, @undefined or null") } }, "When I use Mongo.Find with conditions": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(locations5)": { "lat":100, "long":200 }, " @Mongo.Insert(locations5)": { "lat":300, "long":400 }, " @Mongo.Insert(locations5)": { "lat":500, "long":600 }, "@Mongo.Find(locations5)": { "conditions": { "lat": { "$gt": 250 } } }, "@each": { "lat": { "@get(CurrentItem.lat)": null }, "long": { "@get(CurrentItem.long)": 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, "locations5") // Drop database }, "it should return only the values matched": function(err, result) { assert.equal(err, null) assert.isNotNull(result) assert.isNull(err) assert.equal(result.length, 2) assert.equal(result[0].lat, 300) assert.equal(result[0].long, 400) assert.equal(result[1].lat, 500) assert.equal(result[1].long, 600) } } , "When I use Mongo.Find with conditions and sorting": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(locations6)": { "lat":100, "long":200 }, " @Mongo.Insert(locations6)": { "lat":300, "long":400 }, " @Mongo.Insert(locations6)": { "lat":500, "long":600 }, " @Mongo.Insert(locations6)": { "lat":600, "long":700 }, " @Mongo.Insert(locations6)": { "lat":800, "long":900 }, "@Mongo.Find(locations6)": { "conditions": { "lat": { "$gt": 100 } }, "sort": { "long": -1 } }, "@each": { "lat": { "@get(CurrentItem.lat)": null }, "long": { "@get(CurrentItem.long)": 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, "locations6") // Drop database }, "it should return only the values matched in the right sort": function(err, result) { assert.isNotNull(result) assert.isNull(err) assert.equal(result.length, 4) assert.equal(result[0].lat, 800) assert.equal(result[0].long, 900) assert.equal(result[1].lat, 600) assert.equal(result[1].long, 700) assert.equal(result[2].lat, 500) assert.equal(result[2].long, 600) assert.equal(result[3].lat, 300) assert.equal(result[3].long, 400) } }, "When I use Mongo.Find with an array as sorting": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Find(locations6)": { "sort": [] }, } }); 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, "locations6") // Drop database }, "it should return an error because sort must be an array": function(err, result) { assert.equal(err.error, "Mongo.Find sort must be an object, @undefined or null") } }, "When I use Mongo.Find with specific fields as a non-array": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Find(locations6)": { "fields": 2 }, } }); 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, "locations6") // Drop database }, "it should return an error because fields must be an array": function(err, result) { assert.equal(err.error, "Mongo.Find fields must be an array, @undefined or null") } }, "When I use Mongo.Find with specific fields": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(contactFieldsCollection)": { "email": "email0@example.com", "name": "Person0" }, " @Mongo.Insert(contactFieldsCollection)": { "email": "email1@example.com", "name": "Person1" }, " @Mongo.Insert(contactFieldsCollection)": { "email": "email2@example.com", "name": "Person2" }, "@Mongo.Find(contactFieldsCollection)": { "fields": ["email"] }, } }); 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, "contactFieldsCollection") // Drop database }, "it should return the results with only the specified fields": function(err, result) { assert.equal(result.length,3) assert.equal(result[0].email,"email0@example.com") assert.isUndefined(result[0].name) assert.equal(result[1].email,"email1@example.com") assert.isUndefined(result[1].name) assert.equal(result[2].email,"email2@example.com") assert.isUndefined(result[2].name) } }, "When I use Mongo.Find with limit": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(contactFieldsCollection2)": { "email": "email0@example.com", "name": "Person0" }, " @Mongo.Insert(contactFieldsCollection2)": { "email": "email1@example.com", "name": "Person1" }, " @Mongo.Insert(contactFieldsCollection2)": { "email": "email2@example.com", "name": "Person2" }, " @Mongo.Insert(contactFieldsCollection2)": { "email": "email3@example.com", "name": "Person3" }, " @Mongo.Insert(contactFieldsCollection2)": { "email": "email4@example.com", "name": "Person4" }, "@Mongo.Find(contactFieldsCollection2)": { "limit":2 }, } }); 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, "contactFieldsCollection2") // Drop database }, "it should return only the results in the given range": function(err, result) { assert.equal(err, null) assert.equal(result.length,2) assert.equal(result[0].email,"email0@example.com") assert.equal(result[0].name,"Person0") assert.equal(result[1].email,"email1@example.com") assert.equal(result[1].name,"Person1") } }, "When I use Mongo.Find with skip": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(contactFieldsCollection3)": { "email": "email0@example.com", "name": "Person0" }, " @Mongo.Insert(contactFieldsCollection3)": { "email": "email1@example.com", "name": "Person1" }, " @Mongo.Insert(contactFieldsCollection3)": { "email": "email2@example.com", "name": "Person2" }, " @Mongo.Insert(contactFieldsCollection3)": { "email": "email3@example.com", "name": "Person3" }, " @Mongo.Insert(contactFieldsCollection3)": { "email": "email4@example.com", "name": "Person4" }, "@Mongo.Find(contactFieldsCollection3)": { "skip":3 }, } }); 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, "contactFieldsCollection3") // Drop database }, "it should return only the results in the given range": function(err, result) { assert.equal(err, null) assert.equal(result.length,2) assert.equal(result[0].email,"email3@example.com") assert.equal(result[0].name,"Person3") assert.equal(result[1].email,"email4@example.com") assert.equal(result[1].name,"Person4") } } }).export(module); vows.describe('Mongo.FindOne').addBatch({ "When I use Mongo.FindOne with null input": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.Insert(locationsOne)": { "lat":100, "long":200 }, " @Mongo.Insert(locationsOne)": { "lat":300, "long":400 }, " @Mongo.Insert(locationsOne)": { "lat":500, "long":600 }, "@set(loc)": { "@Mongo.FindOne(locationsOne)": null }, "@return": { "lat": { "@get(loc.lat)": null }, "long": { "@get(loc.long)": 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, "locationsOne") // Drop database }, "it should retrieve the first document in the collection": function(err, result) { assert.isNotNull(result) assert.isNull(err) assert.equal(result.lat, 100) assert.equal(result.long, 200) } }, "When I use Mongo.FindOne with null input and no documents in the collection": { topic: function() { dropCollection(function() { var self = this var runtime = new FireRuntime() var testExpressionName = "test_Find" runtime.registerWellKnownExpressionDefinition({ name: testExpressionName, json: { "@Mongo.FindOne(locationsTwo)": 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, "locationsTwo") // Drop database }, "it should return null": function(err, result) { assert.isNull(result) assert.isNull(err) } } }).export(module);