1 | _ = require 'underscore'
|
2 |
|
3 | mesa = require '../src/postgres'
|
4 |
|
5 | module.exports =
|
6 |
|
7 | 'set and get': (test) ->
|
8 | m = mesa
|
9 | m1 = m.set('string', 'foo')
|
10 | m2 = m1.set('number', 1)
|
11 | m3 = m2.set('string', 'bar')
|
12 | m4 = m3.set('number', 2)
|
13 | m5 = m4.set('string', 'baz')
|
14 | m6 = m5.set('number', 3)
|
15 |
|
16 | test.ok not m.string?
|
17 | test.ok not m.number?
|
18 |
|
19 | test.equal 'foo', m1.string
|
20 | test.ok not m1.number?
|
21 |
|
22 | test.equal 'foo', m2.string
|
23 | test.equal 1, m2.number
|
24 |
|
25 | test.equal 'bar', m3.string
|
26 | test.equal 1, m3.number
|
27 |
|
28 | test.equal 'bar', m4.string
|
29 | test.equal 2, m4.number
|
30 |
|
31 | test.equal 'baz', m5.string
|
32 | test.equal 2, m5.number
|
33 |
|
34 | test.equal 'baz', m6.string
|
35 | test.equal 3, m6.number
|
36 |
|
37 | test.done()
|
38 |
|
39 | 'throw':
|
40 |
|
41 | "when connection wasn't called": (test) ->
|
42 | userTable = mesa
|
43 |
|
44 | test.throws ->
|
45 | userTable.find {id: 1}, -> test.fail()
|
46 | test.done()
|
47 |
|
48 | "when table wasn't called": (test) ->
|
49 | userTable = mesa
|
50 | .connection(-> test.fail)
|
51 |
|
52 | test.throws ->
|
53 | userTable.delete -> test.fail()
|
54 | test.done()
|
55 |
|
56 | "when attributes wasn't called before insert": (test) ->
|
57 | userTable = mesa
|
58 | .connection(-> test.fail)
|
59 | .table('user')
|
60 |
|
61 | test.throws ->
|
62 | userTable.insert {name: 'foo'}, -> test.fail()
|
63 | test.done()
|
64 |
|
65 | "when attributes wasn't called before update": (test) ->
|
66 | userTable = mesa
|
67 | .connection(-> test.fail)
|
68 | .table('user')
|
69 |
|
70 | test.throws ->
|
71 | userTable.update {name: 'foo'}, -> test.fail()
|
72 | test.done()
|
73 |
|
74 | "when including something that has no association": (test) ->
|
75 | test.expect 1
|
76 |
|
77 | connection =
|
78 | query: -> test.fail()
|
79 |
|
80 | userTable = mesa
|
81 | .table('user')
|
82 | .includes(billing_address: true)
|
83 |
|
84 | test.throws ->
|
85 | userTable.fetchIncludes connection, {id: 3, name: 'foo'}, -> test.fail()
|
86 |
|
87 | test.done()
|
88 |
|
89 | 'command':
|
90 |
|
91 | 'insert a record': (test) ->
|
92 |
|
93 | test.expect 3
|
94 |
|
95 | connection =
|
96 | query: (sql, params, cb) ->
|
97 | test.equal sql, 'INSERT INTO "user"("name", "email") VALUES ($1, $2) RETURNING id'
|
98 | test.deepEqual params, ['foo', 'foo@example.com']
|
99 | cb null, {rows: [{id: 3}]}
|
100 |
|
101 | userTable = mesa
|
102 | .connection(connection)
|
103 | .table('user')
|
104 | .attributes(['name', 'email'])
|
105 |
|
106 | userTable.insert {name: 'foo', email: 'foo@example.com', x: 5}, (err, id) ->
|
107 | throw err if err?
|
108 | test.equal id, 3
|
109 | test.done()
|
110 |
|
111 | 'insert with raw': (test) ->
|
112 |
|
113 | test.expect 3
|
114 |
|
115 | connection =
|
116 | query: (sql, params, cb) ->
|
117 | test.equal sql, 'INSERT INTO "user"("name", "id") VALUES ($1, LOG($2, $3)) RETURNING id'
|
118 | test.deepEqual params, ['foo', 3, 4]
|
119 | cb null, {rows: [{id: 3}]}
|
120 |
|
121 | userTable = mesa
|
122 | .connection(connection)
|
123 | .table('user')
|
124 | .attributes(['name', 'id'])
|
125 |
|
126 | userTable.insert {name: 'foo', id: userTable.raw('LOG(?, ?)', 3, 4)}, (err, id) ->
|
127 | throw err if err?
|
128 | test.equal id, 3
|
129 | test.done()
|
130 |
|
131 | 'insert with custom primaryKey': (test) ->
|
132 |
|
133 | test.expect 3
|
134 |
|
135 | connection =
|
136 | query: (sql, params, cb) ->
|
137 | test.equal sql, 'INSERT INTO "user"("name", "email") VALUES ($1, $2) RETURNING my_id'
|
138 | test.deepEqual params, ['foo', 'foo@example.com']
|
139 | cb null, {rows: [{id: 3, my_id: 5}]}
|
140 |
|
141 | userTable = mesa
|
142 | .connection(connection)
|
143 | .table('user')
|
144 | .attributes(['name', 'email'])
|
145 |
|
146 | userTable
|
147 | .primaryKey('my_id')
|
148 | .insert {name: 'foo', email: 'foo@example.com', x: 5}, (err, id) ->
|
149 | throw err if err?
|
150 | test.equal id, 5
|
151 | test.done()
|
152 |
|
153 | 'insert with returning': (test) ->
|
154 |
|
155 | test.expect 3
|
156 |
|
157 | connection =
|
158 | query: (sql, params, cb) ->
|
159 | test.equal sql, 'INSERT INTO "user"("name", "email") VALUES ($1, $2) RETURNING *'
|
160 | test.deepEqual params, ['foo', 'foo@example.com']
|
161 | cb null, {rows: [{id: 3, name: 'foo', email: 'foo@example.com'}]}
|
162 |
|
163 | userTable = mesa
|
164 | .connection(connection)
|
165 | .table('user')
|
166 | .attributes(['name', 'email'])
|
167 |
|
168 | userTable
|
169 | .primaryKey('my_id')
|
170 | .returning('*')
|
171 | .insert {name: 'foo', email: 'foo@example.com', x: 5}, (err, record) ->
|
172 | throw err if err?
|
173 | test.deepEqual record,
|
174 | id: 3
|
175 | name: 'foo'
|
176 | email: 'foo@example.com'
|
177 |
|
178 | test.done()
|
179 |
|
180 | 'insert multiple records': (test) ->
|
181 |
|
182 | test.expect 3
|
183 |
|
184 | connection =
|
185 | query: (sql, params, cb) ->
|
186 | test.equal sql, 'INSERT INTO "user"("name", "email") VALUES ($1, $2), ($3, $4) RETURNING id'
|
187 | test.deepEqual params, ['foo', 'foo@example.com', 'bar', 'bar@example.com']
|
188 | cb null, {rows: [{id: 3}, {id: 4}]}
|
189 |
|
190 | userTable = mesa
|
191 | .connection(connection)
|
192 | .table('user')
|
193 | .attributes(['name', 'email'])
|
194 |
|
195 | userTable.insertMany [
|
196 | {name: 'foo', email: 'foo@example.com', x: 5}
|
197 | {name: 'bar', email: 'bar@example.com', x: 6}
|
198 | ], (err, ids) ->
|
199 | throw err if err?
|
200 | test.deepEqual ids, [3, 4]
|
201 | test.done()
|
202 |
|
203 | 'delete': (test) ->
|
204 | test.expect 2
|
205 |
|
206 | connection =
|
207 | query: (sql, params, cb) ->
|
208 | test.equal sql, 'DELETE FROM "user" WHERE (id = $1) AND (name = $2)'
|
209 | test.deepEqual params, [3, 'foo']
|
210 | cb()
|
211 |
|
212 | userTable = mesa
|
213 | .connection(connection)
|
214 | .table('user')
|
215 |
|
216 | userTable.where(id: 3).where(name: 'foo').delete (err) ->
|
217 | throw err if err?
|
218 | test.done()
|
219 |
|
220 | 'update': (test) ->
|
221 | test.expect 2
|
222 |
|
223 | connection =
|
224 | query: (sql, params, cb) ->
|
225 | test.equal sql, 'UPDATE "user" SET "name" = $1, "email" = $2 WHERE (id = $3) AND (name = $4)'
|
226 | test.deepEqual params, ['bar', 'bar@example.com', 3, 'foo']
|
227 | cb()
|
228 |
|
229 | userTable = mesa
|
230 | .connection(connection)
|
231 | .table('user')
|
232 | .attributes(['name', 'email'])
|
233 |
|
234 | updates = {name: 'bar', x: 5, y: 8, email: 'bar@example.com'}
|
235 |
|
236 | userTable.where(id: 3).where(name: 'foo').update updates, (err) ->
|
237 | throw err if err?
|
238 | test.done()
|
239 |
|
240 | 'update with returning': (test) ->
|
241 | test.expect 3
|
242 |
|
243 | connection =
|
244 | query: (sql, params, cb) ->
|
245 | test.equal sql, 'UPDATE "user" SET "name" = $1, "email" = $2 WHERE (id = $3) AND (name = $4) RETURNING *'
|
246 | test.deepEqual params, ['bar', 'bar@example.com', 3, 'foo']
|
247 | cb null, {rows: [{id: 3}, {id: 4}]}
|
248 |
|
249 | userTable = mesa
|
250 | .connection(connection)
|
251 | .table('user')
|
252 | .returning('*')
|
253 | .attributes(['name', 'email'])
|
254 |
|
255 | updates = {name: 'bar', x: 5, y: 8, email: 'bar@example.com'}
|
256 |
|
257 | userTable.where(id: 3).where(name: 'foo').update updates, (err, results) ->
|
258 | throw err if err?
|
259 | test.deepEqual results, [{id: 3}, {id: 4}]
|
260 | test.done()
|
261 |
|
262 | 'update with raw': (test) ->
|
263 | test.expect 2
|
264 |
|
265 | connection =
|
266 | query: (sql, params, cb) ->
|
267 | test.equal sql, 'UPDATE "user" SET "id" = LOG($1, $2), "name" = $3 WHERE (id = LOG($4, $5)) AND (name = $6)'
|
268 | test.deepEqual params, [7, 8, 'bar', 11, 12, 'foo']
|
269 | cb()
|
270 |
|
271 | userTable = mesa
|
272 | .connection(connection)
|
273 | .table('user')
|
274 | .attributes(['id', 'name'])
|
275 |
|
276 | updates =
|
277 | name: 'bar'
|
278 | id: userTable.raw('LOG(?, ?)', 7, 8)
|
279 | x: 5
|
280 | y: 8
|
281 | email: 'bar@example.com'
|
282 |
|
283 | userTable
|
284 | .where(id: userTable.raw('LOG(?, ?)', 11, 12))
|
285 | .where(name: 'foo')
|
286 | .update updates, (err) ->
|
287 | throw err if err?
|
288 | test.done()
|
289 |
|
290 | 'query':
|
291 |
|
292 | 'find all': (test) ->
|
293 | test.expect 3
|
294 |
|
295 | connection =
|
296 | query: (sql, params, cb) ->
|
297 | test.equal sql, 'SELECT name FROM "user" WHERE id = $1'
|
298 | test.deepEqual params, [3]
|
299 | cb null, {rows: [{name: 'foo'}, {name: 'bar'}]}
|
300 |
|
301 | userTable = mesa
|
302 | .connection(connection)
|
303 | .table('user')
|
304 |
|
305 | userTable.where(id: 3).select('name').find (err, users) ->
|
306 | throw err if err?
|
307 | test.deepEqual users, [{name: 'foo'}, {name: 'bar'}]
|
308 | test.done()
|
309 |
|
310 | 'find the first': (test) ->
|
311 | test.expect 3
|
312 |
|
313 | connection =
|
314 | query: (sql, params, cb) ->
|
315 | test.equal sql, 'SELECT name FROM "user" WHERE id = $1'
|
316 | test.deepEqual params, [3]
|
317 | cb null, {rows: [{name: 'foo'}, {name: 'bar'}]}
|
318 |
|
319 | userTable = mesa
|
320 | .connection(connection)
|
321 | .table('user')
|
322 |
|
323 | userTable.where(id: 3).select('name').first (err, user) ->
|
324 | throw err if err?
|
325 | test.deepEqual user, {name: 'foo'}
|
326 | test.done()
|
327 |
|
328 | 'test for existence': (test) ->
|
329 | test.expect 3
|
330 |
|
331 | connection =
|
332 | query: (sql, params, cb) ->
|
333 | test.equal sql, 'SELECT * FROM "user" WHERE id = $1'
|
334 | test.deepEqual params, [3]
|
335 | cb null, {rows: [{name: 'foo'}, {name: 'bar'}]}
|
336 |
|
337 | userTable = mesa
|
338 | .connection(connection)
|
339 | .table('user')
|
340 |
|
341 | userTable.where(id: 3).exists (err, exists) ->
|
342 | throw err if err?
|
343 | test.ok exists
|
344 | test.done()
|
345 |
|
346 | 'everything together': (test) ->
|
347 | test.expect 3
|
348 |
|
349 | connection =
|
350 | query: (sql, params, cb) ->
|
351 | test.equal sql, 'SELECT user.*, count(project.id) AS project_count FROM "user" JOIN project ON user.id = project.user_id WHERE (id = $1) AND (name = $2) GROUP BY user.id ORDER BY created DESC, name ASC LIMIT $3 OFFSET $4'
|
352 | test.deepEqual params, [3, 'foo', 10, 20]
|
353 | cb null, {rows: [{name: 'foo'}, {name: 'bar'}]}
|
354 |
|
355 | userTable = mesa
|
356 | .connection(connection)
|
357 | .table('user')
|
358 | .select('user.*, count(project.id) AS project_count')
|
359 | .where(id: 3)
|
360 | .where('name = ?', 'foo')
|
361 | .join('JOIN project ON user.id = project.user_id')
|
362 | .group('user.id')
|
363 | .order('created DESC, name ASC')
|
364 | .limit(10)
|
365 | .offset(20)
|
366 | .find (err, users) ->
|
367 | throw err if err?
|
368 | test.deepEqual users, [{name: 'foo'}, {name: 'bar'}]
|
369 | test.done()
|
370 |
|
371 | 'extending': (test) ->
|
372 |
|
373 | test.expect 8
|
374 |
|
375 | userTable = Object.create mesa
|
376 |
|
377 | userTable.insert = (data, cb) ->
|
378 | @getConnection (err, connection) =>
|
379 | return cb err if err?
|
380 |
|
381 | connection.query 'BEGIN;', [], (err) =>
|
382 | return cb err if err?
|
383 |
|
384 |
|
385 |
|
386 | mesa.insert.call @connection(connection), data, (err, userId) =>
|
387 | return cb err if err?
|
388 |
|
389 | test.equal userId, 200
|
390 |
|
391 |
|
392 |
|
393 | connection.query 'COMMIT;', [], (err) =>
|
394 | return cb err if err?
|
395 | cb null, 500
|
396 |
|
397 | getConnection = (cb) ->
|
398 | call = 1
|
399 |
|
400 | cb null,
|
401 | query: (sql, params, cb) ->
|
402 |
|
403 | switch call++
|
404 | when 1
|
405 | test.equal sql, 'BEGIN;'
|
406 | test.deepEqual params, []
|
407 | cb()
|
408 | when 2
|
409 | test.equal sql, 'INSERT INTO "user"("name", "email") VALUES ($1, $2) RETURNING id'
|
410 | test.deepEqual params, ['foo', 'foo@example.com']
|
411 | cb null, {rows: [{id: 200}]}
|
412 | when 3
|
413 | test.equal sql, 'COMMIT;'
|
414 | test.deepEqual params, []
|
415 | cb()
|
416 |
|
417 | userTable
|
418 | .table('user')
|
419 | .connection(getConnection)
|
420 | .attributes(['name', 'email'])
|
421 | .insert {name: 'foo', email: 'foo@example.com'}, (err, userId) ->
|
422 | throw err if err?
|
423 | test.equal userId, 500
|
424 | test.done()
|