attributes.test.js
7.02 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict";
/* jshint camelcase: false */
/* jshint expr: true */
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 () {
return Post.find({ attributes: ['id','text',Sequelize.literal('EXISTS(SELECT 1) AS "someBoolean"')] });
}).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');
});
});
});
});
describe('set', function () {
it('should only be called once when used on a join model called with an association getter', function () {
var self = this;
self.callCount = 0;
this.Student = this.sequelize.define('student', {
no: {type:Sequelize.INTEGER, primaryKey:true},
name: Sequelize.STRING,
}, {
tableName: "student",
timestamps: false
});
this.Course = this.sequelize.define('course', {
no: {type:Sequelize.INTEGER,primaryKey:true},
name: Sequelize.STRING,
},{
tableName: 'course',
timestamps: false
});
this.Score = this.sequelize.define('score',{
score: Sequelize.INTEGER,
test_value: {
type: Sequelize.INTEGER,
set: function(v) {
self.callCount++;
this.setDataValue('test_value', v+1);
}
}
}, {
tableName: 'score',
timestamps: false
});
this.Student.hasMany(this.Course, {through: this.Score, foreignKey: 'StudentId'});
this.Course.hasMany(this.Student, {through: this.Score, foreignKey: 'CourseId'});
return this.sequelize.sync({force:true}).then(function () {
return Promise.join(
self.Student.create({no:1, name:'ryan'}),
self.Course.create({no:100, name:'history'})
).spread(function(student, course){
return student.addCourse(course, {score: 98, test_value: 1000});
}).then(function(){
expect(self.callCount).to.equal(1);
return self.Score.find({StudentId: 1, CourseId: 100}).then(function(score){
expect(score.test_value).to.equal(1001);
});
})
.then(function(){
return Promise.join(
self.Student.build({no: 1}).getCourses({where: {no: 100}}),
self.Score.find({StudentId: 1, CourseId:100})
);
})
.spread(function(courses, score) {
expect(score.test_value).to.equal(1001);
expect(courses[0].score.toJSON().test_value).to.equal(1001);
expect(self.callCount).to.equal(1);
});
});
});
});
});
});