@@ -6,6 +6,7 @@ var common = require('../common');
6
6
describe ( "Model.find() chaining" , function ( ) {
7
7
var db = null ;
8
8
var Person = null ;
9
+ var Dog = null ;
9
10
10
11
var setup = function ( ) {
11
12
return function ( done ) {
@@ -36,6 +37,30 @@ describe("Model.find() chaining", function() {
36
37
} ;
37
38
} ;
38
39
40
+ var setup2 = function ( ) {
41
+ return function ( done ) {
42
+ Dog = db . define ( "dog" , {
43
+ name : String ,
44
+ } ) ;
45
+ Dog . hasMany ( "friends" ) ;
46
+ Dog . hasMany ( "family" ) ;
47
+
48
+ ORM . singleton . clear ( ) ; // clear cache
49
+
50
+ return helper . dropSync ( Dog , function ( ) {
51
+ Dog . create ( [ {
52
+ name : "Fido" ,
53
+ friends : [ { name : "Gunner" } , { name : "Chainsaw" } ] ,
54
+ family : [ { name : "Chester" } ]
55
+ } , {
56
+ name : "Thumper" ,
57
+ friends : [ { name : "Bambi" } ] ,
58
+ family : [ { name : "Princess" } , { name : "Butch" } ]
59
+ } ] , done ) ;
60
+ } ) ;
61
+ } ;
62
+ } ;
63
+
39
64
before ( function ( done ) {
40
65
helper . connect ( function ( connection ) {
41
66
db = connection ;
@@ -479,6 +504,76 @@ describe("Model.find() chaining", function() {
479
504
} ) ;
480
505
} ) ;
481
506
507
+ describe ( ".eager()" , function ( ) {
508
+ before ( setup2 ( ) ) ;
509
+
510
+ // TODO: Remove this code once the Mongo eager loading is implemented
511
+ var isMongo = function ( ) {
512
+ if ( db . driver . config . protocol == "mongodb:" ) {
513
+ ( function ( ) {
514
+ Dog . find ( ) . eager ( "friends" ) . all ( function ( ) {
515
+ // Should not ever run.
516
+ } ) ;
517
+ } ) . should . throw ( ) ;
518
+
519
+ return true ;
520
+ }
521
+ return false ;
522
+ } ;
523
+
524
+ it ( "should fetch all listed associations in a single query" , function ( done ) {
525
+ if ( isMongo ( ) ) { return done ( ) ; } ;
526
+
527
+ Dog . find ( { name : [ "Fido" , "Thumper" ] } ) . eager ( "friends" ) . all ( function ( err , dogs ) {
528
+ should . equal ( err , null ) ;
529
+
530
+ should ( Array . isArray ( dogs ) ) ;
531
+
532
+ dogs . length . should . equal ( 2 ) ;
533
+
534
+ dogs [ 0 ] . friends . length . should . equal ( 2 ) ;
535
+ dogs [ 1 ] . friends . length . should . equal ( 1 ) ;
536
+ done ( ) ;
537
+ } ) ;
538
+ } ) ;
539
+
540
+ it ( "should be able to handle multiple associations" , function ( done ) {
541
+ if ( isMongo ( ) ) { return done ( ) ; } ;
542
+
543
+ Dog . find ( { name : [ "Fido" , "Thumper" ] } ) . eager ( "friends" , "family" ) . all ( function ( err , dogs ) {
544
+ should . equal ( err , null ) ;
545
+
546
+ should ( Array . isArray ( dogs ) ) ;
547
+
548
+ dogs . length . should . equal ( 2 ) ;
549
+
550
+ dogs [ 0 ] . friends . length . should . equal ( 2 ) ;
551
+ dogs [ 0 ] . family . length . should . equal ( 1 ) ;
552
+ dogs [ 1 ] . friends . length . should . equal ( 1 ) ;
553
+ dogs [ 1 ] . family . length . should . equal ( 2 ) ;
554
+ done ( ) ;
555
+ } ) ;
556
+ } ) ;
557
+
558
+ it ( "should work with array parameters too" , function ( done ) {
559
+ if ( isMongo ( ) ) { return done ( ) ; } ;
560
+
561
+ Dog . find ( { name : [ "Fido" , "Thumper" ] } ) . eager ( [ "friends" , "family" ] ) . all ( function ( err , dogs ) {
562
+ should . equal ( err , null ) ;
563
+
564
+ should ( Array . isArray ( dogs ) ) ;
565
+
566
+ dogs . length . should . equal ( 2 ) ;
567
+
568
+ dogs [ 0 ] . friends . length . should . equal ( 2 ) ;
569
+ dogs [ 0 ] . family . length . should . equal ( 1 ) ;
570
+ dogs [ 1 ] . friends . length . should . equal ( 1 ) ;
571
+ dogs [ 1 ] . family . length . should . equal ( 2 ) ;
572
+ done ( ) ;
573
+ } ) ;
574
+ } ) ;
575
+ } ) ;
576
+
482
577
describe ( ".success()" , function ( ) {
483
578
before ( setup ( ) ) ;
484
579
0 commit comments