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,89 +175,85 @@ describe AnnotateModels do ...@@ -175,89 +175,85 @@ 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)
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
# notes :text(55) not null
#
EOS
end 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 []
#
# Table name: users
#
# id :integer not null
# name :string(50) not null
#
EOS
end 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
context 'when the primary key is not specified' do
let :primary_key do
nil
end
context 'when the columns are normal' do
let :columns do
[ [
mock_column(:a_id, :integer), mock_column(:id, :integer),
mock_column(:b_id, :integer),
mock_column(:name, :string, limit: 50) 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
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) it 'returns schema info' do
# Schema Info is_expected.to eq(expected_result)
# end
# Table name: users
#
# a_id :integer not null, primary key
# b_id :integer not null, primary key
# name :string(50) not null
#
EOS
end end
it 'should get schema info with enum type' do context 'when an enum column exists' do
klass = mock_class(:users, let :columns do
nil,
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:name, :enum, limit: [:enum1, :enum2]) 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 :enum not null, (enum1, enum2)
EOS #
EOS
end 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
context 'when unsigned columns exist' do
let :columns do
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:integer, :integer, unsigned?: true), mock_column(:integer, :integer, unsigned?: true),
...@@ -265,423 +261,446 @@ EOS ...@@ -265,423 +261,446 @@ EOS
mock_column(:bigint, :bigint, unsigned?: true), mock_column(:bigint, :bigint, unsigned?: true),
mock_column(:float, :float, unsigned?: true), mock_column(:float, :float, unsigned?: true),
mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2), mock_column(:decimal, :decimal, unsigned?: true, precision: 10, scale: 2),
]) ]
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 #
# integer :integer unsigned, not null # id :integer not null
# bigint :bigint unsigned, not null # integer :integer unsigned, not null
# bigint :bigint unsigned, not null # bigint :bigint unsigned, not null
# float :float unsigned, not null # bigint :bigint unsigned, not null
# decimal :decimal(10, 2) unsigned, not null # float :float unsigned, not null
# # decimal :decimal(10, 2) unsigned, not null
EOS #
end EOS
end
it 'should get schema info for integer and boolean with default' do
klass = mock_class(:users, it 'returns schema info' do
:id, 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(:id, :integer),
mock_column(:size, :integer, default: 20), mock_column(:size, :integer, default: 20),
mock_column(:flag, :boolean, default: false) mock_column(:flag, :boolean, default: false)
]) ]
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) end
# Schema Info
# let :expected_result do
# Table name: users <<~EOS
# # Schema Info
# id :integer not null, primary key #
# size :integer default(20), not null # Table name: users
# flag :boolean default(FALSE), not null #
# # id :integer not null, primary key
EOS # size :integer default(20), not null
end # flag :boolean default(FALSE), not null
#
it 'sets correct default value for integer column when ActiveRecord::Enum is used' do EOS
klass = mock_class(:users, end
:id,
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(:id, :integer),
mock_column(:status, :integer, default: 0) mock_column(:status, :integer, default: 0)
]) ]
end
before :each do
# column_defaults may be overritten when ActiveRecord::Enum is used, e.g: # column_defaults may be overritten when ActiveRecord::Enum is used, e.g:
# class User < ActiveRecord::Base # class User < ActiveRecord::Base
# enum status: [ :disabled, :enabled ] # enum status: [ :disabled, :enabled ]
# end # end
allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled') allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled')
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS) end
# Schema Info
# let :expected_result do
# Table name: users <<~EOS
# # Schema Info
# id :integer not null, primary key #
# status :integer default(0), not null # Table name: users
# #
EOS # id :integer not null, primary key
end # status :integer default(0), not null
#
it 'should get foreign key info' do EOS
klass = mock_class(:users, end
:id,
[ it 'returns schema info with default values' do
mock_column(:id, :integer), is_expected.to eq(expected_result)
mock_column(:foreign_thing_id, :integer) end
], end
[],
[ context 'when indexes exist' do
mock_foreign_key('fk_rails_cf2568e89e', context 'when option "show_indexes" is true' do
'foreign_thing_id', subject do
'foreign_things'), AnnotateModels.get_schema_info(klass, header, show_indexes: true)
mock_foreign_key('custom_fk_name', end
'other_thing_id',
'other_things'), context 'when indexes are normal' do
mock_foreign_key('fk_rails_a70234b26c', let :columns do
'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
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
],
[],
[
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')
])
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
klass = mock_class(:users,
:id,
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer) mock_column(:foreign_thing_id, :integer)
], ]
[], end
[
mock_foreign_key('fk_rails_02e851e3b7', let :indexes do
'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
klass = mock_class(:users,
:id,
[ [
mock_column(:id, :integer), mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_column(:foreign_thing_id, :integer) mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])
], [mock_index('index_rails_02e851e3b7', columns: ['id']), ]
mock_index('index_rails_02e851e3b8', columns: ['foreign_thing_id'])]) end
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS)
# Schema Info let :expected_result do
# <<~EOS
# Table name: users # Schema Info
# #
# id :integer not null, primary key # Table name: users
# foreign_thing_id :integer not null #
# # id :integer not null, primary key
# Indexes # foreign_thing_id :integer not null
# #
# index_rails_02e851e3b7 (id) # Indexes
# index_rails_02e851e3b8 (foreign_thing_id) #
# # index_rails_02e851e3b7 (id)
EOS # index_rails_02e851e3b8 (foreign_thing_id)
end #
EOS
it 'should get ordered indexes keys' do end
klass = mock_class(:users,
:id, 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("id", :integer),
mock_column("firstname", :string), mock_column("firstname", :string),
mock_column("surname", :string), mock_column("surname", :string),
mock_column("value", :string) mock_column("value", :string)
], ]
end
let :indexes do
[ [
mock_index('index_rails_02e851e3b7', columns: ['id']), mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname value), columns: %w(firstname surname value),
orders: { 'surname' => :asc, 'value' => :desc }) orders: { 'surname' => :asc, 'value' => :desc })
]) ]
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS) end
# Schema Info
# let :expected_result do
# Table name: users <<~EOS
# # Schema Info
# id :integer not null, primary key #
# firstname :string not null # Table name: users
# surname :string not null #
# value :string not null # id :integer not null, primary key
# # firstname :string not null
# Indexes # surname :string not null
# # value :string not null
# index_rails_02e851e3b7 (id) #
# index_rails_02e851e3b8 (firstname,surname ASC,value DESC) # Indexes
# #
EOS # index_rails_02e851e3b7 (id)
end # index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
#
it 'should get indexes keys with where clause' do EOS
klass = mock_class(:users, end
:id,
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("id", :integer),
mock_column("firstname", :string), mock_column("firstname", :string),
mock_column("surname", :string), mock_column("surname", :string),
mock_column("value", :string) mock_column("value", :string)
], ]
end
let :indexes do
[ [
mock_index('index_rails_02e851e3b7', columns: ['id']), mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname), columns: %w(firstname surname),
where: 'value IS NOT NULL') where: 'value IS NOT NULL')
]) ]
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS) end
# Schema Info
# let :expected_result do
# Table name: users <<~EOS
# # Schema Info
# id :integer not null, primary key #
# firstname :string not null # Table name: users
# surname :string not null #
# value :string not null # id :integer not null, primary key
# # firstname :string not null
# Indexes # surname :string not null
# # value :string not null
# index_rails_02e851e3b7 (id) #
# index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL # Indexes
# #
EOS # index_rails_02e851e3b7 (id)
end # index_rails_02e851e3b8 (firstname,surname) WHERE value IS NOT NULL
#
it 'should get indexes keys with using clause other than btree' do EOS
klass = mock_class(:users, end
:id,
[ it 'returns schema info with index information' do
mock_column("id", :integer), 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("firstname", :string),
mock_column("surname", :string), mock_column("surname", :string),
mock_column("value", :string) mock_column("value", :string)
], ]
end
let :indexes do
[ [
mock_index('index_rails_02e851e3b7', columns: ['id']), mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: %w(firstname surname), columns: %w(firstname surname),
using: 'hash') using: 'hash')
]) ]
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', show_indexes: true)).to eql(<<-EOS) end
# Schema Info
# let :expected_result do
# Table name: users <<~EOS
# # Schema Info
# id :integer not null, primary key #
# firstname :string not null # Table name: users
# surname :string not null #
# value :string not null # id :integer not null, primary key
# # firstname :string not null
# Indexes # surname :string not null
# # value :string not null
# index_rails_02e851e3b7 (id) #
# index_rails_02e851e3b8 (firstname,surname) USING hash # Indexes
# #
EOS # index_rails_02e851e3b7 (id)
end # index_rails_02e851e3b8 (firstname,surname) USING hash
#
it 'should get simple indexes keys' do EOS
klass = mock_class(:users, end
:id,
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(:id, :integer),
mock_column(:foreign_thing_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_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc }) orders: { 'foreign_thing_id' => :desc })
]) ]
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS) end
# Schema Info
# let :expected_result do
# Table name: users <<~EOS
# # Schema Info
# id :integer not null, primary key #
# foreign_thing_id :integer not null # Table name: users
# #
EOS # id :integer not null, primary key
end # foreign_thing_id :integer not null
#
it 'should get simple indexes keys if one is in string form' do EOS
klass = mock_class(:users, end
:id,
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("id", :integer),
mock_column("name", :string) mock_column("name", :string)
], [mock_index('index_rails_02e851e3b7', columns: ['id']), ]
mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)')]) end
expect(AnnotateModels.get_schema_info(klass, 'Schema Info', simple_indexes: true)).to eql(<<-EOS)
# Schema Info let :indexes do
#
# Table name: users
#
# id :integer not null, primary key, indexed
# name :string not null
#
EOS
end
it 'should not crash getting indexes keys' do
klass = mock_class(:users,
:id,
[
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
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:name, :string, limit: 50)
])
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
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:name, :string, limit: 50),
])
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
klass = mock_class(:users,
:id,
[ [
mock_column(:id, :integer), mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_column(:name, :string, limit: 50) mock_index('index_rails_02e851e3b8', columns: 'LOWER(name)')
]) ]
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: 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, indexed
# **`id`** | `integer` | `not null, primary key` # name :string not null
# **`name`** | `string(50)` | `not null` #
# EOS
EOS end
end
it 'returns schema info with index information' do
it 'should get schema info as Markdown with foreign keys' do is_expected.to eq expected_result
klass = mock_class(:users, end
:id, end
end
end
context 'when foreign keys exist' do
let :columns do
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:foreign_thing_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', mock_foreign_key('fk_rails_02e851e3b7',
'foreign_thing_id', 'foreign_thing_id',
...@@ -689,200 +708,436 @@ EOS ...@@ -689,200 +708,436 @@ EOS
'id', 'id',
on_delete: 'on_delete_value', on_delete: 'on_delete_value',
on_update: 'on_update_value') on_update: 'on_update_value')
]) ]
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_foreign_keys: 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
# **`foreign_thing_id`** | `integer` | `not null` #
# # Foreign Keys
# ### Foreign Keys #
# # fk_rails_... (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value
# * `fk_rails_...` (_ON DELETE => on_delete_value ON UPDATE => on_update_value_): #
# * **`foreign_thing_id => foreign_things.id`** EOS
# end
EOS
end it 'returns schema info with foreign keys' do
is_expected.to eq(expected_result)
it 'should get schema info as Markdown with indexes' do end
klass = mock_class(:users, end
:id, 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(:id, :integer), mock_column(:a_id, :integer),
mock_column(:b_id, :integer),
mock_column(:name, :string, limit: 50) mock_column(:name, :string, limit: 50)
], ]
[ end
mock_index('index_rails_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', let :expected_result do
columns: ['foreign_thing_id']) <<~EOS
]) # Schema Info
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, show_indexes: true)).to eql(<<-EOS) #
# #{AnnotateModels::PREFIX} # Table name: users
# #
# Table name: `users` # a_id :integer not null, primary key
# # b_id :integer not null, primary key
# ### Columns # name :string(50) not null
# #
# Name | Type | Attributes EOS
# ----------- | ------------------ | --------------------------- end
# **`id`** | `integer` | `not null, primary key`
# **`name`** | `string(50)` | `not null` it 'returns schema info' do
# is_expected.to eq(expected_result)
# ### Indexes end
# end
# * `index_rails_02e851e3b7`: end
# * **`id`** end
# * `index_rails_02e851e3b8`:
# * **`foreign_thing_id`** context 'when header is "== Schema Information"' do
# let :header do
EOS AnnotateModels::PREFIX
end end
it 'should get schema info as Markdown with unique indexes' 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
end
let :columns do
[ [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:name, :string, limit: 50) 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_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], columns: ['foreign_thing_id'],
unique: true) unique: true)
]) ]
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 Information
# ### Columns #
# # Table name: `users`
# Name | Type | Attributes #
# ----------- | ------------------ | --------------------------- # ### Columns
# **`id`** | `integer` | `not null, primary key` #
# **`name`** | `string(50)` | `not null` # Name | Type | Attributes
# # ----------- | ------------------ | ---------------------------
# ### Indexes # **`id`** | `integer` | `not null, primary key`
# # **`name`** | `string(50)` | `not null`
# * `index_rails_02e851e3b7`: #
# * **`id`** # ### Indexes
# * `index_rails_02e851e3b8` (_unique_): #
# * **`foreign_thing_id`** # * `index_rails_02e851e3b7`:
# # * **`id`**
EOS # * `index_rails_02e851e3b8` (_unique_):
end # * **`foreign_thing_id`**
#
it 'should get schema info as Markdown with ordered indexes' do EOS
klass = mock_class(:users, end
:id,
[ it 'returns schema info with index information in Markdown format' do
mock_column(:id, :integer), is_expected.to eq expected_result
mock_column(:name, :string, limit: 50) 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_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], columns: ['foreign_thing_id'],
orders: { 'foreign_thing_id' => :desc }) orders: { 'foreign_thing_id' => :desc })
]) ]
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 Information
# ### Columns #
# # Table name: `users`
# Name | Type | Attributes #
# ----------- | ------------------ | --------------------------- # ### Columns
# **`id`** | `integer` | `not null, primary key` #
# **`name`** | `string(50)` | `not null` # Name | Type | Attributes
# # ----------- | ------------------ | ---------------------------
# ### Indexes # **`id`** | `integer` | `not null, primary key`
# # **`name`** | `string(50)` | `not null`
# * `index_rails_02e851e3b7`: #
# * **`id`** # ### Indexes
# * `index_rails_02e851e3b8`: #
# * **`foreign_thing_id DESC`** # * `index_rails_02e851e3b7`:
# # * **`id`**
EOS # * `index_rails_02e851e3b8`:
end # * **`foreign_thing_id DESC`**
#
it 'should get schema info as Markdown with indexes with WHERE clause' do EOS
klass = mock_class(:users, end
:id,
[ it 'returns schema info with index information in Markdown format' do
mock_column(:id, :integer), is_expected.to eq expected_result
mock_column(:name, :string, limit: 50) 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_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], columns: ['foreign_thing_id'],
unique: true, unique: true,
where: 'name IS NOT NULL') where: 'name IS NOT NULL')
]) ]
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 Information
# ### Columns #
# # Table name: `users`
# Name | Type | Attributes #
# ----------- | ------------------ | --------------------------- # ### Columns
# **`id`** | `integer` | `not null, primary key` #
# **`name`** | `string(50)` | `not null` # Name | Type | Attributes
# # ----------- | ------------------ | ---------------------------
# ### Indexes # **`id`** | `integer` | `not null, primary key`
# # **`name`** | `string(50)` | `not null`
# * `index_rails_02e851e3b7`: #
# * **`id`** # ### Indexes
# * `index_rails_02e851e3b8` (_unique_ _where_ name IS NOT NULL): #
# * **`foreign_thing_id`** # * `index_rails_02e851e3b7`:
# # * **`id`**
EOS # * `index_rails_02e851e3b8` (_unique_ _where_ name IS NOT NULL):
end # * **`foreign_thing_id`**
#
it 'should get schema info as Markdown with indexes with using clause other than btree' do EOS
klass = mock_class(:users, end
:id,
[ it 'returns schema info with index information in Markdown format' do
mock_column(:id, :integer), is_expected.to eq expected_result
mock_column(:name, :string, limit: 50) 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_02e851e3b7', columns: ['id']),
mock_index('index_rails_02e851e3b8', mock_index('index_rails_02e851e3b8',
columns: ['foreign_thing_id'], columns: ['foreign_thing_id'],
using: 'hash') using: 'hash')
]) ]
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 Information
# ### Columns #
# # Table name: `users`
# Name | Type | Attributes #
# ----------- | ------------------ | --------------------------- # ### Columns
# **`id`** | `integer` | `not null, primary key` #
# **`name`** | `string(50)` | `not null` # Name | Type | Attributes
# # ----------- | ------------------ | ---------------------------
# ### Indexes # **`id`** | `integer` | `not null, primary key`
# # **`name`** | `string(50)` | `not null`
# * `index_rails_02e851e3b7`: #
# * **`id`** # ### Indexes
# * `index_rails_02e851e3b8` (_using_ hash): #
# * **`foreign_thing_id`** # * `index_rails_02e851e3b7`:
# # * **`id`**
EOS # * `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