Repository navigation
Expand file tree
/
Copy pathmodel-aggregate.js
More file actions
167 lines (128 loc) · 3.57 KB
/
Copy pathmodel-aggregate.js
File metadata and controls
167 lines (128 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var should = require('should');
var helper = require('../support/spec_helper');
var ORM = require('../../');
describe("Model.aggregate()", function() {
var db = null;
var Person = null;
var setup = function () {
return function (done) {
Person = db.define("person", {
name : String
});
return helper.dropSync(Person, function () {
Person.create([{
id : 1,
name: "John Doe"
}, {
id : 2,
name: "Jane Doe"
}, {
id : 3,
name: "John Doe"
}], done);
});
};
};
before(function (done) {
helper.connect(function (connection) {
db = connection;
return done();
});
});
after(function () {
return db.close();
});
describe("with multiple methods", function () {
before(setup());
it("should return value for everyone of them", function (done) {
Person.aggregate().count('id').min('id').max('id').get(function (err, count, min, max) {
should.equal(err, null);
count.should.equal(3);
min.should.equal(1);
max.should.equal(3);
return done();
});
});
});
describe("with select() without arguments", function () {
before(setup());
it("should throw", function (done) {
Person.aggregate().select.should.throw();
return done();
});
});
describe("with get() without callback", function () {
before(setup());
it("should throw", function (done) {
Person.aggregate().count('id').get.should.throw();
return done();
});
});
describe("with get() without aggregates", function () {
before(setup());
it("should throw", function (done) {
(function () {
Person.aggregate().get(function () {});
}).should.throw();
return done();
});
});
describe("with distinct()", function () {
before(setup());
it("should return a list of distinct properties", function (done) {
Person.aggregate().distinct('name').get(function (err, names) {
should.equal(err, null);
names.should.be.a("object");
names.should.have.property("length", 2);
return done();
});
});
describe("with limit(1)", function () {
it("should return only one value", function (done) {
Person.aggregate().distinct('name').limit(1).order("name").get(function (err, names) {
should.equal(err, null);
names.should.be.a("object");
names.should.have.property("length", 1);
names[0].should.equal("Jane Doe");
return done();
});
});
});
describe("with limit(1, 1)", function () {
it("should return only one value", function (done) {
Person.aggregate().distinct('name').limit(1, 1).order("name").get(function (err, names) {
should.equal(err, null);
names.should.be.a("object");
names.should.have.property("length", 1);
names[0].should.equal("John Doe");
return done();
});
});
});
});
describe("with groupBy()", function () {
before(setup());
it("should return items grouped by property", function (done) {
Person.aggregate().count().groupBy('name').get(function (err, rows) {
should.equal(err, null);
rows.should.be.a("object");
rows.should.have.property("length", 2);
(rows[0].count + rows[1].count).should.equal(3); // 1 + 2
return done();
});
});
describe("with order()", function () {
before(setup());
it("should order items", function (done) {
Person.aggregate().count().groupBy('name').order('-count').get(function (err, rows) {
should.equal(err, null);
rows.should.be.a("object");
rows.should.have.property("length", 2);
rows[0].count.should.equal(2);
rows[1].count.should.equal(1);
return done();
});
});
});
});
});