From 9ad8485c88eaf7fc9d8185bbc3b743c1d6093098 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 20 Jan 2020 22:16:39 +0900 Subject: [PATCH 1/4] Structuralize RSpec test cases of AnnotateModels.get_schema_info (with custom options) --- spec/lib/annotate/annotate_models_spec.rb | 587 ++++++++++++---------- 1 file changed, 323 insertions(+), 264 deletions(-) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 34a49a210..655fdc117 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -954,283 +954,342 @@ def mock_column(name, type, options = {}) end end - describe '#get_schema_info with custom options' do - def self.when_called_with(options = {}) - expected = options.delete(:returns) - default_columns = [ - [:id, :integer, { limit: 8 }], - [:active, :boolean, { limit: 1 }], - [:name, :string, { limit: 50 }], - [:notes, :text, { limit: 55 }] - ] - - it "should work with options = #{options}" do - with_columns = (options.delete(:with_columns) || default_columns).map do |column| - mock_column(column[0], column[1], column[2]) - end - - klass = mock_class(:users, :id, with_columns) - - schema_info = AnnotateModels.get_schema_info(klass, 'Schema Info', options) - expect(schema_info).to eql(expected) - end - end - - describe 'hide_limit_column_types option' do - when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc - # 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 + describe '.get_schema_info (with custom options)' do + context 'when header is "Schema Info"' do + def self.when_called_with(options = {}) + expected = options.delete(:returns) + default_columns = [ + [:id, :integer, { limit: 8 }], + [:active, :boolean, { limit: 1 }], + [:name, :string, { limit: 50 }], + [:notes, :text, { limit: 55 }] + ] + + it "should work with options = #{options}" do + with_columns = (options.delete(:with_columns) || default_columns).map do |column| + mock_column(column[0], column[1], column[2]) + end - when_called_with hide_limit_column_types: 'integer,boolean', returns: - <<-EOS.strip_heredoc - # 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 + klass = mock_class(:users, :id, with_columns) - when_called_with hide_limit_column_types: 'integer,boolean,string,text', - returns: - <<-EOS.strip_heredoc - # 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 + schema_info = AnnotateModels.get_schema_info(klass, 'Schema Info', options) + expect(schema_info).to eql(expected) + end + end - describe 'hide_default_column_types option' do - mocked_columns_without_id = [ - [:profile, :json, default: {}], - [:settings, :jsonb, default: {}], - [:parameters, :hstore, default: {}] - ] - - when_called_with hide_default_column_types: '', - with_columns: mocked_columns_without_id, - returns: - <<-EOS.strip_heredoc - # Schema Info - # - # Table name: users - # - # profile :json not null - # settings :jsonb not null - # parameters :hstore not null - # - EOS + context 'with options' do + context 'when "hide_limit_column_types" is specified in options' do + context 'when "hide_limit_column_types" is blank string' do + expected_result = <<~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 - when_called_with hide_default_column_types: 'skip', - with_columns: mocked_columns_without_id, - returns: - <<-EOS.strip_heredoc - # 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_limit_column_types: '', returns: expected_result + end - when_called_with hide_default_column_types: 'json', - with_columns: mocked_columns_without_id, - returns: - <<-EOS.strip_heredoc - # Schema Info - # - # Table name: users - # - # profile :json not null - # settings :jsonb default({}), not null - # parameters :hstore default({}), not null - # - EOS - end + context 'when "hide_limit_column_types" is "integer,boolean"' do + expected_result = <<~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 - describe 'classified_sort option' do - mocked_columns_without_id = [ - [:active, :boolean, { limit: 1 }], - [:name, :string, { limit: 50 }], - [:notes, :text, { limit: 55 }] - ] - - when_called_with classified_sort: 'yes', - with_columns: mocked_columns_without_id, returns: - <<-EOS.strip_heredoc - # Schema Info - # - # Table name: users - # - # active :boolean not null - # name :string(50) not null - # notes :text(55) not null - # - EOS - end + when_called_with hide_limit_column_types: 'integer,boolean', returns: expected_result + end - describe 'with_comment option' do - mocked_columns_with_comment = [ - [:id, :integer, { limit: 8, comment: 'ID' }], - [:active, :boolean, { limit: 1, comment: 'Active' }], - [: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', - with_columns: mocked_columns_with_comment, returns: - <<-EOS.strip_heredoc - # 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 - # no_comment :text(20) not null - # - EOS + context 'when "hide_limit_column_types" is "integer,boolean,string,text"' do + expected_result = <<~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 - mocked_columns_with_multibyte_comment = [ - [:id, :integer, { limit: 8, comment: 'ID' }], - [:active, :boolean, { limit: 1, comment: 'ACTIVE' }], - [:name, :string, { limit: 50, comment: 'NAME' }], - [:notes, :text, { limit: 55, comment: 'NOTES' }], - [: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', - with_columns: mocked_columns_with_multibyte_comment, returns: - <<-EOS.strip_heredoc - # 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 + when_called_with hide_limit_column_types: 'integer,boolean,string,text', + returns: expected_result + end + end - mocked_columns_with_geometries = [ - [:id, :integer, { limit: 8 }], - [:active, :boolean, { default: false, null: false }], - [:geometry, :geometry, { - geometric_type: 'Geometry', srid: 4326, - limit: { srid: 4326, type: 'geometry' } - }], - [:location, :geography, { - geometric_type: 'Point', srid: 0, - limit: { srid: 0, type: 'geometry' } - }] - ] - - when_called_with with_columns: mocked_columns_with_geometries, returns: - <<-EOS.strip_heredoc - # 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 + context 'when "hide_default_column_types" is specified in options' do + mocked_columns_without_id = [ + [:profile, :json, default: {}], + [:settings, :jsonb, default: {}], + [:parameters, :hstore, default: {}] + ] - it 'should get schema info as RDoc' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer, comment: 'ID'), - 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) - # #{AnnotateModels::PREFIX} - # - # Table name: users - # - # *id(ID)*:: integer, not null, primary key - # *name(Name)*:: string(50), not null - #-- - # #{AnnotateModels::END_MARK} - #++ - EOS - end + context 'when "hide_default_column_types" is blank string' do + expected_result = <<~EOS + # Schema Info + # + # Table name: users + # + # profile :json not null + # settings :jsonb not null + # parameters :hstore not null + # + EOS - it 'should get schema info as Markdown with multibyte comment' do - klass = mock_class(:users, - :id, - [ - 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 + when_called_with hide_default_column_types: '', + with_columns: mocked_columns_without_id, + returns: expected_result + end + + context 'when "hide_default_column_types" is "skip"' do + expected_result = <<~EOS + # 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: 'skip', + with_columns: mocked_columns_without_id, + returns: expected_result + end + + context 'when "hide_default_column_types" is "json"' do + expected_result = <<~EOS + # Schema Info + # + # Table name: users + # + # profile :json not null + # settings :jsonb default({}), not null + # parameters :hstore default({}), not null + # + EOS + + when_called_with hide_default_column_types: 'json', + with_columns: mocked_columns_without_id, + returns: expected_result + end + end + + context 'when "classified_sort" is specified in options' do + context 'when "classified_sort" is "yes"' do + mocked_columns_without_id = [ + [:active, :boolean, { limit: 1 }], + [:name, :string, { limit: 50 }], + [:notes, :text, { limit: 55 }] + ] + + expected_result = <<~EOS + # Schema Info + # + # Table name: users + # + # active :boolean not null + # name :string(50) not null + # notes :text(55) not null + # + EOS + + when_called_with classified_sort: 'yes', + with_columns: mocked_columns_without_id, + returns: expected_result + end + end + + context 'when "with_comment" is specified in options' do + context 'when "with_comment" is "yes"' do + context 'when columns have comments' do + mocked_columns_with_comment = [ + [:id, :integer, { limit: 8, comment: 'ID' }], + [:active, :boolean, { limit: 1, comment: 'Active' }], + [:name, :string, { limit: 50, comment: 'Name' }], + [:notes, :text, { limit: 55, comment: 'Notes' }], + [:no_comment, :text, { limit: 20, comment: nil }] + ] + + expected_result = <<~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 + # no_comment :text(20) not null + # + EOS + + when_called_with with_comment: 'yes', + with_columns: mocked_columns_with_comment, + returns: expected_result + end + + context 'when columns have multibyte comments' do + mocked_columns_with_multibyte_comment = [ + [:id, :integer, { limit: 8, comment: 'ID' }], + [:active, :boolean, { limit: 1, comment: 'ACTIVE' }], + [:name, :string, { limit: 50, comment: 'NAME' }], + [:notes, :text, { limit: 55, comment: 'NOTES' }], + [: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 }] + ] + + expected_result = <<~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 + + when_called_with with_comment: 'yes', + with_columns: mocked_columns_with_multibyte_comment, + returns: expected_result + end + + context 'when geometry columns are included' do + mocked_columns_with_geometries = [ + [:id, :integer, { limit: 8 }], + [:active, :boolean, { default: false, null: false }], + [:geometry, :geometry, { + geometric_type: 'Geometry', srid: 4326, + limit: { srid: 4326, type: 'geometry' } + }], + [:location, :geography, { + geometric_type: 'Point', srid: 0, + limit: { srid: 0, type: 'geometry' } + }] + ] + + expected_result = <<~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 + + when_called_with with_columns: mocked_columns_with_geometries, returns: expected_result + end + end + end end + end - it 'should get schema info as Markdown' do - klass = mock_class(:users, - :id, - [ - 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 + context 'when header is "== Schema Information"' do + context 'when "format_doc" and "with_comment" are specified in options' do + it 'should get schema info as RDoc' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer, comment: 'ID'), + mock_column(:name, :string, limit: 50, comment: 'Name') + ]) + + expected_result = <<~EOS + # #{AnnotateModels::PREFIX} + # + # Table name: users + # + # *id(ID)*:: integer, not null, primary key + # *name(Name)*:: string(50), not null + #-- + # #{AnnotateModels::END_MARK} + #++ + EOS + + expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)).to eql(expected_result) + end + end + + context 'when "format_markdown" and "with_comment" are specified in options' do + context 'when columns have multibyte comments' do + it 'should get schema info as Markdown with multibyte comment' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer, comment: 'ID'), + mock_column(:name, :string, limit: 50, comment: 'NAME') + ]) + + expected_result = <<~EOS + # #{AnnotateModels::PREFIX} + # + # Table name: `users` + # + # ### Columns + # + # Name | Type | Attributes + # --------------------- | ------------------ | --------------------------- + # **`id(ID)`** | `integer` | `not null, primary key` + # **`name(NAME)`** | `string(50)` | `not null` + # + EOS + + expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(expected_result) + end + end + + context 'when columns have comments' do + it 'should get schema info as Markdown' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer, comment: 'ID'), + mock_column(:name, :string, limit: 50, comment: 'Name') + ]) + + expected_result = <<~EOS + # #{AnnotateModels::PREFIX} + # + # Table name: `users` + # + # ### Columns + # + # Name | Type | Attributes + # ----------------- | ------------------ | --------------------------- + # **`id(ID)`** | `integer` | `not null, primary key` + # **`name(Name)`** | `string(50)` | `not null` + # + EOS + + expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(expected_result) + end + end end end end From 784ddeaf9acb8cbb13dd17da1152278668635edf Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 20 Jan 2020 22:19:42 +0900 Subject: [PATCH 2/4] Replace expression expansion to plain text --- spec/lib/annotate/annotate_models_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 655fdc117..02afe896f 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -1221,14 +1221,14 @@ def self.when_called_with(options = {}) ]) expected_result = <<~EOS - # #{AnnotateModels::PREFIX} + # == Schema Information # # Table name: users # # *id(ID)*:: integer, not null, primary key # *name(Name)*:: string(50), not null #-- - # #{AnnotateModels::END_MARK} + # == Schema Information End #++ EOS @@ -1247,7 +1247,7 @@ def self.when_called_with(options = {}) ]) expected_result = <<~EOS - # #{AnnotateModels::PREFIX} + # == Schema Information # # Table name: `users` # @@ -1274,7 +1274,7 @@ def self.when_called_with(options = {}) ]) expected_result = <<~EOS - # #{AnnotateModels::PREFIX} + # == Schema Information # # Table name: `users` # From 5ab62f9a1797a523a07353baa3ab7a0f7fcc2e99 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 20 Jan 2020 22:21:14 +0900 Subject: [PATCH 3/4] Refactor RSpec test cases of AnnotateModels.get_schema_info (with custom options) --- spec/lib/annotate/annotate_models_spec.rb | 557 +++++++++++++--------- 1 file changed, 322 insertions(+), 235 deletions(-) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 02afe896f..637a68a30 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -955,255 +955,322 @@ def mock_column(name, type, options = {}) end describe '.get_schema_info (with custom options)' do - context 'when header is "Schema Info"' do - def self.when_called_with(options = {}) - expected = options.delete(:returns) - default_columns = [ - [:id, :integer, { limit: 8 }], - [:active, :boolean, { limit: 1 }], - [:name, :string, { limit: 50 }], - [:notes, :text, { limit: 55 }] - ] - - it "should work with options = #{options}" do - with_columns = (options.delete(:with_columns) || default_columns).map do |column| - mock_column(column[0], column[1], column[2]) - end + let :klass do + mock_class(:users, :id, columns) + end - klass = mock_class(:users, :id, with_columns) + subject do + AnnotateModels.get_schema_info(klass, header, options) + end - schema_info = AnnotateModels.get_schema_info(klass, 'Schema Info', options) - expect(schema_info).to eql(expected) - 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 - expected_result = <<~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 + let :options do + { hide_limit_column_types: '' } + end - when_called_with hide_limit_column_types: '', returns: expected_result + 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 - expected_result = <<~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 + let :options do + { hide_limit_column_types: 'integer,boolean' } + end - when_called_with hide_limit_column_types: 'integer,boolean', returns: expected_result + 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,string,text"' do - expected_result = <<~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 + let :options do + { hide_limit_column_types: 'integer,boolean,string,text' } + end - when_called_with hide_limit_column_types: 'integer,boolean,string,text', - returns: expected_result + 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 context 'when "hide_default_column_types" is specified in options' do - mocked_columns_without_id = [ - [:profile, :json, default: {}], - [:settings, :jsonb, default: {}], - [:parameters, :hstore, default: {}] - ] + let :columns do + [ + mock_column(:profile, :json, default: {}), + mock_column(:settings, :jsonb, default: {}), + mock_column(:parameters, :hstore, default: {}) + ] + end context 'when "hide_default_column_types" is blank string' do - expected_result = <<~EOS - # Schema Info - # - # Table name: users - # - # profile :json not null - # settings :jsonb not null - # parameters :hstore not null - # - EOS + let :options do + { hide_default_column_types: '' } + end + + let :expected_result do + <<~EOS + # Schema Info + # + # Table name: users + # + # profile :json not null + # settings :jsonb not null + # parameters :hstore not null + # + EOS + end - when_called_with hide_default_column_types: '', - with_columns: mocked_columns_without_id, - returns: expected_result + it 'works with option "hide_default_column_types"' do + is_expected.to eq expected_result + end end context 'when "hide_default_column_types" is "skip"' do - expected_result = <<~EOS - # Schema Info - # - # Table name: users - # - # profile :json default({}), not null - # settings :jsonb default({}), not null - # parameters :hstore default({}), not null - # - EOS + let :options do + { hide_default_column_types: 'skip' } + end - when_called_with hide_default_column_types: 'skip', - with_columns: mocked_columns_without_id, - returns: expected_result + let :expected_result do + <<~EOS + # Schema Info + # + # Table name: users + # + # profile :json default({}), not null + # settings :jsonb default({}), not null + # parameters :hstore default({}), not null + # + EOS + end + + it 'works with option "hide_default_column_types"' do + is_expected.to eq expected_result + end end context 'when "hide_default_column_types" is "json"' do - expected_result = <<~EOS - # Schema Info - # - # Table name: users - # - # profile :json not null - # settings :jsonb default({}), not null - # parameters :hstore default({}), not null - # - EOS + let :options do + { hide_default_column_types: 'json' } + end + + let :expected_result do + <<~EOS + # Schema Info + # + # Table name: users + # + # profile :json not null + # settings :jsonb default({}), not null + # parameters :hstore default({}), not null + # + EOS + end - when_called_with hide_default_column_types: 'json', - with_columns: mocked_columns_without_id, - returns: expected_result + it 'works with option "hide_limit_column_types"' do + is_expected.to eq expected_result + end end end context 'when "classified_sort" is specified in options' do - context 'when "classified_sort" is "yes"' do - mocked_columns_without_id = [ - [:active, :boolean, { limit: 1 }], - [:name, :string, { limit: 50 }], - [:notes, :text, { limit: 55 }] + let :columns do + [ + mock_column(:active, :boolean, limit: 1), + mock_column(:name, :string, limit: 50), + mock_column(:notes, :text, limit: 55) ] - - expected_result = <<~EOS - # Schema Info - # - # Table name: users - # - # active :boolean not null - # name :string(50) not null - # notes :text(55) not null - # - EOS - - when_called_with classified_sort: 'yes', - with_columns: mocked_columns_without_id, - returns: expected_result end - end - context 'when "with_comment" is specified in options' do - context 'when "with_comment" is "yes"' do - context 'when columns have comments' do - mocked_columns_with_comment = [ - [:id, :integer, { limit: 8, comment: 'ID' }], - [:active, :boolean, { limit: 1, comment: 'Active' }], - [:name, :string, { limit: 50, comment: 'Name' }], - [:notes, :text, { limit: 55, comment: 'Notes' }], - [:no_comment, :text, { limit: 20, comment: nil }] - ] - - expected_result = <<~EOS + context 'when "classified_sort" is "yes"' do + let :options do + { classified_sort: 'yes' } + 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 - # no_comment :text(20) not null + # active :boolean not null + # name :string(50) not null + # notes :text(55) not null # EOS + end + + it 'works with option "classified_sort"' do + is_expected.to eq expected_result + end + end + end - when_called_with with_comment: 'yes', - with_columns: mocked_columns_with_comment, - returns: expected_result + 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 + + 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 + # no_comment :text(20) not null + # + EOS + end + + it 'works with option "with_comment"' do + is_expected.to eq expected_result + end end context 'when columns have multibyte comments' do - mocked_columns_with_multibyte_comment = [ - [:id, :integer, { limit: 8, comment: 'ID' }], - [:active, :boolean, { limit: 1, comment: 'ACTIVE' }], - [:name, :string, { limit: 50, comment: 'NAME' }], - [:notes, :text, { limit: 55, comment: 'NOTES' }], - [: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 }] - ] - - expected_result = <<~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 + 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(:cyrillic, :text, limit: 30, comment: 'Кириллица'), + mock_column(:japanese, :text, limit: 60, comment: '熊本大学 イタリア 宝島'), + 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 + + 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 - when_called_with with_comment: 'yes', - with_columns: mocked_columns_with_multibyte_comment, - returns: expected_result + it 'works with option "with_comment"' do + is_expected.to eq expected_result + end end context 'when geometry columns are included' do - mocked_columns_with_geometries = [ - [:id, :integer, { limit: 8 }], - [:active, :boolean, { default: false, null: false }], - [:geometry, :geometry, { - geometric_type: 'Geometry', srid: 4326, - limit: { srid: 4326, type: 'geometry' } - }], - [:location, :geography, { - geometric_type: 'Point', srid: 0, - limit: { srid: 0, type: 'geometry' } - }] - ] - - expected_result = <<~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 + 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 - when_called_with with_columns: mocked_columns_with_geometries, returns: expected_result + 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 @@ -1211,42 +1278,58 @@ def self.when_called_with(options = {}) 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 - it 'should get schema info as RDoc' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer, comment: 'ID'), - mock_column(:name, :string, limit: 50, comment: 'Name') - ]) + subject do + AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true) + end - expected_result = <<~EOS - # == Schema Information - # - # Table name: users - # - # *id(ID)*:: integer, not null, primary key - # *name(Name)*:: string(50), not null - #-- - # == Schema Information End - #++ - EOS + context 'when columns are normal' 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 + # + # *id(ID)*:: integer, not null, primary key + # *name(Name)*:: string(50), not null + #-- + # == Schema Information End + #++ + EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true, with_comment: true)).to eql(expected_result) + 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 multibyte comments' do - it 'should get schema info as Markdown with multibyte comment' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer, comment: 'ID'), - mock_column(:name, :string, limit: 50, comment: 'NAME') - ]) - - expected_result = <<~EOS + 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` @@ -1259,21 +1342,23 @@ def self.when_called_with(options = {}) # **`name(NAME)`** | `string(50)` | `not null` # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(expected_result) + it 'returns schema info in Markdown format' do + is_expected.to eq expected_result end end context 'when columns have comments' do - it 'should get schema info as Markdown' do - klass = mock_class(:users, - :id, - [ - mock_column(:id, :integer, comment: 'ID'), - mock_column(:name, :string, limit: 50, comment: 'Name') - ]) - - expected_result = <<~EOS + 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` @@ -1286,8 +1371,10 @@ def self.when_called_with(options = {}) # **`name(Name)`** | `string(50)` | `not null` # EOS + end - expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(expected_result) + it 'returns schema info in Markdown format' do + is_expected.to eq expected_result end end end From 5d0238911e63a3c7bf8f118e0f83ca03dff7272e Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 20 Jan 2020 22:32:34 +0900 Subject: [PATCH 4/4] Change position of test cases --- spec/lib/annotate/annotate_models_spec.rb | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 637a68a30..6e71cb2bf 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -1320,11 +1320,11 @@ def mock_column(name, type, options = {}) AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true) end - context 'when columns have multibyte comments' do + context 'when columns have comments' do let :columns do [ - mock_column(:id, :integer, comment: 'ID'), - mock_column(:name, :string, limit: 50, comment: 'NAME') + mock_column(:id, :integer, comment: 'ID'), + mock_column(:name, :string, limit: 50, comment: 'Name') ] end @@ -1336,10 +1336,10 @@ def mock_column(name, type, options = {}) # # ### Columns # - # Name | Type | Attributes - # --------------------- | ------------------ | --------------------------- - # **`id(ID)`** | `integer` | `not null, primary key` - # **`name(NAME)`** | `string(50)` | `not null` + # Name | Type | Attributes + # ----------------- | ------------------ | --------------------------- + # **`id(ID)`** | `integer` | `not null, primary key` + # **`name(Name)`** | `string(50)` | `not null` # EOS end @@ -1349,11 +1349,11 @@ def mock_column(name, type, options = {}) end end - context 'when columns have comments' do + context 'when columns have multibyte comments' do let :columns do [ - mock_column(:id, :integer, comment: 'ID'), - mock_column(:name, :string, limit: 50, comment: 'Name') + mock_column(:id, :integer, comment: 'ID'), + mock_column(:name, :string, limit: 50, comment: 'NAME') ] end @@ -1365,10 +1365,10 @@ def mock_column(name, type, options = {}) # # ### Columns # - # Name | Type | Attributes - # ----------------- | ------------------ | --------------------------- - # **`id(ID)`** | `integer` | `not null, primary key` - # **`name(Name)`** | `string(50)` | `not null` + # Name | Type | Attributes + # --------------------- | ------------------ | --------------------------- + # **`id(ID)`** | `integer` | `not null, primary key` + # **`name(NAME)`** | `string(50)` | `not null` # EOS end