From 4dc7d64fe6319586ac1071e8a39d6c97c0258319 Mon Sep 17 00:00:00 2001 From: Enrico Stano Date: Fri, 25 May 2018 17:39:09 +0200 Subject: [PATCH] Enqueue PushNotification background job on Post persistence --- app/services/persister/post_persister.rb | 12 +++++- .../services/persister/post_persister_spec.rb | 43 +++++++++++++++---- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/services/persister/post_persister.rb b/app/services/persister/post_persister.rb index 0c0bb8004..d4f0a027b 100644 --- a/app/services/persister/post_persister.rb +++ b/app/services/persister/post_persister.rb @@ -10,6 +10,7 @@ def save ::ActiveRecord::Base.transaction do post.save! create_save_event! + enqueue_push_notification_job! post end rescue ActiveRecord::RecordInvalid => _exception @@ -20,6 +21,7 @@ def update_attributes(params) ::ActiveRecord::Base.transaction do post.update_attributes!(params) create_update_event! + enqueue_push_notification_job! post end rescue ActiveRecord::RecordInvalid => _exception @@ -28,12 +30,18 @@ def update_attributes(params) private + attr_accessor :event + def create_save_event! - ::Event.create! action: :created, post: post + @event = ::Event.create! action: :created, post: post end def create_update_event! - ::Event.create! action: :updated, post: post + @event = ::Event.create! action: :updated, post: post + end + + def enqueue_push_notification_job! + CreatePushNotificationsJob.perform_later(event_id: event.id) end end end diff --git a/spec/services/persister/post_persister_spec.rb b/spec/services/persister/post_persister_spec.rb index 6ae8139b6..852f9059f 100644 --- a/spec/services/persister/post_persister_spec.rb +++ b/spec/services/persister/post_persister_spec.rb @@ -14,30 +14,57 @@ ) end let(:persister) { ::Persister::PostPersister.new(post) } + let(:event) { Fabricate.build(:event, id: 27) } describe '#save' do - before { persister.save } - it 'saves the post' do + persister.save + expect(post).to be_persisted end - # TODO: write better expectation it 'creates an event' do - expect(Event.where(post_id: post.id).first.action).to eq('created') + expect(::Event).to receive(:create!).with(action: :created, post: post).and_return(event) + + persister.save + end + + context 'background job' do + before do + ActiveJob::Base.queue_adapter = :test + allow(::Event).to receive(:create!).and_return(event) + persister.save + end + + it 'enqueues a CreatePushNotificationsJob background job' do + expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27) + end end end describe '#update_attributes' do - before { persister.update_attributes(title: 'New title') } - it 'updates the resource attributes' do + persister.update_attributes(title: 'New title') + expect(post.title).to eq('New title') end - # TODO: write better expectation it 'creates an event' do - expect(Event.where(post_id: post.id).first.action).to eq('updated') + expect(::Event).to receive(:create!).with(action: :updated, post: post).and_return(event) + + persister.update_attributes(title: 'New title') + end + + context 'background job' do + before do + ActiveJob::Base.queue_adapter = :test + allow(::Event).to receive(:create!).and_return(event) + persister.update_attributes(title: 'New title') + end + + it 'enqueues a CreatePushNotificationsJob background job' do + expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27) + end end end end