Refactoring: Migrate second test case in cti_caller_test.rb

This commit is contained in:
Ryan Lue 2019-01-17 14:57:22 +08:00 committed by Martin Edenhofer
parent 9bb1edea69
commit 39f769aa13
2 changed files with 64 additions and 99 deletions

View file

@ -102,22 +102,43 @@ RSpec.describe Cti::CallerId do
end end
context 'when given a recognized number' do context 'when given a recognized number' do
subject!(:caller_id) { create(:caller_id) } subject!(:caller_id) { create(:caller_id, caller_id: number) }
let(:number) { '1234567890' }
it 'returns an array with the corresponding CallerId' do it 'returns an array with the corresponding CallerId' do
expect(Cti::CallerId.lookup(caller_id.caller_id)) expect(Cti::CallerId.lookup(number)).to match_array([caller_id])
.to match_array([caller_id])
end end
context 'shared by multiple CallerIds' do context 'shared by multiple CallerIds' do
context '(for different users)' do
subject!(:caller_ids) do subject!(:caller_ids) do
User.last(2).map { |u| create(:caller_id, caller_id: '1234567890', user: u) } [create(:caller_id, caller_id: number, user: User.last),
create(:caller_id, caller_id: number, user: User.offset(1).last)]
end end
# NOTE: this only works if the CallerId records are for distinct Users. it 'returns all corresponding CallerId records' do
# Not sure if that's necessary to the spec, though. expect(Cti::CallerId.lookup(number)).to match_array(caller_ids)
it 'returns all CallerId records with that number' do end
expect(Cti::CallerId.lookup('1234567890')).to match_array(caller_ids) end
context '(for the same user)' do
subject!(:caller_ids) { create_list(:caller_id, 2, caller_id: number) }
it 'returns one corresponding CallerId record by MAX(id)' do
expect(Cti::CallerId.lookup(number)).to match_array(caller_ids.last(1))
end
end
context '(some for the same user, some for another)' do
subject!(:caller_ids) do
[create(:caller_id, caller_id: number, user: User.last),
create(:caller_id, caller_id: number, user: User.last),
create(:caller_id, caller_id: number, user: User.offset(1).last)]
end
it 'returns one CallerId record per unique #user_id, by MAX(id)' do
expect(Cti::CallerId.lookup(number)).to match_array(caller_ids.last(2))
end
end end
end end
end end
@ -194,6 +215,40 @@ RSpec.describe Cti::CallerId do
end end
end end
describe '.maybe_add' do
let(:attributes) { attributes_for(:caller_id) }
it 'wraps .find_or_initialize_by (passing only five defining attributes)' do
expect(described_class)
.to receive(:find_or_initialize_by)
.with(attributes.slice(:caller_id, :level, :object, :o_id, :user_id))
.and_call_original
Cti::CallerId.maybe_add(attributes)
end
context 'if no matching record found' do
it 'adds given #comment attribute' do
expect { Cti::CallerId.maybe_add(attributes.merge(comment: 'foo')) }
.to change { Cti::CallerId.count }.by(1)
expect(Cti::CallerId.last.comment).to eq('foo')
end
end
context 'if matching record found' do
let(:attributes) { caller_id.attributes.symbolize_keys }
let(:caller_id) { create(:caller_id) }
it 'ignores given #comment attribute' do
expect(Cti::CallerId.maybe_add(attributes.merge(comment: 'foo')))
.to eq(caller_id)
expect(caller_id.comment).to be_blank
end
end
end
describe 'callbacks' do describe 'callbacks' do
subject!(:caller_id) { build(:cti_caller_id, caller_id: phone) } subject!(:caller_id) { build(:cti_caller_id, caller_id: phone) }
let(:phone) { '1234567890' } let(:phone) { '1234567890' }

View file

@ -55,96 +55,6 @@ class CtiCallerIdTest < ActiveSupport::TestCase
Scheduler.worker(true) Scheduler.worker(true)
end end
test '2 lookups' do
Cti::CallerId.destroy_all
Cti::CallerId.maybe_add(
caller_id: '4999999999',
level: 'maybe',
user_id: 2,
object: 'Ticket',
o_id: 2,
)
Cti::CallerId.maybe_add(
caller_id: '4912345678901',
comment: 'Hairdresser Bob Smith, San Francisco',
level: 'public',
user_id: 2,
object: 'GoYello',
o_id: 1,
)
caller_ids = Cti::CallerId.lookup('4912345678901')
assert_equal(1, caller_ids.length)
assert_equal('public', caller_ids[0].level)
assert_equal(2, caller_ids[0].user_id)
assert_equal('Hairdresser Bob Smith, San Francisco', caller_ids[0].comment)
Cti::CallerId.maybe_add(
caller_id: '4912345678901',
level: 'maybe',
user_id: 2,
object: 'Ticket',
o_id: 2,
)
caller_ids = Cti::CallerId.lookup('4912345678901')
assert_equal(1, caller_ids.length)
assert_equal('maybe', caller_ids[0].level)
assert_equal(2, caller_ids[0].user_id)
assert_nil(caller_ids[0].comment)
Cti::CallerId.maybe_add(
caller_id: '4912345678901',
level: 'maybe',
user_id: 2,
object: 'Ticket',
o_id: 2,
)
caller_ids = Cti::CallerId.lookup('4912345678901')
assert_equal(1, caller_ids.length)
assert_equal('maybe', caller_ids[0].level)
assert_equal(2, caller_ids[0].user_id)
assert_nil(caller_ids[0].comment)
user_id = User.find_by(login: 'ticket-caller_id-customer1@example.com').id
Cti::CallerId.maybe_add(
caller_id: '4912345678901',
level: 'maybe',
user_id: user_id,
object: 'Ticket',
o_id: 2,
)
caller_ids = Cti::CallerId.lookup('4912345678901')
assert_equal(2, caller_ids.length)
assert_equal('maybe', caller_ids[0].level)
assert_equal(user_id, caller_ids[0].user_id)
assert_nil(caller_ids[0].comment)
assert_equal('maybe', caller_ids[1].level)
assert_equal(2, caller_ids[1].user_id)
assert_nil(caller_ids[1].comment)
Cti::CallerId.maybe_add(
caller_id: '4912345678901',
level: 'known',
user_id: user_id,
object: 'User',
o_id: 2,
)
caller_ids = Cti::CallerId.lookup('4912345678901')
assert_equal(1, caller_ids.length)
assert_equal('known', caller_ids[0].level)
assert_equal(user_id, caller_ids[0].user_id)
assert_nil(caller_ids[0].comment)
end
test '3 process - log' do test '3 process - log' do
ticket1 = Ticket.create!( ticket1 = Ticket.create!(