attributes.test.js
4.41 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
'use strict';
const chai = require('chai'),
Sequelize = require('../../../index'),
expect = chai.expect,
Support = require('../support');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('attributes', () => {
describe('set', () => {
it('should only be called once when used on a join model called with an association getter', async function() {
let 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(v) {
callCount++;
this.setDataValue('test_value', v + 1);
}
}
}, {
tableName: 'score',
timestamps: false
});
this.Student.belongsToMany(this.Course, { through: this.Score, foreignKey: 'StudentId' });
this.Course.belongsToMany(this.Student, { through: this.Score, foreignKey: 'CourseId' });
await this.sequelize.sync({ force: true });
const [student, course] = await Promise.all([
this.Student.create({ no: 1, name: 'ryan' }),
this.Course.create({ no: 100, name: 'history' })
]);
await student.addCourse(course, { through: { score: 98, test_value: 1000 } });
expect(callCount).to.equal(1);
const score0 = await this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } });
expect(score0.test_value).to.equal(1001);
const [courses, score] = await Promise.all([
this.Student.build({ no: 1 }).getCourses({ where: { no: 100 } }),
this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } })
]);
expect(score.test_value).to.equal(1001);
expect(courses[0].score.toJSON().test_value).to.equal(1001);
expect(callCount).to.equal(1);
});
it('allows for an attribute to be called "toString"', async function() {
const Person = this.sequelize.define('person', {
name: Sequelize.STRING,
nick: Sequelize.STRING
}, {
timestamps: false
});
await this.sequelize.sync({ force: true });
await Person.create({ name: 'Jozef', nick: 'Joe' });
const person = await Person.findOne({
attributes: [
'nick',
['name', 'toString']
],
where: {
name: 'Jozef'
}
});
expect(person.dataValues['toString']).to.equal('Jozef');
expect(person.get('toString')).to.equal('Jozef');
});
it('allows for an attribute to be called "toString" with associations', async function() {
const Person = this.sequelize.define('person', {
name: Sequelize.STRING,
nick: Sequelize.STRING
});
const Computer = this.sequelize.define('computer', {
hostname: Sequelize.STRING
});
Person.hasMany(Computer);
await this.sequelize.sync({ force: true });
const person = await Person.create({ name: 'Jozef', nick: 'Joe' });
await person.createComputer({ hostname: 'laptop' });
const result = await Person.findAll({
attributes: [
'nick',
['name', 'toString']
],
include: {
model: Computer
},
where: {
name: 'Jozef'
}
});
expect(result.length).to.equal(1);
expect(result[0].dataValues['toString']).to.equal('Jozef');
expect(result[0].get('toString')).to.equal('Jozef');
expect(result[0].get('computers')[0].hostname).to.equal('laptop');
});
});
describe('quote', () => {
it('allows for an attribute with dots', async function() {
const User = this.sequelize.define('user', {
'foo.bar.baz': Sequelize.TEXT
});
await this.sequelize.sync({ force: true });
const result = await User.findAll();
expect(result.length).to.equal(0);
});
});
});
});