5
5
require 'active_support/core_ext/string'
6
6
7
7
describe AnnotateModels do
8
+ def mock_index ( name , columns = [ ] , unique = false )
9
+ double ( "IndexKeyDefinition" ,
10
+ :name => name ,
11
+ :columns => columns ,
12
+ :unique => unique
13
+ )
14
+ end
15
+
8
16
def mock_foreign_key ( name , from_column , to_table , to_column = 'id' , constraints = { } )
9
17
double ( "ForeignKeyDefinition" ,
10
18
:name => name ,
@@ -24,9 +32,9 @@ def mock_connection(indexes = [], foreign_keys = [])
24
32
)
25
33
end
26
34
27
- def mock_class ( table_name , primary_key , columns , foreign_keys = [ ] )
35
+ def mock_class ( table_name , primary_key , columns , indexes = [ ] , foreign_keys = [ ] )
28
36
options = {
29
- :connection => mock_connection ( [ ] , foreign_keys ) ,
37
+ :connection => mock_connection ( indexes , foreign_keys ) ,
30
38
:table_exists? => true ,
31
39
:table_name => table_name ,
32
40
:primary_key => primary_key ,
@@ -181,7 +189,7 @@ def mock_column(name, type, options={})
181
189
klass = mock_class ( :users , :id , [
182
190
mock_column ( :id , :integer ) ,
183
191
mock_column ( :foreign_thing_id , :integer ) ,
184
- ] ,
192
+ ] , [ ] ,
185
193
[
186
194
mock_foreign_key (
187
195
'fk_rails_cf2568e89e' ,
@@ -220,7 +228,7 @@ def mock_column(name, type, options={})
220
228
klass = mock_class ( :users , :id , [
221
229
mock_column ( :id , :integer ) ,
222
230
mock_column ( :foreign_thing_id , :integer ) ,
223
- ] ,
231
+ ] , [ ] ,
224
232
[
225
233
mock_foreign_key (
226
234
'fk_rails_02e851e3b7' ,
@@ -246,6 +254,28 @@ def mock_column(name, type, options={})
246
254
EOS
247
255
end
248
256
257
+ it "should get indexes keys" do
258
+ klass = mock_class ( :users , :id , [
259
+ mock_column ( :id , :integer ) ,
260
+ mock_column ( :foreign_thing_id , :integer ) ,
261
+ ] , [ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
262
+ mock_index ( 'index_rails_02e851e3b8' , [ 'foreign_thing_id' ] ) ] )
263
+ expect ( AnnotateModels . get_schema_info ( klass , "Schema Info" , :show_indexes => true ) ) . to eql ( <<-EOS )
264
+ # Schema Info
265
+ #
266
+ # Table name: users
267
+ #
268
+ # id :integer not null, primary key
269
+ # foreign_thing_id :integer not null
270
+ #
271
+ # Indexes
272
+ #
273
+ # index_rails_02e851e3b7 (id)
274
+ # index_rails_02e851e3b8 (foreign_thing_id)
275
+ #
276
+ EOS
277
+ end
278
+
249
279
it "should get schema info as RDoc" do
250
280
klass = mock_class ( :users , :id , [
251
281
mock_column ( :id , :integer ) ,
0 commit comments