Skip to content

Commit c3f19ec

Browse files
committed
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.
1 parent 38e47b1 commit c3f19ec

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

ferret.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,4 @@ Ferret.listeners = function(event) {
381381

382382
Ferret.emit = function() {
383383
return _ensureSharedFerret().emit.apply(sharedFerret, arguments)
384-
}
384+
}

model.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,15 @@ Ferret.prototype.model = function(name, schema) {
156156

157157
ferret.findOne(name, query)
158158
.on('success', function(documentLoaded) {
159+
if (documentLoaded == null) {
160+
ee.emit('success', null)
161+
return
162+
}
159163
try {
160164
var model = new FerretModel(documentLoaded, { deserialize: true })
161-
ee.emit('success', model)
165+
process.nextTick(function() {
166+
ee.emit('success', model)
167+
})
162168
} catch (err) {
163169
ee.emit('error', err)
164170
}
@@ -184,7 +190,10 @@ Ferret.prototype.model = function(name, schema) {
184190
return;
185191
}
186192
try {
187-
ee.emit('each', new FerretModel(documentLoaded, { deserialize: true }))
193+
var model = new FerretModel(documentLoaded, { deserialize: true })
194+
process.nextTick(function(){
195+
ee.emit('each', model)
196+
})
188197
} catch (err) {
189198
ee.emit('error', err)
190199
}
@@ -278,4 +287,4 @@ Ferret.prototype.model = function(name, schema) {
278287
return this._models[name] = FerretModel
279288
}
280289

281-
}
290+
}

test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ try {
1010
}
1111

1212
var currentTest = -1
13-
var start, next, tests, numErrors = 0, numSkips = 0, processErrors = 0;
13+
var start, next, tests, numErrors = 0, numSkips = 0, processErrors = 0, expectedSpecialErrors = 0, specialErrors = 0;
1414
start = next = function(err, shouldStop, skipped) {
1515
if (currentTest >= 0) {
1616
if (err) {
@@ -393,6 +393,8 @@ tests = [
393393
assert(person._id !== undefined)
394394
count++
395395
}
396+
expectedSpecialErrors++
397+
throw "special"
396398
})
397399
.on('error', function(err) {
398400
error = err
@@ -401,6 +403,7 @@ tests = [
401403
setTimeout(function() {
402404
assert(count > 0)
403405
assert(error == null)
406+
assert(hasBeenNull == true)
404407

405408
next()
406409
}, 200)
@@ -471,15 +474,36 @@ tests = [
471474
next()
472475
}, 200)
473476
},
477+
function() {
478+
var TestModel = ferret.model('hello')
479+
TestModel.findOne({ name: 'test123' })
480+
.on('success', function(hello) {
481+
assert(hello == null)
482+
next()
483+
expectedSpecialErrors++
484+
throw "special"
485+
})
486+
.on('error', function(err) {
487+
next(err)
488+
})
489+
},
474490
function() {
475491
assert(processErrors == 0)
476492
next()
493+
},
494+
function() {
495+
assert(specialErrors == expectedSpecialErrors)
496+
next()
477497
}
478498
]
479499

480500
process.on('uncaughtException', function(err){
481-
processErrors++;
482-
console.error(err);
501+
if (err == "special") {
502+
specialErrors++
503+
} else {
504+
processErrors++
505+
console.error(err)
506+
}
483507
})
484508

485-
start()
509+
start()

0 commit comments

Comments
 (0)