2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-02-11 07:46:26 +00:00
|
|
|
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
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.by_name(object_lookup.name))
|
2019-02-11 07:46:26 +00:00
|
|
|
.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
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.by_name(name) }
|
|
|
|
.to change(described_class, :count).by(1)
|
2019-02-11 07:46:26 +00:00
|
|
|
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.last.name).to eq(name)
|
2019-02-11 07:46:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns its id' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.by_name(name))
|
|
|
|
.to eq(described_class.last.id)
|
2019-02-11 07:46:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for names not in strict CamelCase' do
|
|
|
|
let(:name) { 'Foo_Bar' }
|
|
|
|
|
|
|
|
it 'does not modify the format' do
|
2019-09-16 15:04:17 +00:00
|
|
|
described_class.by_name(name)
|
2019-02-11 07:46:26 +00:00
|
|
|
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.last.name).to eq(name)
|
2019-02-11 07:46:26 +00:00
|
|
|
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
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.by_id(object_lookup.id))
|
2019-02-11 07:46:26 +00:00
|
|
|
.to eq(object_lookup.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|