@@ -41,16 +41,17 @@ 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_connection ( indexes = [ ] , foreign_keys = [ ] , table_comment = nil )
45
45
double ( 'Conn' ,
46
46
indexes : indexes ,
47
47
foreign_keys : foreign_keys ,
48
- supports_foreign_keys? : true )
48
+ supports_foreign_keys? : true ,
49
+ table_comment : table_comment )
49
50
end
50
51
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 )
52
53
options = {
53
- connection : mock_connection ( indexes , foreign_keys ) ,
54
+ connection : mock_connection ( indexes , foreign_keys , table_comment ) ,
54
55
table_exists? : true ,
55
56
table_name : table_name ,
56
57
primary_key : primary_key ,
@@ -217,7 +218,7 @@ def mock_column(name, type, options = {})
217
218
end
218
219
219
220
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 )
221
222
end
222
223
223
224
let :indexes do
@@ -228,6 +229,10 @@ def mock_column(name, type, options = {})
228
229
[ ]
229
230
end
230
231
232
+ let :table_comment do
233
+ [ ]
234
+ end
235
+
231
236
context 'when option is not present' do
232
237
let :options do
233
238
{ }
@@ -1061,6 +1066,60 @@ def mock_column(name, type, options = {})
1061
1066
{ with_comment : 'yes' }
1062
1067
end
1063
1068
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
+
1064
1123
context 'when columns have comments' do
1065
1124
let :columns do
1066
1125
[
@@ -1194,6 +1253,41 @@ def mock_column(name, type, options = {})
1194
1253
is_expected . to eq expected_result
1195
1254
end
1196
1255
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
1197
1291
end
1198
1292
end
1199
1293
end
0 commit comments