findOrBuild.test.js
1.56 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
age: DataTypes.INTEGER
});
this.Project = this.sequelize.define('Project', {
name: DataTypes.STRING
});
this.User.hasMany(this.Project);
this.Project.belongsTo(this.User);
return this.sequelize.sync({ force: true });
});
describe('findOrBuild', () => {
it('initialize with includes', function() {
return this.User.bulkCreate([
{ username: 'Mello', age: 10 },
{ username: 'Mello', age: 20 }
], { returning: true }).then(([, user2]) => {
return this.Project.create({
name: 'Investigate'
}).then(project => user2.setProjects([project]));
}).then(() => {
return this.User.findOrBuild({
defaults: {
username: 'Mello',
age: 10
},
where: {
age: 20
},
include: [{
model: this.Project
}]
});
}).then(([user, created]) => {
expect(created).to.be.false;
expect(user.get('id')).to.be.ok;
expect(user.get('username')).to.equal('Mello');
expect(user.get('age')).to.equal(20);
expect(user.Projects).to.have.length(1);
expect(user.Projects[0].get('name')).to.equal('Investigate');
});
});
});
});