scope.test.js
1.62 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
'use strict';
const chai = require('chai'),
Sequelize = require('../../../index'),
expect = chai.expect,
Support = require(__dirname + '/../support');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
beforeEach(function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
scopes: {
lowAccess: {
attributes: ['other_value', 'access_level'],
where: {
access_level: {
lte: 5
}
}
},
withName: {
attributes: ['username']
}
}
});
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 be able to merge attributes as array', function() {
return this.ScopeMe.scope('lowAccess', 'withName').findOne()
.then(record => {
expect(record.other_value).to.exist;
expect(record.username).to.exist;
expect(record.access_level).to.exist;
});
});
});
});