1- var ORM = require ( '../../' ) ;
2- var helper = require ( '../support/spec_helper' ) ;
1+ var _ = require ( 'lodash' ) ;
32var should = require ( 'should' ) ;
43var async = require ( 'async' ) ;
5- var _ = require ( 'lodash' ) ;
4+ var helper = require ( '../support/spec_helper' ) ;
5+ var ORM = require ( '../../' ) ;
66
77describe ( "hasOne" , function ( ) {
88 var db = null ;
99 var Person = null ;
10- var Pet = null ;
10+ var Pet = null ;
1111
1212 var setup = function ( autoFetch ) {
1313 return function ( done ) {
1414 db . settings . set ( 'instance.cache' , false ) ;
1515 db . settings . set ( 'instance.returnAllErrors' , true ) ;
1616
17- Person = db . define ( 'person' , {
18- id : { type : "integer" , key :true } ,
19- firstName : { type : "text" , size :"255" } ,
20- lastName : { type : "text" , size :"255" }
17+ Person = db . define ( 'person' , {
18+ id : { type : "integer" , mapsTo : "personID" , key : true } ,
19+ firstName : { type : "text" , size : "255" } ,
20+ lastName : { type : "text" , size : "255" }
2121 } ) ;
2222
23- Pet = db . define ( 'pet' , {
24- id : { type : "integer" , key :true } ,
25- petName : { type : "text" , size :"255" } ,
26- ownerID : { type : "integer" , size :"4" }
23+ Pet = db . define ( 'pet' , {
24+ id : { type : "integer" , mapsTo : "petID" , key : true } ,
25+ petName : { type : "text" , size : "255" } ,
26+ ownerID : { type : "integer" , size : "4" }
2727 } ) ;
2828
29- Pet . hasOne ( 'owner' , Person , { field : 'ownerID' , autoFetch :autoFetch } ) ;
30-
31- helper . dropSync ( [ Person , Pet ] , function ( err ) {
32- if ( err ) return done ( err ) ;
33- Pet . create ( [ {
34- id : 10 ,
35- petName : 'Muttley' ,
36- owner : {
37- id : 12 ,
38- firstName : 'Stuey' ,
39- lastName : 'McG'
40- }
41- } ,
42- {
43- id : 11 ,
44- petName : 'Snagglepuss' ,
45- owner : {
46- id : 0 ,
47- firstName : 'John' ,
48- lastName : 'Doe'
49- }
50- } ] , done ) ;
51- } ) ;
29+ Pet . hasOne ( 'owner' , Person , { field : 'ownerID' , autoFetch : autoFetch } ) ;
30+
31+ helper . dropSync ( [ Person , Pet ] , function ( err ) {
32+ if ( err ) return done ( err ) ;
33+
34+ Pet . create ( [
35+ {
36+ id : 10 ,
37+ petName : 'Muttley' ,
38+ owner : {
39+ id : 12 ,
40+ firstName : 'Stuey' ,
41+ lastName : 'McG'
42+ }
43+ } ,
44+ {
45+ id : 11 ,
46+ petName : 'Snagglepuss' ,
47+ owner : {
48+ id : 0 ,
49+ firstName : 'John' ,
50+ lastName : 'Doe'
51+ }
52+ }
53+ ] , done ) ;
54+ } ) ;
5255 } ;
5356 } ;
5457
5558 before ( function ( done ) {
56- helper . connect ( function ( connection ) {
57- db = connection ;
58- done ( ) ;
59- } ) ;
59+ helper . connect ( function ( connection ) {
60+ db = connection ;
61+ done ( ) ;
62+ } ) ;
6063 } ) ;
6164
6265 describe ( "auto fetch" , function ( ) {
6366 before ( setup ( true ) ) ;
6467
6568 it ( "should work for non-zero ownerID " , function ( done ) {
6669 Pet . find ( { petName : "Muttley" } , function ( err , pets ) {
67- should . not . exist ( err ) ;
70+ should . not . exist ( err ) ;
6871
69- pets [ 0 ] . petName . should . equal ( "Muttley" ) ;
70- pets [ 0 ] . should . have . property ( "id" ) ;
71- pets [ 0 ] . id . should . equal ( 10 ) ;
72- pets [ 0 ] . ownerID . should . equal ( 12 ) ;
72+ pets [ 0 ] . petName . should . equal ( "Muttley" ) ;
73+ pets [ 0 ] . should . have . property ( "id" ) ;
74+ pets [ 0 ] . id . should . equal ( 10 ) ;
75+ pets [ 0 ] . ownerID . should . equal ( 12 ) ;
7376
74- pets [ 0 ] . should . have . property ( "owner" ) ;
75- pets [ 0 ] . owner . firstName . should . equal ( "Stuey" ) ;
77+ pets [ 0 ] . should . have . property ( "owner" ) ;
78+ pets [ 0 ] . owner . firstName . should . equal ( "Stuey" ) ;
7679
77- return done ( ) ;
80+ return done ( ) ;
7881 } ) ;
7982 } ) ;
8083
8184 it ( "should work for zero ownerID " , function ( done ) {
8285 Pet . find ( { petName : "Snagglepuss" } , function ( err , pets ) {
83- should . not . exist ( err ) ;
84-
85- pets [ 0 ] . petName . should . equal ( "Snagglepuss" ) ;
86- pets [ 0 ] . should . have . property ( "id" ) ;
87- pets [ 0 ] . id . should . equal ( 11 ) ;
88- pets [ 0 ] . ownerID . should . equal ( 0 ) ;
86+ should . not . exist ( err ) ;
8987
90- pets [ 0 ] . should . have . property ( "owner" ) ;
91- pets [ 0 ] . owner . firstName . should . equal ( "John" ) ;
88+ pets [ 0 ] . petName . should . equal ( "Snagglepuss" ) ;
89+ pets [ 0 ] . should . have . property ( "id" ) ;
90+ pets [ 0 ] . id . should . equal ( 11 ) ;
9291
93- return done ( ) ;
92+ db . models . person . all ( function ( err , people ) {
93+ return done ( ) ;
94+ } ) ;
9495 } ) ;
9596 } ) ;
9697 } ) ;
@@ -100,51 +101,55 @@ describe("hasOne", function() {
100101
101102 it ( "should work for non-zero ownerID " , function ( done ) {
102103 Pet . find ( { petName : "Muttley" } , function ( err , pets ) {
103- should . not . exist ( err ) ;
104+ should . not . exist ( err ) ;
104105
105- pets [ 0 ] . petName . should . equal ( "Muttley" ) ;
106- pets [ 0 ] . should . have . property ( "id" ) ;
107- pets [ 0 ] . id . should . equal ( 10 ) ;
108- pets [ 0 ] . ownerID . should . equal ( 12 ) ;
106+ pets [ 0 ] . petName . should . equal ( "Muttley" ) ;
107+ pets [ 0 ] . should . have . property ( "id" ) ;
108+ pets [ 0 ] . id . should . equal ( 10 ) ;
109+ pets [ 0 ] . ownerID . should . equal ( 12 ) ;
109110
110- pets [ 0 ] . should . not . have . property ( "owner" ) ;
111+ pets [ 0 ] . should . not . have . property ( "owner" ) ;
111112
112- // But we should be able to see if its there
113- pets [ 0 ] . hasOwner ( function ( err , result ) {
114- should . equal ( result , true ) ;
115- } ) ;
113+ // But we should be able to see if its there
114+ pets [ 0 ] . hasOwner ( function ( err , result ) {
115+ should . not . exist ( err ) ;
116+ should . equal ( result , true ) ;
116117
117- // ...and then get it
118- pets [ 0 ] . getOwner ( function ( err , result ) {
119- result . firstName . should . equal ( "Stuey" ) ;
120- } ) ;
118+ // ...and then get it
119+ pets [ 0 ] . getOwner ( function ( err , result ) {
120+ should . not . exist ( err ) ;
121+ result . firstName . should . equal ( "Stuey" ) ;
121122
122- return done ( ) ;
123+ return done ( )
124+ } ) ;
125+ } ) ;
123126 } ) ;
124127 } ) ;
125128
126129 it ( "should work for zero ownerID " , function ( done ) {
127130 Pet . find ( { petName : "Snagglepuss" } , function ( err , pets ) {
128- should . not . exist ( err ) ;
131+ should . not . exist ( err ) ;
129132
130- pets [ 0 ] . petName . should . equal ( "Snagglepuss" ) ;
131- pets [ 0 ] . should . have . property ( "id" ) ;
132- pets [ 0 ] . id . should . equal ( 11 ) ;
133- pets [ 0 ] . ownerID . should . equal ( 0 ) ;
133+ pets [ 0 ] . petName . should . equal ( "Snagglepuss" ) ;
134+ pets [ 0 ] . should . have . property ( "id" ) ;
135+ pets [ 0 ] . id . should . equal ( 11 ) ;
136+ pets [ 0 ] . ownerID . should . equal ( 0 ) ;
134137
135- pets [ 0 ] . should . not . have . property ( "owner" ) ;
138+ pets [ 0 ] . should . not . have . property ( "owner" ) ;
136139
137- // But we should be able to see if its there
138- pets [ 0 ] . hasOwner ( function ( err , result ) {
139- should . equal ( result , true ) ;
140- } ) ;
140+ // But we should be able to see if its there
141+ pets [ 0 ] . hasOwner ( function ( err , result ) {
142+ should . not . exist ( err ) ;
143+ should . equal ( result , true ) ;
141144
142- // ...and then get it
143- pets [ 0 ] . getOwner ( function ( err , result ) {
144- result . firstName . should . equal ( "John" ) ;
145- } ) ;
145+ // ...and then get it
146+ pets [ 0 ] . getOwner ( function ( err , result ) {
147+ should . not . exist ( err ) ;
148+ result . firstName . should . equal ( "John" ) ;
146149
147- return done ( ) ;
150+ return done ( )
151+ } ) ;
152+ } ) ;
148153 } ) ;
149154 } ) ;
150155 } ) ;
0 commit comments