adding several tests from recent modifications

This commit is contained in:
Tim Griesser 2014-02-21 20:37:24 -05:00
parent d5da83dbfc
commit 8bddaea5e2
5 changed files with 43 additions and 2 deletions

View File

@ -403,6 +403,7 @@ module.exports = function(client) {
},
offset: single(function(value) {
if (value == null) return {type: 'offset'};
return {
type: 'offset',
value: 'offset ' + this._parameter(value)
@ -410,6 +411,7 @@ module.exports = function(client) {
}),
limit: single(function(value) {
if (value == null) return {type: 'limit'};
return {
type: 'limit',
value: 'limit ' + this._parameter(value)
@ -436,6 +438,11 @@ module.exports = function(client) {
return this._aggregate('sum', column);
},
// Retrieve the average of the values of a given column.
avg: function(column) {
return this._aggregate('avg', column);
},
// Increments a column's value by the specified amount.
increment: function(column, amount) {
return this._counter(column, amount);
@ -509,6 +516,7 @@ module.exports = function(client) {
// Executes a delete statement on the query;
del: function() {
this._method = 'delete';
return this;
},
truncate: function() {
@ -540,9 +548,11 @@ module.exports = function(client) {
options: function(opts) {
this.flags.options = this.flags.options || [];
this.flags.options.push(opts);
return this;
},
debug: function(val) {
this.flags.debug = (val == null ? true : val);
return this;
},
// ----------------------------------------------------------------------

View File

@ -14,7 +14,7 @@ module.exports = function(client) {
'whereNull', 'orWhereNull', 'whereNotNull', 'orWhereNotNull', 'whereBetween',
'orWhereBetween', 'whereNotBetween', 'groupBy', 'orderBy', 'union', 'unionAll',
'having', 'havingRaw', 'orHaving', 'orHavingRaw', 'offset', 'limit', 'count',
'min', 'max', 'sum', 'increment', 'decrement', 'select', 'pluck', 'insert',
'min', 'max', 'sum', 'avg', 'increment', 'decrement', 'select', 'pluck', 'insert',
'update', 'delete', 'del', 'returning', 'truncate',
'forUpdate', 'forShare',

View File

@ -167,6 +167,23 @@ module.exports = function(knex) {
});
it('has an avg', function() {
return knex('accounts').avg('logins').testSql(function(tester) {
tester('mysql', 'select avg(`logins`) from `accounts`', [], [{
'avg(`logins`)': 1.6667
}]);
tester('postgresql', 'select avg("logins") from "accounts"', [], [{
avg: '1.6666666666666667'
}]);
tester('sqlite3', 'select avg("logins") from "accounts"', [], [{
'avg("logins")': 1.6666666666666667
}]);
});
});
});

View File

@ -515,6 +515,11 @@ module.exports = function(postgresclient, mysqlclient, sqlite3client) {
expect(chain.sql).to.equal('select * from "student" left outer join "student_languages" on "student"."id" = "student_languages"."student_id" and "student_languages"."code" = ?');
});
it('should not break with null call #182', function() {
chain = sql.from('test').limit(null).offset(null).toSql();
expect(chain.sql).to.eql('select * from "test"');
});
});
};

View File

@ -396,6 +396,15 @@ module.exports = function(client) {
expect(tableSql[0].sql).to.equal('alter table `users` add `foo` blob');
});
});
it('is possible to set raw statements in defaultTo, #146', function() {
tableSql = new SchemaBuilder().createTable('default_raw_test', function(t) {
t.timestamp('created_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
}).toSql();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('create table `default_raw_test` (`created_at` timestamp default CURRENT_TIMESTAMP)');
});
});
};