count.test.js
2.03 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
Sequelize = Support.Sequelize,
current = Support.sequelize,
sinon = require('sinon'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('method count', () => {
before(function() {
this.oldFindAll = Sequelize.Model.findAll;
this.oldAggregate = Sequelize.Model.aggregate;
Sequelize.Model.findAll = sinon.stub().resolves();
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(function() {
Sequelize.Model.findAll = this.oldFindAll;
Sequelize.Model.aggregate = this.oldAggregate;
});
beforeEach(function() {
this.stub = Sequelize.Model.aggregate = sinon.stub().resolves();
});
describe('should pass the same options to model.aggregate as findAndCountAll', () => {
it('with includes', async function() {
const queryObject = {
include: [this.Project]
};
await this.User.count(queryObject);
await this.User.findAndCountAll(queryObject);
const count = this.stub.getCall(0).args;
const findAndCountAll = this.stub.getCall(1).args;
expect(count).to.eql(findAndCountAll);
});
it('attributes should be stripped in case of findAndCountAll', async function() {
const queryObject = {
attributes: ['username']
};
await this.User.count(queryObject);
await this.User.findAndCountAll(queryObject);
const count = this.stub.getCall(0).args;
const findAndCountAll = this.stub.getCall(1).args;
expect(count).not.to.eql(findAndCountAll);
count[2].attributes = undefined;
expect(count).to.eql(findAndCountAll);
});
});
});
});