findAndCountAll.test.js
4.83 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
/* 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) }, { paranoid: true }),
SomeConnection = S.define('SomeConnection', {
m: DT.STRING(40),
fk: DT.INTEGER,
u: DT.INTEGER,
}, { paranoid: true }),
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 })
// Associate them
User.hasMany( SomeConnection, { foreignKey: 'u' })
SomeConnection.belongsTo( User, { foreignKey: 'u' })
SomeConnection.belongsTo( A, { foreignKey: 'fk', constraints: false })
SomeConnection.belongsTo( B, { foreignKey: 'fk', constraints: false })
SomeConnection.belongsTo( C, { foreignKey: 'fk', constraints: false })
A.hasMany( SomeConnection, { foreignKey: 'fk', constraints: false })
B.hasMany( SomeConnection, { foreignKey: 'fk', constraints: false })
C.hasMany( SomeConnection, { foreignKey: 'fk', constraints: false })
// 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 // Will be deleted
{ u: 2, m: 'A', fk: 1 },
{ u: 3, m: 'A', fk: 1 },
{ u: 4, m: 'A', fk: 1 },
{ u: 5, m: 'A', fk: 1 },
{ u: 1, m: 'B', fk: 1 },
{ u: 2, m: 'B', fk: 1 },
{ u: 3, m: 'B', fk: 1 },
{ u: 4, m: 'B', fk: 1 },
{ u: 5, m: 'B', fk: 1 },
{ u: 1, m: 'C', fk: 1 },
{ u: 2, m: 'C', fk: 1 },
{ u: 3, m: 'C', fk: 1 },
{ u: 4, m: 'C', fk: 1 },
{ u: 5, m: 'C', fk: 1 },
{ u: 1, m: 'A', fk: 2 }, // 2 // Will be deleted
{ 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( 24 )
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 )
// Delete some of conns to prove the concept
SomeConnection.destroy({where: {
m: 'A',
u: 1,
fk: [ 1, 2 ],
}}).done( function ( err ) { expect( err ).not.to.be.ok
// Last and most important queries ( we connected 4, but deleted 2, witch means we must get 2 only )
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( 2 )
expect( result.rows.length ).to.be.equal( 2 )
done()
// Last and most important queries - END
})
// Delete some of conns to prove the concept - END
})
// Create an enviroment - END
})
})
})
})
})
// Sync them - END
})
})
})
})