From 2dd4c74be40120e6315787786c0c09ebc22345a2 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 18 Jan 2019 10:26:15 +0800 Subject: [PATCH] Refactoring: Migrate seventh test case in cti_caller_id_test.rb --- spec/factories/cti/log.rb | 2 +- spec/models/cti/log_spec.rb | 115 ++++++++++++++++++++++++-------- test/unit/cti_caller_id_test.rb | 45 ------------- 3 files changed, 87 insertions(+), 75 deletions(-) diff --git a/spec/factories/cti/log.rb b/spec/factories/cti/log.rb index b522963ad..eb9db6173 100644 --- a/spec/factories/cti/log.rb +++ b/spec/factories/cti/log.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :'cti/log', aliases: %i[cti_log] do direction { %w[in out].sample } - state { %w[newCall answer hangup].sample } + state { 'newCall' } from { '4930609854180' } to { '4930609811111' } call_id { (Cti::Log.pluck(:call_id).map(&:to_i).max || 0).next } # has SQL UNIQUE constraint diff --git a/spec/models/cti/log_spec.rb b/spec/models/cti/log_spec.rb index cd7d3916f..0ab42bc68 100644 --- a/spec/models/cti/log_spec.rb +++ b/spec/models/cti/log_spec.rb @@ -28,45 +28,102 @@ RSpec.describe Cti::Log do end describe '.process' do + let(:attributes) do + { + 'cause' => '', + 'event' => event, + 'user' => 'user 1', + 'from' => '49123456', + 'to' => '49123457', + 'call_id' => '1', + 'direction' => 'in', + } + end + context 'for event "newCall"' do - let(:attributes) do - { - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '49123456', - 'to' => '49123457', - 'callId' => '1', - 'direction' => 'in', - } - end + let(:event) { 'newCall' } - it 'creates a new Log record' do - expect { Cti::Log.process(attributes) } - .to change { Cti::Log.count }.by(1) - end + context 'with unrecognized "call_id"' do + it 'creates a new Log record (#state: "newCall", #done: false)' do + expect { Cti::Log.process(attributes) } + .to change { Cti::Log.count }.by(1) - context 'for direction "in", with a CallerId record matching the "from" number' do - let!(:caller_id) { create(:caller_id, caller_id: '49123456') } - before { attributes.merge!('direction' => 'in') } + expect(Cti::Log.last.attributes) + .to include('state' => 'newCall', 'done' => false) + end - it 'saves that CallerId’s attributes in the new Log’s #preferences[:from] attribute' do - Cti::Log.process(attributes) + context 'for direction "in", with a CallerId record matching the "from" number' do + let!(:caller_id) { create(:caller_id, caller_id: '49123456') } + before { attributes.merge!('direction' => 'in') } - expect(Cti::Log.last.preferences[:from].first) - .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone + it 'saves that CallerId’s attributes in the new Log’s #preferences[:from] attribute' do + Cti::Log.process(attributes) + + expect(Cti::Log.last.preferences[:from].first) + .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone + end + end + + context 'for direction "out", with a CallerId record matching the "to" number' do + let!(:caller_id) { create(:caller_id, caller_id: '49123457') } + before { attributes.merge!('direction' => 'out') } + + it 'saves that CallerId’s attributes in the new Log’s #preferences[:to] attribute' do + Cti::Log.process(attributes) + + expect(Cti::Log.last.preferences[:to].first) + .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone + end end end - context 'for direction "out", with a CallerId record matching the "to" number' do - let!(:caller_id) { create(:caller_id, caller_id: '49123457') } - before { attributes.merge!('direction' => 'out') } + context 'with recognized "call_id"' do + before { create(:'cti/log', call_id: '1') } - it 'saves that CallerId’s attributes in the new Log’s #preferences[:to] attribute' do - Cti::Log.process(attributes) + it 'raises an error' do + expect { Cti::Log.process(attributes) }.to raise_error(/call_id \S+ already exists!/) + end + end + end - expect(Cti::Log.last.preferences[:to].first) - .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone + context 'for event "answer"' do + let(:event) { 'answer' } + + context 'with unrecognized "call_id"' do + it 'raises an error' do + expect { Cti::Log.process(attributes) }.to raise_error(/No such call_id/) + end + end + + context 'with recognized "call_id"' do + context 'for Log with #state "hangup"' do + let(:log) { create(:'cti/log', call_id: 1, state: 'hangup', done: false) } + + it 'returns early with no changes' do + expect { Cti::Log.process(attributes) } + .not_to change { log.reload } + end + end + end + end + + context 'for event "hangup"' do + let(:event) { 'hangup' } + + context 'with unrecognized "call_id"' do + it 'raises an error' do + expect { Cti::Log.process(attributes) }.to raise_error(/No such call_id/) + end + end + + context 'with recognized "call_id"' do + context 'for Log with #state "newCall"' do + let(:log) { create(:'cti/log', call_id: 1, done: true) } + + it 'sets attributes #state: "hangup", #done: false' do + expect { Cti::Log.process(attributes) } + .to change { log.reload.state }.to('hangup').and change { log.reload.done }.to(false) + end end end end diff --git a/test/unit/cti_caller_id_test.rb b/test/unit/cti_caller_id_test.rb index 8de8e2e49..7a20c08f3 100644 --- a/test/unit/cti_caller_id_test.rb +++ b/test/unit/cti_caller_id_test.rb @@ -56,51 +56,6 @@ class CtiCallerIdTest < ActiveSupport::TestCase Scheduler.worker(true) end - test 'order of events' do - Cti::Log.process( - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => 'touch-loop-1', - 'direction' => 'in', - ) - - last = Cti::Log.last - assert_equal(last.state, 'newCall') - assert_equal(last.done, false) - - travel 2.seconds - Cti::Log.process( - 'cause' => '', - 'event' => 'hangup', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => 'touch-loop-1', - 'direction' => 'in', - ) - last.reload - assert_equal(last.state, 'hangup') - assert_equal(last.done, false) - - travel 2.seconds - Cti::Log.process( - 'cause' => '', - 'event' => 'answer', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => 'touch-loop-1', - 'direction' => 'in', - ) - last.reload - assert_equal(last.state, 'hangup') - assert_equal(last.done, false) - - end - test 'not answered should be not marked as done' do Cti::Log.process(