Skip to content

Commit b85a80c

Browse files
committed
Fixes some aggregate bugs, updates aggregate tests
1 parent b74e468 commit b85a80c

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/AggregateFunctions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function AggregateFunctions(opts) {
6060
return this;
6161
},
6262
as: function (alias) {
63-
if (aggregates.length === 0) {
63+
if (aggregates.length === 0 || (aggregates.length == 1 && aggregates[0].length === 0)) {
6464
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "No aggregate functions defined yet");
6565
}
6666

@@ -78,7 +78,7 @@ function AggregateFunctions(opts) {
7878
aggregates[aggregates.length - 1].push({ f: fun, alias: aggregateAlias(fun, args) });
7979
}
8080

81-
if (fun == "distinct") {
81+
if (fun.toLowerCase() == "distinct") {
8282
used_distinct = true;
8383
}
8484

test/integration/model-aggregate.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,56 @@ describe("Model.aggregate()", function() {
5555
});
5656
});
5757

58+
describe("with call()", function () {
59+
before(setup());
60+
61+
it("should accept a function", function (done) {
62+
Person.aggregate().call('COUNT').get(function (err, count) {
63+
should.equal(err, null);
64+
65+
count.should.equal(3);
66+
67+
return done();
68+
});
69+
});
70+
71+
it("should accept arguments to the funciton as an Array", function (done) {
72+
Person.aggregate().call('COUNT', [ 'id' ]).get(function (err, count) {
73+
should.equal(err, null);
74+
75+
count.should.equal(3);
76+
77+
return done();
78+
});
79+
});
80+
81+
describe("if function is DISTINCT", function () {
82+
it("should work as calling .distinct() directly", function (done) {
83+
Person.aggregate().call('DISTINCT', [ 'name' ]).as('name').order('name').get(function (err, rows) {
84+
should.equal(err, null);
85+
86+
should(Array.isArray(rows));
87+
rows.length.should.equal(2);
88+
89+
rows[0].should.equal('Jane Doe');
90+
rows[1].should.equal('John Doe');
91+
92+
return done();
93+
});
94+
});
95+
});
96+
});
97+
98+
describe("with as() without previous aggregates", function () {
99+
before(setup());
100+
101+
it("should throw", function (done) {
102+
Person.aggregate().as.should.throw();
103+
104+
return done();
105+
});
106+
});
107+
58108
describe("with select() without arguments", function () {
59109
before(setup());
60110

0 commit comments

Comments
 (0)