1- var ORM = require ( '../../' ) ;
2- var helper = require ( '../support/spec_helper' ) ;
3- var should = require ( 'should' ) ;
4- var async = require ( 'async' ) ;
5- var _ = require ( 'lodash' ) ;
1+ var ORM = require ( '../../' ) ;
2+ var helper = require ( '../support/spec_helper' ) ;
3+ var should = require ( 'should' ) ;
4+ var async = require ( 'async' ) ;
5+ var _ = require ( 'lodash' ) ;
6+ var common = require ( '../common' ) ;
7+ var protocol = common . protocol ( ) ;
68
79describe ( "hasOne" , function ( ) {
810 var db = null ;
@@ -12,23 +14,27 @@ describe("hasOne", function() {
1214 var leafId = null ;
1315 var treeId = null ;
1416 var stalkId = null ;
17+ var holeId = null ;
1518
1619 var setup = function ( opts ) {
1720 opts = opts || { } ;
1821 return function ( done ) {
1922 db . settings . set ( 'instance.cache' , false ) ;
2023 db . settings . set ( 'instance.returnAllErrors' , true ) ;
21- Tree = db . define ( "tree" , { type : { type : 'text' } } ) ;
24+ Tree = db . define ( "tree" , { type : { type : 'text' } } ) ;
2225 Stalk = db . define ( "stalk" , { length : { type : 'integer' } } ) ;
26+ Hole = db . define ( "hole" , { width : { type : 'integer' } } ) ;
2327 Leaf = db . define ( "leaf" , {
24- size : { type : 'integer' }
28+ size : { type : 'integer' } ,
29+ holeId : { type : 'integer' , mapsTo : 'hole_id' }
2530 } , {
2631 validations : opts . validations
2732 } ) ;
2833 Leaf . hasOne ( 'tree' , Tree , { field : 'treeId' , autoFetch : ! ! opts . autoFetch } ) ;
29- Leaf . hasOne ( 'stalk' , Stalk , { field : 'stalkId' } ) ;
34+ Leaf . hasOne ( 'stalk' , Stalk , { field : 'stalkId' , mapsTo : 'stalk_id' } ) ;
35+ Leaf . hasOne ( 'hole' , Hole , { field : 'holeId' } ) ;
3036
31- return helper . dropSync ( [ Tree , Stalk , Leaf ] , function ( ) {
37+ return helper . dropSync ( [ Tree , Stalk , Hole , Leaf ] , function ( ) {
3238 Tree . create ( { type : 'pine' } , function ( err , tree ) {
3339 should . not . exist ( err ) ;
3440 treeId = tree [ Tree . id ] ;
@@ -41,7 +47,11 @@ describe("hasOne", function() {
4147 should . not . exist ( err ) ;
4248 should . exist ( stalk ) ;
4349 stalkId = stalk [ Stalk . id ] ;
44- done ( ) ;
50+ Hole . create ( { width : 3 } , function ( err , hole ) {
51+ should . not . exist ( err ) ;
52+ holeId = hole . id ;
53+ done ( ) ;
54+ } ) ;
4555 } ) ;
4656 } ) ;
4757 } ) ;
@@ -370,4 +380,92 @@ describe("hasOne", function() {
370380 return done ( ) ;
371381 } ) ;
372382 } ) ;
383+
384+ if ( protocol != "mongodb" ) {
385+ describe ( "mapsTo" , function ( ) {
386+ describe ( "with `mapsTo` set via `hasOne`" , function ( ) {
387+ var leaf = null ;
388+
389+ before ( setup ( ) ) ;
390+
391+ before ( function ( done ) {
392+ Leaf . create ( { size : 444 , stalkId : stalkId , holeId : holeId } , function ( err , lf ) {
393+ should . not . exist ( err ) ;
394+ leaf = lf ;
395+ done ( ) ;
396+ } ) ;
397+ } ) ;
398+
399+ it ( "should have correct fields in the DB" , function ( done ) {
400+ var sql = db . driver . query . select ( )
401+ . from ( 'leaf' )
402+ . select ( 'size' , 'stalk_id' )
403+ . where ( { size : 444 } )
404+ . build ( ) ;
405+
406+ db . driver . execQuery ( sql , function ( err , rows ) {
407+ should . not . exist ( err ) ;
408+
409+ should . equal ( rows [ 0 ] . size , 444 ) ;
410+ should . equal ( rows [ 0 ] . stalk_id , 1 ) ;
411+
412+ done ( ) ;
413+ } ) ;
414+ } ) ;
415+
416+ it ( "should get parent" , function ( done ) {
417+ leaf . getStalk ( function ( err , stalk ) {
418+ should . not . exist ( err ) ;
419+
420+ should . exist ( stalk ) ;
421+ should . equal ( stalk . id , stalkId ) ;
422+ should . equal ( stalk . length , 20 ) ;
423+ done ( ) ;
424+ } ) ;
425+ } ) ;
426+ } ) ;
427+
428+ describe ( "with `mapsTo` set via property definition" , function ( ) {
429+ var leaf = null ;
430+
431+ before ( setup ( ) ) ;
432+
433+ before ( function ( done ) {
434+ Leaf . create ( { size : 444 , stalkId : stalkId , holeId : holeId } , function ( err , lf ) {
435+ should . not . exist ( err ) ;
436+ leaf = lf ;
437+ done ( ) ;
438+ } ) ;
439+ } ) ;
440+
441+ it ( "should have correct fields in the DB" , function ( done ) {
442+ var sql = db . driver . query . select ( )
443+ . from ( 'leaf' )
444+ . select ( 'size' , 'hole_id' )
445+ . where ( { size : 444 } )
446+ . build ( ) ;
447+
448+ db . driver . execQuery ( sql , function ( err , rows ) {
449+ should . not . exist ( err ) ;
450+
451+ should . equal ( rows [ 0 ] . size , 444 ) ;
452+ should . equal ( rows [ 0 ] . hole_id , 1 ) ;
453+
454+ done ( ) ;
455+ } ) ;
456+ } ) ;
457+
458+ it ( "should get parent" , function ( done ) {
459+ leaf . getHole ( function ( err , hole ) {
460+ should . not . exist ( err ) ;
461+
462+ should . exist ( hole ) ;
463+ should . equal ( hole . id , stalkId ) ;
464+ should . equal ( hole . width , 3 ) ;
465+ done ( ) ;
466+ } ) ;
467+ } ) ;
468+ } ) ;
469+ } ) ;
470+ } ;
373471} ) ;
0 commit comments