Unverified Commit 3f3c8867 by Shu Fujita Committed by GitHub

Refactor RSpec for AnnotateModels - structuralize test cases (#755)

The test cases of AnnotateModels.get_schema_info was separated into two sections. So I merged them into one, and refactor test cases. This PR is new version of #741.
parent 5176bb88
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config` # `rubocop --auto-gen-config`
# on 2020-02-12 18:43:06 +0900 using RuboCop version 0.68.1. # on 2020-02-13 18:10:52 +0900 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
...@@ -528,7 +528,7 @@ Style/UnneededPercentQ: ...@@ -528,7 +528,7 @@ Style/UnneededPercentQ:
Exclude: Exclude:
- 'annotate.gemspec' - 'annotate.gemspec'
# Offense count: 344 # Offense count: 346
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https # URISchemes: http, https
......
...@@ -177,7 +177,7 @@ describe AnnotateModels do ...@@ -177,7 +177,7 @@ describe AnnotateModels do
describe '.get_schema_info' do describe '.get_schema_info' do
subject do subject do
AnnotateModels.get_schema_info(klass, header) AnnotateModels.get_schema_info(klass, header, **options)
end end
let :klass do let :klass do
...@@ -192,6 +192,11 @@ describe AnnotateModels do ...@@ -192,6 +192,11 @@ describe AnnotateModels do
[] []
end end
context 'when option is not present' do
let :options do
{}
end
context 'when header is "Schema Info"' do context 'when header is "Schema Info"' do
let :header do let :header do
'Schema Info' 'Schema Info'
...@@ -379,10 +384,100 @@ describe AnnotateModels do ...@@ -379,10 +384,100 @@ describe AnnotateModels do
end end
end end
context 'with Globalize gem' do
let :translation_klass do
double('Post::Translation',
to_s: 'Post::Translation',
columns: [
mock_column(:id, :integer, limit: 8),
mock_column(:post_id, :integer, limit: 8),
mock_column(:locale, :string, limit: 50),
mock_column(:title, :string, limit: 50),
])
end
let :klass do
mock_class(:posts, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
allow(mock_klass).to receive(:translation_class).and_return(translation_klass)
end
end
let :columns do
[
mock_column(:id, :integer, limit: 8),
mock_column(:author_name, :string, limit: 50),
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: posts
#
# id :integer not null, primary key
# author_name :string(50) not null
# title :string(50) not null
#
EOS
end
it 'returns schema info' do
is_expected.to eq expected_result
end
end
end
context 'when the primary key is an array (using composite_primary_keys)' do
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
#
# a_id :integer not null, primary key
# b_id :integer not null, primary key
# name :string(50) not null
#
EOS
end
it 'returns schema info' do
is_expected.to eq(expected_result)
end
end
end
end
end
context 'when option is present' do
context 'when header is "Schema Info"' do
let :header do
'Schema Info'
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 indexes exist' do context 'when indexes exist' do
context 'when option "show_indexes" is true' do context 'when option "show_indexes" is true' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, show_indexes: true) { show_indexes: true }
end end
context 'when indexes are normal' do context 'when indexes are normal' do
...@@ -582,11 +677,11 @@ describe AnnotateModels do ...@@ -582,11 +677,11 @@ describe AnnotateModels do
end end
context 'when option "simple_indexes" is true' do context 'when option "simple_indexes" is true' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, simple_indexes: true) { simple_indexes: true }
end end
context 'when one of indexes includes "orders" clause' do # TODO context 'when one of indexes includes "orders" clause' do
let :columns do let :columns do
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
...@@ -671,8 +766,8 @@ describe AnnotateModels do ...@@ -671,8 +766,8 @@ describe AnnotateModels do
end end
context 'when option "show_foreign_keys" is specified' do context 'when option "show_foreign_keys" is specified' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, show_foreign_keys: true) { show_foreign_keys: true }
end end
context 'when foreign_keys does not have option' do context 'when foreign_keys does not have option' do
...@@ -734,8 +829,8 @@ describe AnnotateModels do ...@@ -734,8 +829,8 @@ describe AnnotateModels do
end end
context 'when option "show_foreign_keys" and "show_complete_foreign_keys" are specified' do context 'when option "show_foreign_keys" and "show_complete_foreign_keys" are specified' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, show_foreign_keys: true, show_complete_foreign_keys: true) { show_foreign_keys: true, show_complete_foreign_keys: true }
end end
let :expected_result do let :expected_result do
...@@ -762,61 +857,43 @@ describe AnnotateModels do ...@@ -762,61 +857,43 @@ describe AnnotateModels do
end end
end end
context 'with Globalize gem' do context 'when "hide_limit_column_types" is specified in options' do
let :translation_klass do
double('Post::Translation',
to_s: 'Post::Translation',
columns: [
mock_column(:id, :integer, limit: 8),
mock_column(:post_id, :integer, limit: 8),
mock_column(:locale, :string, limit: 50),
mock_column(:title, :string, limit: 50),
])
end
let :klass do
mock_class(:posts, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
allow(mock_klass).to receive(:translation_class).and_return(translation_klass)
end
end
let :columns do let :columns do
[ [
mock_column(:id, :integer, limit: 8), mock_column(:id, :integer, limit: 8),
mock_column(:author_name, :string, limit: 50), 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
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# Schema Info # Schema Info
# #
# Table name: posts # Table name: users
# #
# id :integer not null, primary key # id :integer not null, primary key
# author_name :string(50) not null # active :boolean not null
# title :string(50) not null # name :string(50) not null
# notes :text(55) not null
# #
EOS EOS
end end
it 'returns schema info' do it 'works with option "hide_limit_column_types"' do
is_expected.to eq expected_result is_expected.to eq expected_result
end end
end end
end
context 'when the primary key is an array (using composite_primary_keys)' do
let :primary_key do
[:a_id, :b_id]
end
let :columns do context 'when "hide_limit_column_types" is "integer,boolean"' do
[ let :options do
mock_column(:a_id, :integer), { hide_limit_column_types: 'integer,boolean' }
mock_column(:b_id, :integer),
mock_column(:name, :string, limit: 50)
]
end end
let :expected_result do let :expected_result do
...@@ -825,116 +902,366 @@ describe AnnotateModels do ...@@ -825,116 +902,366 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# a_id :integer not null, primary key # id :integer not null, primary key
# b_id :integer not null, primary key # active :boolean not null
# name :string(50) not null # name :string(50) not null
# notes :text(55) not null
# #
EOS EOS
end end
it 'returns schema info' do it 'works with option "hide_limit_column_types"' do
is_expected.to eq(expected_result) is_expected.to eq expected_result
end
end end
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 end
context 'when header is "== Schema Information"' do let :expected_result do
let :header do <<~EOS
AnnotateModels::PREFIX # 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 end
context 'when the primary key is specified' do it 'works with option "hide_limit_column_types"' do
context 'when the primary_key is :id' do is_expected.to eq expected_result
let :primary_key do end
:id end
end end
context 'when "hide_default_column_types" is specified in options' do
let :columns do let :columns do
[ [
mock_column(:id, :integer), mock_column(:profile, :json, default: {}),
mock_column(:name, :string, limit: 50) mock_column(:settings, :jsonb, default: {}),
mock_column(:parameters, :hstore, default: {})
] ]
end end
context 'when option "format_rdoc" is true' do context 'when "hide_default_column_types" is blank string' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, format_rdoc: true) { hide_default_column_types: '' }
end end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# == Schema Information # Schema Info
# #
# Table name: users # Table name: users
# #
# *id*:: <tt>integer, not null, primary key</tt> # profile :json not null
# *name*:: <tt>string(50), not null</tt> # settings :jsonb not null
#-- # parameters :hstore not null
# == Schema Information End #
#++
EOS EOS
end end
it 'returns schema info in RDoc format' do it 'works with option "hide_default_column_types"' do
is_expected.to eq(expected_result) is_expected.to eq expected_result
end end
end end
context 'when option "format_yard" is true' do context 'when "hide_default_column_types" is "skip"' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, format_yard: true) { hide_default_column_types: 'skip' }
end end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# == Schema Information # Schema Info
# #
# Table name: users # Table name: users
# #
# @!attribute id # profile :json default({}), not null
# @return [Integer] # settings :jsonb default({}), not null
# @!attribute name # parameters :hstore default({}), not null
# @return [String]
# #
EOS EOS
end end
it 'returns schema info in YARD format' do it 'works with option "hide_default_column_types"' do
is_expected.to eq(expected_result) is_expected.to eq expected_result
end end
end end
context 'when option "format_markdown" is true' do context 'when "hide_default_column_types" is "json"' do
context 'when other option is not specified' do let :options do
subject do { hide_default_column_types: 'json' }
AnnotateModels.get_schema_info(klass, header, format_markdown: true)
end end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# == Schema Information # Schema Info
#
# Table name: `users`
# #
# ### Columns # Table name: users
# #
# Name | Type | Attributes # profile :json not null
# ----------- | ------------------ | --------------------------- # settings :jsonb default({}), not null
# **`id`** | `integer` | `not null, primary key` # parameters :hstore default({}), not null
# **`name`** | `string(50)` | `not null`
# #
EOS EOS
end end
it 'returns schema info in Markdown format' do it 'works with option "hide_limit_column_types"' do
is_expected.to eq(expected_result) is_expected.to eq expected_result
end
end
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
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# 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
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
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
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
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
let :options do
{ format_rdoc: true }
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: users
#
# *id*:: <tt>integer, not null, primary key</tt>
# *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
context 'when option "format_yard" is true' do
let :options do
{ format_yard: true }
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: users
#
# @!attribute id
# @return [Integer]
# @!attribute name
# @return [String]
#
EOS
end
it 'returns schema info in YARD 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
let :options do
{ format_markdown: true }
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`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
context 'when option "show_indexes" is true' do context 'when option "show_indexes" is true' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_indexes: true) { format_markdown: true, show_indexes: true }
end end
context 'when indexes are normal' do context 'when indexes are normal' do
...@@ -1128,8 +1455,8 @@ describe AnnotateModels do ...@@ -1128,8 +1455,8 @@ describe AnnotateModels do
end end
context 'when option "show_foreign_keys" is true' do context 'when option "show_foreign_keys" is true' do
subject do let :options do
AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_foreign_keys: true) { format_markdown: true, show_foreign_keys: true }
end end
let :columns do let :columns do
...@@ -1178,522 +1505,193 @@ describe AnnotateModels do ...@@ -1178,522 +1505,193 @@ describe AnnotateModels do
end end
end end
end end
end
end
end
end
describe '.set_defaults' do context 'when "format_doc" and "with_comment" are specified in options' do
subject do let :options do
Annotate::Helpers.true?(ENV['show_complete_foreign_keys']) { format_rdoc: true, with_comment: true }
end end
context 'when default value of "show_complete_foreign_keys" is not set' do context 'when columns are normal' do
it 'returns false' do
is_expected.to be(false)
end
end
context 'when default value of "show_complete_foreign_keys" is set' do
before do
Annotate.set_defaults('show_complete_foreign_keys' => 'true')
end
it 'returns true' do
is_expected.to be(true)
end
end
after :each do
ENV.delete('show_complete_foreign_keys')
end
end
describe '.files_by_pattern' do
subject { AnnotateModels.files_by_pattern(root_directory, pattern_type, options) }
context 'when pattern_type is "additional_file_patterns"' do
let(:root_directory) { nil }
let(:pattern_type) { 'additional_file_patterns' }
context 'when additional_file_patterns is specified in the options' do
let(:additional_file_patterns) do
[
'%PLURALIZED_MODEL_NAME%/**/*.rb',
'%PLURALIZED_MODEL_NAME%/*_form'
]
end
let(:options) { { additional_file_patterns: additional_file_patterns } }
it 'returns additional_file_patterns in the argument "options"' do
is_expected.to eq(additional_file_patterns)
end
end
context 'when additional_file_patterns is not specified in the options' do
let(:options) { {} }
it 'returns an empty array' do
is_expected.to eq([])
end
end
end
end
describe '.get_patterns' do
subject { AnnotateModels.get_patterns(options, pattern_type) }
context 'when pattern_type is "additional_file_patterns"' do
let(:pattern_type) { 'additional_file_patterns' }
context 'when additional_file_patterns is specified in the options' do
let(:additional_file_patterns) do
[
'/%PLURALIZED_MODEL_NAME%/**/*.rb',
'/bar/%PLURALIZED_MODEL_NAME%/*_form'
]
end
let(:options) { { additional_file_patterns: additional_file_patterns } }
it 'returns additional_file_patterns in the argument "options"' do
is_expected.to eq(additional_file_patterns)
end
end
context 'when additional_file_patterns is not specified in the options' do
let(:options) { {} }
it 'returns an empty array' do
is_expected.to eq([])
end
end
end
end
describe '.get_schema_info (with custom options)' do
let :klass do
mock_class(:users, :id, columns)
end
subject do
AnnotateModels.get_schema_info(klass, header, options)
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 let :columns do
[ [
mock_column(:id, :integer, limit: 8), mock_column(:id, :integer, comment: 'ID'),
mock_column(:active, :boolean, limit: 1), mock_column(:name, :string, limit: 50, comment: 'Name')
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
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# Schema Info # == Schema Information
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # *id(ID)*:: <tt>integer, not null, primary key</tt>
# active :boolean not null # *name(Name)*:: <tt>string(50), not null</tt>
# name :string(50) not null #--
# notes :text(55) not null # == Schema Information End
# #++
EOS EOS
end end
it 'works with option "hide_limit_column_types"' do it 'returns schema info in RDoc format' do
is_expected.to eq expected_result is_expected.to eq expected_result
end end
end end
context 'when "hide_limit_column_types" is "integer,boolean"' do
let :options do
{ hide_limit_column_types: 'integer,boolean' }
end end
let :expected_result do context 'when "format_markdown" and "with_comment" are specified in options' 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
let :options do let :options do
{ hide_limit_column_types: 'integer,boolean,string,text' } { format_markdown: true, with_comment: true }
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 end
it 'works with option "hide_limit_column_types"' do context 'when columns have comments' do
is_expected.to eq expected_result
end
end
end
context 'when "hide_default_column_types" is specified in options' do
let :columns do let :columns do
[ [
mock_column(:profile, :json, default: {}), mock_column(:id, :integer, comment: 'ID'),
mock_column(:settings, :jsonb, default: {}), mock_column(:name, :string, limit: 50, comment: 'Name')
mock_column(:parameters, :hstore, default: {})
] ]
end end
context 'when "hide_default_column_types" is blank string' do
let :options do
{ hide_default_column_types: '' }
end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# Schema Info # == Schema Information
# #
# Table name: users # Table name: `users`
# #
# profile :json not null # ### Columns
# settings :jsonb not null #
# parameters :hstore not null # Name | Type | Attributes
# ----------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key`
# **`name(Name)`** | `string(50)` | `not null`
# #
EOS EOS
end end
it 'works with option "hide_default_column_types"' do it 'returns schema info in Markdown format' do
is_expected.to eq expected_result is_expected.to eq expected_result
end end
end end
context 'when "hide_default_column_types" is "skip"' do context 'when columns have multibyte comments' do
let :options do let :columns do
{ hide_default_column_types: 'skip' } [
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'NAME')
]
end end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# Schema Info # == Schema Information
# #
# Table name: users # Table name: `users`
# #
# profile :json default({}), not null # ### Columns
# settings :jsonb default({}), not null #
# parameters :hstore default({}), not null # Name | Type | Attributes
# --------------------- | ------------------ | ---------------------------
# **`id(ID)`** | `integer` | `not null, primary key`
# **`name(NAME)`** | `string(50)` | `not null`
# #
EOS EOS
end end
it 'works with option "hide_default_column_types"' do it 'returns schema info in Markdown format' do
is_expected.to eq expected_result is_expected.to eq expected_result
end end
end end
context 'when "hide_default_column_types" is "json"' do
let :options do
{ hide_default_column_types: 'json' }
end 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 end
it 'works with option "hide_limit_column_types"' do
is_expected.to eq expected_result
end end
end end
end 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 end
context 'when "classified_sort" is "yes"' do describe '.set_defaults' do
let :options do subject do
{ classified_sort: 'yes' } Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS
end end
it 'works with option "classified_sort"' do context 'when default value of "show_complete_foreign_keys" is not set' do
is_expected.to eq expected_result it 'returns false' do
end is_expected.to be(false)
end end
end end
context 'when "with_comment" is specified in options' do context 'when default value of "show_complete_foreign_keys" is set' do
context 'when "with_comment" is "yes"' do before do
let :options do Annotate.set_defaults('show_complete_foreign_keys' => 'true')
{ with_comment: 'yes' }
end end
context 'when columns have comments' do it 'returns true' do
let :columns do is_expected.to be(true)
[
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 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 end
it 'works with option "with_comment"' do after :each do
is_expected.to eq expected_result ENV.delete('show_complete_foreign_keys')
end
end end
context 'when columns have multibyte 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(: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 end
let :expected_result do describe '.files_by_pattern' do
<<~EOS subject { AnnotateModels.files_by_pattern(root_directory, pattern_type, options) }
# 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 context 'when pattern_type is "additional_file_patterns"' do
is_expected.to eq expected_result let(:root_directory) { nil }
end let(:pattern_type) { 'additional_file_patterns' }
end
context 'when geometry columns are included' do context 'when additional_file_patterns is specified in the options' do
let :columns do let(:additional_file_patterns) do
[ [
mock_column(:id, :integer, limit: 8), '%PLURALIZED_MODEL_NAME%/**/*.rb',
mock_column(:active, :boolean, default: false, null: false), '%PLURALIZED_MODEL_NAME%/*_form'
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 end
let :expected_result do let(:options) { { additional_file_patterns: additional_file_patterns } }
<<~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
context 'when header is "== Schema Information"' do it 'returns additional_file_patterns in the argument "options"' do
let :header do is_expected.to eq(additional_file_patterns)
AnnotateModels::PREFIX
end 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 end
context 'when columns are normal' do context 'when additional_file_patterns is not specified in the options' do
let :columns do let(:options) { {} }
[
mock_column(:id, :integer, comment: 'ID'),
mock_column(:name, :string, limit: 50, comment: 'Name')
]
end
let :expected_result do it 'returns an empty array' do
<<~EOS is_expected.to eq([])
# == Schema Information
#
# Table name: users
#
# *id(ID)*:: <tt>integer, not null, primary key</tt>
# *name(Name)*:: <tt>string(50), not null</tt>
#--
# == Schema Information End
#++
EOS
end end
it 'returns schema info in RDoc format' do
is_expected.to eq expected_result
end end
end end
end end
context 'when "format_markdown" and "with_comment" are specified in options' do describe '.get_patterns' do
subject do subject { AnnotateModels.get_patterns(options, pattern_type) }
AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)
end
context 'when columns have comments' do context 'when pattern_type is "additional_file_patterns"' do
let :columns do let(:pattern_type) { 'additional_file_patterns' }
context 'when additional_file_patterns is specified in the options' do
let(:additional_file_patterns) do
[ [
mock_column(:id, :integer, comment: 'ID'), '/%PLURALIZED_MODEL_NAME%/**/*.rb',
mock_column(:name, :string, limit: 50, comment: 'Name') '/bar/%PLURALIZED_MODEL_NAME%/*_form'
] ]
end end
let :expected_result do let(:options) { { additional_file_patterns: additional_file_patterns } }
<<~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 it 'returns additional_file_patterns in the argument "options"' do
is_expected.to eq expected_result is_expected.to eq(additional_file_patterns)
end
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 end
let :expected_result do context 'when additional_file_patterns is not specified in the options' do
<<~EOS let(:options) { {} }
# == 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 it 'returns an empty array' do
is_expected.to eq expected_result is_expected.to eq([])
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