5
5
require 'active_support/core_ext/string'
6
6
7
7
describe AnnotateModels do
8
- def mock_index ( name , columns = [ ] , unique = false )
8
+ def mock_index ( name , columns = [ ] , orders = { } , unique = false )
9
9
double ( 'IndexKeyDefinition' ,
10
10
name : name ,
11
11
columns : columns ,
12
- unique : unique )
12
+ unique : unique ,
13
+ orders : orders )
13
14
end
14
15
15
16
def mock_foreign_key ( name , from_column , to_table , to_column = 'id' , constraints = { } )
@@ -320,14 +321,52 @@ def mock_column(name, type, options = {})
320
321
EOS
321
322
end
322
323
324
+ it 'should get ordered indexes keys' do
325
+ klass = mock_class ( :users ,
326
+ :id ,
327
+ [
328
+ mock_column ( "id" , :integer ) ,
329
+ mock_column ( "firstname" , :string ) ,
330
+ mock_column ( "surname" , :string ) ,
331
+ mock_column ( "value" , :string )
332
+ ] ,
333
+ [
334
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
335
+ mock_index ( 'index_rails_02e851e3b8' ,
336
+ %w( firstname surname value ) ,
337
+ 'surname' => :asc , 'value' => :desc )
338
+ ] )
339
+ expect ( AnnotateModels . get_schema_info ( klass , 'Schema Info' , show_indexes : true ) ) . to eql ( <<-EOS )
340
+ # Schema Info
341
+ #
342
+ # Table name: users
343
+ #
344
+ # id :integer not null, primary key
345
+ # firstname :string not null
346
+ # surname :string not null
347
+ # value :string not null
348
+ #
349
+ # Indexes
350
+ #
351
+ # index_rails_02e851e3b7 (id)
352
+ # index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
353
+ #
354
+ EOS
355
+ end
356
+
323
357
it 'should get simple indexes keys' do
324
358
klass = mock_class ( :users ,
325
359
:id ,
326
360
[
327
361
mock_column ( :id , :integer ) ,
328
362
mock_column ( :foreign_thing_id , :integer )
329
- ] , [ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
330
- mock_index ( 'index_rails_02e851e3b8' , [ 'foreign_thing_id' ] ) ] )
363
+ ] ,
364
+ [
365
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
366
+ mock_index ( 'index_rails_02e851e3b8' ,
367
+ [ 'foreign_thing_id' ] ,
368
+ 'foreign_thing_id' => :desc )
369
+ ] )
331
370
expect ( AnnotateModels . get_schema_info ( klass , 'Schema Info' , simple_indexes : true ) ) . to eql ( <<-EOS )
332
371
# Schema Info
333
372
#
@@ -460,8 +499,13 @@ def mock_column(name, type, options = {})
460
499
[
461
500
mock_column ( :id , :integer ) ,
462
501
mock_column ( :name , :string , limit : 50 )
463
- ] , [ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
464
- mock_index ( 'index_rails_02e851e3b8' , [ 'foreign_thing_id' ] ) ] )
502
+ ] ,
503
+ [
504
+ mock_index ( 'index_rails_02e851e3b7' , [ 'id' ] ) ,
505
+ mock_index ( 'index_rails_02e851e3b8' ,
506
+ [ 'foreign_thing_id' ] ,
507
+ 'foreign_thing_id' => :desc )
508
+ ] )
465
509
expect ( AnnotateModels . get_schema_info ( klass , AnnotateModels ::PREFIX , format_markdown : true , show_indexes : true ) ) . to eql ( <<-EOS )
466
510
# #{ AnnotateModels ::PREFIX }
467
511
#
@@ -479,7 +523,7 @@ def mock_column(name, type, options = {})
479
523
# * `index_rails_02e851e3b7`:
480
524
# * **`id`**
481
525
# * `index_rails_02e851e3b8`:
482
- # * **`foreign_thing_id`**
526
+ # * **`foreign_thing_id DESC `**
483
527
#
484
528
EOS
485
529
end
0 commit comments