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,283 +987,429 @@ EOS ...@@ -987,283 +987,429 @@ 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 }], subject do
[:name, :string, { limit: 50 }], AnnotateModels.get_schema_info(klass, header, options)
[:notes, :text, { limit: 55 }] end
]
context 'when header is "Schema Info"' do
let :header do
'Schema Info'
end
context 'with options' do
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
context 'when "hide_limit_column_types" is blank string' do
let :options do
{ hide_limit_column_types: '' }
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
it 'works with option "hide_limit_column_types"' do
is_expected.to eq expected_result
end
end
context 'when "hide_limit_column_types" is "integer,boolean"' do
let :options do
{ hide_limit_column_types: 'integer,boolean' }
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
it 'works with option "hide_limit_column_types"' do
is_expected.to eq expected_result
end
end
it "should work with options = #{options}" do context 'when "hide_limit_column_types" is "integer,boolean,string,text"' do
with_columns = (options.delete(:with_columns) || default_columns).map do |column| let :options do
mock_column(column[0], column[1], column[2]) { hide_limit_column_types: 'integer,boolean,string,text' }
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string not null
# notes :text not null
#
EOS
end
it 'works with option "hide_limit_column_types"' do
is_expected.to eq expected_result
end
end
end end
klass = mock_class(:users, :id, with_columns) 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
schema_info = AnnotateModels.get_schema_info(klass, 'Schema Info', options) context 'when "hide_default_column_types" is blank string' do
expect(schema_info).to eql(expected) let :options do
end { hide_default_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
# #
# id :integer not null, primary key # profile :json not null
# active :boolean not null # settings :jsonb not null
# name :string(50) not null # parameters :hstore not null
# notes :text(55) not null #
# EOS
EOS end
when_called_with hide_limit_column_types: 'integer,boolean', returns: it 'works with option "hide_default_column_types"' do
<<-EOS.strip_heredoc is_expected.to eq expected_result
# Schema Info end
# end
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS
when_called_with hide_limit_column_types: 'integer,boolean,string,text', context 'when "hide_default_column_types" is "skip"' do
returns: let :options do
<<-EOS.strip_heredoc { hide_default_column_types: 'skip' }
# Schema Info end
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string not null
# notes :text not null
#
EOS
end
describe 'hide_default_column_types option' do let :expected_result do
mocked_columns_without_id = [ <<~EOS
[:profile, :json, default: {}], # Schema Info
[:settings, :jsonb, default: {}], #
[:parameters, :hstore, default: {}] # Table name: users
] #
# profile :json default({}), not null
# settings :jsonb default({}), not null
# parameters :hstore default({}), not null
#
EOS
end
when_called_with hide_default_column_types: '', 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
# Schema Info
#
# Table name: users
#
# profile :json not null
# settings :jsonb not null
# parameters :hstore not null
#
EOS
when_called_with hide_default_column_types: 'skip', context 'when "hide_default_column_types" is "json"' do
with_columns: mocked_columns_without_id, let :options do
returns: { hide_default_column_types: 'json' }
<<-EOS.strip_heredoc end
# Schema Info
#
# Table name: users
#
# profile :json default({}), not null
# settings :jsonb default({}), not null
# parameters :hstore default({}), not null
#
EOS
when_called_with hide_default_column_types: 'json', let :expected_result do
with_columns: mocked_columns_without_id, <<~EOS
returns: # Schema Info
<<-EOS.strip_heredoc #
# Schema Info # Table name: users
# #
# Table name: users # profile :json not null
# # settings :jsonb default({}), not null
# profile :json not null # parameters :hstore default({}), not null
# settings :jsonb default({}), not null #
# parameters :hstore default({}), not null EOS
# end
EOS
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
]
when_called_with classified_sort: 'yes', context 'when "classified_sort" is specified in options' do
with_columns: mocked_columns_without_id, returns: let :columns do
<<-EOS.strip_heredoc [
# Schema Info mock_column(:active, :boolean, limit: 1),
# mock_column(:name, :string, limit: 50),
# Table name: users mock_column(:notes, :text, limit: 55)
# ]
# active :boolean not null end
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
describe 'with_comment option' do context 'when "classified_sort" is "yes"' do
mocked_columns_with_comment = [ let :options do
[:id, :integer, { limit: 8, comment: 'ID' }], { classified_sort: 'yes' }
[:active, :boolean, { limit: 1, comment: 'Active' }], end
[:name, :string, { limit: 50, comment: 'Name' }],
[:notes, :text, { limit: 55, comment: 'Notes' }],
[:no_comment, :text, { limit: 20, comment: nil }]
]
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 #
# # active :boolean not null
# id(ID) :integer not null, primary key # name :string(50) not null
# active(Active) :boolean not null # notes :text(55) not null
# name(Name) :string(50) not null #
# notes(Notes) :text(55) not null EOS
# no_comment :text(20) not null end
#
EOS
mocked_columns_with_multibyte_comment = [ it 'works with option "classified_sort"' do
[:id, :integer, { limit: 8, comment: 'ID' }], is_expected.to eq expected_result
[:active, :boolean, { limit: 1, comment: 'ACTIVE' }], end
[:name, :string, { limit: 50, comment: 'NAME' }], end
[:notes, :text, { limit: 55, comment: 'NOTES' }], end
[:cyrillic, :text, { limit: 30, comment: 'Кириллица' }],
[:japanese, :text, { limit: 60, comment: '熊本大学 イタリア 宝島' }],
[:arabic, :text, { limit: 20, comment: 'لغة' }],
[:no_comment, :text, { limit: 20, comment: nil }],
[:location, :geometry_collection, { limit: nil, comment: nil }]
]
when_called_with with_comment: 'yes', context 'when "with_comment" is specified in options' do
with_columns: mocked_columns_with_multibyte_comment, returns: context 'when "with_comment" is "yes"' do
<<-EOS.strip_heredoc let :options do
# Schema Info { with_comment: 'yes' }
# end
# Table name: users
#
# id(ID) :integer not null, primary key
# active(ACTIVE) :boolean not null
# name(NAME) :string(50) not null
# notes(NOTES) :text(55) not null
# cyrillic(Кириллица) :text(30) not null
# japanese(熊本大学 イタリア 宝島) :text(60) not null
# arabic(لغة) :text(20) not null
# no_comment :text(20) not null
# location :geometry_collect not null
#
EOS
mocked_columns_with_geometries = [ context 'when columns have comments' do
[:id, :integer, { limit: 8 }], let :columns do
[:active, :boolean, { default: false, null: false }], [
[:geometry, :geometry, { mock_column(:id, :integer, limit: 8, comment: 'ID'),
geometric_type: 'Geometry', srid: 4326, mock_column(:active, :boolean, limit: 1, comment: 'Active'),
limit: { srid: 4326, type: 'geometry' } mock_column(:name, :string, limit: 50, comment: 'Name'),
}], mock_column(:notes, :text, limit: 55, comment: 'Notes'),
[:location, :geography, { mock_column(:no_comment, :text, limit: 20, comment: nil)
geometric_type: 'Point', srid: 0, ]
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
# #
# id :integer not null, primary key # id(ID) :integer not null, primary key
# active :boolean default(FALSE), not null # active(Active) :boolean not null
# geometry :geometry not null, geometry, 4326 # name(Name) :string(50) not null
# location :geography not null, point, 0 # notes(Notes) :text(55) not null
# # no_comment :text(20) not null
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
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name') context 'when columns have multibyte comments' do
]) let :columns do
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)).to eql(<<-EOS.strip_heredoc) [
# #{AnnotateModels::PREFIX} mock_column(:id, :integer, limit: 8, comment: 'ID'),
# mock_column(:active, :boolean, limit: 1, comment: 'ACTIVE'),
# Table name: users mock_column(:name, :string, limit: 50, comment: 'NAME'),
# mock_column(:notes, :text, limit: 55, comment: 'NOTES'),
# *id(ID)*:: <tt>integer, not null, primary key</tt> mock_column(:cyrillic, :text, limit: 30, comment: 'Кириллица'),
# *name(Name)*:: <tt>string(50), not null</tt> mock_column(:japanese, :text, limit: 60, comment: '熊本大学 イタリア 宝島'),
#-- mock_column(:arabic, :text, limit: 20, comment: 'لغة'),
# #{AnnotateModels::END_MARK} mock_column(:no_comment, :text, limit: 20, comment: nil),
#++ mock_column(:location, :geometry_collection, limit: nil, comment: nil)
EOS ]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id(ID) :integer not null, primary key
# active(ACTIVE) :boolean not null
# name(NAME) :string(50) not null
# notes(NOTES) :text(55) not null
# cyrillic(Кириллица) :text(30) not null
# japanese(熊本大学 イタリア 宝島) :text(60) not null
# arabic(لغة) :text(20) not null
# no_comment :text(20) not null
# location :geometry_collect not null
#
EOS
end
it 'works with option "with_comment"' do
is_expected.to eq expected_result
end
end
context 'when geometry columns are included' do
let :columns do
[
mock_column(:id, :integer, limit: 8),
mock_column(:active, :boolean, default: false, null: false),
mock_column(:geometry, :geometry,
geometric_type: 'Geometry', srid: 4326,
limit: { srid: 4326, type: 'geometry' }),
mock_column(:location, :geography,
geometric_type: 'Point', srid: 0,
limit: { srid: 0, type: 'geometry' })
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean default(FALSE), not null
# geometry :geometry not null, geometry, 4326
# location :geography not null, point, 0
#
EOS
end
it 'works with option "with_comment"' do
is_expected.to eq expected_result
end
end
end
end
end end
end
it 'should get schema info as Markdown with multibyte comment' do context 'when header is "== Schema Information"' do
klass = mock_class(:users, let :header do
:id, AnnotateModels::PREFIX
[
mock_column(:id, :integer, comment: 'ID'),
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)
# #{AnnotateModels::PREFIX}
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# --------------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key`
# **`name(NAME)`** | `string(50)` | `not null`
#
EOS
end end
it 'should get schema info as Markdown' do context 'when "format_doc" and "with_comment" are specified in options' do
klass = mock_class(:users, subject do
:id, AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)
[ end
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name') context 'when columns are normal' do
]) let :columns do
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc) [
# #{AnnotateModels::PREFIX} mock_column(:id, :integer, comment: 'ID'),
# mock_column(:name, :string, limit: 50, comment: 'Name')
# Table name: `users` ]
# end
# ### Columns
# let :expected_result do
# Name | Type | Attributes <<~EOS
# ----------------- | ------------------ | --------------------------- # == Schema Information
# **`id(ID)`** | `integer` | `not null, primary key` #
# **`name(Name)`** | `string(50)` | `not null` # Table name: users
# #
EOS # *id(ID)*:: <tt>integer, not null, primary key</tt>
# *name(Name)*:: <tt>string(50), not null</tt>
#--
# == Schema Information End
#++
EOS
end
it 'returns schema info in RDoc format' do
is_expected.to eq expected_result
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(:name, :string, limit: 50, comment: 'Name')
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key`
# **`name(Name)`** | `string(50)` | `not null`
#
EOS
end
it 'returns schema info in Markdown format' do
is_expected.to eq expected_result
end
end
context 'when columns have multibyte comments' do
let :columns do
[
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'NAME')
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# --------------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key`
# **`name(NAME)`** | `string(50)` | `not null`
#
EOS
end
it 'returns schema info in Markdown format' do
is_expected.to eq expected_result
end
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