From 0ab78d9f1c3327e1a8f4c1fc75a33b0ccb44d380 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Thu, 14 Feb 2019 17:44:03 +0800 Subject: [PATCH] Refactoring: Migrate ticket_state_test to RSpec --- spec/models/ticket/state_spec.rb | 189 +++++++++++++++++++++++++++++-- test/unit/ticket_state_test.rb | 96 ---------------- 2 files changed, 180 insertions(+), 105 deletions(-) delete mode 100644 test/unit/ticket_state_test.rb diff --git a/spec/models/ticket/state_spec.rb b/spec/models/ticket/state_spec.rb index 6635c400d..e07f08639 100644 --- a/spec/models/ticket/state_spec.rb +++ b/spec/models/ticket/state_spec.rb @@ -6,17 +6,188 @@ RSpec.describe Ticket::State, type: :model do it_behaves_like 'ApplicationModel' it_behaves_like 'CanBeImported' - describe '.by_category' do - it 'looks up states by category' do - expect(described_class.by_category(:open)) - .to be_an(ActiveRecord::Relation) - .and include(instance_of(Ticket::State)) + describe 'Default state' do + describe 'of whole table:' do + it 'has seven records' do + expect(Ticket::State.pluck(:name)) + .to match_array(%w[closed merged new open pending\ close pending\ reminder removed]) + end end - context 'with invalid category name' do - it 'raises RuntimeError' do - expect { described_class.by_category(:invalidcategoryname) } - .to raise_error(RuntimeError) + describe 'of "new" state:' do + it 'is the sole #default_create state' do + expect(Ticket::State.where(default_create: true)) + .to match_array([Ticket::State.find_by(name: 'new')]) + end + end + + describe 'of "open" state:' do + it 'is the sole #default_follow_up state' do + expect(Ticket::State.where(default_follow_up: true)) + .to match_array([Ticket::State.find_by(name: 'open')]) + end + end + end + + describe 'Class methods:' do + describe '.by_category' do + it 'looks up states by category' do + expect(described_class.by_category(:open)) + .to be_an(ActiveRecord::Relation) + .and include(instance_of(Ticket::State)) + end + + context 'with invalid category name' do + it 'raises RuntimeError' do + expect { described_class.by_category(:invalidcategoryname) } + .to raise_error(RuntimeError) + end + end + end + end + + describe 'Attributes:' do + describe '#default_create' do + let!(:original_default) { Ticket::State.find_by(default_create: true) } + + context 'for newly created record' do + subject!(:state) { build(:ticket_state, default_create: default_create) } + + context 'when true' do + let(:default_create) { true } + + it 'unsets previous default' do + expect { state.save } + .to change { original_default.reload.default_create }.to(false) + .and not_change { Ticket::State.where(default_create: true).count } + end + end + + context 'when false' do + let(:default_create) { false } + + it 'does not alter existing default' do + expect { state.save } + .to not_change { Ticket::State.find_by(default_create: true) } + .and not_change { Ticket::State.where(default_create: true).count } + end + end + end + + context 'for existing record' do + subject!(:state) { create(:ticket_state, default_create: default_create) } + + context 'when true' do + let(:default_create) { true } + + context 'and updated to false' do + it 'assigns Ticket::State.first as default' do + expect { state.update(default_create: false) } + .to change { Ticket::State.first.default_create }.to(true) + .and not_change { Ticket::State.where(default_create: true).count } + end + end + + context 'and destroyed' do + it 'assigns Ticket::State.first as default' do + expect { state.destroy } + .to change { Ticket::State.first.default_create }.to(true) + .and not_change { Ticket::State.where(default_create: true).count } + end + end + end + + context 'when false' do + let(:default_create) { false } + + context 'and updated to true' do + it 'unsets previous default' do + expect { state.update(default_create: true) } + .to change { original_default.reload.default_create }.to(false) + .and not_change { Ticket::State.where(default_create: true).count } + end + end + + context 'and destroyed' do + it 'does not alter existing default' do + expect { state.destroy } + .to not_change { Ticket::State.find_by(default_create: true) } + .and not_change { Ticket::State.where(default_create: true).count } + end + end + end + end + end + + describe '#default_follow_up' do + let!(:original_default) { Ticket::State.find_by(default_follow_up: true) } + + context 'for newly created record' do + subject!(:state) { build(:ticket_state, default_follow_up: default_follow_up) } + + context 'when true' do + let(:default_follow_up) { true } + + it 'unsets previous default' do + expect { state.save } + .to change { original_default.reload.default_follow_up }.to(false) + .and not_change { Ticket::State.where(default_follow_up: true).count } + end + end + + context 'when false' do + let(:default_follow_up) { false } + + it 'does not alter existing default' do + expect { state.save } + .to not_change { Ticket::State.find_by(default_follow_up: true) } + .and not_change { Ticket::State.where(default_follow_up: true).count } + end + end + end + + context 'for existing record' do + subject!(:state) { create(:ticket_state, default_follow_up: default_follow_up) } + + context 'when true' do + let(:default_follow_up) { true } + + context 'and updated to false' do + it 'assigns Ticket::State.first as default' do + expect { state.update(default_follow_up: false) } + .to change { Ticket::State.first.default_follow_up }.to(true) + .and not_change { Ticket::State.where(default_follow_up: true).count } + end + end + + context 'and destroyed' do + it 'assigns Ticket::State.first as default' do + expect { state.destroy } + .to change { Ticket::State.first.default_follow_up }.to(true) + .and not_change { Ticket::State.where(default_follow_up: true).count } + end + end + end + + context 'when false' do + let(:default_follow_up) { false } + + context 'and updated to true' do + it 'unsets previous default' do + expect { state.update(default_follow_up: true) } + .to change { original_default.reload.default_follow_up }.to(false) + .and not_change { Ticket::State.where(default_follow_up: true).count } + end + end + + context 'and destroyed' do + it 'does not alter existing default' do + expect { state.destroy } + .to not_change { Ticket::State.find_by(default_follow_up: true) } + .and not_change { Ticket::State.where(default_follow_up: true).count } + end + end + end end end end diff --git a/test/unit/ticket_state_test.rb b/test/unit/ticket_state_test.rb deleted file mode 100644 index 8ac6fecb8..000000000 --- a/test/unit/ticket_state_test.rb +++ /dev/null @@ -1,96 +0,0 @@ -require 'test_helper' - -class TicketStateTest < ActiveSupport::TestCase - - test 'base' do - - # check current state - assert_equal(1, Ticket::State.where(default_create: true).count) - assert_equal(1, Ticket::State.where(default_follow_up: true).count) - state_create = Ticket::State.find_by(default_create: true) - state_follow_up = Ticket::State.find_by(default_follow_up: true) - - # add new state - state_new2 = Ticket::State.create_if_not_exists( - name: 'new 2', - state_type_id: Ticket::StateType.find_by(name: 'new').id, - updated_by_id: 1, - created_by_id: 1, - ) - - state_follow_up2 = Ticket::State.create_if_not_exists( - name: 'open 2', - state_type_id: Ticket::StateType.find_by(name: 'open').id, - updated_by_id: 1, - created_by_id: 1, - ) - - # verify states - assert_equal(1, Ticket::State.where(default_create: true).count) - assert_equal(1, Ticket::State.where(default_follow_up: true).count) - assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id) - assert_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id) - - # cleanup - state_new2.destroy - state_follow_up2.destroy - - # verify states - assert_equal(1, Ticket::State.where(default_create: true).count) - assert_equal(1, Ticket::State.where(default_follow_up: true).count) - assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id) - assert_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id) - - # add new state - state_new3 = Ticket::State.create_if_not_exists( - name: 'new 3', - state_type_id: Ticket::StateType.find_by(name: 'new').id, - default_create: true, - updated_by_id: 1, - created_by_id: 1, - ) - - state_follow_up3 = Ticket::State.create_if_not_exists( - name: 'open 3', - state_type_id: Ticket::StateType.find_by(name: 'open').id, - default_follow_up: true, - updated_by_id: 1, - created_by_id: 1, - ) - - # verify states - assert_equal(1, Ticket::State.where(default_create: true).count) - assert_equal(1, Ticket::State.where(default_follow_up: true).count) - assert_not_equal(state_create.id, Ticket::State.find_by(default_create: true).id) - assert_equal(state_new3.id, Ticket::State.find_by(default_create: true).id) - assert_not_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id) - assert_equal(state_follow_up3.id, Ticket::State.find_by(default_follow_up: true).id) - - # cleanup - state_new3.destroy - state_follow_up3.destroy - - # verify states - assert_equal(1, Ticket::State.where(default_create: true).count) - assert_equal(1, Ticket::State.where(default_follow_up: true).count) - assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id) - assert_not_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id) - - # cleanup - state_create.reload - state_create.default_create = true - state_create.save! - - state_follow_up.reload - state_follow_up.default_follow_up = true - state_follow_up.save! - - # verify states - assert_equal(1, Ticket::State.where(default_create: true).count) - assert_equal(1, Ticket::State.where(default_follow_up: true).count) - assert_equal(state_create.id, Ticket::State.find_by(default_create: true).id) - assert_equal(state_follow_up.id, Ticket::State.find_by(default_follow_up: true).id) - - end - -end