find.test.js
4.65 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
'use strict';
const chai = require('chai'),
Sequelize = require('../../../../index'),
expect = chai.expect,
Op = Sequelize.Op,
Support = require('../../support');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scopes', () => {
beforeEach(function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER,
parent_id: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
[Op.gte]: 5
}
}
},
scopes: {
highValue: {
where: {
other_value: {
[Op.gte]: 10
}
}
},
andScope: {
where: {
[Op.and]: [
{
email: {
[Op.like]: '%@sequelizejs.com'
}
},
{ access_level: 3 }
]
}
}
}
});
this.DefaultScopeExclude = this.sequelize.define('DefaultScopeExclude', {
name: Sequelize.STRING,
other_value: {
type: Sequelize.STRING,
field: 'otherValue'
}
}, {
defaultScope: {
attributes: {
exclude: ['name']
}
}
});
this.ScopeMe.hasMany(this.DefaultScopeExclude);
return this.sequelize.sync({ force: true }).then(() => {
const records = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, parent_id: 1 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, parent_id: 2 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, parent_id: 1 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, parent_id: 1 }
];
return this.ScopeMe.bulkCreate(records);
});
});
it('should be able use where in scope', function() {
return this.ScopeMe.scope({ where: { parent_id: 2 } }).findAll().then(users => {
expect(users).to.have.length(1);
expect(users[0].username).to.equal('tobi');
});
});
it('should be able to combine scope and findAll where clauses', function() {
return this.ScopeMe.scope({ where: { parent_id: 1 } }).findAll({ where: { access_level: 3 } }).then(users => {
expect(users).to.have.length(2);
expect(['tony', 'fred'].includes(users[0].username)).to.be.true;
expect(['tony', 'fred'].includes(users[1].username)).to.be.true;
});
});
it('should be able to use a defaultScope if declared', function() {
return this.ScopeMe.findAll().then(users => {
expect(users).to.have.length(2);
expect([10, 5].includes(users[0].access_level)).to.be.true;
expect([10, 5].includes(users[1].access_level)).to.be.true;
expect(['dan', 'tobi'].includes(users[0].username)).to.be.true;
expect(['dan', 'tobi'].includes(users[1].username)).to.be.true;
});
});
it('should be able to handle $and in scopes', function() {
return this.ScopeMe.scope('andScope').findAll().then(users => {
expect(users).to.have.length(1);
expect(users[0].username).to.equal('tony');
});
});
describe('should not overwrite', () => {
it('default scope with values from previous finds', function() {
return this.ScopeMe.findAll({ where: { other_value: 10 } }).then(users => {
expect(users).to.have.length(1);
return this.ScopeMe.findAll();
}).then(users => {
// This should not have other_value: 10
expect(users).to.have.length(2);
});
});
it('other scopes with values from previous finds', function() {
return this.ScopeMe.scope('highValue').findAll({ where: { access_level: 10 } }).then(users => {
expect(users).to.have.length(1);
return this.ScopeMe.scope('highValue').findAll();
}).then(users => {
// This should not have other_value: 10
expect(users).to.have.length(2);
});
});
});
it('should have no problem performing findOrCreate', function() {
return this.ScopeMe.findOrCreate({ where: { username: 'fake' } }).then(([user]) => {
expect(user.username).to.equal('fake');
});
});
it('should work when included with default scope', function() {
return this.ScopeMe.findOne({
include: [this.DefaultScopeExclude]
});
});
});
});