Created
July 22, 2011 21:18
-
-
Save foxbunny/1100451 to your computer and use it in GitHub Desktop.
Problematic fixture module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'), | |
assert = require('assert'); | |
// Clean up database | |
function cleanUp(Model, callback) { | |
Model.remove({}, function(err) { | |
Model.find({}, function(err, docs) { | |
assert.equal(docs.length, 0, 'Documents should not exist, found ' + docs.length); | |
callback(err); | |
}); | |
}); | |
} | |
// Test in a clean database | |
function inClean(Model, fixtures, perform) { | |
var series = [ | |
function initialCleanup(callback) { | |
cleanUp(Model, callback); | |
} | |
]; | |
if (fixtures && fixtures.length) { | |
fixtures.forEach(function(fixture) { | |
series.push(function addDocument(callback) { | |
var document = new Model(fixture); | |
document.save(function(err, doc) { | |
callback(err); | |
}); | |
}); | |
}); | |
series.push(function checkDocuments(callback) { | |
Model.find({}, function(err, docs) { | |
assert.equal(docs.length, fixtures.length, | |
'Number of loaded docs incorrect: expected ' + fixtures.length + ' got ' + docs.length); | |
callback(err); | |
}); | |
}); | |
} | |
series.push(function performTest(callback) { | |
perform(callback); | |
}); | |
series.push(function finalCleanup(callback) { | |
cleanUp(Model, callback); | |
}); | |
async.series(series); | |
} | |
exports.cleanUp = cleanUp; | |
exports.inClean = inClean; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment