@@ -364,6 +364,29 @@ describe('autoupdate', function() {
364
364
365
365
describe ( 'foreign key constraint' , function ( ) {
366
366
it ( 'should create, update, and delete foreign keys' , function ( done ) {
367
+ var product_schema = {
368
+ 'name' : 'Product' ,
369
+ 'options' : {
370
+ 'idInjection' : false ,
371
+ 'postgresql' : {
372
+ 'schema' : 'myapp_test' ,
373
+ 'table' : 'product_test' ,
374
+ } ,
375
+ } ,
376
+ 'properties' : {
377
+ 'id' : {
378
+ 'type' : 'String' ,
379
+ 'length' : 20 ,
380
+ 'id' : 1 ,
381
+ } ,
382
+ 'name' : {
383
+ 'type' : 'String' ,
384
+ 'required' : false ,
385
+ 'length' : 40 ,
386
+ } ,
387
+ } ,
388
+ } ;
389
+
367
390
var customer2_schema = {
368
391
'name' : 'CustomerTest2' ,
369
392
'options' : {
@@ -495,6 +518,10 @@ describe('autoupdate', function() {
495
518
'required' : false ,
496
519
'length' : 40 ,
497
520
} ,
521
+ 'productId' : {
522
+ 'type' : 'String' ,
523
+ 'length' : 20 ,
524
+ } ,
498
525
} ,
499
526
} ;
500
527
@@ -506,6 +533,20 @@ describe('autoupdate', function() {
506
533
'schema' : 'myapp_test' ,
507
534
'table' : 'order_test' ,
508
535
} ,
536
+ 'foreignKeys' : {
537
+ 'fk_ordertest_customerId' : {
538
+ 'name' : 'fk_ordertest_customerId' ,
539
+ 'entity' : 'CustomerTest2' ,
540
+ 'entityKey' : 'id' ,
541
+ 'foreignKey' : 'customerId' ,
542
+ } ,
543
+ 'fk_ordertest_productId' : {
544
+ 'name' : 'fk_ordertest_productId' ,
545
+ 'entity' : 'Product' ,
546
+ 'entityKey' : 'id' ,
547
+ 'foreignKey' : 'productId' ,
548
+ } ,
549
+ } ,
509
550
} ,
510
551
'properties' : {
511
552
'id' : {
@@ -522,9 +563,45 @@ describe('autoupdate', function() {
522
563
'required' : false ,
523
564
'length' : 40 ,
524
565
} ,
566
+ 'productId' : {
567
+ 'type' : 'String' ,
568
+ 'length' : 20 ,
569
+ } ,
570
+ } ,
571
+ } ;
572
+
573
+ var orderTest_schema_v4 = {
574
+ 'name' : 'OrderTest' ,
575
+ 'options' : {
576
+ 'idInjection' : false ,
577
+ 'postgresql' : {
578
+ 'schema' : 'myapp_test' ,
579
+ 'table' : 'order_test' ,
580
+ } ,
581
+ } ,
582
+ 'properties' : {
583
+ 'id' : {
584
+ 'type' : 'String' ,
585
+ 'length' : 20 ,
586
+ 'id' : 1 ,
587
+ } ,
588
+ 'customerId' : {
589
+ 'type' : 'String' ,
590
+ 'length' : 20 ,
591
+ } ,
592
+ 'productId' : {
593
+ 'type' : 'String' ,
594
+ 'length' : 20 ,
595
+ } ,
596
+ 'description' : {
597
+ 'type' : 'String' ,
598
+ 'required' : false ,
599
+ 'length' : 40 ,
600
+ } ,
525
601
} ,
526
602
} ;
527
603
604
+ ds . createModel ( product_schema . name , product_schema . properties , product_schema . options ) ;
528
605
ds . createModel ( customer2_schema . name , customer2_schema . properties , customer2_schema . options ) ;
529
606
ds . createModel ( customer3_schema . name , customer3_schema . properties , customer3_schema . options ) ;
530
607
@@ -553,18 +630,18 @@ describe('autoupdate', function() {
553
630
assert . equal ( foreignKeys [ 0 ] . fkColumnName , 'customerid' ) ;
554
631
assert . equal ( foreignKeys [ 0 ] . fkName , 'fk_ordertest_customerId' ) ;
555
632
556
- // update fk
633
+ // update and add another fk
557
634
ds . createModel ( orderTest_schema_v2 . name , orderTest_schema_v2 . properties , orderTest_schema_v2 . options ) ;
558
635
ds . autoupdate ( function ( err ) {
559
636
assert ( ! err , err ) ;
560
637
ds . discoverModelProperties ( 'order_test' , function ( err , props ) {
561
638
// validate that we have the correct number of properties
562
- assert . equal ( props . length , 3 ) ;
639
+ assert . equal ( props . length , 4 ) ;
563
640
564
641
// get the foreign keys for order_test
565
642
ds . connector . discoverForeignKeys ( 'order_test' , { } , function ( err , foreignKeysUpdated ) {
566
643
assert ( ! err , err ) ;
567
- // validate that the foreign key exists and points to the new column
644
+ // validate that the foreign keys exist and point to the new column
568
645
assert ( foreignKeysUpdated ) ;
569
646
assert . equal ( foreignKeysUpdated . length , 1 ) ;
570
647
assert . equal ( foreignKeysUpdated [ 0 ] . pkColumnName , 'id' ) ;
@@ -573,21 +650,33 @@ describe('autoupdate', function() {
573
650
assert . equal ( foreignKeysUpdated [ 0 ] . fkColumnName , 'customerid' ) ;
574
651
assert . equal ( foreignKeysUpdated [ 0 ] . fkName , 'fk_ordertest_customerId' ) ;
575
652
576
- // remove fk
653
+ // create multiple fks on object
577
654
ds . createModel ( orderTest_schema_v3 . name , orderTest_schema_v3 . properties ,
578
655
orderTest_schema_v3 . options ) ;
579
656
ds . autoupdate ( function ( err ) {
580
- assert ( ! err , err ) ;
581
- ds . discoverModelProperties ( 'order_test' , function ( err , props ) {
582
- // validate that we have the correct number of properties
583
- assert . equal ( props . length , 3 ) ;
584
-
585
- // get the foreign keys for order_test
586
- ds . connector . discoverForeignKeys ( 'order_test' , { } , function ( err , foreignKeysEmpty ) {
587
- if ( err ) return done ( err ) ;
588
- assert ( foreignKeysEmpty ) ;
589
- assert . equal ( foreignKeysEmpty . length , 0 ) ;
590
- done ( ) ;
657
+ // get the foreign keys for order_test
658
+ ds . connector . discoverForeignKeys ( 'order_test' , { } , function ( err , foreignKeysMulti ) {
659
+ assert ( ! err , err ) ;
660
+ assert ( foreignKeysMulti ) ;
661
+ assert . equal ( foreignKeysMulti . length , 2 ) ;
662
+
663
+ // remove fk
664
+ ds . createModel ( orderTest_schema_v4 . name , orderTest_schema_v4 . properties ,
665
+ orderTest_schema_v4 . options ) ;
666
+ ds . autoupdate ( function ( err ) {
667
+ assert ( ! err , err ) ;
668
+ ds . discoverModelProperties ( 'order_test' , function ( err , props ) {
669
+ // validate that we have the correct number of properties
670
+ assert . equal ( props . length , 4 ) ;
671
+
672
+ // get the foreign keys for order_test
673
+ ds . connector . discoverForeignKeys ( 'order_test' , { } , function ( err , foreignKeysEmpty ) {
674
+ if ( err ) return done ( err ) ;
675
+ assert ( foreignKeysEmpty ) ;
676
+ assert . equal ( foreignKeysEmpty . length , 0 ) ;
677
+ done ( ) ;
678
+ } ) ;
679
+ } ) ;
591
680
} ) ;
592
681
} ) ;
593
682
} ) ;
0 commit comments