<commit_before>require 'spec_helper'

describe Paperclip::Storage::Eitheror do

  after(:each) do
    User.delete_all
    File.delete('spec/primary_storage/users/avatars/original/image.jpg') if File.exists?('spec/primary_storage/users/avatars/original/image.jpg')

    File.delete('spec/fallback_storage/users/avatars/original/image.jpg') if File.exists?('spec/fallback_storage/users/avatars/original/image.jpg')
  end

  context 'when creating a new attachment' do
    it 'uses the primary storage' do
      user = User.create avatar: File.open('spec/image.jpg')

      expect(user.avatar.path).to match /primary_storage/
      expect(File.exists? user.avatar.path).to be true
    end
  end

  context 'when deleting' do
    it 'deletes from both storages' do
      user = User.create avatar: File.open('spec/image.jpg')
      primary_storage_path = 'spec/primary_storage/users/avatars/original/image.jpg'
      fallback_storage_path = 'spec/fallback_storage/users/avatars/original/image.jpg'
      FileUtils.cp(primary_storage_path, fallback_storage_path)

      expect(File.exists?(user.avatar.path)).to be true

      user.destroy

      expect(File.exists?(primary_storage_path)).to be false
      expect(File.exists?(fallback_storage_path)).to be false
    end
  end


  context 'when "either" is not available' do
    it 'fallsback to "or" storage' do
      expect(fallback_user.avatar.path).to match /fallback_storage/
    end

    it 'delegates calls to "or" storage' do
      or_storage = double
      allow(or_storage).to receive(:exists?).and_return true
      expect(or_storage).to receive(:public_url).and_return('a public url')
      user = fallback_user
      user.avatar.instance_variable_set(:@or, or_storage)

      expect(user.avatar.public_url).to eq 'a public url'
    end
  end
end

private

def fallback_user
  ActiveRecord::Base.connection.execute("INSERT into users (id, avatar_file_name, avatar_content_type) values (999, 'fallback.png', 'image/png')")
  User.find(999)
end<commit_msg>Use let block to define 'fallback_user' in spec<commit_after>require 'spec_helper'

describe Paperclip::Storage::Eitheror do
  let(:fallback_user) do
    ActiveRecord::Base.connection.execute("INSERT into users (id, avatar_file_name, avatar_content_type) values (999, 'fallback.png', 'image/png')")
    User.find(999)
  end

  after(:each) do
    User.delete_all
    File.delete('spec/primary_storage/users/avatars/original/image.jpg') if File.exists?('spec/primary_storage/users/avatars/original/image.jpg')

    File.delete('spec/fallback_storage/users/avatars/original/image.jpg') if File.exists?('spec/fallback_storage/users/avatars/original/image.jpg')
  end

  context 'when creating a new attachment' do
    it 'uses the primary storage' do
      user = User.create avatar: File.open('spec/image.jpg')

      expect(user.avatar.path).to match /primary_storage/
      expect(File.exists? user.avatar.path).to be true
    end
  end

  context 'when deleting' do
    it 'deletes from both storages' do
      user = User.create avatar: File.open('spec/image.jpg')
      primary_storage_path = 'spec/primary_storage/users/avatars/original/image.jpg'
      fallback_storage_path = 'spec/fallback_storage/users/avatars/original/image.jpg'
      FileUtils.cp(primary_storage_path, fallback_storage_path)

      expect(File.exists?(user.avatar.path)).to be true

      user.destroy

      expect(File.exists?(primary_storage_path)).to be false
      expect(File.exists?(fallback_storage_path)).to be false
    end
  end

  context 'when "either" is not available' do
    it 'fallsback to "or" storage' do
      expect(fallback_user.avatar.path).to match /fallback_storage/
    end

    it 'delegates calls to "or" storage' do
      or_storage = double
      allow(or_storage).to receive(:exists?).and_return true
      expect(or_storage).to receive(:public_url).and_return('a public url')
      user = fallback_user
      user.avatar.instance_variable_set(:@or, or_storage)

      expect(user.avatar.public_url).to eq 'a public url'
    end
  end
end
{'fraction_non_alphanumeric': 0.11256157635467981, 'fraction_numerical': 0.002955665024630542, 'mean_word_length': 3.8576555023923444, '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': '3616485', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1277, 'n_words': 271}