separate.test.js
3.42 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
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../../support');
const DataTypes = require('../../../../lib/data-types');
const Sequelize = require('../../../../lib/sequelize');
const current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAll', () => {
describe('separate with limit', () => {
it('should not throw syntax error (union)', () => {
// #9813 testcase
const Project = current.define('Project', { name: DataTypes.STRING });
const LevelTwo = current.define('LevelTwo', { name: DataTypes.STRING });
const LevelThree = current.define('LevelThree', { type: DataTypes.INTEGER });
Project.hasMany(LevelTwo);
LevelTwo.belongsTo(Project);
LevelTwo.hasMany(LevelThree, { as: 'type_ones' });
LevelTwo.hasMany(LevelThree, { as: 'type_twos' });
LevelThree.belongsTo(LevelTwo);
return current.sync({ force: true }).then(() => {
return Sequelize.Promise.all([
Project.create({ name: 'testProject' }),
LevelTwo.create({ name: 'testL21' }),
LevelTwo.create({ name: 'testL22' })
]);
}).then(([project, level21, level22]) => {
return Sequelize.Promise.all([
project.addLevelTwo(level21),
project.addLevelTwo(level22)
]);
}).then(() => {
// one include case
return Project.findAll({
where: { name: 'testProject' },
include: [
{
model: LevelTwo,
include: [
{
model: LevelThree,
as: 'type_ones',
where: { type: 0 },
separate: true,
limit: 1,
order: [['createdAt', 'DESC']]
}
]
}
]
});
}).then(projects => {
expect(projects).to.have.length(1);
expect(projects[0].LevelTwos).to.have.length(2);
expect(projects[0].LevelTwos[0].type_ones).to.have.length(0);
expect(projects[0].LevelTwos[1].type_ones).to.have.length(0);
}, () => {
expect.fail();
}).then(() => {
// two includes case
return Project.findAll({
where: { name: 'testProject' },
include: [
{
model: LevelTwo,
include: [
{
model: LevelThree,
as: 'type_ones',
where: { type: 0 },
separate: true,
limit: 1,
order: [['createdAt', 'DESC']]
},
{
model: LevelThree,
as: 'type_twos',
where: { type: 1 },
separate: true,
limit: 1,
order: [['createdAt', 'DESC']]
}
]
}
]
});
}).then(projects => {
expect(projects).to.have.length(1);
expect(projects[0].LevelTwos).to.have.length(2);
expect(projects[0].LevelTwos[0].type_ones).to.have.length(0);
expect(projects[0].LevelTwos[1].type_ones).to.have.length(0);
}, () => {
expect.fail();
});
});
});
});
});