associations.test.js
7.09 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../../support'),
dialect = Support.getTestDialect(),
config = require('../../../config/config'),
DataTypes = require('../../../../lib/data-types');
if (dialect.match(/^postgres/)) {
describe('[POSTGRES Specific] associations', () => {
describe('many-to-many', () => {
describe('where tables have the same prefix', () => {
it('should create a table wp_table1wp_table2s', function() {
const Table2 = this.sequelize.define('wp_table2', { foo: DataTypes.STRING }),
Table1 = this.sequelize.define('wp_table1', { foo: DataTypes.STRING });
Table1.belongsToMany(Table2, { through: 'wp_table1swp_table2s' });
Table2.belongsToMany(Table1, { through: 'wp_table1swp_table2s' });
expect(this.sequelize.modelManager.getModel('wp_table1swp_table2s')).to.exist;
});
});
describe('when join table name is specified', () => {
beforeEach(function() {
const Table2 = this.sequelize.define('ms_table1', { foo: DataTypes.STRING }),
Table1 = this.sequelize.define('ms_table2', { foo: DataTypes.STRING });
Table1.belongsToMany(Table2, { through: 'table1_to_table2' });
Table2.belongsToMany(Table1, { through: 'table1_to_table2' });
});
it('should not use a combined name', function() {
expect(this.sequelize.modelManager.getModel('ms_table1sms_table2s')).not.to.exist;
});
it('should use the specified name', function() {
expect(this.sequelize.modelManager.getModel('table1_to_table2')).to.exist;
});
});
});
describe('HasMany', () => {
describe('addDAO / getModel', () => {
beforeEach(function() {
//prevent periods from occurring in the table name since they are used to delimit (table.column)
this.User = this.sequelize.define(`User${config.rand()}`, { name: DataTypes.STRING });
this.Task = this.sequelize.define(`Task${config.rand()}`, { name: DataTypes.STRING });
this.users = null;
this.tasks = null;
this.User.belongsToMany(this.Task, { as: 'Tasks', through: 'usertasks' });
this.Task.belongsToMany(this.User, { as: 'Users', through: 'usertasks' });
const users = [],
tasks = [];
for (let i = 0; i < 5; ++i) {
users[i] = { name: `User${Math.random()}` };
tasks[i] = { name: `Task${Math.random()}` };
}
return this.sequelize.sync({ force: true }).then(() => {
return this.User.bulkCreate(users).then(() => {
return this.Task.bulkCreate(tasks).then(() => {
return this.User.findAll().then(_users => {
return this.Task.findAll().then(_tasks => {
this.user = _users[0];
this.task = _tasks[0];
});
});
});
});
});
});
it('should correctly add an association to the dao', function() {
return this.user.getTasks().then(_tasks => {
expect(_tasks).to.have.length(0);
return this.user.addTask(this.task).then(() => {
return this.user.getTasks().then(_tasks => {
expect(_tasks).to.have.length(1);
});
});
});
});
});
describe('removeDAO', () => {
it('should correctly remove associated objects', function() {
const users = [],
tasks = [];
//prevent periods from occurring in the table name since they are used to delimit (table.column)
this.User = this.sequelize.define(`User${config.rand()}`, { name: DataTypes.STRING });
this.Task = this.sequelize.define(`Task${config.rand()}`, { name: DataTypes.STRING });
this.users = null;
this.tasks = null;
this.User.belongsToMany(this.Task, { as: 'Tasks', through: 'usertasks' });
this.Task.belongsToMany(this.User, { as: 'Users', through: 'usertasks' });
for (let i = 0; i < 5; ++i) {
users[i] = { id: i + 1, name: `User${Math.random()}` };
tasks[i] = { id: i + 1, name: `Task${Math.random()}` };
}
return this.sequelize.sync({ force: true }).then(() => {
return this.User.bulkCreate(users).then(() => {
return this.Task.bulkCreate(tasks).then(() => {
return this.User.findAll().then(_users => {
return this.Task.findAll().then(_tasks => {
this.user = _users[0];
this.task = _tasks[0];
this.users = _users;
this.tasks = _tasks;
return this.user.getTasks().then(__tasks => {
expect(__tasks).to.have.length(0);
return this.user.setTasks(this.tasks).then(() => {
return this.user.getTasks().then(_tasks => {
expect(_tasks).to.have.length(this.tasks.length);
return this.user.removeTask(this.tasks[0]).then(() => {
return this.user.getTasks().then(_tasks => {
expect(_tasks).to.have.length(this.tasks.length - 1);
return this.user.removeTasks([this.tasks[1], this.tasks[2]]).then(() => {
return this.user.getTasks().then(_tasks => {
expect(_tasks).to.have.length(this.tasks.length - 3);
});
});
});
});
});
});
});
});
});
});
});
});
});
it('defaults to schema provided to sync() for references #11276', function() {
const User = this.sequelize.define('UserXYZ', {
uid: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
}
}),
Task = this.sequelize.define('TaskXYZ', {
});
Task.belongsTo(User);
return Support.dropTestSchemas(this.sequelize).then(() => {
return this.sequelize.createSchema('archive');
}).then(() => {
return User.sync({ force: true, schema: 'archive' });
}).then(() => {
return Task.sync({ force: true, schema: 'archive' });
}).then(() => {
return User.schema('archive').create({});
}).then(user => {
return Task.schema('archive').create({}).then(task => {
return task.setUserXYZ(user).then(() => {
return task.getUserXYZ({ schema: 'archive' });
});
});
}).then(user => {
expect(user).to.be.ok;
return this.sequelize.dropSchema('archive');
});
});
});
});
});
}