Skip to content

Commit

Permalink
Refactor RSpec for AnnotateModels - with Globalize gem (ctran#749)
Browse files Browse the repository at this point in the history
Refactor test cases for AnnotateModels.get_schema_info with Globalize gem
  • Loading branch information
nard-tech authored and vfonic committed May 8, 2020
1 parent f0bc83d commit 15320d1
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,49 @@ def mock_column(name, type, options = {})
end
end
end

context 'with Globalize gem' do
let :translation_klass do
double('Post::Translation',
to_s: 'Post::Translation',
columns: [
mock_column(:id, :integer, limit: 8),
mock_column(:post_id, :integer, limit: 8),
mock_column(:locale, :string, limit: 50),
mock_column(:title, :string, limit: 50),
])
end

let :klass do
mock_class(:posts, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
allow(mock_klass).to receive(:translation_class).and_return(translation_klass)
end
end

let :columns do
[
mock_column(:id, :integer, limit: 8),
mock_column(:author_name, :string, limit: 50),
]
end

let :expected_result do
<<~EOS
# Schema Info
#
# Table name: posts
#
# id :integer not null, primary key
# author_name :string(50) not null
# title :string(50) not null
#
EOS
end

it 'returns schema info' do
is_expected.to eq expected_result
end
end
end

context 'when the primary key is an array (using composite_primary_keys)' do
Expand Down Expand Up @@ -1140,39 +1183,6 @@ def mock_column(name, type, options = {})
end
end

it 'should work with the Globalize gem' do
klass = mock_class(:posts,
:id,
[
mock_column(:id, :integer, limit: 8),
mock_column(:author_name, :string, limit: 50),
])

options = {
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),
]
}
translation_klass = double('Post::Translation', options)
allow(klass).to receive(:translation_class).and_return(translation_klass)

expected_schema_info = <<~EOS
# Schema Info
#
# Table name: posts
#
# id :integer not null, primary key
# author_name :string(50) not null
# title :string(50) not null
#
EOS
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(expected_schema_info)
end

describe '.set_defaults' do
subject do
Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
Expand Down

0 comments on commit 15320d1

Please sign in to comment.