Commit 1b51de89 by Shu Fujita Committed by Andrew W. Lee

AnnotateModels.get_schema_info (with custom options) (#732)

* Structuralize RSpec test cases of AnnotateModels.get_schema_info (with custom options) * Replace expression expansion to plain text * Refactor RSpec test cases of AnnotateModels.get_schema_info (with custom options) * Change position of test cases
parent fd663b96
...@@ -987,30 +987,38 @@ EOS ...@@ -987,30 +987,38 @@ EOS
end end
end end
describe '#get_schema_info with custom options' do describe '.get_schema_info (with custom options)' do
def self.when_called_with(options = {}) let :klass do
expected = options.delete(:returns) mock_class(:users, :id, columns)
default_columns = [ end
[:id, :integer, { limit: 8 }],
[:active, :boolean, { limit: 1 }],
[:name, :string, { limit: 50 }],
[:notes, :text, { limit: 55 }]
]
it "should work with options = #{options}" do subject do
with_columns = (options.delete(:with_columns) || default_columns).map do |column| AnnotateModels.get_schema_info(klass, header, options)
mock_column(column[0], column[1], column[2])
end end
klass = mock_class(:users, :id, with_columns) context 'when header is "Schema Info"' do
let :header do
'Schema Info'
end
schema_info = AnnotateModels.get_schema_info(klass, 'Schema Info', options) context 'with options' do
expect(schema_info).to eql(expected) context 'when "hide_limit_column_types" is specified in options' do
let :columns do
[
mock_column(:id, :integer, limit: 8),
mock_column(:active, :boolean, limit: 1),
mock_column(:name, :string, limit: 50),
mock_column(:notes, :text, limit: 55)
]
end end
context 'when "hide_limit_column_types" is blank string' do
let :options do
{ hide_limit_column_types: '' }
end end
describe 'hide_limit_column_types option' do let :expected_result do
when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc <<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1021,9 +1029,20 @@ EOS ...@@ -1021,9 +1029,20 @@ EOS
# notes :text(55) not null # notes :text(55) not null
# #
EOS EOS
end
it 'works with option "hide_limit_column_types"' do
is_expected.to eq expected_result
end
end
when_called_with hide_limit_column_types: 'integer,boolean', returns: context 'when "hide_limit_column_types" is "integer,boolean"' do
<<-EOS.strip_heredoc let :options do
{ hide_limit_column_types: 'integer,boolean' }
end
let :expected_result do
<<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1034,10 +1053,20 @@ EOS ...@@ -1034,10 +1053,20 @@ EOS
# notes :text(55) not null # notes :text(55) not null
# #
EOS EOS
end
when_called_with hide_limit_column_types: 'integer,boolean,string,text', it 'works with option "hide_limit_column_types"' do
returns: is_expected.to eq expected_result
<<-EOS.strip_heredoc end
end
context 'when "hide_limit_column_types" is "integer,boolean,string,text"' do
let :options do
{ hide_limit_column_types: 'integer,boolean,string,text' }
end
let :expected_result do
<<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1050,17 +1079,28 @@ EOS ...@@ -1050,17 +1079,28 @@ EOS
EOS EOS
end end
describe 'hide_default_column_types option' do it 'works with option "hide_limit_column_types"' do
mocked_columns_without_id = [ is_expected.to eq expected_result
[:profile, :json, default: {}], end
[:settings, :jsonb, default: {}], end
[:parameters, :hstore, default: {}] end
context 'when "hide_default_column_types" is specified in options' do
let :columns do
[
mock_column(:profile, :json, default: {}),
mock_column(:settings, :jsonb, default: {}),
mock_column(:parameters, :hstore, default: {})
] ]
end
when_called_with hide_default_column_types: '', context 'when "hide_default_column_types" is blank string' do
with_columns: mocked_columns_without_id, let :options do
returns: { hide_default_column_types: '' }
<<-EOS.strip_heredoc end
let :expected_result do
<<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1070,11 +1110,20 @@ EOS ...@@ -1070,11 +1110,20 @@ EOS
# parameters :hstore not null # parameters :hstore not null
# #
EOS EOS
end
it 'works with option "hide_default_column_types"' do
is_expected.to eq expected_result
end
end
when_called_with hide_default_column_types: 'skip', context 'when "hide_default_column_types" is "skip"' do
with_columns: mocked_columns_without_id, let :options do
returns: { hide_default_column_types: 'skip' }
<<-EOS.strip_heredoc end
let :expected_result do
<<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1084,11 +1133,20 @@ EOS ...@@ -1084,11 +1133,20 @@ EOS
# parameters :hstore default({}), not null # parameters :hstore default({}), not null
# #
EOS EOS
end
when_called_with hide_default_column_types: 'json', it 'works with option "hide_default_column_types"' do
with_columns: mocked_columns_without_id, is_expected.to eq expected_result
returns: end
<<-EOS.strip_heredoc end
context 'when "hide_default_column_types" is "json"' do
let :options do
{ hide_default_column_types: 'json' }
end
let :expected_result do
<<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1100,16 +1158,28 @@ EOS ...@@ -1100,16 +1158,28 @@ EOS
EOS EOS
end end
describe 'classified_sort option' do it 'works with option "hide_limit_column_types"' do
mocked_columns_without_id = [ is_expected.to eq expected_result
[:active, :boolean, { limit: 1 }], end
[:name, :string, { limit: 50 }], end
[:notes, :text, { limit: 55 }] end
context 'when "classified_sort" is specified in options' do
let :columns do
[
mock_column(:active, :boolean, limit: 1),
mock_column(:name, :string, limit: 50),
mock_column(:notes, :text, limit: 55)
] ]
end
context 'when "classified_sort" is "yes"' do
let :options do
{ classified_sort: 'yes' }
end
when_called_with classified_sort: 'yes', let :expected_result do
with_columns: mocked_columns_without_id, returns: <<~EOS
<<-EOS.strip_heredoc
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1121,18 +1191,31 @@ EOS ...@@ -1121,18 +1191,31 @@ EOS
EOS EOS
end end
describe 'with_comment option' do it 'works with option "classified_sort"' do
mocked_columns_with_comment = [ is_expected.to eq expected_result
[:id, :integer, { limit: 8, comment: 'ID' }], end
[:active, :boolean, { limit: 1, comment: 'Active' }], end
[:name, :string, { limit: 50, comment: 'Name' }], end
[:notes, :text, { limit: 55, comment: 'Notes' }],
[:no_comment, :text, { limit: 20, comment: nil }] context 'when "with_comment" is specified in options' do
context 'when "with_comment" is "yes"' do
let :options do
{ with_comment: 'yes' }
end
context 'when columns have comments' do
let :columns do
[
mock_column(:id, :integer, limit: 8, comment: 'ID'),
mock_column(:active, :boolean, limit: 1, comment: 'Active'),
mock_column(:name, :string, limit: 50, comment: 'Name'),
mock_column(:notes, :text, limit: 55, comment: 'Notes'),
mock_column(:no_comment, :text, limit: 20, comment: nil)
] ]
end
when_called_with with_comment: 'yes', let :expected_result do
with_columns: mocked_columns_with_comment, returns: <<~EOS
<<-EOS.strip_heredoc
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1144,22 +1227,30 @@ EOS ...@@ -1144,22 +1227,30 @@ EOS
# no_comment :text(20) not null # no_comment :text(20) not null
# #
EOS EOS
end
it 'works with option "with_comment"' do
is_expected.to eq expected_result
end
end
mocked_columns_with_multibyte_comment = [ context 'when columns have multibyte comments' do
[:id, :integer, { limit: 8, comment: 'ID' }], let :columns do
[:active, :boolean, { limit: 1, comment: 'ACTIVE' }], [
[:name, :string, { limit: 50, comment: 'NAME' }], mock_column(:id, :integer, limit: 8, comment: 'ID'),
[:notes, :text, { limit: 55, comment: 'NOTES' }], mock_column(:active, :boolean, limit: 1, comment: 'ACTIVE'),
[:cyrillic, :text, { limit: 30, comment: 'Кириллица' }], mock_column(:name, :string, limit: 50, comment: 'NAME'),
[:japanese, :text, { limit: 60, comment: '熊本大学 イタリア 宝島' }], mock_column(:notes, :text, limit: 55, comment: 'NOTES'),
[:arabic, :text, { limit: 20, comment: 'لغة' }], mock_column(:cyrillic, :text, limit: 30, comment: 'Кириллица'),
[:no_comment, :text, { limit: 20, comment: nil }], mock_column(:japanese, :text, limit: 60, comment: '熊本大学 イタリア 宝島'),
[:location, :geometry_collection, { limit: nil, comment: nil }] mock_column(:arabic, :text, limit: 20, comment: 'لغة'),
mock_column(:no_comment, :text, limit: 20, comment: nil),
mock_column(:location, :geometry_collection, limit: nil, comment: nil)
] ]
end
when_called_with with_comment: 'yes', let :expected_result do
with_columns: mocked_columns_with_multibyte_comment, returns: <<~EOS
<<-EOS.strip_heredoc
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1175,22 +1266,29 @@ EOS ...@@ -1175,22 +1266,29 @@ EOS
# location :geometry_collect not null # location :geometry_collect not null
# #
EOS EOS
end
it 'works with option "with_comment"' do
is_expected.to eq expected_result
end
end
mocked_columns_with_geometries = [ context 'when geometry columns are included' do
[:id, :integer, { limit: 8 }], let :columns do
[:active, :boolean, { default: false, null: false }], [
[:geometry, :geometry, { mock_column(:id, :integer, limit: 8),
mock_column(:active, :boolean, default: false, null: false),
mock_column(:geometry, :geometry,
geometric_type: 'Geometry', srid: 4326, geometric_type: 'Geometry', srid: 4326,
limit: { srid: 4326, type: 'geometry' } limit: { srid: 4326, type: 'geometry' }),
}], mock_column(:location, :geography,
[:location, :geography, {
geometric_type: 'Point', srid: 0, geometric_type: 'Point', srid: 0,
limit: { srid: 0, type: 'geometry' } limit: { srid: 0, type: 'geometry' })
}]
] ]
end
when_called_with with_columns: mocked_columns_with_geometries, returns: let :expected_result do
<<-EOS.strip_heredoc <<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -1201,70 +1299,118 @@ EOS ...@@ -1201,70 +1299,118 @@ EOS
# location :geography not null, point, 0 # location :geography not null, point, 0
# #
EOS EOS
end
it 'should get schema info as RDoc' do it 'works with option "with_comment"' do
klass = mock_class(:users, is_expected.to eq expected_result
:id, end
end
end
end
end
end
context 'when header is "== Schema Information"' do
let :header do
AnnotateModels::PREFIX
end
context 'when "format_doc" and "with_comment" are specified in options' do
subject do
AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)
end
context 'when columns are normal' do
let :columns do
[ [
mock_column(:id, :integer, comment: 'ID'), mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name') mock_column(:name, :string, limit: 50, comment: 'Name')
]) ]
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)).to eql(<<-EOS.strip_heredoc) end
# #{AnnotateModels::PREFIX}
let :expected_result do
<<~EOS
# == Schema Information
# #
# Table name: users # Table name: users
# #
# *id(ID)*:: <tt>integer, not null, primary key</tt> # *id(ID)*:: <tt>integer, not null, primary key</tt>
# *name(Name)*:: <tt>string(50), not null</tt> # *name(Name)*:: <tt>string(50), not null</tt>
#-- #--
# #{AnnotateModels::END_MARK} # == Schema Information End
#++ #++
EOS EOS
end end
it 'should get schema info as Markdown with multibyte comment' do it 'returns schema info in RDoc format' do
klass = mock_class(:users, is_expected.to eq expected_result
:id, end
end
end
context 'when "format_markdown" and "with_comment" are specified in options' do
subject do
AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)
end
context 'when columns have comments' do
let :columns do
[ [
mock_column(:id, :integer, comment: 'ID'), mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'NAME') mock_column(:name, :string, limit: 50, comment: 'Name')
]) ]
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc) end
# #{AnnotateModels::PREFIX}
let :expected_result do
<<~EOS
# == Schema Information
# #
# Table name: `users` # Table name: `users`
# #
# ### Columns # ### Columns
# #
# Name | Type | Attributes # Name | Type | Attributes
# --------------------- | ------------------ | --------------------------- # ----------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key` # **`id(ID)`** | `integer` | `not null, primary key`
# **`name(NAME)`** | `string(50)` | `not null` # **`name(Name)`** | `string(50)` | `not null`
# #
EOS EOS
end end
it 'should get schema info as Markdown' do it 'returns schema info in Markdown format' do
klass = mock_class(:users, is_expected.to eq expected_result
:id, end
end
context 'when columns have multibyte comments' do
let :columns do
[ [
mock_column(:id, :integer, comment: 'ID'), mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name') mock_column(:name, :string, limit: 50, comment: 'NAME')
]) ]
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc) end
# #{AnnotateModels::PREFIX}
let :expected_result do
<<~EOS
# == Schema Information
# #
# Table name: `users` # Table name: `users`
# #
# ### Columns # ### Columns
# #
# Name | Type | Attributes # Name | Type | Attributes
# ----------------- | ------------------ | --------------------------- # --------------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key` # **`id(ID)`** | `integer` | `not null, primary key`
# **`name(Name)`** | `string(50)` | `not null` # **`name(NAME)`** | `string(50)` | `not null`
# #
EOS EOS
end end
it 'returns schema info in Markdown format' do
is_expected.to eq expected_result
end
end
end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment