From 31adff6a425342891cd3ae27ba3b5a1d04a3549e Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 15 Feb 2019 13:00:27 +0800 Subject: [PATCH] Refactoring: Migrate ticket_article_time_accounting_test to RSpec --- spec/factories/ticket/time_accounting.rb | 6 +- spec/models/ticket/time_accounting_spec.rb | 27 ++++ .../ticket_article_time_accouting_test.rb | 117 ------------------ 3 files changed, 32 insertions(+), 118 deletions(-) create mode 100644 spec/models/ticket/time_accounting_spec.rb delete mode 100644 test/unit/ticket_article_time_accouting_test.rb diff --git a/spec/factories/ticket/time_accounting.rb b/spec/factories/ticket/time_accounting.rb index 18aab839b..faca6270f 100644 --- a/spec/factories/ticket/time_accounting.rb +++ b/spec/factories/ticket/time_accounting.rb @@ -1,7 +1,11 @@ FactoryBot.define do - factory :ticket_time_accounting, class: Ticket::TimeAccounting do + factory :'ticket/time_accounting', aliases: %i[ticket_time_accounting] do ticket time_unit { rand(100) } created_by_id 1 + + trait :for_article do + ticket_article { create(:'ticket/article', ticket: ticket) } + end end end diff --git a/spec/models/ticket/time_accounting_spec.rb b/spec/models/ticket/time_accounting_spec.rb new file mode 100644 index 000000000..3b65d57c7 --- /dev/null +++ b/spec/models/ticket/time_accounting_spec.rb @@ -0,0 +1,27 @@ +require 'rails_helper' + +RSpec.describe Ticket::TimeAccounting, type: :model do + subject(:time_accounting) { create(:'ticket/time_accounting') } + + describe 'Associations:' do + describe '#ticket_article' do + subject!(:time_accounting) { create(:'ticket/time_accounting', :for_article) } + + context 'when destroyed' do + it 'destroys self' do + expect { time_accounting.ticket_article.destroy } + .to change { time_accounting.persisted? }.to(false) + .and change { Ticket::TimeAccounting.count }.by(-1) + end + + it 'does not destroy other TimeAccountings for same ticket' do + create(:'ticket/time_accounting', ticket: time_accounting.ticket) + create(:'ticket/time_accounting', :for_article, ticket: time_accounting.ticket) + + expect { time_accounting.ticket_article.destroy } + .to change { Ticket::TimeAccounting.count }.by(-1) + end + end + end + end +end diff --git a/test/unit/ticket_article_time_accouting_test.rb b/test/unit/ticket_article_time_accouting_test.rb deleted file mode 100644 index e59990b04..000000000 --- a/test/unit/ticket_article_time_accouting_test.rb +++ /dev/null @@ -1,117 +0,0 @@ -require 'test_helper' - -class TicketArticleTimeAccoutingTest < ActiveSupport::TestCase - - test 'destroy dependent time accounting for ticket and article' do - ticket_test = Ticket.create( - title: 'com test 1', - group: Group.lookup(name: 'Users'), - customer_id: 2, - state: Ticket::State.lookup(name: 'new'), - priority: Ticket::Priority.lookup(name: '2 normal'), - updated_by_id: 1, - created_by_id: 1, - ) - assert(ticket_test, 'ticket created') - - article_test1 = Ticket::Article.create( - ticket_id: ticket_test.id, - from: 'some_customer_com-1@example.com', - to: 'some_zammad_com-1@example.com', - subject: 'com test 1', - message_id: 'some@id_com_1', - body: 'some message 123', - internal: false, - sender: Ticket::Article::Sender.find_by(name: 'Customer'), - type: Ticket::Article::Type.find_by(name: 'email'), - updated_by_id: 1, - created_by_id: 1, - ) - - Ticket::TimeAccounting.create!( - ticket_id: ticket_test.id, - ticket_article_id: article_test1.id, - time_unit: 10, - created_by_id: 1, - ) - - article_test2 = Ticket::Article.create( - ticket_id: ticket_test.id, - from: 'some_customer_com-1@example.com', - to: 'some_zammad_com-1@example.com', - subject: 'com test 1', - message_id: 'some@id_com_1', - body: 'some message 123', - internal: false, - sender: Ticket::Article::Sender.find_by(name: 'Customer'), - type: Ticket::Article::Type.find_by(name: 'email'), - updated_by_id: 1, - created_by_id: 1, - ) - - Ticket::TimeAccounting.create!( - ticket_id: ticket_test.id, - ticket_article_id: article_test2.id, - time_unit: 10, - created_by_id: 1, - ) - - article_test3 = Ticket::Article.create( - ticket_id: ticket_test.id, - from: 'some_customer_com-1@example.com', - to: 'some_zammad_com-1@example.com', - subject: 'com test 1', - message_id: 'some@id_com_1', - body: 'some message 123', - internal: false, - sender: Ticket::Article::Sender.find_by(name: 'Customer'), - type: Ticket::Article::Type.find_by(name: 'email'), - updated_by_id: 1, - created_by_id: 1, - ) - - Ticket::TimeAccounting.create!( - ticket_id: ticket_test.id, - ticket_article_id: article_test3.id, - time_unit: 10, - created_by_id: 1, - ) - - Ticket::TimeAccounting.create!( - ticket_id: ticket_test.id, - time_unit: 10, - created_by_id: 1, - ) - - time_accouting_list = Ticket::TimeAccounting.where( - ticket_id: ticket_test.id, - ) - - found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } - assert_equal(3, found.count) - - article_test1.destroy - - time_accouting_list.reload - found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } - assert_equal(2, found.count) - - article_test2.destroy - - time_accouting_list.reload - found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } - assert_equal(1, found.count) - - article_test3.destroy - - time_accouting_list.reload - found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } - assert_equal(0, found.count) - - # one accouting left for the ticket only - assert_equal(1, time_accouting_list.count) - ticket_test.destroy - time_accouting_list.reload - assert_equal(0, time_accouting_list.count) - end -end