types.test.js
4.89 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';
var chai = require('chai')
, Sequelize = require('../../../index')
, Promise = Sequelize.Promise
, expect = chai.expect
, Support = require(__dirname + '/../../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, dialect = Support.getTestDialect()
, datetime = require('chai-datetime');
chai.use(datetime);
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Model'), function() {
describe('attributes', function() {
describe('types', function() {
describe('VIRTUAL', function() {
beforeEach(function() {
this.User = this.sequelize.define('user', {
storage: Sequelize.STRING,
field1: {
type: Sequelize.VIRTUAL,
set: function(val) {
this.setDataValue('storage', val);
this.setDataValue('field1', val);
},
get: function() {
return this.getDataValue('field1');
}
},
field2: {
type: Sequelize.VIRTUAL,
get: function() {
return 42;
}
},
virtualWithDefault: {
type: Sequelize.VIRTUAL,
defaultValue: 'cake'
}
}, { timestamps: false });
this.Task = this.sequelize.define('task', {});
this.Project = this.sequelize.define('project', {});
this.Task.belongsTo(this.User);
this.Project.hasMany(this.User);
this.User.hasMany(this.Project);
this.sqlAssert = function(sql) {
expect(sql.indexOf('field1')).to.equal(-1);
expect(sql.indexOf('field2')).to.equal(-1);
};
return this.sequelize.sync({ force: true });
});
it('should not be ignored in dataValues get', function() {
var user = this.User.build({
field1: 'field1_value',
field2: 'field2_value'
});
expect(user.get()).to.deep.equal({ storage: 'field1_value', field1: 'field1_value', virtualWithDefault: 'cake', field2: 42, id: null });
});
it('should be ignored in table creation', function() {
return this.sequelize.getQueryInterface().describeTable(this.User.tableName).then(function(fields) {
expect(Object.keys(fields).length).to.equal(2);
});
});
it('should be ignored in find, findAll and includes', function() {
return Promise.all([
this.User.find().on('sql', this.sqlAssert),
this.User.findAll().on('sql', this.sqlAssert),
this.Task.findAll({
include: [
this.User
]
}).on('sql', this.sqlAssert),
this.Project.findAll({
include: [
this.User
]
}).on('sql', this.sqlAssert)
]);
});
it('should allow me to store selected values', function() {
var Post = this.sequelize.define('Post', {
text: Sequelize.TEXT,
someBoolean: {
type: Sequelize.VIRTUAL
}
});
return this.sequelize.sync({ force: true}).then(function() {
return Post.bulkCreate([{ text: 'text1' },{ text: 'text2' }]);
}).then(function() {
var boolQuery = 'EXISTS(SELECT 1) AS "someBoolean"';
if (dialect === 'mssql') {
boolQuery = 'CAST(CASE WHEN EXISTS(SELECT 1) THEN 1 ELSE 0 END AS BIT) AS "someBoolean"';
}
return Post.find({ attributes: ['id', 'text', Sequelize.literal(boolQuery)] });
}).then(function(post) {
expect(post.get('someBoolean')).to.be.ok;
expect(post.get().someBoolean).to.be.ok;
});
});
it('should be ignored in create and updateAttributes', function() {
return this.User.create({
field1: 'something'
}).then(function(user) {
// We already verified that the virtual is not added to the table definition, so if this succeeds, were good
expect(user.virtualWithDefault).to.equal('cake');
expect(user.storage).to.equal('something');
return user.updateAttributes({
field1: 'something else'
});
}).then(function(user) {
expect(user.virtualWithDefault).to.equal('cake');
expect(user.storage).to.equal('something else');
});
});
it('should be ignored in bulkCreate and and bulkUpdate', function() {
var self = this;
return this.User.bulkCreate([{
field1: 'something'
}]).on('sql', this.sqlAssert).then(function() {
return self.User.findAll();
}).then(function(users) {
expect(users[0].storage).to.equal('something');
});
});
});
});
});
});