2014-09-01 17:18:45 +02:00
|
|
|
/*global describe, it*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
module.exports = function(knex) {
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('Aggregate', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 01:00:44 -04:00
|
|
|
it('has a sum', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts').sum('logins').testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select sum(`logins`) from `accounts`',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'sum(`logins`)': 10
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select sum("logins") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
sum: '10'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'sqlite3',
|
|
|
|
'select sum("logins") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'sum("logins")': 10
|
|
|
|
}]
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select sum("logins") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'SUM("LOGINS")': 10
|
|
|
|
}]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2014-01-19 15:00:15 -05:00
|
|
|
it('has an avg', function() {
|
|
|
|
|
2014-04-16 02:50:19 -04:00
|
|
|
return knex('accounts').avg('logins').testSql(function(tester) {
|
2014-08-21 23:39:12 +02:00
|
|
|
|
|
|
|
function checkResRange(key, resp) {
|
|
|
|
return Math.abs(10/6 - +(resp[0][key])) < 0.001;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mysql: 1.6667
|
|
|
|
tester('mysql', 'select avg(`logins`) from `accounts`', [], checkResRange.bind(null, 'avg(`logins`)'));
|
|
|
|
// sqlite: 1.6666666666666667
|
|
|
|
tester('sqlite3', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg("logins")'));
|
|
|
|
// postgres: '1.6666666666666667'
|
|
|
|
tester('postgresql', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg'));
|
|
|
|
// oracle: 1.66666666666667
|
|
|
|
tester('oracle', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'AVG("LOGINS")'));
|
2014-04-16 02:50:19 -04:00
|
|
|
});
|
2014-01-19 15:00:15 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-12 01:00:44 -04:00
|
|
|
it('has a count', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts').count('id').testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select count(`id`) from `accounts`',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count(`id`)': 6
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select count("id") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
count: '6'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'sqlite3',
|
|
|
|
'select count("id") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count("id")': 6
|
|
|
|
}]
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select count("id") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'COUNT("ID")': 6
|
|
|
|
}]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-23 12:10:01 -05:00
|
|
|
it('supports multiple aggregate functions', function() {
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts').count('id').max('logins').min('logins').testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select count(`id`), max(`logins`), min(`logins`) from `accounts`',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count(`id`)': 6,
|
|
|
|
'max(`logins`)': 2,
|
|
|
|
'min(`logins`)': 1
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select count("id"), max("logins"), min("logins") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
count: '6',
|
|
|
|
max: 2,
|
|
|
|
min: 1
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'sqlite3',
|
|
|
|
'select count("id"), max("logins"), min("logins") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count("id")': 6,
|
|
|
|
'max("logins")': 2,
|
|
|
|
'min("logins")': 1
|
|
|
|
}]
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select count("id"), max("logins"), min("logins") from "accounts"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'COUNT("ID")': 6,
|
|
|
|
'MAX("LOGINS")': 2,
|
|
|
|
'MIN("LOGINS")': 1
|
|
|
|
}]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-11-23 12:10:01 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-12 01:00:44 -04:00
|
|
|
it("support the groupBy function", function() {
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts').count('id').groupBy('logins').testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select count(`id`) from `accounts` group by `logins`',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count(`id`)': 2
|
|
|
|
},{
|
|
|
|
'count(`id`)': 4
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select count("id") from "accounts" group by "logins"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
count: '2'
|
|
|
|
},{
|
|
|
|
count: '4'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'sqlite3',
|
|
|
|
'select count("id") from "accounts" group by "logins"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count("id")': 2
|
|
|
|
},{
|
|
|
|
'count("id")': 4
|
|
|
|
}]
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select count("id") from "accounts" group by "logins"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'COUNT("ID")': 2
|
|
|
|
},{
|
|
|
|
'COUNT("ID")': 4
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
|
|
|
|
}).then(function() {
|
|
|
|
return knex('accounts').count('id').groupBy('first_name').testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select count(`id`) from `accounts` group by `first_name`',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count(`id`)': 6
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select count("id") from "accounts" group by "first_name"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
count: '6'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'sqlite3',
|
|
|
|
'select count("id") from "accounts" group by "first_name"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'count("id")': 6
|
|
|
|
}]
|
|
|
|
);
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select count("id") from "accounts" group by "first_name"',
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
'COUNT("ID")': 6
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
});
|
2014-02-21 20:37:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
};
|