Skip to content

Commit e40c243

Browse files
committed
Added indexes test.
1 parent 3b17467 commit e40c243

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

spec/annotate/annotate_models_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
require 'active_support/core_ext/string'
66

77
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+
816
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
917
double("ForeignKeyDefinition",
1018
:name => name,
@@ -24,9 +32,9 @@ def mock_connection(indexes = [], foreign_keys = [])
2432
)
2533
end
2634

27-
def mock_class(table_name, primary_key, columns, foreign_keys = [])
35+
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
2836
options = {
29-
:connection => mock_connection([], foreign_keys),
37+
:connection => mock_connection(indexes, foreign_keys),
3038
:table_exists? => true,
3139
:table_name => table_name,
3240
:primary_key => primary_key,
@@ -181,7 +189,7 @@ def mock_column(name, type, options={})
181189
klass = mock_class(:users, :id, [
182190
mock_column(:id, :integer),
183191
mock_column(:foreign_thing_id, :integer),
184-
],
192+
], [],
185193
[
186194
mock_foreign_key(
187195
'fk_rails_cf2568e89e',
@@ -220,7 +228,7 @@ def mock_column(name, type, options={})
220228
klass = mock_class(:users, :id, [
221229
mock_column(:id, :integer),
222230
mock_column(:foreign_thing_id, :integer),
223-
],
231+
], [],
224232
[
225233
mock_foreign_key(
226234
'fk_rails_02e851e3b7',
@@ -246,6 +254,28 @@ def mock_column(name, type, options={})
246254
EOS
247255
end
248256

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+
249279
it "should get schema info as RDoc" do
250280
klass = mock_class(:users, :id, [
251281
mock_column(:id, :integer),

0 commit comments

Comments
 (0)