count.test.js
2.38 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
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, sinon = require('sinon')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, Promise = require('bluebird');
describe(Support.getTestDialectTeaser('Model'), function () {
describe('method count', function () {
before(function () {
this.oldFindAll = current.Model.prototype.findAll;
this.oldAggregate = current.Model.prototype.aggregate;
current.Model.prototype.findAll = sinon.stub().returns(Promise.resolve());
this.User = current.define('User', {
username: DataTypes.STRING,
age: DataTypes.INTEGER
});
this.Project = current.define('Project', {
name: DataTypes.STRING
});
this.User.hasMany(this.Project);
this.Project.belongsTo(this.User);
});
beforeEach(function () {
this.stub = current.Model.prototype.aggregate = sinon.stub().returns(Promise.resolve());
});
after(function () {
current.Model.prototype.findAll = this.oldFindAll;
current.Model.prototype.aggregate = this.oldAggregate;
});
describe('should pass the same options to model.aggregate as findAndCount', function () {
it('with includes', function () {
var self = this;
var queryObject = {
include: [self.Project]
};
return self.User.count(queryObject).then(function () {
return self.User.findAndCount(queryObject);
}).then(function () {
var count = self.stub.getCall(0).args;
var findAndCount = self.stub.getCall(1).args;
expect(count).to.eql(findAndCount);
});
});
it('attributes should be stripped in case of findAndCount', function () {
var self = this;
var queryObject = {
attributes: ['username']
};
return self.User.count(queryObject).then(function () {
return self.User.findAndCount(queryObject);
}).then(function () {
var count = self.stub.getCall(0).args;
var findAndCount = self.stub.getCall(1).args;
expect(count[2].attributes).to.eql(['username']);
expect(count).not.to.eql(findAndCount);
count[2].attributes = undefined;
expect(count).to.eql(findAndCount);
});
});
});
});
});