diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index c9f36543d..7ffd145e0 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -176,18 +176,42 @@ def mock_column(name, type, options = {}) end describe '.get_schema_info' do + subject do + AnnotateModels.get_schema_info(klass, header) + end + + let :klass do + mock_class(:users, primary_key, columns, indexes, foreign_keys) + end + + let :indexes do + [] + end + + let :foreign_keys do + [] + end + context 'when header is "Schema Info"' do + let :header do + 'Schema Info' + end + context 'when the primary key is not specified' do + let :primary_key do + nil + end + context 'when the columns are normal' do - it 'returns schema info' do - klass = mock_class(:users, - nil, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:name, :string, limit: 50) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -196,21 +220,23 @@ def mock_column(name, type, options = {}) # name :string(50) not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info' do + is_expected.to eq(expected_result) end end context 'when an enum column exists' do - it 'returns schema info' do - klass = mock_class(:users, - nil, - [ - mock_column(:id, :integer), - mock_column(:name, :enum, limit: [:enum1, :enum2]) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:name, :enum, limit: [:enum1, :enum2]) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -219,25 +245,27 @@ def mock_column(name, type, options = {}) # name :enum not null, (enum1, enum2) # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info' do + is_expected.to eq(expected_result) end end context 'when unsigned columns exist' do - it 'returns schema info' do - klass = mock_class(:users, - nil, - [ - mock_column(:id, :integer), - mock_column(:integer, :integer, unsigned?: true), - mock_column(:bigint, :integer, unsigned?: true, bigint?: true), - mock_column(:bigint, :bigint, unsigned?: true), - mock_column(:float, :float, unsigned?: true), - mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2), - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:integer, :integer, unsigned?: true), + mock_column(:bigint, :integer, unsigned?: true, bigint?: true), + mock_column(:bigint, :bigint, unsigned?: true), + mock_column(:float, :float, unsigned?: true), + mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2), + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -250,25 +278,31 @@ def mock_column(name, type, options = {}) # decimal :decimal(10, 2) unsigned, not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info' do + is_expected.to eq(expected_result) end end end context 'when the primary key is specified' do context 'when the primary_key is :id' do + let :primary_key do + :id + end + context 'when columns are normal' do - it 'returns schema info' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer, limit: 8), - mock_column(:name, :string, limit: 50), - mock_column(:notes, :text, limit: 55) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer, limit: 8), + mock_column(:name, :string, limit: 50), + mock_column(:notes, :text, limit: 55) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -278,22 +312,24 @@ def mock_column(name, type, options = {}) # notes :text(55) not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info' do + is_expected.to eq(expected_result) end end context 'when columns have default values' do - it 'returns schema info with default values' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:size, :integer, default: 20), - mock_column(:flag, :boolean, default: false) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:size, :integer, default: 20), + mock_column(:flag, :boolean, default: false) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -303,21 +339,31 @@ def mock_column(name, type, options = {}) # flag :boolean default(FALSE), not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info with default values' do + is_expected.to eq(expected_result) end end context 'when an integer column using ActiveRecord::Enum exists' do - it 'returns schema info with default values' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:status, :integer, default: 0) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:status, :integer, default: 0) + ] + end + + before :each do + # column_defaults may be overritten when ActiveRecord::Enum is used, e.g: + # class User < ActiveRecord::Base + # enum status: [ :disabled, :enabled ] + # end + allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled') + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -326,29 +372,36 @@ def mock_column(name, type, options = {}) # status :integer default(0), not null # EOS + end - # column_defaults may be overritten when ActiveRecord::Enum is used, e.g: - # class User < ActiveRecord::Base - # enum status: [ :disabled, :enabled ] - # end - allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled') - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info with default values' do + is_expected.to eq(expected_result) end end context 'when indexes exist' do context 'when option "show_indexes" is true' do + subject do + AnnotateModels.get_schema_info(klass, header, show_indexes: true) + end + context 'when indexes are normal' do - it 'returns schema info with index information' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], [mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:foreign_thing_id, :integer) + ] + end + + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -362,29 +415,34 @@ def mock_column(name, type, options = {}) # index_rails_02e851e3b8 (foreign_thing_id) # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information' do + is_expected.to eq expected_result end end context 'when one of indexes includes orderd index key' do - it 'returns schema info with index information' do - klass = mock_class(:users, - :id, - [ - mock_column("id", :integer), - mock_column("firstname", :string), - mock_column("surname", :string), - mock_column("value", :string) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: %w(firstname surname value), - orders: { 'surname' => :asc, 'value' => :desc }) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column("id", :integer), + mock_column("firstname", :string), + mock_column("surname", :string), + mock_column("value", :string) + ] + end + + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: %w(firstname surname value), + orders: { 'surname' => :asc, 'value' => :desc }) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -400,29 +458,34 @@ def mock_column(name, type, options = {}) # index_rails_02e851e3b8 (firstname,surname ASC,value DESC) # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information' do + is_expected.to eq expected_result end end context 'when one of indexes includes "where" clause' do - it 'returns schema info with index information' do - klass = mock_class(:users, - :id, - [ - mock_column("id", :integer), - mock_column("firstname", :string), - mock_column("surname", :string), - mock_column("value", :string) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: %w(firstname surname), - where: 'value IS NOT NULL') - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column("id", :integer), + mock_column("firstname", :string), + mock_column("surname", :string), + mock_column("value", :string) + ] + end + + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: %w(firstname surname), + where: 'value IS NOT NULL') + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -438,29 +501,34 @@ def mock_column(name, type, options = {}) # index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information' do + is_expected.to eq expected_result end end context 'when one of indexes includes "using" clause other than "btree"' do - it 'returns schema info with index information' do - klass = mock_class(:users, - :id, - [ - mock_column("id", :integer), - mock_column("firstname", :string), - mock_column("surname", :string), - mock_column("value", :string) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: %w(firstname surname), - using: 'hash') - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column("id", :integer), + mock_column("firstname", :string), + mock_column("surname", :string), + mock_column("value", :string) + ] + end + + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: %w(firstname surname), + using: 'hash') + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -476,21 +544,27 @@ def mock_column(name, type, options = {}) # index_rails_02e851e3b8 (firstname,surname) USING hash # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information' do + is_expected.to eq expected_result end end context 'when index is not defined' do - it 'returns schema info without index information' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], []) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:foreign_thing_id, :integer) + ] + end + + let :indexes do + [] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -499,29 +573,38 @@ def mock_column(name, type, options = {}) # foreign_thing_id :integer not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(expected_result) + it 'returns schema info without index information' do + is_expected.to eq expected_result end end end context 'when option "simple_indexes" is true' do + subject do + AnnotateModels.get_schema_info(klass, header, simple_indexes: true) + end + context 'when one of indexes includes "orders" clause' do # TODO - it 'returns schema info with index information' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: ['foreign_thing_id'], - orders: { 'foreign_thing_id' => :desc }) - ]) - - expected_result = <<~EOS + let :columns do + [ + mock_column(:id, :integer), + mock_column(:foreign_thing_id, :integer) + ] + end + + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: ['foreign_thing_id'], + orders: { 'foreign_thing_id' => :desc }) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -530,22 +613,30 @@ def mock_column(name, type, options = {}) # foreign_thing_id :integer not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(expected_result) + it 'returns schema info with index information' do + is_expected.to eq expected_result end end context 'when one of indexes is in string form' do - it 'returns schema info with index information' do - klass = mock_class(:users, - :id, - [ - mock_column("id", :integer), - mock_column("name", :string) - ], [mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)')]) - - expected_result = <<~EOS + let :columns do + [ + mock_column("id", :integer), + mock_column("name", :string) + ] + end + + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)') + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -554,37 +645,39 @@ def mock_column(name, type, options = {}) # name :string not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(expected_result) + it 'returns schema info with index information' do + is_expected.to eq expected_result end end end end context 'when foreign keys exist' do + let :columns do + [ + mock_column(:id, :integer), + mock_column(:foreign_thing_id, :integer) + ] + end + + let :foreign_keys do + [ + mock_foreign_key('fk_rails_cf2568e89e', 'foreign_thing_id', 'foreign_things'), + mock_foreign_key('custom_fk_name', 'other_thing_id', 'other_things'), + mock_foreign_key('fk_rails_a70234b26c', 'third_thing_id', 'third_things') + ] + end + context 'when option "show_foreign_keys" is specified' do + subject do + AnnotateModels.get_schema_info(klass, header, show_foreign_keys: true) + end + context 'when foreign_keys does not have option' do - it 'returns schema info with foreign keys' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], - [], - [ - mock_foreign_key('fk_rails_cf2568e89e', - 'foreign_thing_id', - 'foreign_things'), - mock_foreign_key('custom_fk_name', - 'other_thing_id', - 'other_things'), - mock_foreign_key('fk_rails_a70234b26c', - 'third_thing_id', - 'third_things') - ]) - - expected_result = <<~EOS + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -599,30 +692,27 @@ def mock_column(name, type, options = {}) # fk_rails_... (third_thing_id => third_things.id) # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true)).to eql(expected_result) + it 'returns schema info with foreign keys' do + is_expected.to eq(expected_result) end end context 'when foreign_keys have option "on_delete" and "on_update"' do - it 'returns schema info with foreign keys' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], - [], - [ - mock_foreign_key('fk_rails_02e851e3b7', - 'foreign_thing_id', - 'foreign_things', - 'id', - on_delete: 'on_delete_value', - on_update: 'on_update_value') - ]) - - expected_result = <<~EOS + let :foreign_keys do + [ + mock_foreign_key('fk_rails_02e851e3b7', + 'foreign_thing_id', + 'foreign_things', + 'id', + on_delete: 'on_delete_value', + on_update: 'on_update_value') + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -635,34 +725,21 @@ def mock_column(name, type, options = {}) # fk_rails_... (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true)).to eql(expected_result) + it 'returns schema info with foreign keys' do + is_expected.to eq(expected_result) end end end context 'when option "show_foreign_keys" and "show_complete_foreign_keys" are specified' do - it 'returns schema info with foreign keys' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], - [], - [ - mock_foreign_key('fk_rails_cf2568e89e', - 'foreign_thing_id', - 'foreign_things'), - mock_foreign_key('custom_fk_name', - 'other_thing_id', - 'other_things'), - mock_foreign_key('fk_rails_a70234b26c', - 'third_thing_id', - 'third_things') - ]) - - expected_result = <<~EOS + subject do + AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true, show_complete_foreign_keys: true) + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -677,24 +754,30 @@ def mock_column(name, type, options = {}) # fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id) # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true, show_complete_foreign_keys: true)).to eql(expected_result) + it 'returns schema info with foreign keys' do + is_expected.to eq(expected_result) end end end end context 'when the primary key is an array (using composite_primary_keys)' do - it 'returns schema info' do - klass = mock_class(:users, - [:a_id, :b_id], - [ - mock_column(:a_id, :integer), - mock_column(:b_id, :integer), - mock_column(:name, :string, limit: 50) - ]) - - expected_result = <<~EOS + let :primary_key do + [:a_id, :b_id] + end + + let :columns do + [ + mock_column(:a_id, :integer), + mock_column(:b_id, :integer), + mock_column(:name, :string, limit: 50) + ] + end + + let :expected_result do + <<~EOS # Schema Info # # Table name: users @@ -704,26 +787,40 @@ def mock_column(name, type, options = {}) # name :string(50) not null # EOS + end - expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_result) + it 'returns schema info' do + is_expected.to eq(expected_result) end end end end context 'when header is "== Schema Information"' do + let :header do + AnnotateModels::PREFIX + end + context 'when the primary key is specified' do context 'when the primary_key is :id' do + let :primary_key do + :id + end + + let :columns do + [ + mock_column(:id, :integer), + mock_column(:name, :string, limit: 50) + ] + end + context 'when option "format_rdoc" is true' do - it 'returns schema info in RDoc format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ]) - - expected_result = <<~EOS + subject do + AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true) + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: users @@ -734,22 +831,21 @@ def mock_column(name, type, options = {}) # == Schema Information End #++ EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true)).to eql(expected_result) + it 'returns schema info in RDoc format' do + is_expected.to eq(expected_result) end end context 'when option "format_markdown" is true' do context 'when other option is not specified' do - it 'returns schema info in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ]) - - expected_result = <<~EOS + subject do + AnnotateModels.get_schema_info(klass, header, format_markdown: true) + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -762,27 +858,28 @@ def mock_column(name, type, options = {}) # **`name`** | `string(50)` | `not null` # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true)).to eql(expected_result) + it 'returns schema info in Markdown format' do + is_expected.to eq(expected_result) end end context 'when option "show_indexes" is true' do + subject do + AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_indexes: true) + end + context 'when indexes are normal' do - it 'returns schema info with index information in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: ['foreign_thing_id']) - ]) - - expected_result = <<~EOS + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']) + ] + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -802,27 +899,25 @@ def mock_column(name, type, options = {}) # * **`foreign_thing_id`** # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information in Markdown format' do + is_expected.to eq expected_result end end context 'when one of indexes includes "unique" clause' do - it 'returns schema info with index information in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: ['foreign_thing_id'], - unique: true) - ]) - - expected_result = <<~EOS + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: ['foreign_thing_id'], + unique: true) + ] + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -842,27 +937,25 @@ def mock_column(name, type, options = {}) # * **`foreign_thing_id`** # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information in Markdown format' do + is_expected.to eq expected_result end end context 'when one of indexes includes orderd index key' do - it 'returns schema info with index information in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: ['foreign_thing_id'], - orders: { 'foreign_thing_id' => :desc }) - ]) - - expected_result = <<~EOS + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: ['foreign_thing_id'], + orders: { 'foreign_thing_id' => :desc }) + ] + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -882,28 +975,26 @@ def mock_column(name, type, options = {}) # * **`foreign_thing_id DESC`** # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information in Markdown format' do + is_expected.to eq expected_result end end context 'when one of indexes includes "where" clause and "unique" clause' do - it 'returns schema info with index information in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: ['foreign_thing_id'], - unique: true, - where: 'name IS NOT NULL') - ]) - - expected_result = <<~EOS + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: ['foreign_thing_id'], + unique: true, + where: 'name IS NOT NULL') + ] + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -923,27 +1014,25 @@ def mock_column(name, type, options = {}) # * **`foreign_thing_id`** # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information in Markdown format' do + is_expected.to eq expected_result end end context 'when one of indexes includes "using" clause other than "btree"' do - it 'returns schema info with index information in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:name, :string, limit: 50) - ], - [ - mock_index('index_rails_02e851e3b7', columns: ['id']), - mock_index('index_rails_02e851e3b8', - columns: ['foreign_thing_id'], - using: 'hash') - ]) - - expected_result = <<~EOS + let :indexes do + [ + mock_index('index_rails_02e851e3b7', columns: ['id']), + mock_index('index_rails_02e851e3b8', + columns: ['foreign_thing_id'], + using: 'hash') + ] + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -963,32 +1052,40 @@ def mock_column(name, type, options = {}) # * **`foreign_thing_id`** # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(expected_result) + it 'returns schema info with index information in Markdown format' do + is_expected.to eq expected_result end end end context 'when option "show_foreign_keys" is true' do + subject do + AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_foreign_keys: true) + end + + let :columns do + [ + mock_column(:id, :integer), + mock_column(:foreign_thing_id, :integer) + ] + end + context 'when foreign_keys have option "on_delete" and "on_update"' do - it 'returns schema info with foreign_keys in Markdown format' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer), - mock_column(:foreign_thing_id, :integer) - ], - [], - [ - mock_foreign_key('fk_rails_02e851e3b7', - 'foreign_thing_id', - 'foreign_things', - 'id', - on_delete: 'on_delete_value', - on_update: 'on_update_value') - ]) - - expected_result = <<~EOS + let :foreign_keys do + [ + mock_foreign_key('fk_rails_02e851e3b7', + 'foreign_thing_id', + 'foreign_things', + 'id', + on_delete: 'on_delete_value', + on_update: 'on_update_value') + ] + end + + let :expected_result do + <<~EOS # == Schema Information # # Table name: `users` @@ -1006,8 +1103,10 @@ def mock_column(name, type, options = {}) # * **`foreign_thing_id => foreign_things.id`** # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_foreign_keys: true)).to eql(expected_result) + it 'returns schema info with foreign_keys in Markdown format' do + is_expected.to eq(expected_result) end end end