self.test.js
5.13 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
'use strict';
const { expect } = require('chai');
const Support = require('../support');
const DataTypes = require('../../../lib/data-types');
const Sequelize = require('../../../index');
const Promise = Sequelize.Promise;
describe(Support.getTestDialectTeaser('Self'), () => {
it('supports freezeTableName', function() {
const Group = this.sequelize.define('Group', {}, {
tableName: 'user_group',
timestamps: false,
underscored: true,
freezeTableName: true
});
Group.belongsTo(Group, { as: 'Parent', foreignKey: 'parent_id' });
return Group.sync({ force: true }).then(() => {
return Group.findAll({
include: [{
model: Group,
as: 'Parent'
}]
});
});
});
it('can handle 1:m associations', function() {
const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
Person.hasMany(Person, { as: 'Children', foreignKey: 'parent_id' });
expect(Person.rawAttributes.parent_id).to.be.ok;
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]);
}).then(([mary, john, chris]) => {
return mary.setChildren([john, chris]);
});
});
it('can handle n:m associations', function() {
const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
Person.belongsToMany(Person, { as: 'Parents', through: 'Family', foreignKey: 'ChildId', otherKey: 'PersonId' });
Person.belongsToMany(Person, { as: 'Childs', through: 'Family', foreignKey: 'PersonId', otherKey: 'ChildId' });
const foreignIdentifiers = Object.values(Person.associations).map(v => v.foreignIdentifier);
const rawAttributes = Object.keys(this.sequelize.models.Family.rawAttributes);
expect(foreignIdentifiers.length).to.equal(2);
expect(rawAttributes.length).to.equal(4);
expect(foreignIdentifiers).to.have.members(['PersonId', 'ChildId']);
expect(rawAttributes).to.have.members(['createdAt', 'updatedAt', 'PersonId', 'ChildId']);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]).then(([mary, john, chris]) => {
return mary.setParents([john]).then(() => {
return chris.addParent(john);
}).then(() => {
return john.getChilds();
}).then(children => {
expect(children.map(v => v.id)).to.have.members([mary.id, chris.id]);
});
});
});
});
it('can handle n:m associations with pre-defined through table', function() {
const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
const Family = this.sequelize.define('Family', {
preexisting_child: {
type: DataTypes.INTEGER,
primaryKey: true
},
preexisting_parent: {
type: DataTypes.INTEGER,
primaryKey: true
}
}, { timestamps: false });
Person.belongsToMany(Person, { as: 'Parents', through: Family, foreignKey: 'preexisting_child', otherKey: 'preexisting_parent' });
Person.belongsToMany(Person, { as: 'Children', through: Family, foreignKey: 'preexisting_parent', otherKey: 'preexisting_child' });
const foreignIdentifiers = Object.values(Person.associations).map(v => v.foreignIdentifier);
const rawAttributes = Object.keys(Family.rawAttributes);
expect(foreignIdentifiers.length).to.equal(2);
expect(rawAttributes.length).to.equal(2);
expect(foreignIdentifiers).to.have.members(['preexisting_parent', 'preexisting_child']);
expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']);
let count = 0;
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]);
}).then(([mary, john, chris]) => {
this.mary = mary;
this.chris = chris;
this.john = john;
return mary.setParents([john], {
logging(sql) {
if (sql.match(/INSERT/)) {
count++;
expect(sql).to.have.string('preexisting_child');
expect(sql).to.have.string('preexisting_parent');
}
}
});
}).then(() => {
return this.mary.addParent(this.chris, {
logging(sql) {
if (sql.match(/INSERT/)) {
count++;
expect(sql).to.have.string('preexisting_child');
expect(sql).to.have.string('preexisting_parent');
}
}
});
}).then(() => {
return this.john.getChildren({
logging(sql) {
count++;
const whereClause = sql.split('FROM')[1]; // look only in the whereClause
expect(whereClause).to.have.string('preexisting_child');
expect(whereClause).to.have.string('preexisting_parent');
}
});
}).then(children => {
expect(count).to.be.equal(3);
expect(children.map(v => v.id)).to.have.members([this.mary.id]);
});
});
});