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,112 +192,26 @@ describe AnnotateModels do ...@@ -192,112 +192,26 @@ describe AnnotateModels do
[] []
end end
context 'when header is "Schema Info"' do context 'when option is not present' do
let :header do let :options do
'Schema Info' {}
end end
context 'when the primary key is not specified' do context 'when header is "Schema Info"' do
let :primary_key do let :header do
nil 'Schema Info'
end end
context 'when the columns are normal' do context 'when the primary key is not specified' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:name, :string, limit: 50)
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null
# name :string(50) not null
#
EOS
end
it 'returns schema info' do
is_expected.to eq(expected_result)
end
end
context 'when an enum column exists' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:name, :enum, limit: [:enum1, :enum2])
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null
# name :enum not null, (enum1, enum2)
#
EOS
end
it 'returns schema info' do
is_expected.to eq(expected_result)
end
end
context 'when unsigned columns exist' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:integer, :integer, unsigned?: true),
mock_column(:bigint, :integer, unsigned?: true, bigint?: true),
mock_column(:bigint, :bigint, unsigned?: true),
mock_column(:float, :float, unsigned?: true),
mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2),
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null
# integer :integer unsigned, not null
# bigint :bigint unsigned, not null
# bigint :bigint unsigned, not null
# float :float unsigned, not null
# decimal :decimal(10, 2) unsigned, not null
#
EOS
end
it 'returns schema info' do
is_expected.to eq(expected_result)
end
end
end
context 'when the primary key is specified' do
context 'when the primary_key is :id' do
let :primary_key do let :primary_key do
:id nil
end end
context 'when columns are normal' do context 'when the columns are normal' do
let :columns do let :columns do
[ [
mock_column(:id, :integer, limit: 8), mock_column(:id, :integer),
mock_column(:name, :string, limit: 50), mock_column(:name, :string, limit: 50)
mock_column(:notes, :text, limit: 55)
] ]
end end
...@@ -307,9 +221,8 @@ describe AnnotateModels do ...@@ -307,9 +221,8 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # id :integer not null
# name :string(50) not null # name :string(50) not null
# notes :text(55) not null
# #
EOS EOS
end end
...@@ -319,12 +232,11 @@ describe AnnotateModels do ...@@ -319,12 +232,11 @@ describe AnnotateModels do
end end
end end
context 'when columns have default values' do context 'when an enum column exists' do
let :columns do let :columns do
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:size, :integer, default: 20), mock_column(:name, :enum, limit: [:enum1, :enum2])
mock_column(:flag, :boolean, default: false)
] ]
end end
...@@ -334,32 +246,199 @@ describe AnnotateModels do ...@@ -334,32 +246,199 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # id :integer not null
# size :integer default(20), not null # name :enum not null, (enum1, enum2)
# flag :boolean default(FALSE), not null
# #
EOS EOS
end end
it 'returns schema info with default values' do it 'returns schema info' do
is_expected.to eq(expected_result) is_expected.to eq(expected_result)
end end
end end
context 'when an integer column using ActiveRecord::Enum exists' do context 'when unsigned columns exist' do
let :columns do let :columns do
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:status, :integer, default: 0) mock_column(:integer, :integer, unsigned?: true),
mock_column(:bigint, :integer, unsigned?: true, bigint?: true),
mock_column(:bigint, :bigint, unsigned?: true),
mock_column(:float, :float, unsigned?: true),
mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2),
] ]
end end
before :each do let :expected_result do
# column_defaults may be overritten when ActiveRecord::Enum is used, e.g: <<~EOS
# class User < ActiveRecord::Base # Schema Info
# enum status: [ :disabled, :enabled ] #
# end # Table name: users
allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled') #
# id :integer not null
# integer :integer unsigned, not null
# bigint :bigint unsigned, not null
# bigint :bigint unsigned, not null
# float :float unsigned, not null
# decimal :decimal(10, 2) unsigned, not null
#
EOS
end
it 'returns schema info' do
is_expected.to eq(expected_result)
end
end
end
context 'when the primary key is specified' do
context 'when the primary_key is :id' do
let :primary_key do
:id
end
context 'when columns are normal' do
let :columns do
[
mock_column(:id, :integer, limit: 8),
mock_column(:name, :string, limit: 50),
mock_column(:notes, :text, limit: 55)
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
it 'returns schema info' do
is_expected.to eq(expected_result)
end
end
context 'when columns have default values' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:size, :integer, default: 20),
mock_column(:flag, :boolean, default: false)
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# size :integer default(20), not null
# flag :boolean default(FALSE), not null
#
EOS
end
it 'returns schema info with default values' do
is_expected.to eq(expected_result)
end
end
context 'when an integer column using ActiveRecord::Enum exists' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:status, :integer, default: 0)
]
end
before :each do
# column_defaults may be overritten when ActiveRecord::Enum is used, e.g:
# class User < ActiveRecord::Base
# enum status: [ :disabled, :enabled ]
# end
allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled')
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# status :integer default(0), not null
#
EOS
end
it 'returns schema info with default values' do
is_expected.to eq(expected_result)
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 end
let :expected_result do let :expected_result do
...@@ -368,36 +447,390 @@ describe AnnotateModels do ...@@ -368,36 +447,390 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # a_id :integer not null, primary key
# status :integer default(0), not null # b_id :integer not null, primary key
# name :string(50) not null
# #
EOS EOS
end end
it 'returns schema info with default values' do it 'returns schema info' do
is_expected.to eq(expected_result) is_expected.to eq(expected_result)
end end
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 indexes exist' do context 'when the primary key is specified' do
context 'when option "show_indexes" is true' do context 'when the primary_key is :id' do
subject do let :primary_key do
AnnotateModels.get_schema_info(klass, header, show_indexes: true) :id
end
context 'when indexes exist' do
context 'when option "show_indexes" is true' do
let :options do
{ show_indexes: true }
end
context 'when indexes are normal' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (foreign_thing_id)
#
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
context 'when one of indexes includes orderd index key' do
let :columns do
[
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname value),
orders: { 'surname' => :asc, 'value' => :desc })
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# firstname :string not null
# surname :string not null
# value :string not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
#
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
context 'when one of indexes includes "where" clause' do
let :columns do
[
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname),
where: 'value IS NOT NULL')
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# firstname :string not null
# surname :string not null
# value :string not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL
#
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
context 'when one of indexes includes "using" clause other than "btree"' do
let :columns do
[
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname),
using: 'hash')
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# firstname :string not null
# surname :string not null
# value :string not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname) USING hash
#
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
context 'when index is not defined' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end
let :indexes do
[]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
EOS
end
it 'returns schema info without index information' do
is_expected.to eq expected_result
end
end
end end
context 'when indexes are normal' do context 'when option "simple_indexes" is true' do
let :columns do let :options do
[ { simple_indexes: true }
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end end
let :indexes do context 'when one of indexes includes "orders" clause' do
[ let :columns do
mock_index('index_rails_02e851e3b7', columns: ['id']), [
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id']) mock_column(:id, :integer),
] mock_column(:foreign_thing_id, :integer)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc })
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
context 'when one of indexes is in string form' do
let :columns do
[
mock_column("id", :integer),
mock_column("name", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)')
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key, indexed
# name :string not null
#
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
end
end
context 'when foreign keys exist' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end
let :foreign_keys do
[
mock_foreign_key('fk_rails_cf2568e89e', 'foreign_thing_id', 'foreign_things'),
mock_foreign_key('custom_fk_name', 'other_thing_id', 'other_things'),
mock_foreign_key('fk_rails_a70234b26c', 'third_thing_id', 'third_things')
]
end
context 'when option "show_foreign_keys" is specified' do
let :options do
{ show_foreign_keys: true }
end
context 'when foreign_keys does not have option' do
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
# Foreign Keys
#
# custom_fk_name (other_thing_id => other_things.id)
# fk_rails_... (foreign_thing_id => foreign_things.id)
# fk_rails_... (third_thing_id => third_things.id)
#
EOS
end
it 'returns schema info with foreign keys' do
is_expected.to eq(expected_result)
end
end
context 'when foreign_keys have option "on_delete" and "on_update"' do
let :foreign_keys do
[
mock_foreign_key('fk_rails_02e851e3b7',
'foreign_thing_id',
'foreign_things',
'id',
on_delete: 'on_delete_value',
on_update: 'on_update_value')
]
end
let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
# Foreign Keys
#
# fk_rails_... (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value
#
EOS
end
it 'returns schema info with foreign keys' do
is_expected.to eq(expected_result)
end
end
end
context 'when option "show_foreign_keys" and "show_complete_foreign_keys" are specified' do
let :options do
{ show_foreign_keys: true, show_complete_foreign_keys: true }
end end
let :expected_result do let :expected_result do
...@@ -409,36 +842,34 @@ describe AnnotateModels do ...@@ -409,36 +842,34 @@ describe AnnotateModels do
# id :integer not null, primary key # id :integer not null, primary key
# foreign_thing_id :integer not null # foreign_thing_id :integer not null
# #
# Indexes # Foreign Keys
# #
# index_rails_02e851e3b7 (id) # custom_fk_name (other_thing_id => other_things.id)
# index_rails_02e851e3b8 (foreign_thing_id) # fk_rails_a70234b26c (third_thing_id => third_things.id)
# fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id)
# #
EOS EOS
end end
it 'returns schema info with index information' do it 'returns schema info with foreign keys' do
is_expected.to eq expected_result is_expected.to eq(expected_result)
end end
end end
end
context 'when one of indexes includes orderd index key' do context 'when "hide_limit_column_types" is specified in options' do
let :columns do let :columns do
[ [
mock_column("id", :integer), mock_column(:id, :integer, limit: 8),
mock_column("firstname", :string), mock_column(:active, :boolean, limit: 1),
mock_column("surname", :string), mock_column(:name, :string, limit: 50),
mock_column("value", :string) mock_column(:notes, :text, limit: 55)
] ]
end end
let :indexes do context 'when "hide_limit_column_types" is blank string' do
[ let :options do
mock_index('index_rails_02e851e3b7', columns: ['id']), { hide_limit_column_types: '' }
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname value),
orders: { 'surname' => :asc, 'value' => :desc })
]
end end
let :expected_result do let :expected_result do
...@@ -447,41 +878,22 @@ describe AnnotateModels do ...@@ -447,41 +878,22 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # id :integer not null, primary key
# firstname :string not null # active :boolean not null
# surname :string not null # name :string(50) not null
# value :string not null # notes :text(55) not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
# #
EOS EOS
end end
it 'returns schema info with index information' 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
context 'when one of indexes includes "where" clause' do context 'when "hide_limit_column_types" is "integer,boolean"' do
let :columns do let :options do
[ { hide_limit_column_types: 'integer,boolean' }
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname),
where: 'value IS NOT NULL')
]
end end
let :expected_result do let :expected_result do
...@@ -490,41 +902,22 @@ describe AnnotateModels do ...@@ -490,41 +902,22 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # id :integer not null, primary key
# firstname :string not null # active :boolean not null
# surname :string not null # name :string(50) not null
# value :string not null # notes :text(55) not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL
# #
EOS EOS
end end
it 'returns schema info with index information' 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
context 'when one of indexes includes "using" clause other than "btree"' do context 'when "hide_limit_column_types" is "integer,boolean,string,text"' do
let :columns do let :options do
[ { hide_limit_column_types: 'integer,boolean,string,text' }
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname),
using: 'hash')
]
end end
let :expected_result do let :expected_result do
...@@ -533,34 +926,32 @@ describe AnnotateModels do ...@@ -533,34 +926,32 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # id :integer not null, primary key
# firstname :string not null # active :boolean not null
# surname :string not null # name :string not null
# value :string not null # notes :text not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname) USING hash
# #
EOS EOS
end end
it 'returns schema info with index information' 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 index is not defined' do 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(:foreign_thing_id, :integer) mock_column(:settings, :jsonb, default: {}),
] mock_column(:parameters, :hstore, default: {})
end ]
end
let :indexes do context 'when "hide_default_column_types" is blank string' do
[] let :options do
{ hide_default_column_types: '' }
end end
let :expected_result do let :expected_result do
...@@ -569,38 +960,21 @@ describe AnnotateModels do ...@@ -569,38 +960,21 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # profile :json not null
# foreign_thing_id :integer not null # settings :jsonb not null
# parameters :hstore not null
# #
EOS EOS
end end
it 'returns schema info without index information' 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
end
context 'when option "simple_indexes" is true' do
subject do
AnnotateModels.get_schema_info(klass, header, simple_indexes: true)
end
context 'when one of indexes includes "orders" clause' do # TODO
let :columns do
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end
let :indexes do context 'when "hide_default_column_types" is "skip"' do
[ let :options do
mock_index('index_rails_02e851e3b7', columns: ['id']), { hide_default_column_types: 'skip' }
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc })
]
end end
let :expected_result do let :expected_result do
...@@ -609,30 +983,21 @@ describe AnnotateModels do ...@@ -609,30 +983,21 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # profile :json default({}), not null
# foreign_thing_id :integer not null # settings :jsonb default({}), not null
# parameters :hstore default({}), not null
# #
EOS EOS
end end
it 'returns schema info with index information' 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 one of indexes is in string form' do context 'when "hide_default_column_types" is "json"' do
let :columns do let :options do
[ { hide_default_column_types: 'json' }
mock_column("id", :integer),
mock_column("name", :string)
]
end
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)')
]
end end
let :expected_result do let :expected_result do
...@@ -641,308 +1006,236 @@ describe AnnotateModels do ...@@ -641,308 +1006,236 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key, indexed # profile :json not null
# name :string not null # settings :jsonb default({}), not null
# parameters :hstore default({}), not null
# #
EOS EOS
end end
it 'returns schema info with index information' 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 end
end
context 'when foreign keys exist' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end
let :foreign_keys do context 'when "classified_sort" is specified in options' do
[ let :columns do
mock_foreign_key('fk_rails_cf2568e89e', 'foreign_thing_id', 'foreign_things'), [
mock_foreign_key('custom_fk_name', 'other_thing_id', 'other_things'), mock_column(:active, :boolean, limit: 1),
mock_foreign_key('fk_rails_a70234b26c', 'third_thing_id', 'third_things') mock_column(:name, :string, limit: 50),
] mock_column(:notes, :text, limit: 55)
end ]
context 'when option "show_foreign_keys" is specified' do
subject do
AnnotateModels.get_schema_info(klass, header, show_foreign_keys: true)
end end
context 'when foreign_keys does not have option' do context 'when "classified_sort" is "yes"' do
let :options do
{ classified_sort: 'yes' }
end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
# #
# id :integer not null, primary key # active :boolean not null
# foreign_thing_id :integer not null # name :string(50) not null
# # notes :text(55) not null
# Foreign Keys
#
# custom_fk_name (other_thing_id => other_things.id)
# fk_rails_... (foreign_thing_id => foreign_things.id)
# fk_rails_... (third_thing_id => third_things.id)
# #
EOS EOS
end end
it 'returns schema info with foreign keys' do it 'works with option "classified_sort"' do
is_expected.to eq(expected_result) is_expected.to eq expected_result
end end
end end
end
context 'when foreign_keys have option "on_delete" and "on_update"' do context 'when "with_comment" is specified in options' do
let :foreign_keys do context 'when "with_comment" is "yes"' do
[ let :options do
mock_foreign_key('fk_rails_02e851e3b7', { with_comment: 'yes' }
'foreign_thing_id',
'foreign_things',
'id',
on_delete: 'on_delete_value',
on_update: 'on_update_value')
]
end end
let :expected_result do context 'when columns have comments' do
<<~EOS let :columns do
# Schema Info [
# mock_column(:id, :integer, limit: 8, comment: 'ID'),
# Table name: users mock_column(:active, :boolean, limit: 1, comment: 'Active'),
# mock_column(:name, :string, limit: 50, comment: 'Name'),
# id :integer not null, primary key mock_column(:notes, :text, limit: 55, comment: 'Notes'),
# foreign_thing_id :integer not null mock_column(:no_comment, :text, limit: 20, comment: nil)
# ]
# Foreign Keys end
#
# fk_rails_... (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value let :expected_result do
# <<~EOS
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 end
it 'returns schema info with foreign keys' do context 'when columns have multibyte comments' do
is_expected.to eq(expected_result) 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 end
end
end
context 'when option "show_foreign_keys" and "show_complete_foreign_keys" are specified' do
subject do
AnnotateModels.get_schema_info(klass, header, show_foreign_keys: true, show_complete_foreign_keys: true)
end
let :expected_result do context 'when geometry columns are included' do
<<~EOS let :columns do
# Schema Info [
# mock_column(:id, :integer, limit: 8),
# Table name: users mock_column(:active, :boolean, default: false, null: false),
# mock_column(:geometry, :geometry,
# id :integer not null, primary key geometric_type: 'Geometry', srid: 4326,
# foreign_thing_id :integer not null limit: { srid: 4326, type: 'geometry' }),
# mock_column(:location, :geography,
# Foreign Keys geometric_type: 'Point', srid: 0,
# limit: { srid: 0, type: 'geometry' })
# custom_fk_name (other_thing_id => other_things.id) ]
# fk_rails_a70234b26c (third_thing_id => third_things.id) end
# fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id)
# let :expected_result do
EOS <<~EOS
end # Schema Info
#
it 'returns schema info with foreign keys' do # Table name: users
is_expected.to eq(expected_result) #
# 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 end
end
end
context 'with Globalize gem' do context 'when header is "== Schema Information"' do
let :translation_klass do let :header do
double('Post::Translation', AnnotateModels::PREFIX
to_s: 'Post::Translation', end
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 context 'when the primary key is specified' do
mock_class(:posts, primary_key, columns, indexes, foreign_keys).tap do |mock_klass| context 'when the primary_key is :id' do
allow(mock_klass).to receive(:translation_class).and_return(translation_klass) let :primary_key do
end :id
end end
let :columns do let :columns do
[ [
mock_column(:id, :integer, limit: 8), mock_column(:id, :integer),
mock_column(:author_name, :string, limit: 50), mock_column(:name, :string, limit: 50)
] ]
end end
let :expected_result do context 'when option "format_rdoc" is true' do
<<~EOS let :options do
# Schema Info { format_rdoc: true }
# end
# 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
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
subject do
AnnotateModels.get_schema_info(klass, header, 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
subject do
AnnotateModels.get_schema_info(klass, header, format_yard: true)
end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# == Schema Information # == Schema Information
# #
# Table name: users # Table name: users
# #
# @!attribute id # *id*:: <tt>integer, not null, primary key</tt>
# @return [Integer] # *name*:: <tt>string(50), not null</tt>
# @!attribute name #--
# @return [String] # == Schema Information End
# #++
EOS EOS
end end
it 'returns schema info in YARD format' 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 option "format_markdown" is true' do context 'when option "format_yard" is true' do
context 'when other option is not specified' do let :options do
subject do { format_yard: true }
AnnotateModels.get_schema_info(klass, header, format_markdown: true)
end end
let :expected_result do let :expected_result do
<<~EOS <<~EOS
# == Schema Information # == Schema Information
# #
# Table name: `users` # Table name: users
#
# ### Columns
# #
# Name | Type | Attributes # @!attribute id
# ----------- | ------------------ | --------------------------- # @return [Integer]
# **`id`** | `integer` | `not null, primary key` # @!attribute name
# **`name`** | `string(50)` | `not null` # @return [String]
# #
EOS EOS
end end
it 'returns schema info in Markdown format' do it 'returns schema info in YARD format' do
is_expected.to eq(expected_result) is_expected.to eq(expected_result)
end end
end end
context 'when option "show_indexes" is true' do context 'when option "format_markdown" is true' do
subject do context 'when other option is not specified' do
AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_indexes: true) let :options do
end { format_markdown: true }
context 'when indexes are normal' do
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
]
end end
let :expected_result do let :expected_result do
...@@ -958,105 +1251,271 @@ describe AnnotateModels do ...@@ -958,105 +1251,271 @@ describe AnnotateModels do
# **`id`** | `integer` | `not null, primary key` # **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null` # **`name`** | `string(50)` | `not null`
# #
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8`:
# * **`foreign_thing_id`**
#
EOS EOS
end end
it 'returns schema info with index information in Markdown format' 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 one of indexes includes "unique" clause' do context 'when option "show_indexes" is true' do
let :indexes do let :options do
[ { format_markdown: true, show_indexes: true }
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
unique: true)
]
end end
let :expected_result do context 'when indexes are normal' do
<<~EOS let :indexes do
# == Schema Information [
# mock_index('index_rails_02e851e3b7', columns: ['id']),
# Table name: `users` mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
# ]
# ### Columns end
#
# Name | Type | Attributes let :expected_result do
# ----------- | ------------------ | --------------------------- <<~EOS
# **`id`** | `integer` | `not null, primary key` # == Schema Information
# **`name`** | `string(50)` | `not null` #
# # Table name: `users`
# ### Indexes #
# # ### Columns
# * `index_rails_02e851e3b7`: #
# * **`id`** # Name | Type | Attributes
# * `index_rails_02e851e3b8` (_unique_): # ----------- | ------------------ | ---------------------------
# * **`foreign_thing_id`** # **`id`** | `integer` | `not null, primary key`
# # **`name`** | `string(50)` | `not null`
EOS #
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8`:
# * **`foreign_thing_id`**
#
EOS
end
it 'returns schema info with index information in Markdown format' do
is_expected.to eq expected_result
end
end end
it 'returns schema info with index information in Markdown format' do context 'when one of indexes includes "unique" clause' do
is_expected.to eq expected_result let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
unique: true)
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null`
#
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8` (_unique_):
# * **`foreign_thing_id`**
#
EOS
end
it 'returns schema info with index information in Markdown format' do
is_expected.to eq expected_result
end
end
context 'when one of indexes includes orderd index key' do
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc })
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null`
#
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8`:
# * **`foreign_thing_id DESC`**
#
EOS
end
it 'returns schema info with index information in Markdown format' do
is_expected.to eq expected_result
end
end
context 'when one of indexes includes "where" clause and "unique" clause' do
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
unique: true,
where: 'name IS NOT NULL')
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null`
#
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8` (_unique_ _where_ name IS NOT NULL):
# * **`foreign_thing_id`**
#
EOS
end
it 'returns schema info with index information in Markdown format' do
is_expected.to eq expected_result
end
end
context 'when one of indexes includes "using" clause other than "btree"' do
let :indexes do
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
using: 'hash')
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null`
#
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8` (_using_ hash):
# * **`foreign_thing_id`**
#
EOS
end
it 'returns schema info with index information in Markdown format' do
is_expected.to eq expected_result
end
end end
end end
context 'when one of indexes includes orderd index key' do context 'when option "show_foreign_keys" is true' do
let :indexes do let :options do
{ format_markdown: true, show_foreign_keys: true }
end
let :columns do
[ [
mock_index('index_rails_02e851e3b7', columns: ['id']), mock_column(:id, :integer),
mock_index('index_rails_02e851e3b8', mock_column(:foreign_thing_id, :integer)
columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc })
] ]
end end
let :expected_result do context 'when foreign_keys have option "on_delete" and "on_update"' do
<<~EOS let :foreign_keys do
# == Schema Information [
# mock_foreign_key('fk_rails_02e851e3b7',
# Table name: `users` 'foreign_thing_id',
# 'foreign_things',
# ### Columns 'id',
# on_delete: 'on_delete_value',
# Name | Type | Attributes on_update: 'on_update_value')
# ----------- | ------------------ | --------------------------- ]
# **`id`** | `integer` | `not null, primary key` end
# **`name`** | `string(50)` | `not null`
# let :expected_result do
# ### Indexes <<~EOS
# # == Schema Information
# * `index_rails_02e851e3b7`: #
# * **`id`** # Table name: `users`
# * `index_rails_02e851e3b8`: #
# * **`foreign_thing_id DESC`** # ### Columns
# #
EOS # Name | Type | Attributes
# ----------------------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`foreign_thing_id`** | `integer` | `not null`
#
# ### Foreign Keys
#
# * `fk_rails_...` (_ON DELETE => on_delete_value ON UPDATE => on_update_value_):
# * **`foreign_thing_id => foreign_things.id`**
#
EOS
end
it 'returns schema info with foreign_keys in Markdown format' do
is_expected.to eq(expected_result)
end
end end
end
end
it 'returns schema info with index information in Markdown format' do context 'when "format_doc" and "with_comment" are specified in options' do
is_expected.to eq expected_result let :options do
end { format_rdoc: true, with_comment: true }
end end
context 'when one of indexes includes "where" clause and "unique" clause' do context 'when columns are normal' do
let :indexes do let :columns do
[ [
mock_index('index_rails_02e851e3b7', columns: ['id']), mock_column(:id, :integer, comment: 'ID'),
mock_index('index_rails_02e851e3b8', mock_column(:name, :string, limit: 50, comment: 'Name')
columns: ['foreign_thing_id'],
unique: true,
where: 'name IS NOT NULL')
] ]
end end
...@@ -1064,37 +1523,32 @@ describe AnnotateModels do ...@@ -1064,37 +1523,32 @@ describe AnnotateModels do
<<~EOS <<~EOS
# == Schema Information # == Schema Information
# #
# Table name: `users` # Table name: users
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null`
#
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8` (_unique_ _where_ name IS NOT NULL):
# * **`foreign_thing_id`**
# #
# *id(ID)*:: <tt>integer, not null, primary key</tt>
# *name(Name)*:: <tt>string(50), not null</tt>
#--
# == Schema Information End
#++
EOS EOS
end end
it 'returns schema info with index information in Markdown format' 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
end
context 'when one of indexes includes "using" clause other than "btree"' do context 'when "format_markdown" and "with_comment" are specified in options' do
let :indexes do let :options do
{ format_markdown: true, with_comment: true }
end
context 'when columns have comments' do
let :columns do
[ [
mock_index('index_rails_02e851e3b7', columns: ['id']), mock_column(:id, :integer, comment: 'ID'),
mock_index('index_rails_02e851e3b8', mock_column(:name, :string, limit: 50, comment: 'Name')
columns: ['foreign_thing_id'],
using: 'hash')
] ]
end end
...@@ -1106,48 +1560,24 @@ describe AnnotateModels do ...@@ -1106,48 +1560,24 @@ describe AnnotateModels do
# #
# ### Columns # ### Columns
# #
# Name | Type | Attributes # Name | Type | Attributes
# ----------- | ------------------ | --------------------------- # ----------------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key` # **`id(ID)`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null` # **`name(Name)`** | `string(50)` | `not null`
#
# ### Indexes
#
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8` (_using_ hash):
# * **`foreign_thing_id`**
# #
EOS EOS
end end
it 'returns schema info with index information in Markdown format' 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
end
context 'when option "show_foreign_keys" is true' do
subject do
AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_foreign_keys: true)
end
let :columns do context 'when columns have multibyte comments' do
[ let :columns do
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end
context 'when foreign_keys have option "on_delete" and "on_update"' do
let :foreign_keys do
[ [
mock_foreign_key('fk_rails_02e851e3b7', mock_column(:id, :integer, comment: 'ID'),
'foreign_thing_id', mock_column(:name, :string, limit: 50, comment: 'NAME')
'foreign_things',
'id',
on_delete: 'on_delete_value',
on_update: 'on_update_value')
] ]
end end
...@@ -1159,21 +1589,16 @@ describe AnnotateModels do ...@@ -1159,21 +1589,16 @@ describe AnnotateModels do
# #
# ### Columns # ### Columns
# #
# Name | Type | Attributes # Name | Type | Attributes
# ----------------------- | ------------------ | --------------------------- # --------------------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key` # **`id(ID)`** | `integer` | `not null, primary key`
# **`foreign_thing_id`** | `integer` | `not null` # **`name(NAME)`** | `string(50)` | `not null`
#
# ### Foreign Keys
#
# * `fk_rails_...` (_ON DELETE => on_delete_value ON UPDATE => on_update_value_):
# * **`foreign_thing_id => foreign_things.id`**
# #
EOS EOS
end end
it 'returns schema info with foreign_keys in Markdown format' 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
end end
...@@ -1272,433 +1697,6 @@ describe AnnotateModels do ...@@ -1272,433 +1697,6 @@ describe AnnotateModels do
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
[
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
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
#
# 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
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
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
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
let :options do
{ hide_default_column_types: 'skip' }
end
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
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
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
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
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(:name, :string, limit: 50, comment: 'Name')
]
end
let :expected_result do
<<~EOS
# == 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
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
describe '.get_model_files' do describe '.get_model_files' do
subject { described_class.get_model_files(options) } subject { described_class.get_model_files(options) }
......
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