count.test.js
2.16 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
'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'), () => {
describe('method count', () => {
before(() => {
this.oldFindAll = current.Model.findAll;
this.oldAggregate = current.Model.aggregate;
current.Model.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);
});
after(() => {
current.Model.findAll = this.oldFindAll;
current.Model.aggregate = this.oldAggregate;
});
beforeEach(() => {
this.stub = current.Model.aggregate = sinon.stub().returns(Promise.resolve());
});
describe('should pass the same options to model.aggregate as findAndCount', () => {
it('with includes', () => {
const queryObject = {
include: [this.Project]
};
return this.User.count(queryObject)
.then(() => this.User.findAndCount(queryObject))
.then(() => {
const count = this.stub.getCall(0).args;
const findAndCount = this.stub.getCall(1).args;
expect(count).to.eql(findAndCount);
});
});
it('attributes should be stripped in case of findAndCount', () => {
const queryObject = {
attributes: ['username']
};
return this.User.count(queryObject)
.then(() => this.User.findAndCount(queryObject))
.then(() => {
const count = this.stub.getCall(0).args;
const findAndCount = this.stub.getCall(1).args;
expect(count).not.to.eql(findAndCount);
count[2].attributes = undefined;
expect(count).to.eql(findAndCount);
});
});
});
});
});