destroy.test.js
3.7 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
'use strict';
const chai = require('chai'),
Sequelize = require('../../../../index'),
Op = Sequelize.Op,
expect = chai.expect,
Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('destroy', () => {
beforeEach(function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
[Op.gte]: 5
}
}
},
scopes: {
lowAccess: {
where: {
access_level: {
[Op.lte]: 5
}
}
}
}
});
return this.sequelize.sync({force: true}).then(() => {
const records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
];
return this.ScopeMe.bulkCreate(records);
});
});
it('should apply defaultScope', function() {
return this.ScopeMe.destroy({ where: {}}).bind(this).then(function() {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
expect(users).to.have.length(2);
expect(users[0].get('username')).to.equal('tony');
expect(users[1].get('username')).to.equal('fred');
});
});
it('should be able to override default scope', function() {
return this.ScopeMe.destroy({ where: { access_level: { [Op.lt]: 5 }}}).bind(this).then(function() {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
expect(users).to.have.length(2);
expect(users[0].get('username')).to.equal('tobi');
expect(users[1].get('username')).to.equal('dan');
});
});
it('should be able to unscope destroy', function() {
return this.ScopeMe.unscoped().destroy({ where: {}}).bind(this).then(function() {
return expect(this.ScopeMe.unscoped().findAll()).to.eventually.have.length(0);
});
});
it('should be able to apply other scopes', function() {
return this.ScopeMe.scope('lowAccess').destroy({ where: {}}).bind(this).then(function() {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
expect(users).to.have.length(1);
expect(users[0].get('username')).to.equal('tobi');
});
});
it('should be able to merge scopes with where', function() {
return this.ScopeMe.scope('lowAccess').destroy({ where: { username: 'dan'}}).bind(this).then(function() {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
expect(users).to.have.length(3);
expect(users[0].get('username')).to.equal('tony');
expect(users[1].get('username')).to.equal('tobi');
expect(users[2].get('username')).to.equal('fred');
});
});
it('should work with empty where', function() {
return this.ScopeMe.scope('lowAccess').destroy().then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
expect(users).to.have.length(1);
expect(users[0].get('username')).to.equal('tobi');
});
});
});
});
});