Commit f3db77f6 by Shu Fujita Committed by Andrew W. Lee

Refactor RSpec for AnnotateModels (4) - AnnotateModels.get_schema_info (without…

Refactor RSpec for AnnotateModels (4) - AnnotateModels.get_schema_info (without custom options) (#735) I refactored test cases of AnnotateModels without custom options.
parent bd305993
...@@ -175,714 +175,969 @@ describe AnnotateModels do ...@@ -175,714 +175,969 @@ describe AnnotateModels do
end end
end end
it 'should get schema info with default options' do describe '.get_schema_info' do
klass = mock_class(:users, subject do
:id, AnnotateModels.get_schema_info(klass, header)
[ end
mock_column(:id, :integer, limit: 8),
mock_column(:name, :string, limit: 50),
mock_column(:notes, :text, limit: 55)
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) let :klass do
# Schema Info mock_class(:users, primary_key, columns, indexes, foreign_keys)
# end
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
it 'should get schema info even if the primary key is not set' do let :indexes do
klass = mock_class(:users, []
nil, end
[
mock_column(:id, :integer),
mock_column(:name, :string, limit: 50)
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) let :foreign_keys do
# Schema Info []
# end
# Table name: users
#
# id :integer not null
# name :string(50) not null
#
EOS
end
it 'should get schema info even if the primary key is array, if using composite_primary_keys' do context 'when header is "Schema Info"' do
klass = mock_class(:users, let :header do
[:a_id, :b_id], 'Schema Info'
[ end
mock_column(:a_id, :integer),
mock_column(:b_id, :integer),
mock_column(:name, :string, limit: 50)
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) context 'when the primary key is not specified' do
# Schema Info let :primary_key do
# nil
# Table name: users end
#
# a_id :integer not null, primary key
# b_id :integer not null, primary key
# name :string(50) not null
#
EOS
end
it 'should get schema info with enum type' do context 'when the columns are normal' do
klass = mock_class(:users, let :columns do
nil, [
[ mock_column(:id, :integer),
mock_column(:id, :integer), mock_column(:name, :string, limit: 50)
mock_column(:name, :enum, limit: [:enum1, :enum2]) ]
]) end
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) let :expected_result do
# Schema Info <<~EOS
# # Schema Info
# Table name: users #
# # Table name: users
# id :integer not null #
# name :enum not null, (enum1, enum2) # id :integer not null
# # name :string(50) not null
EOS #
end EOS
end
it 'should get schema info with unsigned' do it 'returns schema info' do
klass = mock_class(:users, is_expected.to eq(expected_result)
nil, end
[ end
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),
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) context 'when an enum column exists' do
# Schema Info let :columns do
# [
# Table name: users mock_column(:id, :integer),
# mock_column(:name, :enum, limit: [:enum1, :enum2])
# id :integer not null ]
# integer :integer unsigned, not null end
# 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 'should get schema info for integer and boolean with default' do let :expected_result do
klass = mock_class(:users, <<~EOS
:id, # Schema Info
[ #
mock_column(:id, :integer), # Table name: users
mock_column(:size, :integer, default: 20), #
mock_column(:flag, :boolean, default: false) # id :integer not null
]) # name :enum not null, (enum1, enum2)
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) #
# Schema Info EOS
# end
# Table name: users
#
# id :integer not null, primary key
# size :integer default(20), not null
# flag :boolean default(FALSE), not null
#
EOS
end
it 'sets correct default value for integer column when ActiveRecord::Enum is used' do it 'returns schema info' do
klass = mock_class(:users, is_expected.to eq(expected_result)
:id, end
[ end
mock_column(:id, :integer),
mock_column(:status, :integer, default: 0)
])
# 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')
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# status :integer default(0), not null
#
EOS
end
it 'should get foreign key info' do context 'when unsigned columns exist' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column(:id, :integer),
mock_column(:id, :integer), mock_column(:integer, :integer, unsigned?: true),
mock_column(:foreign_thing_id, :integer) 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),
mock_foreign_key('fk_rails_cf2568e89e', ]
'foreign_thing_id', end
'foreign_things'),
mock_foreign_key('custom_fk_name',
'other_thing_id',
'other_things'),
mock_foreign_key('fk_rails_a70234b26c',
'third_thing_id',
'third_things')
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true)).to eql(<<-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 'should get complete foreign key info' do let :expected_result do
klass = mock_class(:users, <<~EOS
:id, # Schema Info
[ #
mock_column(:id, :integer), # Table name: users
mock_column(:foreign_thing_id, :integer) #
], # id :integer not null
[], # integer :integer unsigned, not null
[ # bigint :bigint unsigned, not null
mock_foreign_key('fk_rails_cf2568e89e', # bigint :bigint unsigned, not null
'foreign_thing_id', # float :float unsigned, not null
'foreign_things'), # decimal :decimal(10, 2) unsigned, not null
mock_foreign_key('custom_fk_name', #
'other_thing_id', EOS
'other_things'), end
mock_foreign_key('fk_rails_a70234b26c',
'third_thing_id',
'third_things')
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true, show_complete_foreign_keys: true)).to eql(<<-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_a70234b26c (third_thing_id => third_things.id)
# fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id)
#
EOS
end
it 'should get foreign key info if on_delete/on_update options present' do it 'returns schema info' do
klass = mock_class(:users, is_expected.to eq(expected_result)
:id, end
[ end
mock_column(:id, :integer), end
mock_column(:foreign_thing_id, :integer)
],
[],
[
mock_foreign_key('fk_rails_02e851e3b7',
'foreign_thing_id',
'foreign_things',
'id',
on_delete: 'on_delete_value',
on_update: 'on_update_value')
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_foreign_keys: true)).to eql(<<-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 'should get indexes keys' do context 'when the primary key is specified' do
klass = mock_class(:users, context 'when the primary_key is :id' do
:id, let :primary_key do
[ :id
mock_column(:id, :integer), end
mock_column(:foreign_thing_id, :integer)
], [mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-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 'should get ordered indexes keys' do context 'when columns are normal' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column(:id, :integer, limit: 8),
mock_column("id", :integer), mock_column(:name, :string, limit: 50),
mock_column("firstname", :string), mock_column(:notes, :text, limit: 55)
mock_column("surname", :string), ]
mock_column("value", :string) end
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname value),
orders: { 'surname' => :asc, 'value' => :desc })
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-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 'should get indexes keys with where clause' do let :expected_result do
klass = mock_class(:users, <<~EOS
:id, # Schema Info
[ #
mock_column("id", :integer), # Table name: users
mock_column("firstname", :string), #
mock_column("surname", :string), # id :integer not null, primary key
mock_column("value", :string) # name :string(50) not null
], # notes :text(55) not null
[ #
mock_index('index_rails_02e851e3b7', columns: ['id']), EOS
mock_index('index_rails_02e851e3b8', end
columns: %w(firstname surname),
where: 'value IS NOT NULL')
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-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 'should get indexes keys with using clause other than btree' do it 'returns schema info' do
klass = mock_class(:users, is_expected.to eq(expected_result)
:id, end
[ end
mock_column("id", :integer),
mock_column("firstname", :string),
mock_column("surname", :string),
mock_column("value", :string)
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname),
using: 'hash')
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-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 'should get simple indexes keys' do context 'when columns have default values' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column(:id, :integer),
mock_column(:id, :integer), mock_column(:size, :integer, default: 20),
mock_column(:foreign_thing_id, :integer) mock_column(:flag, :boolean, default: false)
], ]
[ end
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc })
])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
EOS
end
it 'should get simple indexes keys if one is in string form' do let :expected_result do
klass = mock_class(:users, <<~EOS
:id, # Schema Info
[ #
mock_column("id", :integer), # Table name: users
mock_column("name", :string) #
], [mock_index('index_rails_02e851e3b7', columns: ['id']), # id :integer not null, primary key
mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)')]) # size :integer default(20), not null
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS) # flag :boolean default(FALSE), not null
# Schema Info #
# EOS
# Table name: users end
#
# id :integer not null, primary key, indexed
# name :string not null
#
EOS
end
it 'should not crash getting indexes keys' do it 'returns schema info with default values' do
klass = mock_class(:users, is_expected.to eq(expected_result)
:id, end
[ end
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
], [])
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
EOS
end
it 'should get schema info as RDoc' do context 'when an integer column using ActiveRecord::Enum exists' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column(:id, :integer),
mock_column(:id, :integer), mock_column(:status, :integer, default: 0)
mock_column(:name, :string, limit: 50) ]
]) end
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_rdoc: true)).to eql(<<-EOS)
# #{AnnotateModels::PREFIX}
#
# Table name: users
#
# *id*:: <tt>integer, not null, primary key</tt>
# *name*:: <tt>string(50), not null</tt>
#--
# #{AnnotateModels::END_MARK}
#++
EOS
end
it 'should get schema info as YARD' do before :each do
klass = mock_class(:users, # column_defaults may be overritten when ActiveRecord::Enum is used, e.g:
:id, # class User < ActiveRecord::Base
[ # enum status: [ :disabled, :enabled ]
mock_column(:id, :integer), # end
mock_column(:name, :string, limit: 50), allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled')
]) end
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_yard: true)).to eql(<<-EOS)
# #{AnnotateModels::PREFIX}
#
# Table name: users
#
# @!attribute id
# @return [Integer]
# @!attribute name
# @return [String]
#
EOS
end
it 'should get schema info as Markdown' do let :expected_result do
klass = mock_class(:users, <<~EOS
:id, # Schema Info
[ #
mock_column(:id, :integer), # Table name: users
mock_column(:name, :string, limit: 50) #
]) # id :integer not null, primary key
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true)).to eql(<<-EOS) # status :integer default(0), not null
# #{AnnotateModels::PREFIX} #
# EOS
# Table name: `users` end
#
# ### Columns
#
# Name | Type | Attributes
# ----------- | ------------------ | ---------------------------
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null`
#
EOS
end
it 'should get schema info as Markdown with foreign keys' do it 'returns schema info with default values' do
klass = mock_class(:users, is_expected.to eq(expected_result)
:id, end
[ end
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
],
[],
[
mock_foreign_key('fk_rails_02e851e3b7',
'foreign_thing_id',
'foreign_things',
'id',
on_delete: 'on_delete_value',
on_update: 'on_update_value')
])
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_foreign_keys: true)).to eql(<<-EOS)
# #{AnnotateModels::PREFIX}
#
# Table name: `users`
#
# ### Columns
#
# 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 'should get schema info as Markdown with indexes' do context 'when indexes exist' do
klass = mock_class(:users, context 'when option "show_indexes" is true' do
:id, subject do
[ AnnotateModels.get_schema_info(klass, header, show_indexes: true)
mock_column(:id, :integer), end
mock_column(:name, :string, limit: 50)
],
[
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'])
])
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS)
# #{AnnotateModels::PREFIX}
#
# 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`**
#
EOS
end
it 'should get schema info as Markdown with unique indexes' do context 'when indexes are normal' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column(:id, :integer),
mock_column(:id, :integer), mock_column(:foreign_thing_id, :integer)
mock_column(:name, :string, limit: 50) ]
], end
[
mock_index('index_rails_02e851e3b7', columns: ['id']), let :indexes do
mock_index('index_rails_02e851e3b8', [
columns: ['foreign_thing_id'], mock_index('index_rails_02e851e3b7', columns: ['id']),
unique: true) mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
]) ]
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS) end
# #{AnnotateModels::PREFIX}
# let :expected_result do
# Table name: `users` <<~EOS
# # Schema Info
# ### Columns #
# # Table name: users
# Name | Type | Attributes #
# ----------- | ------------------ | --------------------------- # id :integer not null, primary key
# **`id`** | `integer` | `not null, primary key` # foreign_thing_id :integer not null
# **`name`** | `string(50)` | `not null` #
# # Indexes
# ### Indexes #
# # index_rails_02e851e3b7 (id)
# * `index_rails_02e851e3b7`: # index_rails_02e851e3b8 (foreign_thing_id)
# * **`id`** #
# * `index_rails_02e851e3b8` (_unique_): EOS
# * **`foreign_thing_id`** end
#
EOS it 'returns schema info with index information' do
end is_expected.to eq expected_result
end
end
it 'should get schema info as Markdown with ordered indexes' do context 'when one of indexes includes orderd index key' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column("id", :integer),
mock_column(:id, :integer), mock_column("firstname", :string),
mock_column(:name, :string, limit: 50) mock_column("surname", :string),
], mock_column("value", :string)
[ ]
mock_index('index_rails_02e851e3b7', columns: ['id']), end
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], let :indexes do
orders: { 'foreign_thing_id' => :desc }) [
]) mock_index('index_rails_02e851e3b7', columns: ['id']),
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS) mock_index('index_rails_02e851e3b8',
# #{AnnotateModels::PREFIX} columns: %w(firstname surname value),
# orders: { 'surname' => :asc, 'value' => :desc })
# Table name: `users` ]
# end
# ### Columns
# let :expected_result do
# Name | Type | Attributes <<~EOS
# ----------- | ------------------ | --------------------------- # Schema Info
# **`id`** | `integer` | `not null, primary key` #
# **`name`** | `string(50)` | `not null` # Table name: users
# #
# ### Indexes # id :integer not null, primary key
# # firstname :string not null
# * `index_rails_02e851e3b7`: # surname :string not null
# * **`id`** # value :string not null
# * `index_rails_02e851e3b8`: #
# * **`foreign_thing_id DESC`** # Indexes
# #
EOS # index_rails_02e851e3b7 (id)
end # 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
it 'should get schema info as Markdown with indexes with WHERE clause' do context 'when one of indexes includes "where" clause' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column("id", :integer),
mock_column(:id, :integer), mock_column("firstname", :string),
mock_column(:name, :string, limit: 50) mock_column("surname", :string),
], mock_column("value", :string)
[ ]
mock_index('index_rails_02e851e3b7', columns: ['id']), end
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], let :indexes do
unique: true, [
where: 'name IS NOT NULL') mock_index('index_rails_02e851e3b7', columns: ['id']),
]) mock_index('index_rails_02e851e3b8',
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS) columns: %w(firstname surname),
# #{AnnotateModels::PREFIX} where: 'value IS NOT NULL')
# ]
# Table name: `users` end
#
# ### Columns let :expected_result do
# <<~EOS
# Name | Type | Attributes # Schema Info
# ----------- | ------------------ | --------------------------- #
# **`id`** | `integer` | `not null, primary key` # Table name: users
# **`name`** | `string(50)` | `not null` #
# # id :integer not null, primary key
# ### Indexes # firstname :string not null
# # surname :string not null
# * `index_rails_02e851e3b7`: # value :string not null
# * **`id`** #
# * `index_rails_02e851e3b8` (_unique_ _where_ name IS NOT NULL): # Indexes
# * **`foreign_thing_id`** #
# # index_rails_02e851e3b7 (id)
EOS # index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL
end #
EOS
end
it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end
it 'should get schema info as Markdown with indexes with using clause other than btree' do context 'when one of indexes includes "using" clause other than "btree"' do
klass = mock_class(:users, let :columns do
:id, [
[ mock_column("id", :integer),
mock_column(:id, :integer), mock_column("firstname", :string),
mock_column(:name, :string, limit: 50) mock_column("surname", :string),
], mock_column("value", :string)
[ ]
mock_index('index_rails_02e851e3b7', columns: ['id']), end
mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], let :indexes do
using: 'hash') [
]) mock_index('index_rails_02e851e3b7', columns: ['id']),
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS) mock_index('index_rails_02e851e3b8',
# #{AnnotateModels::PREFIX} columns: %w(firstname surname),
# using: 'hash')
# Table name: `users` ]
# end
# ### Columns
# let :expected_result do
# Name | Type | Attributes <<~EOS
# ----------- | ------------------ | --------------------------- # Schema Info
# **`id`** | `integer` | `not null, primary key` #
# **`name`** | `string(50)` | `not null` # Table name: users
# #
# ### Indexes # id :integer not null, primary key
# # firstname :string not null
# * `index_rails_02e851e3b7`: # surname :string not null
# * **`id`** # value :string not null
# * `index_rails_02e851e3b8` (_using_ hash): #
# * **`foreign_thing_id`** # Indexes
# #
EOS # 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
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
[
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
subject do
AnnotateModels.get_schema_info(klass, header, 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
subject do
AnnotateModels.get_schema_info(klass, header, show_foreign_keys: true, show_complete_foreign_keys: true)
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
#
# custom_fk_name (other_thing_id => other_things.id)
# fk_rails_a70234b26c (third_thing_id => third_things.id)
# fk_rails_cf2568e89e (foreign_thing_id => foreign_things.id)
#
EOS
end
it 'returns schema info with foreign keys' do
is_expected.to eq(expected_result)
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
[
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
<<~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
subject do
AnnotateModels.get_schema_info(klass, header, 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
context 'when option "show_indexes" is true' do
subject do
AnnotateModels.get_schema_info(klass, header, format_markdown: true, show_indexes: true)
end
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
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`**
#
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 "unique" clause' do
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
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
[
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',
'foreign_thing_id',
'foreign_things',
'id',
on_delete: 'on_delete_value',
on_update: 'on_update_value')
]
end
let :expected_result do
<<~EOS
# == Schema Information
#
# Table name: `users`
#
# ### Columns
#
# 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
end
end
end end
it 'should work with the Globalize gem' do it 'should work with the Globalize gem' do
......
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