merge.test.js
5.92 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
'use strict';
const chai = require('chai'),
Sequelize = require('../../../../index'),
expect = chai.expect,
Support = require('../../support'),
combinatorics = require('js-combinatorics');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('simple merge', () => {
beforeEach(async function() {
this.Foo = this.sequelize.define('foo', { name: Sequelize.STRING }, { timestamps: false });
this.Bar = this.sequelize.define('bar', { name: Sequelize.STRING }, { timestamps: false });
this.Baz = this.sequelize.define('baz', { name: Sequelize.STRING }, { timestamps: false });
this.Foo.belongsTo(this.Baz, { foreignKey: 'bazId' });
this.Foo.hasOne(this.Bar, { foreignKey: 'fooId' });
this.createEntries = async () => {
const baz = await this.Baz.create({ name: 'The Baz' });
const foo = await this.Foo.create({ name: 'The Foo', bazId: baz.id });
return this.Bar.create({ name: 'The Bar', fooId: foo.id });
};
this.scopes = {
includeBar: { include: this.Bar },
includeBaz: { include: this.Baz }
};
this.Foo.addScope('includeBar', this.scopes.includeBar);
this.Foo.addScope('includeBaz', this.scopes.includeBaz);
await this.createEntries(await this.sequelize.sync({ force: true }));
});
it('should merge simple scopes correctly', async function() {
const result = await this.Foo.scope('includeBar', 'includeBaz').findOne();
const json = result.toJSON();
expect(json.bar).to.be.ok;
expect(json.baz).to.be.ok;
expect(json.bar.name).to.equal('The Bar');
expect(json.baz.name).to.equal('The Baz');
});
});
describe('complex merge', () => {
beforeEach(async function() {
this.Foo = this.sequelize.define('foo', { name: Sequelize.STRING }, { timestamps: false });
this.Bar = this.sequelize.define('bar', { name: Sequelize.STRING }, { timestamps: false });
this.Baz = this.sequelize.define('baz', { name: Sequelize.STRING }, { timestamps: false });
this.Qux = this.sequelize.define('qux', { name: Sequelize.STRING }, { timestamps: false });
this.Foo.hasMany(this.Bar, { foreignKey: 'fooId' });
this.Bar.hasMany(this.Baz, { foreignKey: 'barId' });
this.Baz.hasMany(this.Qux, { foreignKey: 'bazId' });
this.createFooWithDescendants = () => this.Foo.create({
name: 'foo1',
bars: [{
name: 'bar1',
bazs: [{
name: 'baz1',
quxes: [{ name: 'qux1' }, { name: 'qux2' }]
}, {
name: 'baz2',
quxes: [{ name: 'qux3' }, { name: 'qux4' }]
}]
}, {
name: 'bar2',
bazs: [{
name: 'baz3',
quxes: [{ name: 'qux5' }, { name: 'qux6' }]
}, {
name: 'baz4',
quxes: [{ name: 'qux7' }, { name: 'qux8' }]
}]
}]
}, {
include: [{
model: this.Bar,
include: [{
model: this.Baz,
include: [{
model: this.Qux
}]
}]
}]
});
this.scopes = {
includeEverything: {
include: {
model: this.Bar,
include: [{
model: this.Baz,
include: this.Qux
}]
}
},
limitedBars: {
include: [{
model: this.Bar,
limit: 2
}]
},
limitedBazs: {
include: [{
model: this.Bar,
include: [{
model: this.Baz,
limit: 2
}]
}]
},
excludeBazName: {
include: [{
model: this.Bar,
include: [{
model: this.Baz,
attributes: {
exclude: ['name']
}
}]
}]
}
};
this.Foo.addScope('includeEverything', this.scopes.includeEverything);
this.Foo.addScope('limitedBars', this.scopes.limitedBars);
this.Foo.addScope('limitedBazs', this.scopes.limitedBazs);
this.Foo.addScope('excludeBazName', this.scopes.excludeBazName);
this.scopePermutations = combinatorics.permutation([
'includeEverything',
'limitedBars',
'limitedBazs',
'excludeBazName'
]).toArray();
await this.createFooWithDescendants(await this.sequelize.sync({ force: true }));
});
it('should merge complex scopes correctly regardless of their order', async function() {
const results = await Promise.all(this.scopePermutations.map(scopes => this.Foo.scope(...scopes).findOne()));
const first = results.shift().toJSON();
for (const result of results) {
expect(result.toJSON()).to.deep.equal(first);
}
});
it('should merge complex scopes with findAll options correctly regardless of their order', async function() {
const results = await Promise.all(this.scopePermutations.map(async ([a, b, c, d]) => {
const x = await this.Foo.scope(a, b, c).findAll(this.scopes[d]);
return x[0];
}));
const first = results.shift().toJSON();
for (const result of results) {
expect(result.toJSON()).to.deep.equal(first);
}
});
it('should merge complex scopes with findOne options correctly regardless of their order', async function() {
const results = await Promise.all(this.scopePermutations.map(([a, b, c, d]) => this.Foo.scope(a, b, c).findOne(this.scopes[d])));
const first = results.shift().toJSON();
for (const result of results) {
expect(result.toJSON()).to.deep.equal(first);
}
});
});
});
});