findAndCountAll.test.js
4.15 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
/* 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('findAndCountAll', function () {
it( 'Try to include a required model. Result rows should match count', function ( done ) {
var DT = DataTypes,
S = this.sequelize,
User = S.define('User', { name: DT.STRING(40) }),
SomeConnection = S.define('SomeConnection', {
m: DT.STRING(40),
fk: DT.INTEGER.UNSIGNED,
u: DT.INTEGER.UNSIGNED,
}),
A = S.define('A', { name: DT.STRING(40) }),
B = S.define('B', { name: DT.STRING(40) }),
C = S.define('C', { name: DT.STRING(40) })
// Associate them
User.hasMany( SomeConnection )
SomeConnection
.belongsTo( User, { foreignKey: 'u' } )
.belongsTo( A, { foreignKey: 'fk', })
.belongsTo( B, { foreignKey: 'fk', })
.belongsTo( C, { foreignKey: 'fk', })
A.hasMany( SomeConnection )
B.hasMany( SomeConnection )
C.hasMany( SomeConnection )
// Sync them
S.sync({ force: true }).done( function ( err ) { expect( err ).not.to.be.ok;
// Create an enviroment
User.bulkCreate([
{ name: 'Youtube' },
{ name: 'Facebook' },
{ name: 'Google' },
{ name: 'Yahoo' },
{ name: '404' }
]).done( function ( err, users ) { expect( err ).not.to.be.ok; expect( users ).to.be.length( 5 )
SomeConnection.bulkCreate([ // Lets count, m: A and u: 1
{ u: 1, m: 'A', fk: 1 }, // 1
{ u: 2, m: 'A', fk: 1 },
{ u: 3, m: 'A', fk: 1 },
{ u: 1, m: 'B', fk: 1 },
{ u: 1, m: 'C', fk: 1 },
{ u: 1, m: 'A', fk: 2 }, // 2
{ u: 4, m: 'A', fk: 2 },
{ u: 2, m: 'A', fk: 2 },
{ u: 1, m: 'A', fk: 3 }, // 3
{ u: 2, m: 'A', fk: 3 },
{ u: 3, m: 'A', fk: 3 },
{ u: 2, m: 'B', fk: 2 },
{ u: 1, m: 'A', fk: 4 }, // 4
{ u: 4, m: 'A', fk: 2 },
]).done( function ( err, conns ) { expect( err ).not.to.be.ok; expect( conns ).to.be.length( 14 )
A.bulkCreate([
{ name: 'Just' },
{ name: 'for' },
{ name: 'testing' },
{ name: 'proposes' },
{ name: 'only' },
]).done( function ( err, as ) { expect( err ).not.to.be.ok; expect( as ).to.be.length( 5 )
B.bulkCreate([
{ name: 'this should not' },
{ name: 'be loaded' }
]).done( function ( err, bs ) { expect( err ).not.to.be.ok; expect( bs ).to.be.length( 2 )
C.bulkCreate([
{ name: 'because we only want A' }
]).done( function ( err, cs ) { expect( err ).not.to.be.ok; expect( cs ).to.be.length( 1 )
// Now let's query an initial test
A.findAndCountAll().done( function ( err, result ) {
// Test variables
expect( err ).not.to.be.ok
expect( result.count ).to.be.equal( 5 )
expect( result.rows.length ).to.be.equal( 5 )
// Last and most important queries
A.findAndCountAll({
include: [{
model: SomeConnection, required: true,
where: {
m: 'A', // Pseudo Polymorphy
u: 1
}
}],
limit: 5,
}).done( function ( err, result ) {
// Test variables
expect( err ).not.to.be.ok
expect( result.count ).to.be.equal( 4 )
expect( result.rows.length ).to.be.equal( 4 )
done()
// Last and most important queries - END
})
// Now let's query an initial test - END
})
// Create an enviroment - END
})
})
})
})
})
// Sync them - END
})
})
})
})