Refactoring: Migrate seventh test case in cti_caller_id_test.rb

This commit is contained in:
Ryan Lue 2019-01-18 10:26:15 +08:00 committed by Martin Edenhofer
parent 84d0bf605b
commit 2dd4c74be4
3 changed files with 87 additions and 75 deletions

View file

@ -1,7 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory :'cti/log', aliases: %i[cti_log] do factory :'cti/log', aliases: %i[cti_log] do
direction { %w[in out].sample } direction { %w[in out].sample }
state { %w[newCall answer hangup].sample } state { 'newCall' }
from { '4930609854180' } from { '4930609854180' }
to { '4930609811111' } to { '4930609811111' }
call_id { (Cti::Log.pluck(:call_id).map(&:to_i).max || 0).next } # has SQL UNIQUE constraint call_id { (Cti::Log.pluck(:call_id).map(&:to_i).max || 0).next } # has SQL UNIQUE constraint

View file

@ -28,22 +28,28 @@ RSpec.describe Cti::Log do
end end
describe '.process' do describe '.process' do
context 'for event "newCall"' do
let(:attributes) do let(:attributes) do
{ {
'cause' => '', 'cause' => '',
'event' => 'newCall', 'event' => event,
'user' => 'user 1', 'user' => 'user 1',
'from' => '49123456', 'from' => '49123456',
'to' => '49123457', 'to' => '49123457',
'callId' => '1', 'call_id' => '1',
'direction' => 'in', 'direction' => 'in',
} }
end end
it 'creates a new Log record' do context 'for event "newCall"' do
let(:event) { 'newCall' }
context 'with unrecognized "call_id"' do
it 'creates a new Log record (#state: "newCall", #done: false)' do
expect { Cti::Log.process(attributes) } expect { Cti::Log.process(attributes) }
.to change { Cti::Log.count }.by(1) .to change { Cti::Log.count }.by(1)
expect(Cti::Log.last.attributes)
.to include('state' => 'newCall', 'done' => false)
end end
context 'for direction "in", with a CallerId record matching the "from" number' do context 'for direction "in", with a CallerId record matching the "from" number' do
@ -70,6 +76,57 @@ RSpec.describe Cti::Log do
end end
end end
end end
context 'with recognized "call_id"' do
before { create(:'cti/log', call_id: '1') }
it 'raises an error' do
expect { Cti::Log.process(attributes) }.to raise_error(/call_id \S+ already exists!/)
end
end
end
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
end end
describe 'Callbacks -' do describe 'Callbacks -' do

View file

@ -56,51 +56,6 @@ class CtiCallerIdTest < ActiveSupport::TestCase
Scheduler.worker(true) Scheduler.worker(true)
end 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 test 'not answered should be not marked as done' do
Cti::Log.process( Cti::Log.process(