95+% code coverage

This commit is contained in:
Timothy Warren 2015-12-07 14:33:48 -05:00
parent 816911b54d
commit 2cc48b56f8
6 changed files with 407 additions and 0 deletions

View File

@ -117,11 +117,144 @@ module.exports.tests = {
where: ['id', 'CEILING(SQRT(88))'], where: ['id', 'CEILING(SQRT(88))'],
get: [] get: []
} }
},
'Where in tests': {
'Where in': {
from: ['create_test'],
whereIn: ['id', [0, 6, 56, 563, 341]],
get: []
},
'Or Where in': {
from: ['create_test'],
where: ['key', 'false'],
orWhereIn: ['id', [0, 6, 56, 563, 341]],
get: []
},
'Where Not in': {
from: ['create_test'],
where: ['key', 'false'],
whereNotIn: ['id', [0, 6, 56, 563, 341]],
get: []
},
'Or Where Not in': {
from: ['create_test'],
where: ['key', 'false'],
orWhereNotIn: ['id', [0, 6, 56, 563, 341]],
get: []
}
},
'Query modifier tests': {
'Order By': {
select: ['id, key as k, val'],
from: ['create_test'],
where: [
'multiple',
['id >', 0],
['id <', 9000]
],
orderBy: [
'multiple',
['id', 'DESC'],
['k', "ASC"]
],
limit: [5, 2],
get: []
},
'Group By': {
select: ['id, key as k, val'],
from: ['create_test'],
where: [
'multiple',
['id >', 0],
['id <', 9000]
],
groupBy: [
'multiple',
['k'],
[['id', 'val']]
],
orderBy: [
'multiple',
['id', 'DESC'],
['k', "ASC"]
],
limit: [5,2],
get: []
},
'Or Where': {
select: ['id, key as k, val'],
from: ['create_test'],
where: [' id ', 1],
orWhere: ['key > ', 0],
limit: [2, 1],
get: []
},
'Like': {
from: ['create_test'],
like: ['key', 'og'],
get: []
},
'Or Like': {
from: ['create_test'],
like: ['key', 'og'],
orLike: ['key', 'val'],
get: []
},
'Not Like': {
from: ['create_test'],
like: ['key', 'og', 'before'],
notLike: ['key', 'val'],
get: []
},
'Or Not Like': {
from: ['create_test'],
like: ['key', 'og', 'before'],
orNotLike: ['key', 'val'],
get: []
},
'Like Before': {
from: ['create_test'],
like: ['key', 'og', 'before'],
get: []
},
'Like After': {
from: ['create_test'],
like: ['key', 'og', 'after'],
get: []
},
'Basic Join': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id'],
get: []
},
'Left Join': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id', 'left'],
get: []
},
'Inner Join': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id', 'inner'],
get: []
},
'Join with multiple where values': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id', 'inner'],
where: [{
'ct.id < ': 3,
'ct.key ': 'foo'
}],
get: []
}
} }
}; };
let expect = require('chai').expect,
helpers = require('../../lib/helpers'),
State = require('../../lib/State');
module.exports.runner = (tests, qb, callback) => { module.exports.runner = (tests, qb, callback) => {
Object.keys(tests).forEach(suiteName => { Object.keys(tests).forEach(suiteName => {
suite(suiteName, () => { suite(suiteName, () => {
@ -154,4 +287,193 @@ module.exports.runner = (tests, qb, callback) => {
}); });
}); });
}); });
suite('DB update tests', () => {
setup(done => {
let sql = qb.driver.truncate('create_test');
qb.adapter.execute(sql, (err, res) => {
done();
});
});
test('Test Insert', done => {
qb.set('id', 98)
.set('key', "84")
.set('val', new Buffer("120"))
.insert('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Test Insert Object', done => {
qb.insert('create_test', {
id: 587,
key: 1,
val: new Buffer('2')
}, (err, rows) => {
return callback(err, done);
});
});
test('Test Update', done => {
qb.where('id', 7)
.update('create_test', {
id: 7,
key: 'gogle',
val: new Buffer('non-word')
}, (err, rows) => {
return callback(err, done);
});
});
test('Test set Array Update', done => {
let object = {
id: 22,
key: 'gogle',
val: new Buffer('non-word')
};
qb.set(object)
.where('id', 22)
.update('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Test where set update', done => {
qb.where('id', 36)
.set('id', 36)
.set('key', 'gogle')
.set('val', new Buffer('non-word'))
.update('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Test delete', done => {
qb.delete('create_test', {id: 5}, (err, rows) => {
return callback(err, done);
});
});
test('Delete with where', done => {
qb.where('id', 5)
.delete('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Delete multiple where values', done => {
qb.delete('create_test', {
id: 5,
key: 'gogle'
}, (err, rows) => {
return callback(err, done);
});
});
});
suite('Grouping tests', () => {
test('Using grouping method', done => {
qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.limit(2, 1)
.get((err, rows) => {
return callback(err, done);
});
});
test('Using where first grouping', done => {
qb.select('id, key as k, val')
.from('create_test')
.where('id !=', 5)
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.limit(2, 1)
.get((err, rows) => {
return callback(err, done);
});
});
test('Using or grouping method', done => {
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((err, rows) => {
return callback(err, done);
});
});
test('Using or not grouping method', done => {
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((err, rows) => {
return callback(err, done);
});
});
});
suite('Get compiled tests', () => {
test('select', () => {
let sql = qb.select('id')
.from('create_test')
.getCompiledSelect(true);
expect(helpers.isString(sql)).to.be.true;
});
test('select from', () => {
let sql = qb.select('id')
.getCompiledSelect('create_test', true);
expect(helpers.isString(sql)).to.be.true;
});
test('insert', () => {
let sql = qb.set('id', 3)
.getCompiledInsert('create_test');
expect(helpers.isString(sql)).to.be.true;
});
test('update', () => {
let sql = qb.set('id', 3)
.where('id', 5)
.getCompiledUpdate('create_test');
expect(helpers.isString(sql)).to.be.true;
});
test('delete', () => {
let sql = qb.where('id', 5)
.getCompiledDelete('create_test');
expect(helpers.isString(sql)).to.be.true;
});
});
suite('Misc tests', () => {
test('Get State', () => {
qb.select('foo')
.from('bar')
.where('baz', 'foobar');
let state = new State();
expect(JSON.stringify(state)).to.not.be.deep.equal(qb.getState());
});
test('Reset State', () => {
qb.select('foo')
.from('bar')
.where('baz', 'foobar');
qb.resetQuery();
let state = new State();
expect(qb.getState()).to.be.deep.equal(state);
})
});
}; };

View File

@ -53,6 +53,26 @@ if (connection) {
expect(nodeQuery.getQuery()) expect(nodeQuery.getQuery())
.to.be.deep.equal(qb); .to.be.deep.equal(qb);
}); });
test('Test Insert Batch', done => {
let data = [{
id: 544,
key: 3,
val: new Buffer('7')
}, {
id: 89,
key: 34,
val: new Buffer("10 o'clock")
}, {
id: 48,
key: 403,
val: new Buffer('97')
}];
qb.insertBatch('create_test', data, (err, rows) => {
expect(err).is.not.ok;
return done();
});
});
}); });
suiteTeardown(() => { suiteTeardown(() => {
connection.close(); connection.close();

View File

@ -32,6 +32,26 @@ suite('Mysql2 adapter tests', () => {
expect(nodeQuery.getQuery()) expect(nodeQuery.getQuery())
.to.be.deep.equal(qb); .to.be.deep.equal(qb);
}); });
test('Test Insert Batch', done => {
let data = [{
id: 544,
key: 3,
val: new Buffer('7')
}, {
id: 89,
key: 34,
val: new Buffer("10 o'clock")
}, {
id: 48,
key: 403,
val: new Buffer('97')
}];
qb.insertBatch('create_test', data, (err, rows) => {
expect(err).is.not.ok;
return done();
});
});
}); });
suiteTeardown(() => { suiteTeardown(() => {
connection.end(); connection.end();

View File

@ -32,6 +32,26 @@ suite('Mysql adapter tests', () => {
expect(nodeQuery.getQuery()) expect(nodeQuery.getQuery())
.to.be.deep.equal(qb); .to.be.deep.equal(qb);
}); });
test('Test Insert Batch', done => {
let data = [{
id: 544,
key: 3,
val: new Buffer('7')
}, {
id: 89,
key: 34,
val: new Buffer("10 o'clock")
}, {
id: 48,
key: 403,
val: new Buffer('97')
}];
qb.insertBatch('create_test', data, (err, rows) => {
expect(err).is.not.ok;
return done();
});
});
}); });
suiteTeardown(() => { suiteTeardown(() => {
connection.end(); connection.end();

View File

@ -33,6 +33,11 @@ suite('Firebird adapter tests', () => {
expect(nodeQuery.getQuery()) expect(nodeQuery.getQuery())
.to.be.deep.equal(qb); .to.be.deep.equal(qb);
}); });
test('insertBatch throws error', () => {
expect(() => {
qb.driver.insertBatch('create_test', []);
}).to.throw(Error, "Not Implemented");
});
}); });
suiteTeardown(() => { suiteTeardown(() => {
db.detach(); db.detach();

View File

@ -35,6 +35,26 @@ suite('Pg adapter tests', () => {
expect(nodeQuery.getQuery()) expect(nodeQuery.getQuery())
.to.be.deep.equal(qb); .to.be.deep.equal(qb);
}); });
test('Test Insert Batch', done => {
let data = [{
id: 544,
key: 3,
val: new Buffer('7')
}, {
id: 89,
key: 34,
val: new Buffer("10 o'clock")
}, {
id: 48,
key: 403,
val: new Buffer('97')
}];
qb.insertBatch('create_test', data, (err, rows) => {
expect(err).is.not.ok;
return done();
});
});
}); });
suiteTeardown(() => { suiteTeardown(() => {
connection.end(); connection.end();