<commit_before>require 'rails_helper'

RSpec::Matchers.define_negated_matcher :exclude, :include

RSpec.describe AssetTriggerReplicationWorker, type: :worker do
  subject(:worker) { described_class.new }

  let(:asset) { FactoryGirl.create(:asset) }
  let(:s3_storage) { instance_double(S3Storage) }

  before do
    allow(Services).to receive(:cloud_storage).and_return(s3_storage)
    allow(s3_storage).to receive(:exists?).and_return(exists_on_s3)
  end

  context 'when asset has no corresponding S3 object' do
    let(:exists_on_s3) { false }

    it 'does not re-save S3 object' do
      expect(s3_storage).to receive(:save).never

      worker.perform(asset.id.to_s)
    end
  end

  context 'when asset has corresponding S3 object' do
    let(:exists_on_s3) { true }

    before do
      allow(s3_storage).to receive(:never_replicated?)
        .with(asset).and_return(never_replicated)
    end

    context 'and asset has never been replicated' do
      let(:never_replicated) { true }

      it 're-saves S3 object to trigger replication' do
        expect(s3_storage).to receive(:save).with(asset, force: true)

        worker.perform(asset.id.to_s)
      end
    end

    context 'and asset has been replicated' do
      let(:never_replicated) { false }

      it 'does not re-save S3 object' do
        expect(s3_storage).to receive(:save).never

        worker.perform(asset.id.to_s)
      end
    end
  end
end
<commit_msg>Use constructor injection to avoid stubbing Services.cloud_storage<commit_after>require 'rails_helper'

RSpec::Matchers.define_negated_matcher :exclude, :include

RSpec.describe AssetTriggerReplicationWorker, type: :worker do
  subject(:worker) { described_class.new(cloud_storage: s3_storage) }

  let(:asset) { FactoryGirl.create(:asset) }
  let(:s3_storage) { instance_double(S3Storage) }

  before do
    allow(s3_storage).to receive(:exists?).and_return(exists_on_s3)
  end

  context 'when asset has no corresponding S3 object' do
    let(:exists_on_s3) { false }

    it 'does not re-save S3 object' do
      expect(s3_storage).to receive(:save).never

      worker.perform(asset.id.to_s)
    end
  end

  context 'when asset has corresponding S3 object' do
    let(:exists_on_s3) { true }

    before do
      allow(s3_storage).to receive(:never_replicated?)
        .with(asset).and_return(never_replicated)
    end

    context 'and asset has never been replicated' do
      let(:never_replicated) { true }

      it 're-saves S3 object to trigger replication' do
        expect(s3_storage).to receive(:save).with(asset, force: true)

        worker.perform(asset.id.to_s)
      end
    end

    context 'and asset has been replicated' do
      let(:never_replicated) { false }

      it 'does not re-save S3 object' do
        expect(s3_storage).to receive(:save).never

        worker.perform(asset.id.to_s)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09944559944559944, 'fraction_numerical': 0.011088011088011088, 'mean_word_length': 3.3942161339421615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2763655', 'n_tokens_mistral': 956, 'n_tokens_neox': 911, 'n_words': 225}