@@ -41,16 +41,25 @@ def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints
41
41
on_update : constraints [ :on_update ] )
42
42
end
43
43
44
- def mock_connection ( indexes = [ ] , foreign_keys = [ ] )
44
+ def mock_check_constraint ( name , expression )
45
+ double ( 'CheckConstraintDefinition' ,
46
+ name : name ,
47
+ expression : expression )
48
+ end
49
+
50
+ def mock_connection ( indexes = [ ] , foreign_keys = [ ] , check_constraints = [ ] )
45
51
double ( 'Conn' ,
46
52
indexes : indexes ,
47
53
foreign_keys : foreign_keys ,
48
- supports_foreign_keys? : true )
54
+ check_constraints : check_constraints ,
55
+ supports_foreign_keys? : true ,
56
+ supports_check_constraints? : true )
49
57
end
50
58
51
- def mock_class ( table_name , primary_key , columns , indexes = [ ] , foreign_keys = [ ] )
59
+ # rubocop:disable Metrics/ParameterLists
60
+ def mock_class ( table_name , primary_key , columns , indexes = [ ] , foreign_keys = [ ] , check_constraints = [ ] )
52
61
options = {
53
- connection : mock_connection ( indexes , foreign_keys ) ,
62
+ connection : mock_connection ( indexes , foreign_keys , check_constraints ) ,
54
63
table_exists? : true ,
55
64
table_name : table_name ,
56
65
primary_key : primary_key ,
@@ -62,6 +71,7 @@ def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = []
62
71
63
72
double ( 'An ActiveRecord class' , options )
64
73
end
74
+ # rubocop:enable Metrics/ParameterLists
65
75
66
76
def mock_column ( name , type , options = { } )
67
77
default_options = {
@@ -221,7 +231,7 @@ def mock_column(name, type, options = {})
221
231
end
222
232
223
233
let :klass do
224
- mock_class ( :users , primary_key , columns , indexes , foreign_keys )
234
+ mock_class ( :users , primary_key , columns , indexes , foreign_keys , check_constraints )
225
235
end
226
236
227
237
let :indexes do
@@ -232,6 +242,10 @@ def mock_column(name, type, options = {})
232
242
[ ]
233
243
end
234
244
245
+ let :check_constraints do
246
+ [ ]
247
+ end
248
+
235
249
context 'when option is not present' do
236
250
let :options do
237
251
{ }
@@ -391,7 +405,7 @@ def mock_column(name, type, options = {})
391
405
end
392
406
end
393
407
394
- context 'with Globalize gem' do
408
+ context 'with Globalize gem' do # rubocop:disable RSpec/MultipleMemoizedHelpers
395
409
let :translation_klass do
396
410
double ( 'Folder::Post::Translation' ,
397
411
to_s : 'Folder::Post::Translation' ,
@@ -756,6 +770,82 @@ def mock_column(name, type, options = {})
756
770
end
757
771
end
758
772
773
+ context 'when check constraints exist' do
774
+ let :columns do
775
+ [
776
+ mock_column ( :id , :integer ) ,
777
+ mock_column ( :age , :integer )
778
+ ]
779
+ end
780
+
781
+ context 'when option "show_check_constraints" is true' do
782
+ let :options do
783
+ { show_check_constraints : true }
784
+ end
785
+
786
+ context 'when check constraints are defined' do
787
+ let :check_constraints do
788
+ [
789
+ mock_check_constraint ( 'alive' , 'age < 150' ) ,
790
+ mock_check_constraint ( 'must_be_adult' , 'age >= 18' ) ,
791
+ mock_check_constraint ( 'missing_expression' , nil ) ,
792
+ mock_check_constraint ( 'multiline_test' , <<~SQL )
793
+ CASE
794
+ WHEN (age >= 18) THEN (age <= 21)
795
+ ELSE true
796
+ END
797
+ SQL
798
+ ]
799
+ end
800
+
801
+ let :expected_result do
802
+ <<~EOS
803
+ # Schema Info
804
+ #
805
+ # Table name: users
806
+ #
807
+ # id :integer not null, primary key
808
+ # age :integer not null
809
+ #
810
+ # Check Constraints
811
+ #
812
+ # alive (age < 150)
813
+ # missing_expression
814
+ # multiline_test (CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)
815
+ # must_be_adult (age >= 18)
816
+ #
817
+ EOS
818
+ end
819
+
820
+ it 'returns schema info with check constraint information' do
821
+ is_expected . to eq expected_result
822
+ end
823
+ end
824
+
825
+ context 'when check constraint is not defined' do
826
+ let :check_constraints do
827
+ [ ]
828
+ end
829
+
830
+ let :expected_result do
831
+ <<~EOS
832
+ # Schema Info
833
+ #
834
+ # Table name: users
835
+ #
836
+ # id :integer not null, primary key
837
+ # age :integer not null
838
+ #
839
+ EOS
840
+ end
841
+
842
+ it 'returns schema info without check constraint information' do
843
+ is_expected . to eq expected_result
844
+ end
845
+ end
846
+ end
847
+ end
848
+
759
849
context 'when foreign keys exist' do
760
850
let :columns do
761
851
[
@@ -1492,6 +1582,53 @@ def mock_column(name, type, options = {})
1492
1582
end
1493
1583
end
1494
1584
1585
+ context 'when option "show_check_constraints" is true' do
1586
+ let :options do
1587
+ { format_markdown : true , show_check_constraints : true }
1588
+ end
1589
+
1590
+ context 'when check constraints are defined' do
1591
+ let :check_constraints do
1592
+ [
1593
+ mock_check_constraint ( 'min_name_length' , 'LENGTH(name) > 2' ) ,
1594
+ mock_check_constraint ( 'missing_expression' , nil ) ,
1595
+ mock_check_constraint ( 'multiline_test' , <<~SQL )
1596
+ CASE
1597
+ WHEN (age >= 18) THEN (age <= 21)
1598
+ ELSE true
1599
+ END
1600
+ SQL
1601
+ ]
1602
+ end
1603
+
1604
+ let :expected_result do
1605
+ <<~EOS
1606
+ # == Schema Information
1607
+ #
1608
+ # Table name: `users`
1609
+ #
1610
+ # ### Columns
1611
+ #
1612
+ # Name | Type | Attributes
1613
+ # ----------- | ------------------ | ---------------------------
1614
+ # **`id`** | `integer` | `not null, primary key`
1615
+ # **`name`** | `string(50)` | `not null`
1616
+ #
1617
+ # ### Check Constraints
1618
+ #
1619
+ # * `min_name_length`: `(LENGTH(name) > 2)`
1620
+ # * `missing_expression`
1621
+ # * `multiline_test`: `(CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)`
1622
+ #
1623
+ EOS
1624
+ end
1625
+
1626
+ it 'returns schema info with check constraint information in Markdown format' do
1627
+ is_expected . to eq expected_result
1628
+ end
1629
+ end
1630
+ end
1631
+
1495
1632
context 'when option "show_foreign_keys" is true' do
1496
1633
let :options do
1497
1634
{ format_markdown : true , show_foreign_keys : true }
0 commit comments