Skip to content

Instantly share code, notes, and snippets.

@eric-smartlove
Created April 1, 2012 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-smartlove/2276541 to your computer and use it in GitHub Desktop.
Save eric-smartlove/2276541 to your computer and use it in GitHub Desktop.
Test file showing that CarrierWave 0.6 sometimes writes in real fog repository when fog is mocked
require 'test_helper'
# This test class allows us to test CarrierWave specific behaviour, the application relies on.
class CarrierWaveTest < ActiveSupport::TestCase
# We define an artificial ActiveRecord class using CarrierWave and fog.
class TestUser < ActiveRecord::Base
mount_uploader :avatar do
storage :fog
def store_dir
"tmp/test_users/#{model.id}"
end
end
end
def setup
# In setup we create the table TestUser class needs.
ActiveRecord::Base.connection.execute("CREATE TEMPORARY TABLE test_users (id INT NOT NULL AUTO_INCREMENT, avatar VARCHAR(255), PRIMARY KEY (id))")
# Create a mock version of AWS_S3_BUCKET, unless it already exists.
Fog.mock!
new_fog_connection.directories.create(:key => AWS_S3_BUCKET) unless get_fog_directory
# Check mock repository is empty (test is making this assumption).
assert_equal 0, get_file_count
end
def teardown
# Destroy Test User instances (to delete attached files - cleaning fog directory)
Fog.unmock!
TestUser.destroy_all
# Drop TestUser table, we don't need it anymore.
ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE test_users")
end
# With CarrierWave 0.6, the attached file is saved in real repository even if fog in mocked, if fog was unmock and used before.
test "CarrierWave writes in mock repository when fog is mocked, even if fog was unmocked before" do
# Use CarrierWave with an unmock fog
Fog.unmock!
original_count = get_file_count
create_test_user
files_in_real_repository = get_file_count
Fog.mock!
files_in_mock_repository = get_file_count
# We should find one additional file in real repository, and none in mock one
assert_equal [0, original_count + 1], [files_in_mock_repository, files_in_real_repository]
# Use CarrierWave with mock fog
create_test_user
files_in_mock_repository = get_file_count
Fog.unmock!
files_in_real_repository = get_file_count
# We should find one additional file in mock repository
assert_equal [1, original_count + 1], [files_in_mock_repository, files_in_real_repository]
end
# Return a new fog connection.
# In the class, we use a new fog connection for each operation to maximize isolation between each fog command.
def new_fog_connection
return Fog::Storage.new(
provider: 'AWS',
aws_access_key_id: AWS_ACCESS_KEY_ID,
aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
region: AWS_S3_REGION
)
end
# Return fog directory from a newly created connection.
def get_fog_directory
return new_fog_connection.directories.get(AWS_S3_BUCKET)
end
# Return file count in fog directory, using a newly created connection.
def get_file_count
return get_fog_directory.files.size
end
# Create TestUser instance, with an attached CarrierWave file.
def create_test_user
user = TestUser.new do |u|
u.avatar = File.open("test/fixtures/images/portray.jpg")
end
assert user.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment