Browse Source

Refactoring: Migrate seventh test case in cti_caller_id_test.rb

Ryan Lue 6 years ago
parent
commit
2dd4c74be4
3 changed files with 85 additions and 73 deletions
  1. 1 1
      spec/factories/cti/log.rb
  2. 84 27
      spec/models/cti/log_spec.rb
  3. 0 45
      test/unit/cti_caller_id_test.rb

+ 1 - 1
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

+ 84 - 27
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',
-        }
+      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) }
+            .to change { Cti::Log.count }.by(1)
+
+          expect(Cti::Log.last.attributes)
+            .to include('state' => 'newCall', 'done' => false)
+        end
+
+        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') }
+
+          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
 
-      it 'creates a new Log record' do
-        expect { Cti::Log.process(attributes) }
-          .to change { Cti::Log.count }.by(1)
+      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 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') }
+    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
 
-        it 'saves that CallerId’s attributes in the new Log’s #preferences[:from] attribute' do
-          Cti::Log.process(attributes)
+      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) }
 
-          expect(Cti::Log.last.preferences[:from].first)
-            .to include(caller_id.attributes.except('created_at'))  # Checking equality of Time objects is error-prone
+          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 '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 unrecognized "call_id"' do
+        it 'raises an error' do
+          expect { Cti::Log.process(attributes) }.to raise_error(/No such call_id/)
+        end
+      end
 
-        it 'saves that CallerId’s attributes in the new Log’s #preferences[:to] attribute' do
-          Cti::Log.process(attributes)
+      context 'with recognized "call_id"' do
+        context 'for Log with #state "newCall"' do
+          let(:log) { create(:'cti/log', call_id: 1, done: true) }
 
-          expect(Cti::Log.last.preferences[:to].first)
-            .to include(caller_id.attributes.except('created_at'))  # Checking equality of Time objects is error-prone
+          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

+ 0 - 45
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(