Refactoring: Migrate object_type_lookup_test.rb to RSpec

This commit is contained in:
Ryan Lue 2019-02-11 08:46:26 +01:00 committed by Martin Edenhofer
parent 107fd5e995
commit 7639ef46d3
5 changed files with 108 additions and 80 deletions

View file

@ -0,0 +1,5 @@
FactoryBot.define do
factory :object_lookup do
name { (ApplicationModel.descendants.map(&:name) - ObjectLookup.pluck(:name)).sample }
end
end

View file

@ -11,7 +11,7 @@ FactoryBot.define do
.dig([:base, []]) .dig([:base, []])
.merge(TypeLookup.pluck(:name)) .merge(TypeLookup.pluck(:name))
Faker::Verb.unique.base Faker::Verb.unique.past_participle
end end
end end
end end

View file

@ -0,0 +1,51 @@
require 'rails_helper'
RSpec.describe ObjectLookup, type: :model do
describe '.by_name' do
context 'with name matching an existing ObjectLookup record' do
subject(:object_lookup) { create(:object_lookup) }
it 'returns its id' do
expect(ObjectLookup.by_name(object_lookup.name))
.to eq(object_lookup.id)
end
end
context 'with name not matching any ObjectLookup records' do
let(:name) { 'FooBar' }
it 'creates a new one with that name' do
expect { ObjectLookup.by_name(name) }
.to change { ObjectLookup.count }.by(1)
expect(ObjectLookup.last.name).to eq(name)
end
it 'returns its id' do
expect(ObjectLookup.by_name(name))
.to eq(ObjectLookup.last.id)
end
context 'for names not in strict CamelCase' do
let(:name) { 'Foo_Bar' }
it 'does not modify the format' do
ObjectLookup.by_name(name)
expect(ObjectLookup.last.name).to eq(name)
end
end
end
end
describe '.by_id' do
context 'with number matching an existing ObjectLookup#id' do
subject(:object_lookup) { create(:object_lookup) }
it 'returns its name' do
expect(ObjectLookup.by_id(object_lookup.id))
.to eq(object_lookup.name)
end
end
end
end

View file

@ -0,0 +1,51 @@
require 'rails_helper'
RSpec.describe TypeLookup, type: :model do
describe '.by_name' do
context 'with name matching an existing TypeLookup record' do
subject(:type_lookup) { create(:type_lookup) }
it 'returns its id' do
expect(TypeLookup.by_name(type_lookup.name))
.to eq(type_lookup.id)
end
end
context 'with name not matching any TypeLookup records' do
let(:name) { 'FooBar' }
it 'creates a new one with that name' do
expect { TypeLookup.by_name(name) }
.to change { TypeLookup.count }.by(1)
expect(TypeLookup.last.name).to eq(name)
end
it 'returns its id' do
expect(TypeLookup.by_name(name))
.to eq(TypeLookup.last.id)
end
context 'for names not in strict CamelCase' do
let(:name) { 'Foo_Bar' }
it 'does not modify the format' do
TypeLookup.by_name(name)
expect(TypeLookup.last.name).to eq(name)
end
end
end
end
describe '.by_id' do
context 'with number matching an existing TypeLookup#id' do
subject(:type_lookup) { create(:type_lookup) }
it 'returns its name' do
expect(TypeLookup.by_id(type_lookup.id))
.to eq(type_lookup.name)
end
end
end
end

View file

@ -1,79 +0,0 @@
require 'test_helper'
class ObjectTypeLookupTest < ActiveSupport::TestCase
test 'object tests' do
object_lookup_id = ObjectLookup.by_name( 'SomeObject' )
assert( object_lookup_id, 'first by_name' )
object_lookup_name = ObjectLookup.by_id( object_lookup_id )
assert( object_lookup_name, 'first by_id' )
assert_equal( object_lookup_name, 'SomeObject' )
object_lookup_id2 = ObjectLookup.by_name( 'Some_Object' )
assert( object_lookup_id2, 'by_name - Some_Object' )
object_lookup_name2 = ObjectLookup.by_id( object_lookup_id2 )
assert( object_lookup_name2, 'by_id - Some_Object' )
assert_equal( object_lookup_name2, 'Some_Object' )
object_lookup_id3 = ObjectLookup.by_name( 'SomeObject' )
assert( object_lookup_id3, 'by_name 2 - SomeObject' )
object_lookup_name3 = ObjectLookup.by_id( object_lookup_id3 )
assert( object_lookup_name3, 'by_id 2 - SomeObject' )
assert_equal( object_lookup_name3, 'SomeObject' )
assert_equal( object_lookup_name3, object_lookup_name, 'SomeObject' )
assert_equal( object_lookup_id3, object_lookup_id, 'SomeObject' )
end
test 'type tests' do
type_lookup_id = TypeLookup.by_name( 'SomeType' )
assert( type_lookup_id, 'first by_name' )
type_lookup_name = TypeLookup.by_id( type_lookup_id )
assert( type_lookup_name, 'first by_id' )
assert_equal( type_lookup_name, 'SomeType' )
type_lookup_id2 = TypeLookup.by_name( 'Some_Type' )
assert( type_lookup_id2, 'by_name - Some_Type' )
type_lookup_name2 = TypeLookup.by_id( type_lookup_id2 )
assert( type_lookup_name2, 'by_id - Some_Type' )
assert_equal( type_lookup_name2, 'Some_Type' )
type_lookup_id3 = TypeLookup.by_name( 'SomeType' )
assert( type_lookup_id3, 'by_name 2 - SomeType' )
type_lookup_name3 = TypeLookup.by_id( type_lookup_id3 )
assert( type_lookup_name3, 'by_id 2 - SomeType' )
assert_equal( type_lookup_name3, 'SomeType' )
assert_equal( type_lookup_name3, type_lookup_name, 'SomeType' )
assert_equal( type_lookup_id3, type_lookup_id, 'SomeType' )
end
test 'type and object tests' do
object_lookup_id = ObjectLookup.by_name( 'SomeObject' )
assert( object_lookup_id, 'first by_name' )
object_lookup_name = ObjectLookup.by_id( object_lookup_id )
assert( object_lookup_name, 'first by_id' )
assert_equal( object_lookup_name, 'SomeObject' )
type_lookup_id = TypeLookup.by_name( 'SomeType' )
assert( type_lookup_id, 'first by_name' )
type_lookup_name = TypeLookup.by_id( type_lookup_id )
assert( type_lookup_name, 'first by_id' )
assert_equal( type_lookup_name, 'SomeType' )
assert_not_equal( object_lookup_name, type_lookup_name, 'verify lookups' )
end
end