diff --git a/spec/factories/object_lookup.rb b/spec/factories/object_lookup.rb new file mode 100644 index 000000000..c958bf4f3 --- /dev/null +++ b/spec/factories/object_lookup.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :object_lookup do + name { (ApplicationModel.descendants.map(&:name) - ObjectLookup.pluck(:name)).sample } + end +end diff --git a/spec/factories/type_lookup.rb b/spec/factories/type_lookup.rb index eccd1d79b..9a5ae2aec 100644 --- a/spec/factories/type_lookup.rb +++ b/spec/factories/type_lookup.rb @@ -11,7 +11,7 @@ FactoryBot.define do .dig([:base, []]) .merge(TypeLookup.pluck(:name)) - Faker::Verb.unique.base + Faker::Verb.unique.past_participle end end end diff --git a/spec/models/object_lookup_spec.rb b/spec/models/object_lookup_spec.rb new file mode 100644 index 000000000..6e6f5f64f --- /dev/null +++ b/spec/models/object_lookup_spec.rb @@ -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 diff --git a/spec/models/type_lookup_spec.rb b/spec/models/type_lookup_spec.rb new file mode 100644 index 000000000..71ad9e7ba --- /dev/null +++ b/spec/models/type_lookup_spec.rb @@ -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 diff --git a/test/unit/object_type_lookup_test.rb b/test/unit/object_type_lookup_test.rb deleted file mode 100644 index 18f7bd5eb..000000000 --- a/test/unit/object_type_lookup_test.rb +++ /dev/null @@ -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