5
5
require 'active_support/core_ext/string'
6
6
7
7
describe AnnotateModels do
8
- def mock_index ( name , columns = [ ] , orders = { } , unique = false )
8
+ def mock_index ( name , columns = [ ] , orders = { } , unique = false , where = nil , using = nil )
9
9
double ( 'IndexKeyDefinition' ,
10
10
name : name ,
11
11
columns : columns ,
12
12
unique : unique ,
13
- orders : orders )
13
+ orders : orders ,
14
+ where : where ,
15
+ using : using )
14
16
end
15
17
16
18
def mock_foreign_key ( name , from_column , to_table , to_column = 'id' , constraints = { } )
@@ -354,6 +356,77 @@ def mock_column(name, type, options = {})
354
356
EOS
355
357
end
356
358
359
+ it 'should get indexes keys with where clause' do
360
+ klass = mock_class ( :users ,
361
+ :id ,
362
+ [
363
+ mock_column ( "id" , :integer ) ,
364
+ mock_column ( "firstname" , :string ) ,
365
+ mock_column ( "surname" , :string ) ,
366
+ mock_column ( "value" , :string )
367
+ ] ,
368
+ [
369
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
370
+ mock_index ( 'index_rails_02e851e3b8' ,
371
+ %w( firstname surname ) ,
372
+ { } ,
373
+ false ,
374
+ 'value IS NOT NULL' )
375
+ ] )
376
+ expect ( AnnotateModels . get_schema_info ( klass , 'Schema Info' , show_indexes : true ) ) . to eql ( <<-EOS )
377
+ # Schema Info
378
+ #
379
+ # Table name: users
380
+ #
381
+ # id :integer not null, primary key
382
+ # firstname :string not null
383
+ # surname :string not null
384
+ # value :string not null
385
+ #
386
+ # Indexes
387
+ #
388
+ # index_rails_02e851e3b7 (id)
389
+ # index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL
390
+ #
391
+ EOS
392
+ end
393
+
394
+ it 'should get indexes keys with using clause other than btree' do
395
+ klass = mock_class ( :users ,
396
+ :id ,
397
+ [
398
+ mock_column ( "id" , :integer ) ,
399
+ mock_column ( "firstname" , :string ) ,
400
+ mock_column ( "surname" , :string ) ,
401
+ mock_column ( "value" , :string )
402
+ ] ,
403
+ [
404
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
405
+ mock_index ( 'index_rails_02e851e3b8' ,
406
+ %w( firstname surname ) ,
407
+ { } ,
408
+ false ,
409
+ nil ,
410
+ 'hash' )
411
+ ] )
412
+ expect ( AnnotateModels . get_schema_info ( klass , 'Schema Info' , show_indexes : true ) ) . to eql ( <<-EOS )
413
+ # Schema Info
414
+ #
415
+ # Table name: users
416
+ #
417
+ # id :integer not null, primary key
418
+ # firstname :string not null
419
+ # surname :string not null
420
+ # value :string not null
421
+ #
422
+ # Indexes
423
+ #
424
+ # index_rails_02e851e3b7 (id)
425
+ # index_rails_02e851e3b8 (firstname,surname) USING hash
426
+ #
427
+ EOS
428
+ end
429
+
357
430
it 'should get simple indexes keys' do
358
431
klass = mock_class ( :users ,
359
432
:id ,
@@ -494,6 +567,40 @@ def mock_column(name, type, options = {})
494
567
end
495
568
496
569
it 'should get schema info as Markdown with indexes' do
570
+ klass = mock_class ( :users ,
571
+ :id ,
572
+ [
573
+ mock_column ( :id , :integer ) ,
574
+ mock_column ( :name , :string , limit : 50 )
575
+ ] ,
576
+ [
577
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
578
+ mock_index ( 'index_rails_02e851e3b8' ,
579
+ [ 'foreign_thing_id' ] )
580
+ ] )
581
+ expect ( AnnotateModels . get_schema_info ( klass , AnnotateModels ::PREFIX , format_markdown : true , show_indexes : true ) ) . to eql ( <<-EOS )
582
+ # #{ AnnotateModels ::PREFIX }
583
+ #
584
+ # Table name: `users`
585
+ #
586
+ # ### Columns
587
+ #
588
+ # Name | Type | Attributes
589
+ # ----------- | ------------------ | ---------------------------
590
+ # **`id`** | `integer` | `not null, primary key`
591
+ # **`name`** | `string(50)` | `not null`
592
+ #
593
+ # ### Indexes
594
+ #
595
+ # * `index_rails_02e851e3b7`:
596
+ # * **`id`**
597
+ # * `index_rails_02e851e3b8`:
598
+ # * **`foreign_thing_id`**
599
+ #
600
+ EOS
601
+ end
602
+
603
+ it 'should get schema info as Markdown with unique indexes' do
497
604
klass = mock_class ( :users ,
498
605
:id ,
499
606
[
@@ -504,7 +611,43 @@ def mock_column(name, type, options = {})
504
611
mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
505
612
mock_index ( 'index_rails_02e851e3b8' ,
506
613
[ 'foreign_thing_id' ] ,
507
- 'foreign_thing_id' => :desc )
614
+ { } ,
615
+ true )
616
+ ] )
617
+ expect ( AnnotateModels . get_schema_info ( klass , AnnotateModels ::PREFIX , format_markdown : true , show_indexes : true ) ) . to eql ( <<-EOS )
618
+ # #{ AnnotateModels ::PREFIX }
619
+ #
620
+ # Table name: `users`
621
+ #
622
+ # ### Columns
623
+ #
624
+ # Name | Type | Attributes
625
+ # ----------- | ------------------ | ---------------------------
626
+ # **`id`** | `integer` | `not null, primary key`
627
+ # **`name`** | `string(50)` | `not null`
628
+ #
629
+ # ### Indexes
630
+ #
631
+ # * `index_rails_02e851e3b7`:
632
+ # * **`id`**
633
+ # * `index_rails_02e851e3b8` (_unique_):
634
+ # * **`foreign_thing_id`**
635
+ #
636
+ EOS
637
+ end
638
+
639
+ it 'should get schema info as Markdown with ordered indexes' do
640
+ klass = mock_class ( :users ,
641
+ :id ,
642
+ [
643
+ mock_column ( :id , :integer ) ,
644
+ mock_column ( :name , :string , limit : 50 )
645
+ ] ,
646
+ [
647
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
648
+ mock_index ( 'index_rails_02e851e3b8' ,
649
+ [ 'foreign_thing_id' ] ,
650
+ { 'foreign_thing_id' => :desc } )
508
651
] )
509
652
expect ( AnnotateModels . get_schema_info ( klass , AnnotateModels ::PREFIX , format_markdown : true , show_indexes : true ) ) . to eql ( <<-EOS )
510
653
# #{ AnnotateModels ::PREFIX }
@@ -528,6 +671,81 @@ def mock_column(name, type, options = {})
528
671
EOS
529
672
end
530
673
674
+ it 'should get schema info as Markdown with indexes with WHERE clause' do
675
+ klass = mock_class ( :users ,
676
+ :id ,
677
+ [
678
+ mock_column ( :id , :integer ) ,
679
+ mock_column ( :name , :string , limit : 50 )
680
+ ] ,
681
+ [
682
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
683
+ mock_index ( 'index_rails_02e851e3b8' ,
684
+ [ 'foreign_thing_id' ] ,
685
+ { } ,
686
+ true ,
687
+ 'name IS NOT NULL' )
688
+ ] )
689
+ expect ( AnnotateModels . get_schema_info ( klass , AnnotateModels ::PREFIX , format_markdown : true , show_indexes : true ) ) . to eql ( <<-EOS )
690
+ # #{ AnnotateModels ::PREFIX }
691
+ #
692
+ # Table name: `users`
693
+ #
694
+ # ### Columns
695
+ #
696
+ # Name | Type | Attributes
697
+ # ----------- | ------------------ | ---------------------------
698
+ # **`id`** | `integer` | `not null, primary key`
699
+ # **`name`** | `string(50)` | `not null`
700
+ #
701
+ # ### Indexes
702
+ #
703
+ # * `index_rails_02e851e3b7`:
704
+ # * **`id`**
705
+ # * `index_rails_02e851e3b8` (_unique_ _where_ name IS NOT NULL):
706
+ # * **`foreign_thing_id`**
707
+ #
708
+ EOS
709
+ end
710
+
711
+ it 'should get schema info as Markdown with indexes with using clause other than btree' do
712
+ klass = mock_class ( :users ,
713
+ :id ,
714
+ [
715
+ mock_column ( :id , :integer ) ,
716
+ mock_column ( :name , :string , limit : 50 )
717
+ ] ,
718
+ [
719
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
720
+ mock_index ( 'index_rails_02e851e3b8' ,
721
+ [ 'foreign_thing_id' ] ,
722
+ { } ,
723
+ false ,
724
+ nil ,
725
+ 'hash' )
726
+ ] )
727
+ expect ( AnnotateModels . get_schema_info ( klass , AnnotateModels ::PREFIX , format_markdown : true , show_indexes : true ) ) . to eql ( <<-EOS )
728
+ # #{ AnnotateModels ::PREFIX }
729
+ #
730
+ # Table name: `users`
731
+ #
732
+ # ### Columns
733
+ #
734
+ # Name | Type | Attributes
735
+ # ----------- | ------------------ | ---------------------------
736
+ # **`id`** | `integer` | `not null, primary key`
737
+ # **`name`** | `string(50)` | `not null`
738
+ #
739
+ # ### Indexes
740
+ #
741
+ # * `index_rails_02e851e3b7`:
742
+ # * **`id`**
743
+ # * `index_rails_02e851e3b8` (_using_ hash):
744
+ # * **`foreign_thing_id`**
745
+ #
746
+ EOS
747
+ end
748
+
531
749
describe '#set_defaults' do
532
750
it 'should default show_complete_foreign_keys to false' do
533
751
expect ( Annotate . true? ( ENV [ 'show_complete_foreign_keys' ] ) ) . to be ( false )
0 commit comments