Skip to content

Instantly share code, notes, and snippets.

@flockonus
Created August 23, 2011 21:01
Show Gist options
  • Save flockonus/1166520 to your computer and use it in GitHub Desktop.
Save flockonus/1166520 to your computer and use it in GitHub Desktop.
Testing models using Expresso
GLOBAL.testing_time = true
var models = require('../models.js'),
assert = require('assert')
exports.isModels = function(){
assert.isDefined( models, "Expected model to be defined here!" )
assert.type( models, 'object', "models should be a Object" )
}
// Run a count against a model
function verify_row_presence( model_name ){
models[model_name].count().on('success', function(num){
exports["Row Presence"+model_name] = function(){
assert.ok( num, "Table '"+model_name+"' has no row inside. Expected at least one!" )
}
})
}
for( prop in models ){
if( prop != 'db' && models.hasOwnProperty( prop )){
verify_row_presence( prop )
}
}
/* OUTPUTS:
fps@piolho:~/workspace/dbserver$ expresso test/models-test.js
Executing: SELECT count(*) FROM `companies`;
Executing: SELECT count(*) FROM `employees`;
Executing: SELECT count(*) FROM `games`;
Executing: SELECT count(*) FROM `levels`;
Executing: SELECT count(*) FROM `actionstypes`;
Executing: SELECT count(*) FROM `actions`;
Executing: SELECT count(*) FROM `employeesgames`;
Executing: SELECT count(*) FROM `companiesgames`;
100% 1 tests
*/
GLOBAL.testing_time = true
var models = require('../models.js'),
assert = require('assert')
// returns a function, binded to 1 arg
function fn_caller(fn, arg ){
return function(){
fn.call( {}, arg)
}
}
exports.isModels = function(){
assert.isDefined( models, "Expected model to be defined here!" )
assert.type( models, 'object', "models should be a Object" )
}
// Run a count against a model
function verify_row_presence( model_name ){
models[model_name].count().on('success', function(num){
assert.ok( num, "Table '"+model_name+"' has no row inside. Expected at least one!" )
})
}
for( prop in models ){
if( prop != 'db' && models.hasOwnProperty( prop )){
exports["Row Presence"+prop] = fn_caller( verify_row_presence, prop )
}
}
/* OUTPUTS
fps@piolho:~/workspace/dbserver$ expresso test/models-test.js
Executing: SELECT count(*) FROM `companies`;
Executing: SELECT count(*) FROM `employees`;
Executing: SELECT count(*) FROM `games`;
Executing: SELECT count(*) FROM `levels`;
Executing: SELECT count(*) FROM `actionstypes`;
Executing: SELECT count(*) FROM `actions`;
Executing: SELECT count(*) FROM `employeesgames`;
Executing: SELECT count(*) FROM `companiesgames`;
100% 9 tests
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment