model-factory.spec.js
8.87 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var config = require("./config/config")
, Sequelize = require("../index")
, sequelize = new Sequelize(config.database, config.username, config.password, { logging: false })
, Helpers = new (require("./config/helpers"))(sequelize)
describe('ModelFactory', function() {
var User = null
var setup = function() {
Helpers.async(function(done) {
User = sequelize.define('User', {
age: Sequelize.INTEGER,
name: Sequelize.STRING,
bio: Sequelize.TEXT
})
User.sync({force: true}).success(done)
})
}
beforeEach(function() { Helpers.dropAllTables(); setup() })
afterEach(function() { Helpers.dropAllTables() })
describe('constructor', function() {
it("uses the passed model name as tablename if freezeTableName", function() {
var User = sequelize.define('User', {}, {freezeTableName: true})
expect(User.tableName).toEqual('User')
})
it("uses the pluralized modelname as tablename unless freezeTableName", function() {
var User = sequelize.define('User', {}, {freezeTableName: false})
expect(User.tableName).toEqual('Users')
})
it("attaches class and instance methods", function() {
var User = sequelize.define('User', {}, {
classMethods: { doSmth: function(){ return 1 } },
instanceMethods: { makeItSo: function(){ return 2}}
})
expect(User.doSmth).toBeDefined()
expect(User.doSmth()).toEqual(1)
expect(User.makeItSo).toBeUndefined()
expect(User.build().makeItSo).toBeDefined()
expect(User.build().makeItSo()).toEqual(2)
})
it("throws an error if 2 autoIncrements are passed", function() {
expect(function () {
var User = sequelize.define('User', {
userid: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
userscore: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
})
}).toThrow('Invalid model definition. Only one autoincrement field allowed.')
})
})
describe('build', function() {
it("doesn't create database entries", function() {
Helpers.async(function(done) {
User.build({ name: 'John Wayne', bio: 'noot' })
User.all().success(function(users) {
expect(users.length).toEqual(0)
done()
})
})
})
it("fills the objects with default values", function() {
var Task = sequelize.define('Task' + config.rand(), {
title: {type: Sequelize.STRING, defaultValue: 'a task!'},
foo: {type: Sequelize.INTEGER, defaultValue: 2},
bar: {type: Sequelize.DATE},
foobar: {type: Sequelize.TEXT, defaultValue: 'asd'},
flag: {type: Sequelize.BOOLEAN, defaultValue: false}
})
expect(Task.build().title).toEqual('a task!')
expect(Task.build().foo).toEqual(2)
expect(Task.build().bar).toEqual(null)
expect(Task.build().foobar).toEqual('asd')
expect(Task.build().flag).toEqual(false)
})
})
describe('create', function() {
var setup = function(userOptions) {
Helpers.dropAllTables()
Helpers.async(function(done) {
User = sequelize.define('User', userOptions)
User.sync({ force: true }).success(done)
})
}
it("doesn't allow duplicated records with unique:true", function() {
setup({ username: {type: Sequelize.STRING, unique: true} })
Helpers.async(function(done) {
User.create({ username:'foo' }).success(function() {
User.create({ username: 'foo' }).error(function(err) {
expect(err.message).toEqual("Duplicate entry 'foo' for key 'username'")
done()
})
})
})
})
it("raises an error if created object breaks definition contraints", function() {
setup({
username: {type: Sequelize.STRING, unique: true},
smth: {type: Sequelize.STRING, allowNull: false}
})
Helpers.async(function(done) {
User.create({ username: 'foo', smth: null }).error(function(err) {
expect(err.message).toEqual("Column 'smth' cannot be null")
User.create({username: 'foo', smth: 'foo'}).success(function() {
User.create({username: 'foo', smth: 'bar'}).error(function(err) {
expect(err.message).toEqual("Duplicate entry 'foo' for key 'username'")
done()
})
})
})
})
})
it('sets auto increment fields', function() {
setup({
userid: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false}
})
Helpers.async(function(done) {
User.create({}).on('success', function(user) {
expect(user.userid).toEqual(1)
done()
})
})
Helpers.async(function(done) {
User.create({}).on('success', function(user) {
expect(user.userid).toEqual(2)
done()
})
})
})
it('allows the usage of options as attribute', function() {
setup({ name: Sequelize.STRING, options: Sequelize.TEXT })
Helpers.async(function(done) {
var options = JSON.stringify({ foo: 'bar', bar: 'foo' })
User
.create({ name: 'John Doe', options: options })
.success(function(user) {
expect(user.options).toEqual(options)
done()
})
})
})
})
describe('destroy', function() {
it('deletes a record from the database if model is not paranoid', function() {
Helpers.async(function(done) {
User = sequelize.define('User', {
name: Sequelize.STRING,
bio: Sequelize.TEXT
})
User.sync({force: true}).success(done)
})
Helpers.async(function(done) {
User.create({name: 'hallo', bio: 'welt'}).success(function(u) {
User.all().success(function(users) {
expect(users.length).toEqual(1)
u.destroy().success(function() {
User.all().success(function(users) {
expect(users.length).toEqual(0)
done()
})
})
})
})
})
})
it('marks the database entry as deleted if model is paranoid', function() {
Helpers.async(function(done) {
User = sequelize.define('User', {
name: Sequelize.STRING, bio: Sequelize.TEXT
}, { paranoid:true })
User.sync({ force: true }).success(done)
})
Helpers.async(function(done) {
User.create({ name: 'asd', bio: 'asd' }).success(function(u) {
expect(u.deletedAt).toBeNull()
u.destroy().success(function(u) {
expect(u.deletedAt).toBeTruthy()
done()
})
})
})
})
})
describe('find', function() {
beforeEach(function() {
Helpers.Factories.User({name: 'user', bio: 'foobar'}, null, 2)
})
it("should make aliased attributes available", function() {
Helpers.async(function(done) {
User.find({ where: 'id = 1', attributes: ['id', ['name', 'username']] }).success(function(user) {
expect(user.username).toEqual('user')
done()
})
})
})
})
describe('all', function() {
beforeEach(function() {
Helpers.Factories.User({name: 'user', bio: 'foobar'}, null, 2)
})
it("should return all users", function() {
Helpers.async(function(done) {
User.all().on('success', function(users) {
done()
expect(users.length).toEqual(2)
}).on('failure', function(err) { console.log(err) })
})
})
})
describe('count', function() {
it('counts all created objects', function() {
Helpers.async(function(done) {
User.create({name: 'user1'}).success(function() {
User.create({name: 'user2'}).success(done)
})
})
Helpers.async(function(done) {
User.count().success(function(count) {
expect(count).toEqual(2)
done()
})
})
})
it('filters object', function() {
Helpers.async(function(done) {
User.create({name: 'user1'}).success(function() {
User.create({name: 'foo'}).success(done)
})
})
Helpers.async(function(done) {
User.count({where: "name LIKE '%us%'"}).success(function(count) {
expect(count).toEqual(1)
done()
})
})
})
})
describe('min', function() {
it("should return the min value", function() {
for(var i = 2; i < 5; i++) Helpers.Factories.User({ age: i })
Helpers.async(function(done) {
User.min('age').on('success', function(min) {
expect(min).toEqual(2); done()
})
})
})
})
describe('max', function() {
it("should return the max value", function() {
for(var i = 2; i <= 5; i++) Helpers.Factories.User({ age: i })
Helpers.async(function(done) {
User.max('age').on('success', function(max) {
expect(max).toEqual(5); done()
})
})
})
})
})