find.test.js
2.34 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
/* jshint camelcase: false */
/* jshint expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, datetime = require('chai-datetime')
chai.use(datetime)
chai.config.includeStack = true
describe(Support.getTestDialectTeaser("Include"), function () {
describe('find', function () {
it( 'Try to include a non required model, with conditions and two includes N:M 1:M', function ( done ) {
var DT = DataTypes,
S = this.sequelize,
A = S.define('A', { name: DT.STRING(40) }, { paranoid: true }),
B = S.define('B', { name: DT.STRING(40) }, { paranoid: true }),
C = S.define('C', { name: DT.STRING(40) }, { paranoid: true }),
D = S.define('D', { name: DT.STRING(40) }, { paranoid: true })
// Associations
A.hasMany( B )
B.belongsTo( B )
B.belongsTo( D )
B.hasMany( C, {
through: 'BC',
})
C
.hasMany( B, {
through: 'BC',
})
D
.hasMany( B )
S.sync({ force: true }).done( function ( err ) { expect( err ).not.to.be.ok
A.find({
include: [
{ model: B, required: false, include: [
{ model: C, required: false },
{ model: D }
]}
]
}).done( function ( err ) {
expect( err ).not.to.be.ok
done()
})
})
});
it("should still pull the main record when an included model is not required and has where restrictions without matches", function () {
var DT = DataTypes,
S = this.sequelize,
A = S.define( 'A', {name: DT.STRING(40)} ),
B = S.define( 'B', {name: DT.STRING(40)} );
A.hasMany(B);
B.hasMany(A);
return S
.sync({force: true})
.then(function () {
return A.create({
name: 'Foobar'
});
})
.then(function () {
return A.find({
where: {name: 'Foobar'},
include: [
{model: B, where: {name: 'idontexist'}, require: false}
]
});
})
.then(function (a) {
expect(a).to.not.equal(null);
expect(a.get('bs')).to.deep.equal([]);
});
});
});
})