diff --git a/app/workers/articles/update_page_views_worker.rb b/app/workers/articles/update_page_views_worker.rb index 622c8270c..574983d30 100644 --- a/app/workers/articles/update_page_views_worker.rb +++ b/app/workers/articles/update_page_views_worker.rb @@ -8,9 +8,11 @@ module Articles retry: false def perform(create_params) + article = Article.find_by(id: create_params["article_id"]) + return unless article + PageView.create!(create_params) - article = Article.find(create_params["article_id"]) updated_count = article.page_views.sum(:counts_for_number_of_views) if updated_count > article.page_views_count article.update_column(:page_views_count, updated_count) diff --git a/spec/workers/articles/update_page_views_worker_spec.rb b/spec/workers/articles/update_page_views_worker_spec.rb new file mode 100644 index 000000000..6cde17c4a --- /dev/null +++ b/spec/workers/articles/update_page_views_worker_spec.rb @@ -0,0 +1,35 @@ +require "rails_helper" + +RSpec.describe Articles::UpdatePageViewsWorker, type: :worker do + let(:worker) { described_class.new } + + context "when the article id is invalid" do + let(:article_id) { :no_article_with_this_id } + + it "exits gracefully" do + expect { worker.perform(article_id: article_id) }.not_to raise_error + end + + it "does not attempt to create a page view for an invalid article" do + allow(PageView).to receive(:create!) + + worker.perform(article_id: article_id) + + expect(PageView).not_to have_received(:create!) + end + end + + context "when the article exists" do + let(:user) { create(:user) } + let(:referrer) { nil } + let(:article) { create(:article) } + + it "creates a page view" do + expect do + worker.perform("article_id" => article.id, + "user_id" => user.id, + "referrer" => referrer) + end.to change(PageView, :count).by(1) + end + end +end