-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathassociate_test.js
More file actions
104 lines (95 loc) · 2.35 KB
/
associate_test.js
File metadata and controls
104 lines (95 loc) · 2.35 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
/* global describe, it */
var Sequelize = require('sequelize'),
sequelize = require('./environment'),
assert = require('assert');
describe('associate support', function () {
'use strict';
var User, Project, Task;
before(function(done) {
User = sequelize.define('User', { });
Project = sequelize.define('Project', { });
Task = sequelize.define('Task', { });
User.hasOne(Project);
Project.hasMany(Task);
Task.belongsTo(User);
User.hasMany(Task);
sequelize.sync({force:true}).success(function() {
done();
});
});
it('should support hasMany', function (done) {
var fixture = Project.fixtures({
include: [{
model: Task
}]
}).success(function(projects) {
for (var i in projects) {
var project = projects[i];
assert.ok(project.tasks);
for (var j in project.tasks) {
assert.equal(project.tasks[j].ProjectId, project.id);
}
}
done();
});
});
it('should generate multi data when associate hasMany', function (done) {
var fixture = Project.fixtures({
include: [{
model: Task,
fixture: {
num: 10
}
}]
}).success(function(projects) {
for (var i in projects) {
assert.equal(projects[i].tasks.length, 10);
}
done();
});
});
it('should support hasOne', function (done) {
var fixture = User.fixtures({
include: [{
model: Project
}]
}).success(function(users) {
for (var i in users) {
var user = users[i];
assert.ok(user.project);
assert.equal(user.project.UserId, user.id);
}
done();
});
});
it('should support belongsTo', function (done) {
var fixture = Task.fixtures({
include: [{
model: User
}]
}).success(function(tasks) {
for (var i in tasks) {
var task = tasks[i];
assert.ok(task.user);
assert.equal(task.UserId, task.user.id);
}
done();
});
});
it('should support nested associate', function (done) {
var fixture = User.fixtures({
include: [{
model: Project,
include: [{
model: Task
}]
}]
}).success(function(users) {
for (var i in users) {
var user = users[i];
assert.ok(user.project.tasks);
}
done();
});
});
});