UNPKG

29.8 kBtext/coffeescriptView Raw
1mesa = require '../src/postgres'
2
3mesa.enableConnectionReuseForIncludes = true
4
5module.exports =
6
7 'associations':
8
9 'first':
10
11 'hasOne': (test) ->
12 test.expect 5
13
14 call = 1
15
16 connection =
17 query: (sql, params, cb) ->
18 switch call++
19 when 1
20 test.equal sql, 'SELECT * FROM "user" WHERE id = $1'
21 test.deepEqual params, [3]
22 cb null, {rows: [
23 {name: 'foo', id: 3}
24 {name: 'bar', id: 4}
25 ]}
26 when 2
27 test.equal sql, 'SELECT * FROM "address" WHERE user_id IN ($1)'
28 test.deepEqual params, [3]
29 cb null, {rows: [
30 {street: 'foo street', zip_code: 12345, user_id: 3}
31 {street: 'djfslkfj', zip_code: 12345, user_id: 4}
32 ]}
33
34 addressTable = mesa
35 .connection(-> test.fail())
36 .table('address')
37
38 userTable = mesa
39 .connection(connection)
40 .table('user')
41 .hasOne('billing_address', addressTable)
42
43 userTable
44 .includes(billing_address: true)
45 .where(id: 3)
46 .first (err, user) ->
47 throw err if err?
48 test.deepEqual user,
49 name: 'foo'
50 id: 3
51 billing_address:
52 street: 'foo street'
53 zip_code: 12345
54 user_id: 3
55
56 test.done()
57
58 'belongsTo': (test) ->
59 test.expect 5
60
61 call = 1
62
63 connection =
64 query: (sql, params, cb) ->
65 switch call++
66 when 1
67 test.equal sql, 'SELECT * FROM "address" WHERE id = $1'
68 test.deepEqual params, [3]
69 cb null, {rows: [
70 {street: 'foo street', zip_code: 12345, user_id: 3}
71 {street: 'bar street', zip_code: 12345, user_id: 10}
72 ]}
73 when 2
74 test.equal sql, 'SELECT * FROM "user" WHERE id IN ($1)'
75 test.deepEqual params, [3]
76 cb null, {rows: [
77 {name: 'foo', id: 3}
78 {name: 'bar', id: 10}
79 {name: 'baz', id: 4}
80 ]}
81
82 userTable = mesa
83 .connection(-> test.fail())
84 .table('user')
85
86 addressTable = mesa
87 .connection(connection)
88 .table('address')
89 .belongsTo('person', userTable)
90
91 addressTable.hookBeforeIncludes = ->
92 console.log 'before includes'
93
94 addressTable.hookBeforeGetIncludesForFirst = ->
95 console.log 'before get includes for first'
96
97 addressTable
98 .includes(person: true)
99 .where(id: 3)
100 .first (err, address) ->
101 throw err if err?
102 test.deepEqual address,
103 street: 'foo street'
104 zip_code: 12345
105 user_id: 3
106 person:
107 name: 'foo'
108 id: 3
109
110 test.done()
111
112 'hasMany': (test) ->
113 test.expect 5
114
115 call = 1
116
117 connection =
118 query: (sql, params, cb) ->
119 switch call++
120 when 1
121 test.equal sql, 'SELECT * FROM "user" WHERE id = $1'
122 test.deepEqual params, [3]
123 cb null, {rows: [
124 {name: 'foo', id: 3}
125 {name: 'bar', id: 4}
126 ]}
127 when 2
128 test.equal sql, 'SELECT * FROM "task" WHERE user_id IN ($1)'
129 test.deepEqual params, [3]
130 cb null, {rows: [
131 {name: 'do laundry', user_id: 3}
132 {name: 'buy groceries', user_id: 4}
133 {name: 'buy the iphone 5', user_id: 3}
134 {name: 'learn clojure', user_id: 3}
135 ]}
136
137 taskTable = mesa
138 .connection(-> test.fail())
139 .table('task')
140
141 userTable = mesa
142 .connection(connection)
143 .table('user')
144 .hasMany('tasks', taskTable)
145
146 userTable
147 .includes(tasks: true)
148 .where(id: 3)
149 .first (err, user) ->
150 throw err if err?
151 test.deepEqual user,
152 name: 'foo'
153 id: 3
154 tasks: [
155 {name: 'do laundry', user_id: 3}
156 {name: 'buy the iphone 5', user_id: 3}
157 {name: 'learn clojure', user_id: 3}
158 ]
159
160 test.done()
161
162 'hasManyThrough': (test) ->
163 test.expect 7
164
165 call = 1
166
167 connection =
168 query: (sql, params, cb) ->
169 switch call++
170 when 1
171 test.equal sql, 'SELECT * FROM "user" WHERE id = $1'
172 test.deepEqual params, [3]
173 cb null, {rows: [
174 {name: 'foo', id: 3}
175 {name: 'bar', id: 4}
176 ]}
177 when 2
178 test.equal sql, 'SELECT * FROM "user_role" WHERE user_id IN ($1)'
179 test.deepEqual params, [3]
180 cb null, {rows: [
181 {user_id: 3, role_id: 30}
182 {user_id: 3, role_id: 40}
183 {user_id: 3, role_id: 60}
184 ]}
185 when 3
186 test.equal sql, 'SELECT * FROM "role" WHERE id IN ($1, $2, $3)'
187 test.deepEqual params, [30, 40, 60]
188 cb null, {rows: [
189 {id: 30, name: 'jedi'}
190 {id: 40, name: 'administrator'}
191 {id: 60, name: 'master of the universe'}
192 {id: 50, name: 'bad bad role'}
193 ]}
194
195 roleTable = mesa
196 .connection(connection)
197 .table('role')
198
199 joinTable = mesa
200 .connection(connection)
201 .table('user_role')
202
203 userTable = mesa
204 .connection(connection)
205 .table('user')
206 .hasManyThrough('roles', roleTable, joinTable)
207
208 userTable
209 .includes(roles: true)
210 .where(id: 3)
211 .first (err, user) ->
212 throw err if err?
213 test.deepEqual user,
214 name: 'foo'
215 id: 3
216 roles: [
217 {id: 30, name: 'jedi'}
218 {id: 40, name: 'administrator'}
219 {id: 60, name: 'master of the universe'}
220 ]
221
222 test.done()
223
224 'find':
225
226 'hasOne': (test) ->
227 test.expect 5
228
229 call = 1
230
231 connection =
232 query: (sql, params, cb) ->
233 switch call++
234 when 1
235 test.equal sql, 'SELECT * FROM "user"'
236 test.deepEqual params, []
237 cb null, {rows: [
238 {name: 'foo', id: 3}
239 {name: 'bar', id: 10}
240 ]}
241 when 2
242 test.equal sql, 'SELECT * FROM "address" WHERE user_id IN ($1, $2)'
243 test.deepEqual params, [3, 10]
244 cb null, {rows: [
245 {street: 'foo street', zip_code: 12345, user_id: 3}
246 {street: 'djfslkfj', zip_code: 12345, user_id: 4}
247 {street: 'bar street', zip_code: 12345, user_id: 10}
248 ]}
249
250 addressTable = mesa
251 .connection(-> test.fail())
252 .table('address')
253
254 userTable = mesa
255 .connection(connection)
256 .table('user')
257 .hasOne('billing_address', addressTable)
258
259 userTable
260 .includes(billing_address: true)
261 .find (err, users) ->
262 test.deepEqual users, [
263 {
264 name: 'foo'
265 id: 3
266 billing_address:
267 street: 'foo street'
268 zip_code: 12345
269 user_id: 3
270 }
271 {
272 name: 'bar'
273 id: 10
274 billing_address:
275 street: 'bar street'
276 zip_code: 12345
277 user_id: 10
278 }
279 ]
280
281 test.done()
282
283 'belongsTo': (test) ->
284 test.expect 5
285
286 call = 1
287
288 connection =
289 query: (sql, params, cb) ->
290 switch call++
291 when 1
292 test.equal sql, 'SELECT * FROM "address"'
293 test.deepEqual params, []
294 cb null, {rows: [
295 {street: 'foo street', zip_code: 12345, user_id: 3}
296 {street: 'bar street', zip_code: 12345, user_id: 10}
297 ]}
298 when 2
299 test.equal sql, 'SELECT * FROM "user" WHERE id IN ($1, $2)'
300 test.deepEqual params, [3, 10]
301 cb null, {rows: [
302 {name: 'foo', id: 3}
303 {name: 'bar', id: 10}
304 {name: 'baz', id: 4}
305 ]}
306
307 userTable = mesa
308 .connection(-> test.fail())
309 .table('user')
310
311 addressTable = mesa
312 .connection(connection)
313 .table('address')
314 .belongsTo('person', userTable)
315
316 addressTable
317 .includes(person: true)
318 .find (err, addresses) ->
319 test.deepEqual addresses, [
320 {
321 street: 'foo street'
322 zip_code: 12345
323 user_id: 3
324 person:
325 name: 'foo'
326 id: 3
327 }
328 {
329 street: 'bar street'
330 zip_code: 12345
331 user_id: 10
332 person:
333 name: 'bar'
334 id: 10
335 }
336 ]
337
338 test.done()
339
340 'hasMany': (test) ->
341 test.expect 5
342
343 call = 1
344
345 connection =
346 query: (sql, params, cb) ->
347 switch call++
348 when 1
349 test.equal sql, 'SELECT * FROM "user"'
350 test.deepEqual params, []
351 cb null, {rows: [
352 {name: 'foo', id: 3}
353 {name: 'bar', id: 4}
354 ]}
355 when 2
356 test.equal sql, 'SELECT * FROM "task" WHERE user_id IN ($1, $2)'
357 test.deepEqual params, [3, 4]
358 cb null, {rows: [
359 {name: 'do laundry', user_id: 3}
360 {name: 'buy groceries', user_id: 4}
361 {name: 'foo', user_id: 3}
362 {name: 'bar', user_id: 3}
363 {name: 'buy the iphone 5', user_id: 5}
364 {name: 'learn clojure', user_id: 4}
365 ]}
366
367 taskTable = mesa
368 .connection(-> test.fail())
369 .table('task')
370
371 userTable = mesa
372 .connection(connection)
373 .table('user')
374 .hasMany('tasks', taskTable)
375
376 userTable
377 .includes(tasks: true)
378 .find (err, users) ->
379 test.deepEqual users, [
380 {
381 name: 'foo'
382 id: 3
383 tasks: [
384 {name: 'do laundry', user_id: 3}
385 {name: 'foo', user_id: 3}
386 {name: 'bar', user_id: 3}
387 ]
388 }
389 {
390 name: 'bar'
391 id: 4
392 tasks: [
393 {name: 'buy groceries', user_id: 4}
394 {name: 'learn clojure', user_id: 4}
395 ]
396 }
397 ]
398
399 test.done()
400
401 'hasManyThrough': (test) ->
402 test.expect 7
403
404 call = 1
405
406 connection =
407 query: (sql, params, cb) ->
408 switch call++
409 when 1
410 test.equal sql, 'SELECT * FROM "user"'
411 test.deepEqual params, []
412 cb null, {rows: [
413 {name: 'foo', id: 3}
414 {name: 'bar', id: 4}
415 {name: 'baz', id: 5}
416 ]}
417 when 2
418 test.equal sql, 'SELECT * FROM "user_role" WHERE user_id IN ($1, $2, $3)'
419 test.deepEqual params, [3, 4, 5]
420 cb null, {rows: [
421 {user_id: 5, role_id: 40}
422 {user_id: 5, role_id: 60}
423 {user_id: 3, role_id: 30}
424 {user_id: 4, role_id: 60}
425 {user_id: 3, role_id: 40}
426 {user_id: 3, role_id: 60}
427 {user_id: 5, role_id: 50}
428 ]}
429 when 3
430 test.equal sql, 'SELECT * FROM "role" WHERE id IN ($1, $2, $3, $4)'
431 test.deepEqual params, [40, 60, 30, 50]
432 cb null, {rows: [
433 {id: 30, name: 'jedi'}
434 {id: 40, name: 'administrator'}
435 {id: 60, name: 'master of the universe'}
436 {id: 50, name: 'bad bad role'}
437 ]}
438
439 roleTable = mesa
440 .connection(connection)
441 .table('role')
442
443 joinTable = mesa
444 .connection(connection)
445 .table('user_role')
446
447 userTable = mesa
448 .connection(connection)
449 .table('user')
450 .hasManyThrough('roles', roleTable, joinTable)
451
452 userTable
453 .includes(roles: true)
454 .find (err, users) ->
455 test.deepEqual users, [
456 {
457 name: 'foo'
458 id: 3
459 roles: [
460 {id: 30, name: 'jedi'}
461 {id: 40, name: 'administrator'}
462 {id: 60, name: 'master of the universe'}
463 ]
464 }
465 {
466 name: 'bar'
467 id: 4
468 roles: [
469 {id: 60, name: 'master of the universe'}
470 ]
471 }
472 {
473 name: 'baz'
474 id: 5
475 roles: [
476 {id: 40, name: 'administrator'}
477 {id: 60, name: 'master of the universe'}
478 {id: 50, name: 'bad bad role'}
479 ]
480 }
481 ]
482
483 test.done()
484
485 'self associations with custom keys and nested includes': (test) ->
486 test.expect 15
487
488 call = 1
489
490 connection =
491 query: (sql, params, cb) ->
492 switch call++
493 when 1
494 test.equal sql, 'SELECT * FROM "user"'
495 test.deepEqual params, []
496 cb null, {rows: [
497 {name: 'foo', id: 1, shipping_id: 11, billing_id: 101}
498 {name: 'bar', id: 2, shipping_id: 12, billing_id: 102}
499 {name: 'baz', id: 3, shipping_id: 13, billing_id: 103}
500 ]}
501 when 2
502 test.equal sql, 'SELECT * FROM "friend" WHERE user_id1 IN ($1, $2, $3)'
503 test.deepEqual params, [1, 2, 3]
504 cb null, {rows: [
505 {user_id1: 1, user_id2: 2}
506 {user_id1: 2, user_id2: 3}
507 {user_id1: 3, user_id2: 1}
508 {user_id1: 3, user_id2: 2}
509 ]}
510 when 3
511 test.equal sql, 'SELECT * FROM "user" WHERE id IN ($1, $2, $3)'
512 test.deepEqual params, [2, 3, 1]
513 cb null, {rows: [
514 {name: 'bar', id: 2, shipping_id: 12, billing_id: 102}
515 {name: 'baz', id: 3, shipping_id: 13, billing_id: 103}
516 {name: 'foo', id: 1, shipping_id: 11, billing_id: 101}
517 ]}
518 when 4
519 test.equal sql, 'SELECT * FROM "address" WHERE id IN ($1, $2, $3)'
520 test.deepEqual params, [12, 13, 11]
521 cb null, {rows: [
522 {street: 'bar shipping street', id: 12}
523 {street: 'baz shipping street', id: 13}
524 {street: 'foo shipping street', id: 11}
525 ]}
526 when 5
527 test.equal sql, 'SELECT * FROM "address" WHERE id IN ($1, $2, $3)'
528 test.deepEqual params, [101, 102, 103]
529 cb null, {rows: [
530 {street: 'foo billing street', id: 101}
531 {street: 'bar billing street', id: 102}
532 {street: 'baz billing street', id: 103}
533 ]}
534 when 6
535 test.equal sql, 'SELECT * FROM "user" WHERE billing_id IN ($1, $2, $3)'
536 test.deepEqual params, [101, 102, 103]
537 cb null, {rows: [
538 {name: 'bar', id: 2, shipping_id: 12, billing_id: 102}
539 {name: 'foo', id: 1, shipping_id: 11, billing_id: 101}
540 {name: 'baz', id: 3, shipping_id: 13, billing_id: 103}
541 ]}
542
543 table = {}
544 table.address = mesa
545 .connection(connection)
546 .table('address')
547 .hasOne('user', (-> table.user),
548 foreignKey: 'billing_id'
549 )
550
551 table.friend = mesa
552 .connection(connection)
553 .table('friend')
554
555 table.user = mesa
556 .connection(connection)
557 .table('user')
558 .belongsTo('billing_address', (-> table.address),
559 foreignKey: 'billing_id'
560 )
561 .belongsTo('shipping_address', (-> table.address),
562 foreignKey: 'shipping_id'
563 )
564 .hasManyThrough('friends', (-> table.user), (-> table.friend),
565 foreignKey: 'user_id1'
566 otherForeignKey: 'user_id2'
567 )
568
569 # include the billing address and all the friends with their
570 # shipping adresses
571
572 table.user
573 .includes(
574 friends: {shipping_address: true}
575 billing_address: {user: true}
576 )
577 .find (err, users) ->
578
579 test.deepEqual users[0],
580 name: 'foo'
581 id: 1
582 shipping_id: 11
583 billing_id: 101
584 friends: [
585 {
586 name: 'bar'
587 id: 2
588 shipping_id: 12
589 billing_id: 102
590 shipping_address: {
591 street: 'bar shipping street'
592 id: 12
593 }
594 }
595 ]
596 billing_address: {
597 street: 'foo billing street'
598 id: 101
599 user: {
600 name: 'foo'
601 id: 1
602 shipping_id: 11
603 billing_id: 101
604 }
605 }
606
607 test.deepEqual users[1],
608 name: 'bar'
609 id: 2
610 shipping_id: 12
611 billing_id: 102
612 friends: [
613 {
614 name: 'baz'
615 id: 3
616 shipping_id: 13
617 billing_id: 103
618 shipping_address: {
619 street: 'baz shipping street'
620 id: 13
621 }
622 }
623 ]
624 billing_address: {
625 street: 'bar billing street'
626 id: 102
627 user: {
628 name: 'bar'
629 id: 2
630 shipping_id: 12
631 billing_id: 102
632 }
633 }
634
635 test.deepEqual users[2],
636 name: 'baz'
637 id: 3
638 shipping_id: 13
639 billing_id: 103
640 friends: [
641 {
642 name: 'bar'
643 id: 2
644 shipping_id: 12
645 billing_id: 102
646 shipping_address: {
647 street: 'bar shipping street'
648 id: 12
649 }
650 }
651 {
652 name: 'foo'
653 id: 1
654 shipping_id: 11
655 billing_id: 101
656 shipping_address: {
657 street: 'foo shipping street'
658 id: 11
659 }
660 }
661 ]
662 billing_address: {
663 street: 'baz billing street'
664 id: 103
665 user: {
666 name: 'baz'
667 id: 3
668 shipping_id: 13
669 billing_id: 103
670 }
671 }
672
673 test.done()
674
675 'hasManyThrough works if there are no associated': (test) ->
676 test.expect 5
677
678 call = 1
679
680 connection =
681 query: (sql, params, cb) ->
682 switch call++
683 when 1
684 test.equal sql, 'SELECT * FROM "user"'
685 test.deepEqual params, []
686 cb null, {rows: [
687 {name: 'foo', id: 3}
688 {name: 'bar', id: 4}
689 {name: 'baz', id: 5}
690 ]}
691 when 2
692 test.equal sql, 'SELECT * FROM "user_role" WHERE user_id IN ($1, $2, $3)'
693 test.deepEqual params, [3, 4, 5]
694 cb null, {rows: []}
695
696 roleTable = mesa
697 .connection(connection)
698 .table('role')
699
700 userRoleTable = mesa
701 .connection(connection)
702 .table('user_role')
703
704 userTable = mesa
705 .connection(connection)
706 .table('user')
707 .hasManyThrough('roles', roleTable, userRoleTable)
708
709 userTable
710 .includes(roles: true)
711 .find (err, users) ->
712 test.deepEqual users, [
713 {
714 name: 'foo'
715 id: 3
716 roles: []
717 }
718 {
719 name: 'bar'
720 id: 4
721 roles: []
722 }
723 {
724 name: 'baz'
725 id: 5
726 roles: []
727 }
728 ]
729
730 test.done()