A lot more tests

This commit is contained in:
Timothy Warren 2014-10-24 10:30:54 -04:00
parent 48f3597b19
commit 3b1284ec95
5 changed files with 202 additions and 13 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build
build/*
coverage
coverage/*

View File

@ -1,9 +1,14 @@
module.exports = function(grunt) { module.exports = function(grunt) {
'use strict'; 'use strict';
var tests = 'tests/**/*_test.js';
var src = 'lib/**/*.js';
var reportDir = 'coverage';
// Project configuration // Project configuration
grunt.initConfig({ grunt.initConfig({
pkg: grunt.file.readJSON('package.json'), pkg: grunt.file.readJSON('package.json'),
clean: [ 'build' ],
jsdoc: { jsdoc: {
dist: { dist: {
src: ['lib/*.js', 'README.md'], src: ['lib/*.js', 'README.md'],
@ -15,18 +20,45 @@ module.exports = function(grunt) {
} }
}, },
nodeunit: { nodeunit: {
all: ['tests/**/*_test.js'], all: [tests],
options: { options: {
reporter: 'verbose' reporter: 'verbose'
} }
},
watch: {
files: [src, tests],
tasks: 'default'
},
instrument: {
files: src,
options: {
lazy: true,
basePath : 'build/instrument/'
}
},
storeCoverage : {
options : {
dir: reportDir
}
},
makeReport: {
src: 'build/reports/**/*.json',
options: {
type: ['lcov', 'html'],
dir: reportDir,
print: 'detail'
}
} }
}); });
grunt.loadNpmTasks('grunt-jsdoc'); grunt.loadNpmTasks('grunt-jsdoc');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit'); grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-istanbul');
grunt.registerTask('default', ['nodeunit','jsdoc']); grunt.registerTask('default', ['nodeunit','jsdoc']);
grunt.registerTask('tests', 'nodeunit'); grunt.registerTask('tests', 'nodeunit');
grunt.registerTask('docs', 'jsdoc'); grunt.registerTask('docs', 'jsdoc');
grunt.registerTask('cover', ['clean', 'instrument', 'tests', 'storeCoverage', 'makeReport']);
}; };

View File

@ -192,12 +192,12 @@ var QueryBuilder = function(driver, adapter) {
Object.keys(state.whereMap).forEach(function(field) { Object.keys(state.whereMap).forEach(function(field) {
// Split each key by spaces, in case there // Split each key by spaces, in case there
// is an operator such as >, <, !=, etc. // is an operator such as >, <, !=, etc.
var fieldArray = field.split(' '); var fieldArray = field.trim().split(' ').map(helpers.stringTrim);
var item = driver.quoteIdentifiers(fieldArray[0]); var item = driver.quoteIdentifiers(fieldArray[0]);
// Simple key value, or an operator? // Simple key value, or an operator?
item += (fieldArray.length === 1) ? '=?' : " " + fieldArray[1] + " ?"; item += (fieldArray.length === 1 || fieldArray[1] === '') ? '=?' : " " + fieldArray[1] + " ?";
var firstItem = state.queryMap[0], var firstItem = state.queryMap[0],
lastItem = state.queryMap[state.queryMap.length - 1]; lastItem = state.queryMap[state.queryMap.length - 1];
@ -244,25 +244,29 @@ var QueryBuilder = function(driver, adapter) {
}); });
}); });
}, },
whereIn: function(key, val, inClause, conj) { whereIn: function(/*key, val, inClause, conj*/) {
key = driver.quoteIdentifiers(key); var args = getArgs('key:string, val:array, inClause:string, conj:string', arguments);
var params = [];
params.fill('?', 0, val.length);
val.forEach(function(value) { args.key = driver.quoteIdentifiers(args.key);
var params = new Array(args.val.length);
params.fill('?');
args.val.forEach(function(value) {
state.whereValues.push(value); state.whereValues.push(value);
}); });
conj = (state.queryMap.length > 0) ? " " + conj + " " : ' WHERE '; args.conj = (state.queryMap.length > 0) ? " " + args.conj + " " : ' WHERE ';
var str = key + " " + inClause + " (" + params.join(',') + ") "; var str = args.key + " " + args.inClause + " (" + params.join(',') + ") ";
_p.appendMap(conj, str, 'whereIn'); _p.appendMap(args.conj, str, 'whereIn');
}, },
run: function(type, table, callback, sql, vals) { run: function(type, table, callback, sql, vals) {
if ( ! sql) if ( ! sql)
{ {
sql = _p.compile(type, table); sql = _p.compile(type, table);
} }
//console.log(sql);
//console.log('------------------------');
if ( ! vals) if ( ! vals)
{ {

View File

@ -17,7 +17,9 @@
}, },
"devDependencies": { "devDependencies": {
"grunt": "^0.4.5", "grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-nodeunit": "^0.4.1", "grunt-contrib-nodeunit": "^0.4.1",
"grunt-istanbul": "^0.3.0",
"grunt-jsdoc": ">=0.6.1", "grunt-jsdoc": ">=0.6.1",
"jsdoc": "^3.3.0-alpha9", "jsdoc": "^3.3.0-alpha9",
"mysql": "^2.5.2", "mysql": "^2.5.2",
@ -30,5 +32,8 @@
"mysql2": "", "mysql2": "",
"pg": "" "pg": ""
}, },
"license": "MIT" "license": "MIT",
"scripts": {
"test": "grunt tests"
}
} }

View File

@ -84,17 +84,161 @@ module.exports = (function() {
.orderBy('id, key') .orderBy('id, key')
.get(base.testCallback.bind(test, test)); .get(base.testCallback.bind(test, test));
test.done();
},
'Select get': function(test) {
base.qb.select('id, key as k, val')
.get('create_test', 2, 1, base.testCallback.bind(test, test));
test.done();
},
'Select from get': function(test) {
base.qb.select('id, key as k, val')
.from('create_test ct')
.where('id >', 1)
.get(base.testCallback.bind(test, test));
test.done();
},
'Select from limit get': function(test) {
base.qb.select('id, key as k, val')
.from('create_test ct')
.where('id >', 1)
.limit(3)
.get(base.testCallback.bind(test, test));
test.done(); test.done();
} }
}, },
'Grouping tests' : { 'Grouping tests' : {
'Using grouping method': function(test) {
base.qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.limit(2, 1)
.get(base.testCallback.bind(test, test));
test.done();
},
'Using or grouping method': function(test) {
base.qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.orGroupStart()
.where('id', 0)
.groupEnd()
.limit(2, 1)
.get(base.testCallback.bind(test, test));
test.done();
},
'Using or not grouping method': function(test) {
base.qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.orNotGroupStart()
.where('id', 0)
.groupEnd()
.limit(2, 1)
.get(base.testCallback.bind(test, test));
test.done();
}
}, },
'Where in tests' : { 'Where in tests' : {
'Where in': function(test) {
base.qb.from('create_test')
.whereIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(test, test));
test.done();
},
'Or Where in': function(test) {
base.qb.from('create_test')
.where('key', 'false')
.orWhereIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(test, test));
test.done();
},
'Where Not in': function(test) {
base.qb.from('create_test')
.where('key', 'false')
.whereNotIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(test, test));
test.done();
},
'Or Where Not in': function(test) {
base.qb.from('create_test')
.where('key', 'false')
.orWhereNotIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(test, test));
test.done();
}
}, },
'Query modifier tests': { 'Query modifier tests': {
'Order By': function(test) {
base.qb.select('id, key as k, val')
.from('create_test')
.where('id >', 0)
.where('id <', 9000)
.orderBy('id', 'DESC')
.orderBy('k', "ASC")
.limit(5, 2)
.get(base.testCallback.bind(test, test));
test.done();
},
'Group by': function(test) {
base.qb.select('id, key as k, val')
.from('create_test')
.where('id >', 0)
.where('id <', 9000)
.groupBy('k')
.groupBy(['id', 'val'])
.orderBy('id', 'DESC')
.orderBy('k', "ASC")
.limit(5, 2)
.get(base.testCallback.bind(test, test));
test.done();
},
'Or Where': function(test) {
base.qb.select('id, key as k, val')
.from('create_test')
.where(' id ', 1)
.orWhere('key >', 0)
.limit(2, 1)
.get(base.testCallback.bind(test, test));
test.done();
},
'Like' : function(test) {
base.qb.from('create_test')
.like('key', 'og')
.get(base.testCallback.bind(test, test));
test.done();
},
'Or Like': function(test) {
base.qb.from('create_test')
.like('key', 'og')
.orLike('key', 'val')
.get(base.testCallback.bind(test, test));
test.done();
}
}, },
'DB update tests' : { 'DB update tests' : {