@@ -41,16 +41,25 @@ def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints
4141 on_update : constraints [ :on_update ] )
4242 end
4343
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 = [ ] )
4551 double ( 'Conn' ,
4652 indexes : indexes ,
4753 foreign_keys : foreign_keys ,
48- supports_foreign_keys? : true )
54+ check_constraints : check_constraints ,
55+ supports_foreign_keys? : true ,
56+ supports_check_constraints? : true )
4957 end
5058
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 = [ ] )
5261 options = {
53- connection : mock_connection ( indexes , foreign_keys ) ,
62+ connection : mock_connection ( indexes , foreign_keys , check_constraints ) ,
5463 table_exists? : true ,
5564 table_name : table_name ,
5665 primary_key : primary_key ,
@@ -62,6 +71,7 @@ def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = []
6271
6372 double ( 'An ActiveRecord class' , options )
6473 end
74+ # rubocop:enable Metrics/ParameterLists
6575
6676 def mock_column ( name , type , options = { } )
6777 default_options = {
@@ -221,7 +231,7 @@ def mock_column(name, type, options = {})
221231 end
222232
223233 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 )
225235 end
226236
227237 let :indexes do
@@ -232,6 +242,10 @@ def mock_column(name, type, options = {})
232242 [ ]
233243 end
234244
245+ let :check_constraints do
246+ [ ]
247+ end
248+
235249 context 'when option is not present' do
236250 let :options do
237251 { }
@@ -391,7 +405,7 @@ def mock_column(name, type, options = {})
391405 end
392406 end
393407
394- context 'with Globalize gem' do
408+ context 'with Globalize gem' do # rubocop:disable RSpec/MultipleMemoizedHelpers
395409 let :translation_klass do
396410 double ( 'Folder::Post::Translation' ,
397411 to_s : 'Folder::Post::Translation' ,
@@ -756,6 +770,82 @@ def mock_column(name, type, options = {})
756770 end
757771 end
758772
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+
759849 context 'when foreign keys exist' do
760850 let :columns do
761851 [
@@ -1492,6 +1582,53 @@ def mock_column(name, type, options = {})
14921582 end
14931583 end
14941584
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+
14951632 context 'when option "show_foreign_keys" is true' do
14961633 let :options do
14971634 { format_markdown : true , show_foreign_keys : true }
0 commit comments