-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
246 lines (209 loc) · 9.1 KB
/
test.js
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
var core = require('rijs.core').default
, data = require('rijs.data').default
, db = require('rijs.db').default
, update = require('utilise/update')
, remove = require('utilise/remove')
, falsy = require('utilise/falsy')
, keys = require('utilise/keys')
, push = require('utilise/push')
, pop = require('utilise/pop')
, expect = require('chai').expect
, mysql = require('./').default
, mockery = require('mockery')
, sql, next
describe('MySQL', function(){
before(function(){
mockery.enable()
mockery.registerMock('mysql', {
createPool: function(){
return {
escape: function(d){ return "'" + d + "'" }
, query: function(d, fn){ next = fn; console.log('sql', sql = d) }
}
}
})
})
beforeEach(function(){
sql = ''
})
after(function(){
mockery.disable()
})
// it('should create connection hooks', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// expect(keys(ripple.connections)).to.be.eql(['mysql'])
// expect(ripple.connections.mysql.add).to.be.a('function')
// expect(ripple.connections.mysql.update).to.be.a('function')
// expect(ripple.connections.mysql.remove).to.be.a('function')
// })
// it('should create correct load SQL and behaviour', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// ripple('foo', [1,2,3])
// expect(sql).to.be.eql('SHOW COLUMNS FROM foo')
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// expect(ripple.resources.foo.headers.mysql.table).to.be.eql('foo')
// expect(sql).to.be.eql('SELECT * FROM foo')
// next(null, [1,2,3,4])
// expect(ripple('foo')).to.be.eql([1,2,3,4])
// next = null
// ripple('foo', [1,2,3])
// expect(next).to.be.not.ok
// })
// it('should create correct insert SQL and behaviour', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo' }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [])
// push(record)(ripple('foo'))
// expect(sql).to.eql("INSERT INTO foo (`name`) VALUES ('foo');")
// next(null, { insertId: 7 })
// expect(ripple('foo')).to.eql([ { name: 'foo', id: 7 } ])
// })
// it('should create correct update SQL and behaviour', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { id: 7 }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [record])
// update(0, (ripple('foo')[0].name = 'foo', ripple('foo')[0]))(ripple('foo'))
// expect(sql).to.eql("UPDATE foo SET `name`='foo' WHERE id = 7;")
// next(null, {})
// expect(ripple('foo')).to.eql([ { name: 'foo', id: 7 } ])
// })
// it('should create correct update SQL and behaviour - deep', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { id: 7 }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'city' }, { Field: 'id' }])
// next(null, [record])
// update('0.name', 'foo')(ripple('foo'))
// expect(sql).to.eql("UPDATE foo SET `name`='foo' WHERE id = 7;")
// next(null, {})
// update('0.city', 'bar')(ripple('foo'))
// expect(sql).to.eql("UPDATE foo SET `city`='bar' WHERE id = 7;")
// next(null, {})
// expect(ripple('foo')).to.eql([ { name: 'foo', city: 'bar', id: 7 } ])
// })
// it('should fail on deep update not in fields', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo', id: 7 }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [record])
// sql = ''
// update('0.city', 'bar')(ripple('foo'))
// expect(sql).to.eql("")
// expect(ripple('foo')).to.eql([ { name: 'foo', city: 'bar', id: 7 } ])
// })
// it('should create correct remove SQL and behaviour', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { id: 7 }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [record])
// pop(ripple('foo'))
// expect(sql).to.eql("DELETE FROM foo WHERE id = 7;")
// next(null, {})
// expect(ripple('foo')).to.eql([])
// })
// it('should skip if resource not linked to table', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo' }
// ripple('foo', [])
// next({ code: 'ER_NO_SUCH_TABLE' })
// expect(ripple.resources.foo.headers.mysql.table === '').to.be.ok
// sql = ''
// push(record)(ripple('foo'))
// expect(sql).to.eql('')
// expect(ripple('foo')).to.eql([ { name: 'foo' } ])
// })
// it('should not do anything with non-objects', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = 'foo'
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [])
// sql = ''
// push(record)(ripple('foo'))
// expect(sql).to.eql('')
// expect(ripple('foo')).to.eql(['foo'])
// })
// it('should deal with mysql errors (crud)', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo' }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [])
// push(record)(ripple('foo'))
// expect(sql).to.eql("INSERT INTO foo (`name`) VALUES ('foo');")
// next('err')
// expect(ripple('foo')).to.eql([ { name: 'foo' } ])
// })
// it('should deal with mysql errors (show table)', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo' }
// ripple('foo', [])
// next('err')
// expect(ripple('foo')).to.eql([])
// })
// it('should deal with mysql errors (select table)', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo' }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next('err', [])
// expect(ripple('foo')).to.eql([])
// })
// it('should skip props if not in db', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { id: 7 }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [record])
// sql = ''
// update('0', { name: 'foo', baz: 'baz', id: 7 })(ripple('foo'))
// expect(sql).to.eql("UPDATE foo SET `name`='foo' WHERE id = 7;")
// next(null, {})
// expect(ripple('foo')).to.eql([ { name: 'foo', baz: 'baz', id: 7 } ])
// })
// it('should skip props if not in db - deep', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { id: 7 }
// ripple('foo', [])
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [record])
// sql = ''
// update('0.baz', 'baz')(ripple('foo'))
// expect(sql).to.eql('')
// expect(ripple('foo')).to.eql([ { baz: 'baz', id: 7 } ])
// })
// it('should strip mysql headers before sending', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// ripple('foo', [1,2,3])
// expect(sql).to.be.eql('SHOW COLUMNS FROM foo')
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// expect(ripple.resources.foo.headers.mysql.table).to.be.eql('foo')
// expect(ripple.resources.foo.headers.mysql.fields).to.be.eql(['name', 'id'])
// expect(sql).to.be.eql('SELECT * FROM foo')
// next(null, [1,2,3,4])
// expect(ripple('foo')).to.be.eql([1,2,3,4])
// expect('loaded' in ripple.resources.foo.headers.mysql).to.be.ok
// expect('fields' in ripple.resources.foo.headers.mysql).to.be.ok
// expect('table' in ripple.resources.foo.headers.mysql).to.be.ok
// var headers = ripple.types['application/data'].to(ripple.resources.foo).headers
// expect('mysql' in headers).to.not.be.ok
// })
// it('should allow blocking action', function(){
// var ripple = db(mysql(data(core())), { db: { mysql: 'mysql://user:password@host:port/database' }})
// , record = { name: 'foo' }
// ripple('foo', [], { mysql: { to: falsy }})
// next(null, [{ Field: 'name' }, { Field: 'id' }])
// next(null, [])
// next = null
// push(record)(ripple('foo'))
// expect(sql).to.be.not.ok
// expect(next).to.be.not.ok
// expect(ripple('foo')).to.eql([ { name: 'foo' } ])
// })
})