belongsTo.js
4.54 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
var assert = require("assert")
, config = require("./../config")
, Sequelize = require("./../../index")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
module.exports = {
'it should correctly add the foreign id': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User)
assert.eql(Task.attributes['User'+num+'Id'], "INT")
},
'it should correctly add the foreign id with underscore': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true})
Task.belongsTo(User)
assert.eql(Task.attributes['user'+num+'_id'], "INT")
},
'it should correctly add the foreign id if foreignKey is passed': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User, {foreignKey: 'person_id'})
assert.eql(Task.attributes['person_id'], "INT")
},
'it should define getter and setter': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User)
var t = Task.build({title: 'asd'})
assert.isDefined(t['setUser'+num])
assert.isDefined(t['getUser'+num])
},
'it should define getter and setter according to passed as option': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User, {as: 'Person'})
var t = Task.build({title: 'asd'})
assert.isDefined(t.setPerson)
assert.isDefined(t.getPerson)
},
'it should set the foreign id to null': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User)
var t = Task.build({title: 'asd'})
assert.isNull(t['User'+num+'Id'])
},
'it should set and get the correct object': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User, {as: 'User'})
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) {
t.setUser(u).on('success', function() {
t.getUser().on('success', function(user) {
assert.eql(user.username, 'asd')
exit(function(){})
})
})
})
})
})
})
},
'it should correctly delete associations': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo(User, {as: 'User'})
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) {
t.setUser(u).on('success', function() {
t.getUser().on('success', function(user) {
assert.eql(user.username, 'asd')
t.setUser(null).on('success', function() {
t.getUser().on('success', function(user) {
assert.isNull(user)
exit(function(){})
})
})
})
})
})
})
})
})
}
}