Spec feed insert worker (#2965)
* Spec for feed insert worker when missing records * more specs! * Refactor feed insert worker
This commit is contained in:
		
				
					committed by
					
						
						Eugen Rochko
					
				
			
			
				
	
			
			
			
						parent
						
							2fba4196ef
						
					
				
				
					commit
					cc9a6a710f
				
			@@ -3,13 +3,34 @@
 | 
			
		||||
class FeedInsertWorker
 | 
			
		||||
  include Sidekiq::Worker
 | 
			
		||||
 | 
			
		||||
  def perform(status_id, follower_id)
 | 
			
		||||
    status   = Status.find(status_id)
 | 
			
		||||
    follower = Account.find(follower_id)
 | 
			
		||||
  attr_reader :status, :follower
 | 
			
		||||
 | 
			
		||||
    return if FeedManager.instance.filter?(:home, status, follower.id)
 | 
			
		||||
  def perform(status_id, follower_id)
 | 
			
		||||
    @status = Status.find_by(id: status_id)
 | 
			
		||||
    @follower = Account.find_by(id: follower_id)
 | 
			
		||||
 | 
			
		||||
    check_and_insert
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def check_and_insert
 | 
			
		||||
    if records_available?
 | 
			
		||||
      perform_push unless feed_filtered?
 | 
			
		||||
    else
 | 
			
		||||
      true
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def records_available?
 | 
			
		||||
    status.present? && follower.present?
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def feed_filtered?
 | 
			
		||||
    FeedManager.instance.filter?(:home, status, follower.id)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def perform_push
 | 
			
		||||
    FeedManager.instance.push(:home, follower, status)
 | 
			
		||||
  rescue ActiveRecord::RecordNotFound
 | 
			
		||||
    true
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										52
									
								
								spec/workers/feed_insert_worker_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								spec/workers/feed_insert_worker_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
describe FeedInsertWorker do
 | 
			
		||||
  subject { described_class.new }
 | 
			
		||||
 | 
			
		||||
  describe 'perform' do
 | 
			
		||||
    let(:follower) { Fabricate(:account) }
 | 
			
		||||
    let(:status) { Fabricate(:status) }
 | 
			
		||||
 | 
			
		||||
    context 'when there are no records' do
 | 
			
		||||
      it 'skips push with missing status' do
 | 
			
		||||
        instance = double(push: nil)
 | 
			
		||||
        allow(FeedManager).to receive(:instance).and_return(instance)
 | 
			
		||||
        result = subject.perform(nil, follower.id)
 | 
			
		||||
 | 
			
		||||
        expect(result).to eq true
 | 
			
		||||
        expect(instance).not_to have_received(:push)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'skips push with missing account' do
 | 
			
		||||
        instance = double(push: nil)
 | 
			
		||||
        allow(FeedManager).to receive(:instance).and_return(instance)
 | 
			
		||||
        result = subject.perform(status.id, nil)
 | 
			
		||||
 | 
			
		||||
        expect(result).to eq true
 | 
			
		||||
        expect(instance).not_to have_received(:push)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when there are real records' do
 | 
			
		||||
      it 'skips the push when there is a filter' do
 | 
			
		||||
        instance = double(push: nil, filter?: true)
 | 
			
		||||
        allow(FeedManager).to receive(:instance).and_return(instance)
 | 
			
		||||
        result = subject.perform(status.id, follower.id)
 | 
			
		||||
 | 
			
		||||
        expect(result).to be_nil
 | 
			
		||||
        expect(instance).not_to have_received(:push)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'pushes the status onto the home timeline without filter' do
 | 
			
		||||
        instance = double(push: nil, filter?: false)
 | 
			
		||||
        allow(FeedManager).to receive(:instance).and_return(instance)
 | 
			
		||||
        result = subject.perform(status.id, follower.id)
 | 
			
		||||
 | 
			
		||||
        expect(result).to be_nil
 | 
			
		||||
        expect(instance).to have_received(:push).with(:home, follower, status)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user