Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bug fixes. Added a few tests so we don't hit them again.

- FerretModel.findOne was emitting 'error' when no model was found (instead of 'success' with argument null)
- Throwing an exception on the 'each' listener to FerretModel.find would cause the 'error' event to also be emmited.
  • Loading branch information...
commit c3f19ec3e934248174391a0cd1b552a16555b1b2 1 parent 38e47b1
@coreh authored
Showing with 41 additions and 8 deletions.
  1. +1 −1  ferret.js
  2. +12 −3 model.js
  3. +28 −4 test.js
View
2  ferret.js
@@ -381,4 +381,4 @@ Ferret.listeners = function(event) {
Ferret.emit = function() {
return _ensureSharedFerret().emit.apply(sharedFerret, arguments)
-}
+}
View
15 model.js
@@ -156,9 +156,15 @@ Ferret.prototype.model = function(name, schema) {
ferret.findOne(name, query)
.on('success', function(documentLoaded) {
+ if (documentLoaded == null) {
+ ee.emit('success', null)
+ return
+ }
try {
var model = new FerretModel(documentLoaded, { deserialize: true })
- ee.emit('success', model)
+ process.nextTick(function() {
+ ee.emit('success', model)
+ })
} catch (err) {
ee.emit('error', err)
}
@@ -184,7 +190,10 @@ Ferret.prototype.model = function(name, schema) {
return;
}
try {
- ee.emit('each', new FerretModel(documentLoaded, { deserialize: true }))
+ var model = new FerretModel(documentLoaded, { deserialize: true })
+ process.nextTick(function(){
+ ee.emit('each', model)
+ })
} catch (err) {
ee.emit('error', err)
}
@@ -278,4 +287,4 @@ Ferret.prototype.model = function(name, schema) {
return this._models[name] = FerretModel
}
-}
+}
View
32 test.js
@@ -10,7 +10,7 @@ try {
}
var currentTest = -1
-var start, next, tests, numErrors = 0, numSkips = 0, processErrors = 0;
+var start, next, tests, numErrors = 0, numSkips = 0, processErrors = 0, expectedSpecialErrors = 0, specialErrors = 0;
start = next = function(err, shouldStop, skipped) {
if (currentTest >= 0) {
if (err) {
@@ -393,6 +393,8 @@ tests = [
assert(person._id !== undefined)
count++
}
+ expectedSpecialErrors++
+ throw "special"
})
.on('error', function(err) {
error = err
@@ -401,6 +403,7 @@ tests = [
setTimeout(function() {
assert(count > 0)
assert(error == null)
+ assert(hasBeenNull == true)
next()
}, 200)
@@ -472,14 +475,35 @@ tests = [
}, 200)
},
function() {
+ var TestModel = ferret.model('hello')
+ TestModel.findOne({ name: 'test123' })
+ .on('success', function(hello) {
+ assert(hello == null)
+ next()
+ expectedSpecialErrors++
+ throw "special"
+ })
+ .on('error', function(err) {
+ next(err)
+ })
+ },
+ function() {
assert(processErrors == 0)
next()
+ },
+ function() {
+ assert(specialErrors == expectedSpecialErrors)
+ next()
}
]
process.on('uncaughtException', function(err){
- processErrors++;
- console.error(err);
+ if (err == "special") {
+ specialErrors++
+ } else {
+ processErrors++
+ console.error(err)
+ }
})
-start()
+start()
Please sign in to comment.
Something went wrong with that request. Please try again.