mongodb-expressions
Version:
MongoDB expressions for fire.js
198 lines (192 loc) • 5.78 kB
JavaScript
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.Insert').addBatch({
"When I insert a document into a collection": {
topic: function() {
dropCollection(function() {
var self = this
var runtime = new FireRuntime()
var testExpressionName = "test_MongoInsert"
runtime.registerWellKnownExpressionDefinition({
name: testExpressionName,
json: {
"@Mongo.Insert(points)": {
x:100,
y:200
}
}
});
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,"points") // Drop database
},
"the result should be the same document with an _id": function(err, result) {
assert.typeOf(result,'object')
assert.equal(result.x, 100)
assert.equal(result.y, 200)
assert.isNotNull(result._id)
}
},
"using Mongo.Insert with no hint": {
topic: function() {
var self = this
var runtime = new FireRuntime()
var testExpressionName = "test_MongoInsertWithNoHint"
runtime.registerWellKnownExpressionDefinition({
name: testExpressionName,
json: {
"@Mongo.Insert": {
x:100,
y:200
}
}
});
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)
}
})
},
"should return an error because the collection name is required": function(err, result) {
assert.equal(err.error, "Mongo.Insert requires a hint with the name of the collection")
}
},
"When I want to use Mongo.Insert to insert an array": {
topic: function() {
var self = this
var runtime = new FireRuntime()
var testExpressionName = "test_MongoInsertWithNoHint"
runtime.registerWellKnownExpressionDefinition({
name: testExpressionName,
json: {
"@Mongo.Insert(points)": []
}
});
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)
}
})
},
"should return an error because Mongo.Insert works with only one document at the time": function(err, result) {
assert.equal(err.error, "Mongo.Insert can only insert one document at the time")
}
},
"When I want to use Mongo.Insert to insert a null document": {
topic: function() {
dropCollection(function() {
var self = this
var runtime = new FireRuntime()
var testExpressionName = "test_MongoInsertWithNoHint"
runtime.registerWellKnownExpressionDefinition({
name: testExpressionName,
json: {
"@Mongo.Insert(points)": 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,"points") // Drop database
},
"should return an error because Mongo.Insert can not insert null documents": function(err, result) {
assert.equal(err.error, "Mongo.Insert can not insert null documents")
}
}
,
"When I want to use Mongo.Insert to insert a non-object document": {
topic: function() {
var self = this
var runtime = new FireRuntime()
var testExpressionName = "test_MongoInsertWithNoHint"
runtime.registerWellKnownExpressionDefinition({
name: testExpressionName,
json: {
"@Mongo.Insert(points)": 5
}
});
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)
}
})
},
"should return an error because Mongo.Insert can only insert object documents": function(err, result) {
assert.equal(err.error, "Mongo.Insert can only insert object documents")
}
}
}).export(module);