count.test.js
4.78 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
'use strict';
const chai = require('chai'),
Sequelize = require('../../../../index'),
Op = Sequelize.Op,
expect = chai.expect,
Support = require('../../support');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('count', () => {
beforeEach(async function() {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
aliasValue: {
field: 'alias_value',
type: Sequelize.INTEGER
},
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
[Op.gte]: 5
}
},
attributes: ['id', 'username', 'email', 'access_level']
},
scopes: {
lowAccess: {
where: {
access_level: {
[Op.lte]: 5
}
}
},
withOrder: {
order: ['username']
},
withInclude: {
include: [{
model: this.Child,
where: {
priority: 1
}
}]
},
withIncludeFunction: () => {
return {
include: [{
model: this.Child,
where: {
priority: 1
}
}]
};
},
withIncludeFunctionAndStringAssociation: () => {
return {
include: [{
association: 'Children',
where: {
priority: 1
}
}]
};
},
withAliasedField: {
where: {
aliasValue: { [Op.ne]: 1 }
}
}
}
});
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
await this.sequelize.sync({ force: true });
const records0 = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, aliasValue: 12 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, aliasValue: 5 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, aliasValue: 1 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, aliasValue: 10 }
];
await this.ScopeMe.bulkCreate(records0);
const records = await this.ScopeMe.findAll();
await Promise.all([
records[0].createChild({
priority: 1
}),
records[1].createChild({
priority: 2
})
]);
});
it('should apply defaultScope', async function() {
await expect(this.ScopeMe.count()).to.eventually.equal(2);
});
it('should be able to override default scope', async function() {
await expect(this.ScopeMe.count({ where: { access_level: { [Op.gt]: 5 } } })).to.eventually.equal(1);
});
it('should be able to unscope', async function() {
await expect(this.ScopeMe.unscoped().count()).to.eventually.equal(4);
});
it('should be able to apply other scopes', async function() {
await expect(this.ScopeMe.scope('lowAccess').count()).to.eventually.equal(3);
});
it('should be able to merge scopes with where', async function() {
await expect(this.ScopeMe.scope('lowAccess').count({ where: { username: 'dan' } })).to.eventually.equal(1);
});
it('should be able to merge scopes with where on aliased fields', async function() {
await expect(this.ScopeMe.scope('withAliasedField').count({ where: { aliasValue: 5 } })).to.eventually.equal(1);
});
it('should ignore the order option if it is found within the scope', async function() {
await expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4);
});
it('should be able to use where on include', async function() {
await expect(this.ScopeMe.scope('withInclude').count()).to.eventually.equal(1);
});
it('should be able to use include with function scope', async function() {
await expect(this.ScopeMe.scope('withIncludeFunction').count()).to.eventually.equal(1);
});
it('should be able to use include with function scope and string association', async function() {
await expect(this.ScopeMe.scope('withIncludeFunctionAndStringAssociation').count()).to.eventually.equal(1);
});
});
});
});