@@ -12,23 +12,27 @@ describe("hasOne", function() {
1212 var leafId = null ;
1313 var treeId = null ;
1414 var stalkId = null ;
15+ var holeId = null ;
1516
1617 var setup = function ( opts ) {
1718 opts = opts || { } ;
1819 return function ( done ) {
1920 db . settings . set ( 'instance.cache' , false ) ;
2021 db . settings . set ( 'instance.returnAllErrors' , true ) ;
21- Tree = db . define ( "tree" , { type : { type : 'text' } } ) ;
22+ Tree = db . define ( "tree" , { type : { type : 'text' } } ) ;
2223 Stalk = db . define ( "stalk" , { length : { type : 'integer' } } ) ;
24+ Hole = db . define ( "hole" , { width : { type : 'integer' } } ) ;
2325 Leaf = db . define ( "leaf" , {
24- size : { type : 'integer' }
26+ size : { type : 'integer' } ,
27+ holeId : { type : 'integer' , mapsTo : 'hole_id' }
2528 } , {
2629 validations : opts . validations
2730 } ) ;
2831 Leaf . hasOne ( 'tree' , Tree , { field : 'treeId' , autoFetch : ! ! opts . autoFetch } ) ;
29- Leaf . hasOne ( 'stalk' , Stalk , { field : 'stalkId' } ) ;
32+ Leaf . hasOne ( 'stalk' , Stalk , { field : 'stalkId' , mapsTo : 'stalk_id' } ) ;
33+ Leaf . hasOne ( 'hole' , Hole , { field : 'holeId' } ) ;
3034
31- return helper . dropSync ( [ Tree , Stalk , Leaf ] , function ( ) {
35+ return helper . dropSync ( [ Tree , Stalk , Hole , Leaf ] , function ( ) {
3236 Tree . create ( { type : 'pine' } , function ( err , tree ) {
3337 should . not . exist ( err ) ;
3438 treeId = tree [ Tree . id ] ;
@@ -41,7 +45,11 @@ describe("hasOne", function() {
4145 should . not . exist ( err ) ;
4246 should . exist ( stalk ) ;
4347 stalkId = stalk [ Stalk . id ] ;
44- done ( ) ;
48+ Hole . create ( { width : 3 } , function ( err , hole ) {
49+ should . not . exist ( err ) ;
50+ holeId = hole . id ;
51+ done ( ) ;
52+ } ) ;
4553 } ) ;
4654 } ) ;
4755 } ) ;
@@ -370,4 +378,90 @@ describe("hasOne", function() {
370378 return done ( ) ;
371379 } ) ;
372380 } ) ;
381+
382+ describe ( "mapsTo" , function ( ) {
383+ describe ( "with `mapsTo` set via `hasOne`" , function ( ) {
384+ var leaf = null ;
385+
386+ before ( setup ( ) ) ;
387+
388+ before ( function ( done ) {
389+ Leaf . create ( { size : 444 , stalkId : stalkId , holeId : holeId } , function ( err , lf ) {
390+ should . not . exist ( err ) ;
391+ leaf = lf ;
392+ done ( ) ;
393+ } ) ;
394+ } ) ;
395+
396+ it ( "should have correct fields in the DB" , function ( done ) {
397+ var sql = db . driver . query . select ( )
398+ . from ( 'leaf' )
399+ . select ( 'size' , 'stalk_id' )
400+ . where ( { size : 444 } )
401+ . build ( ) ;
402+
403+ db . driver . execQuery ( sql , function ( err , rows ) {
404+ should . not . exist ( err ) ;
405+
406+ should . equal ( rows [ 0 ] . size , 444 ) ;
407+ should . equal ( rows [ 0 ] . stalk_id , 1 ) ;
408+
409+ done ( ) ;
410+ } ) ;
411+ } ) ;
412+
413+ it ( "should get parent" , function ( done ) {
414+ leaf . getStalk ( function ( err , stalk ) {
415+ should . not . exist ( err ) ;
416+
417+ should . exist ( stalk ) ;
418+ should . equal ( stalk . id , stalkId ) ;
419+ should . equal ( stalk . length , 20 ) ;
420+ done ( ) ;
421+ } ) ;
422+ } ) ;
423+ } ) ;
424+
425+ describe ( "with `mapsTo` set via property definition" , function ( ) {
426+ var leaf = null ;
427+
428+ before ( setup ( ) ) ;
429+
430+ before ( function ( done ) {
431+ Leaf . create ( { size : 444 , stalkId : stalkId , holeId : holeId } , function ( err , lf ) {
432+ should . not . exist ( err ) ;
433+ leaf = lf ;
434+ done ( ) ;
435+ } ) ;
436+ } ) ;
437+
438+ it ( "should have correct fields in the DB" , function ( done ) {
439+ var sql = db . driver . query . select ( )
440+ . from ( 'leaf' )
441+ . select ( 'size' , 'hole_id' )
442+ . where ( { size : 444 } )
443+ . build ( ) ;
444+
445+ db . driver . execQuery ( sql , function ( err , rows ) {
446+ should . not . exist ( err ) ;
447+
448+ should . equal ( rows [ 0 ] . size , 444 ) ;
449+ should . equal ( rows [ 0 ] . hole_id , 1 ) ;
450+
451+ done ( ) ;
452+ } ) ;
453+ } ) ;
454+
455+ it ( "should get parent" , function ( done ) {
456+ leaf . getHole ( function ( err , hole ) {
457+ should . not . exist ( err ) ;
458+
459+ should . exist ( hole ) ;
460+ should . equal ( hole . id , stalkId ) ;
461+ should . equal ( hole . width , 3 ) ;
462+ done ( ) ;
463+ } ) ;
464+ } ) ;
465+ } ) ;
466+ } ) ;
373467} ) ;
0 commit comments