@@ -11,91 +11,77 @@ describe('loopback json api hasMany relationships', function () {
1111 ds = loopback . createDataSource ( 'memory' ) ;
1212
1313 Post = ds . createModel ( 'post' , {
14- id : {
15- type : Number ,
16- id : true
17- } ,
1814 title : String ,
1915 content : String
2016 } ) ;
2117 app . model ( Post ) ;
2218
2319 Comment = ds . createModel ( 'comment' , {
24- id : {
25- type : Number ,
26- id : true
27- } ,
28- postId : Number ,
2920 title : String ,
3021 comment : String
3122 } ) ;
3223 Comment . settings . plural = 'comments' ;
3324 app . model ( Comment ) ;
3425
3526 Author = ds . createModel ( 'author' , {
36- id : {
37- type : Number ,
38- id : true
39- } ,
4027 firstName : String ,
4128 lastName : String
4229 } ) ;
4330 Author . settings . plural = 'authors' ;
4431 app . model ( Author ) ;
4532
46- Post . hasMany ( Comment , {
47- as : 'comments' ,
48- foreignKey : 'postId'
49- } ) ;
50- Post . hasOne ( Author , {
51- as : 'author' ,
52- foreignKey : 'id'
53- } ) ;
33+ Post . hasMany ( Comment ) ;
34+ Comment . belongsTo ( Post ) ;
35+ Post . belongsTo ( Author ) ;
36+ Author . hasMany ( Post ) ;
5437
5538 app . use ( loopback . rest ( ) ) ;
5639 JSONAPIComponent ( app , { restApiRoot : '/' } ) ;
5740 } ) ;
5841
59- describe ( 'Requesting multiple via `includes` should return relationships ' , function ( done ) {
42+ describe ( 'Multiple `includes`' , function ( done ) {
6043 beforeEach ( function ( done ) {
6144 Author . create ( {
6245 firstName : 'Joe' ,
6346 lastName : 'Shmoe'
6447 } , function ( err , author ) {
6548 expect ( err ) . to . equal ( null ) ;
66- Post . create ( {
49+ author . posts . create ( {
6750 title : 'my post' ,
6851 content : 'my post content'
6952 } , function ( err , post ) {
7053 expect ( err ) . to . equal ( null ) ;
71- post . comments . create ( {
54+ post . comments . create ( [ {
7255 title : 'My comment' ,
7356 comment : 'My comment text'
74- } , function ( ) {
75- post . comments . create ( {
76- title : 'My second comment' ,
77- comment : 'My second comment text'
78- } , done ) ;
79- } ) ;
57+ } , {
58+ title : 'My second comment' ,
59+ comment : 'My second comment text'
60+ } ] , done ) ;
8061 } ) ;
8162 } ) ;
8263 } ) ;
8364
84- it ( 'should return stuff ' , function ( done ) {
65+ it ( 'should sideload author and comments ' , function ( done ) {
8566 request ( app ) . get ( '/posts/1/?include=author,comments' )
8667 . expect ( 200 )
8768 . end ( function ( err , res ) {
69+ var data = res . body . data ;
8870 expect ( err ) . to . equal ( null ) ;
89- //expect(res.body.data.id).to.equal('1');
90- expect ( res . body . data . type ) . to . equal ( 'posts' ) ;
91- expect ( res . body . data . relationships ) . to . be . a ( 'object' ) ;
92- expect ( res . body . data . relationships . author ) . to . be . a ( 'object' ) ;
93- expect ( res . body . data . relationships . comments ) . to . be . a ( 'object' ) ;
94- expect ( res . body . data . attributes ) . to . deep . equal ( {
71+ expect ( data . id ) . to . equal ( '1' ) ;
72+ expect ( data . type ) . to . equal ( 'posts' ) ;
73+ expect ( data . relationships ) . to . be . a ( 'object' ) ;
74+ expect ( data . relationships . author ) . to . be . a ( 'object' ) ;
75+ expect ( data . relationships . author . data . id ) . to . equal ( '1' ) ;
76+ expect ( data . relationships . author . data . type ) . to . equal ( 'authors' ) ;
77+ expect ( data . relationships . comments . data ) . to . be . a ( 'array' ) ;
78+ expect ( data . relationships . comments . data [ 0 ] . id ) . to . equal ( '1' ) ;
79+ expect ( data . relationships . comments . data [ 0 ] . type ) . to . equal ( 'comments' ) ;
80+ expect ( data . relationships . comments . data [ 1 ] . id ) . to . equal ( '2' ) ;
81+ expect ( data . relationships . comments . data [ 1 ] . type ) . to . equal ( 'comments' ) ;
82+ expect ( data . attributes ) . to . deep . equal ( {
9583 title : 'my post' ,
96- content : 'my post content' ,
97- author : '1' ,
98- comments : [ '1' , '2' ]
84+ content : 'my post content'
9985 } ) ;
10086 expect ( res . body . included ) . to . be . an ( 'array' ) ;
10187 expect ( res . body . included . length ) . to . equal ( 3 ) ;
@@ -111,7 +97,6 @@ describe('loopback json api hasMany relationships', function () {
11197 id : '1' ,
11298 type : 'comments' ,
11399 attributes : {
114- postId : 1 ,
115100 title : 'My comment' ,
116101 comment : 'My comment text'
117102 }
@@ -120,7 +105,6 @@ describe('loopback json api hasMany relationships', function () {
120105 id : '2' ,
121106 type : 'comments' ,
122107 attributes : {
123- postId : 1 ,
124108 title : 'My second comment' ,
125109 comment : 'My second comment text'
126110 }
0 commit comments