Add RSpec for newly refactored CanLookup

This commit is contained in:
Ryan Lue 2018-07-05 21:24:26 +08:00
parent 901cc3fadf
commit 6ca5054d03
4 changed files with 79 additions and 1 deletions

View file

@ -0,0 +1,73 @@
require 'rails_helper'
RSpec.shared_examples 'CanLookup' do
describe '::lookup' do
let(:subject) { described_class }
let(:ensure_instance) { create(subject.to_s.downcase) if subject.none? }
let(:string_attributes) { (%i[name login email number] & subject.attribute_names.map(&:to_sym)) }
let(:non_attributes) { (%i[id name login email number] - subject.attribute_names.map(&:to_sym)) }
let(:lookup_id) { 1 }
let(:cache_key) { "#{subject}::#{lookup_id}" }
let(:cache_expiry_param) { { expires_in: 7.days } }
it 'finds records by id (like ::find_by)' do
ensure_instance
expect(subject.lookup(id: lookup_id)).to eql(subject.find_by(id: lookup_id))
end
it 'finds records by other attributes (like ::find_by)' do
ensure_instance
# don't run this example on models with no valid string attributes
if string_attributes.select! { |a| subject.pluck(a).any? }
attribute_name = string_attributes.sample
attribute_value = subject.pluck(target_attribute).sample
expect(subject.lookup(attribute_name => attribute_value))
.to eql(subject.find_by(attribute_name => attribute_value))
end
end
it 'only accepts attributes that uniquely identify the record' do
expect { subject.lookup(created_by_id: 1) }
.to raise_error(ArgumentError)
end
it 'accepts exactly one attribute-value pair' do
expect { subject.lookup(:id => lookup_id, string_attributes.sample => 'foo') }
.to raise_error(ArgumentError)
end
it 'does not accept attributes not present in model' do
expect { subject.lookup(non_attributes.sample => 'foo') }
.to raise_error(ArgumentError)
end
it 'saves results to cache' do
ensure_instance
allow(Rails.cache)
.to receive(:write)
.and_call_original
subject.lookup(id: lookup_id)
expect(Rails.cache)
.to have_received(:write)
.with(cache_key, subject.first, cache_expiry_param)
end
it 'retrieves results from cache, if stored' do
ensure_instance
allow(Rails.cache)
.to receive(:read)
.and_call_original
subject.lookup(id: lookup_id)
expect(Rails.cache)
.to have_received(:read)
.with(cache_key)
end
end
end

View file

@ -1,6 +1,8 @@
require 'rails_helper' require 'rails_helper'
require 'models/concerns/can_lookup'
RSpec.describe Organization do RSpec.describe Organization do
include_examples 'CanLookup'
context '.where_or_cis' do context '.where_or_cis' do

View file

@ -1,6 +1,8 @@
require 'rails_helper' require 'rails_helper'
require 'models/concerns/can_lookup'
RSpec.describe Ticket do RSpec.describe Ticket do
include_examples 'CanLookup'
describe '#merge_to' do describe '#merge_to' do

View file

@ -2,9 +2,9 @@ require 'rails_helper'
require 'models/concerns/has_groups_examples' require 'models/concerns/has_groups_examples'
require 'models/concerns/has_roles_examples' require 'models/concerns/has_roles_examples'
require 'models/concerns/has_groups_permissions_examples' require 'models/concerns/has_groups_permissions_examples'
require 'models/concerns/can_lookup'
RSpec.describe User do RSpec.describe User do
let(:group_access_instance) { create(:agent_user) } let(:group_access_instance) { create(:agent_user) }
let(:new_group_access_instance) { build(:agent_user) } let(:new_group_access_instance) { build(:agent_user) }
let(:group_access_no_permission_instance) { build(:user) } let(:group_access_no_permission_instance) { build(:user) }
@ -12,6 +12,7 @@ RSpec.describe User do
include_examples 'HasGroups' include_examples 'HasGroups'
include_examples 'HasRoles' include_examples 'HasRoles'
include_examples 'HasGroups and Permissions' include_examples 'HasGroups and Permissions'
include_examples 'CanLookup'
let(:new_password) { 'N3W54V3PW!' } let(:new_password) { 'N3W54V3PW!' }