@@ -41,16 +41,17 @@ 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_connection ( indexes = [ ] , foreign_keys = [ ] , table_comment = nil )
4545 double ( 'Conn' ,
4646 indexes : indexes ,
4747 foreign_keys : foreign_keys ,
48- supports_foreign_keys? : true )
48+ supports_foreign_keys? : true ,
49+ table_comment : table_comment )
4950 end
5051
51- def mock_class ( table_name , primary_key , columns , indexes = [ ] , foreign_keys = [ ] )
52+ def mock_class ( table_name , primary_key , columns , indexes = [ ] , foreign_keys = [ ] , table_comment = nil )
5253 options = {
53- connection : mock_connection ( indexes , foreign_keys ) ,
54+ connection : mock_connection ( indexes , foreign_keys , table_comment ) ,
5455 table_exists? : true ,
5556 table_name : table_name ,
5657 primary_key : primary_key ,
@@ -217,7 +218,7 @@ def mock_column(name, type, options = {})
217218 end
218219
219220 let :klass do
220- mock_class ( :users , primary_key , columns , indexes , foreign_keys )
221+ mock_class ( :users , primary_key , columns , indexes , foreign_keys , table_comment )
221222 end
222223
223224 let :indexes do
@@ -228,6 +229,10 @@ def mock_column(name, type, options = {})
228229 [ ]
229230 end
230231
232+ let :table_comment do
233+ [ ]
234+ end
235+
231236 context 'when option is not present' do
232237 let :options do
233238 { }
@@ -1061,6 +1066,60 @@ def mock_column(name, type, options = {})
10611066 { with_comment : 'yes' }
10621067 end
10631068
1069+ context 'when table have comments' do
1070+ let :table_comment do
1071+ 'users table comment'
1072+ end
1073+
1074+ let :columns do
1075+ [
1076+ mock_column ( :id , :integer , limit : 8 ) ,
1077+ ]
1078+ end
1079+
1080+ let :expected_result do
1081+ <<~EOS
1082+ # Schema Info
1083+ #
1084+ # Table name: users(users table comment)
1085+ #
1086+ # id :integer not null, primary key
1087+ #
1088+ EOS
1089+ end
1090+
1091+ it 'works with option "with_comment"' do
1092+ is_expected . to eq expected_result
1093+ end
1094+ end
1095+
1096+ context 'when table have multiline comments' do
1097+ let :table_comment do
1098+ "Notes.\n Users table comment"
1099+ end
1100+
1101+ let :columns do
1102+ [
1103+ mock_column ( :id , :integer , limit : 8 ) ,
1104+ ]
1105+ end
1106+
1107+ let :expected_result do
1108+ <<~EOS
1109+ # Schema Info
1110+ #
1111+ # Table name: users(Notes.\\ nUsers table comment)
1112+ #
1113+ # id :integer not null, primary key
1114+ #
1115+ EOS
1116+ end
1117+
1118+ it 'works with option "with_comment"' do
1119+ is_expected . to eq expected_result
1120+ end
1121+ end
1122+
10641123 context 'when columns have comments' do
10651124 let :columns do
10661125 [
@@ -1194,6 +1253,41 @@ def mock_column(name, type, options = {})
11941253 is_expected . to eq expected_result
11951254 end
11961255 end
1256+
1257+ context 'when both table and columns have comments' do
1258+ let :table_comment do
1259+ 'users table comment'
1260+ end
1261+
1262+ let :columns do
1263+ [
1264+ mock_column ( :id , :integer , limit : 8 , comment : 'ID' ) ,
1265+ mock_column ( :active , :boolean , limit : 1 , comment : 'Active' ) ,
1266+ mock_column ( :name , :string , limit : 50 , comment : 'Name' ) ,
1267+ mock_column ( :notes , :text , limit : 55 , comment : 'Notes' ) ,
1268+ mock_column ( :no_comment , :text , limit : 20 , comment : nil )
1269+ ]
1270+ end
1271+
1272+ let :expected_result do
1273+ <<~EOS
1274+ # Schema Info
1275+ #
1276+ # Table name: users(users table comment)
1277+ #
1278+ # id(ID) :integer not null, primary key
1279+ # active(Active) :boolean not null
1280+ # name(Name) :string(50) not null
1281+ # notes(Notes) :text(55) not null
1282+ # no_comment :text(20) not null
1283+ #
1284+ EOS
1285+ end
1286+
1287+ it 'works with option "with_comment' do
1288+ is_expected . to eq expected_result
1289+ end
1290+ end
11971291 end
11981292 end
11991293 end
0 commit comments