general.js
3.64 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
'use strict'
let co = require('co')
let lab = exports.lab = require('lab').script()
let expect = require('code').expect
let valid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
let db = require('../db')
let User = db.bookshelf.model('User')
lab.experiment('general tests', () => {
lab.beforeEach(co.wrap(function * () {
yield db.reset()
yield db.knex.seed.run()
}))
lab.test('should work', co.wrap(function * () {
let user = yield User
.forge({ name: 'Hello World', email: 'hello@world.com' })
.save()
expect(user.id).to.match(valid)
}))
lab.test('should not interfere with queries', co.wrap(function * () {
let user = yield User.forge({ email: 'Brandyn_Collier44@yahoo.com' }).fetch()
expect(user.id).to.equal('6b7a192f-6e1c-4dcb-8e57-14ab16d5fdf4')
}))
lab.test('should generate a new UUID for each insert', co.wrap(function * () {
let users = yield [
User
.forge({ name: 'Hello World', email: 'hello@world.com' })
.save(),
User
.forge({ name: 'Foo Bar', email: 'foo@bar.com' })
.save()
]
expect(users[0].id).to.match(valid)
expect(users[1].id).to.match(valid)
expect(users[0].id).to.not.equal(users[1].id)
}))
lab.test('should not modify the original defaults', co.wrap(function * () {
let user = yield User
.forge({ name: 'Hello World', email: 'hello@world.com' })
.save()
expect(user.id).to.match(valid)
expect(user.get('bio')).to.equal('No bio')
expect(User.prototype.defaults).to.deep.equal({ bio: 'No bio' })
}))
lab.test('should merge defaults even if it\'s a function', co.wrap(function * () {
let Model = db.bookshelf.Model.extend({
uuid: true,
tableName: 'users',
defaults: () => {
return { bio: 'new bio' }
}
})
let user = yield Model
.forge({ name: 'Hello World', email: 'hello@world.com' })
.save()
expect(user.id).to.match(valid)
expect(user.get('bio')).to.equal('new bio')
expect(Model.prototype.defaults()).to.deep.equal({ bio: 'new bio' })
}))
lab.test('should not override when ID is explicit', co.wrap(function * () {
let user = yield User
.forge({
id: 'lol-this-isnt-guid',
name: 'Hello World',
email: 'hello@world.com'
})
.save(null, { method: 'insert' })
expect(user.id).to.equal('lol-this-isnt-guid')
}))
lab.test('should be disabled by default', co.wrap(function * () {
let Model = db.bookshelf.Model.extend({
tableName: 'users'
})
let user = yield Model
.forge({
name: 'Hello World',
email: 'hello@world.com'
})
.save()
expect(user.id).to.be.a.number()
// Enable it again
Model.prototype.uuid = true
user = yield Model
.forge({
name: 'Foo Bar',
email: 'foo@bar'
})
.save()
expect(user.id).to.match(valid)
}))
lab.test('should be able to define own function', co.wrap(function * () {
let bookshelf = require('bookshelf')(db.knex)
bookshelf.plugin(require('../../'), {
type: function (attr, options) {
expect(this.uuid).to.be.true()
expect(attr).to.deep.equal({
name: 'Hello World',
email: 'hello@world.com'
})
expect(options).to.deep.equal({ custom: 'param' })
return 'hello-world'
}
})
let Model = bookshelf.Model.extend({
tableName: 'users',
uuid: true
})
let user = yield Model
.forge({
name: 'Hello World',
email: 'hello@world.com'
}, { custom: 'param' })
.save()
expect(user.id).to.be.equal('hello-world')
}))
})