rails--rails/activestorage/test/test_helper.rb

71 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
ENV["RAILS_ENV"] ||= "test"
require_relative "dummy/config/environment.rb"
2017-07-21 21:12:29 +00:00
2017-07-01 10:06:08 +00:00
require "bundler/setup"
require "active_support"
2017-07-04 16:10:53 +00:00
require "active_support/test_case"
2017-07-01 10:06:08 +00:00
require "active_support/testing/autorun"
2017-09-28 20:43:37 +00:00
require "mini_magick"
begin
require "byebug"
rescue LoadError
end
2017-07-01 10:06:08 +00:00
2017-07-22 15:00:16 +00:00
require "active_job"
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.logger = ActiveSupport::Logger.new(nil)
2017-07-22 15:00:16 +00:00
# Filter out Minitest backtrace while allowing backtrace from other libraries
# to be shown.
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
2017-07-09 16:03:13 +00:00
require "yaml"
SERVICE_CONFIGURATIONS = begin
erb = ERB.new(Pathname.new(File.expand_path("service/configurations.yml", __dir__)).read)
configuration = YAML.load(erb.result) || {}
configuration.deep_symbolize_keys
2017-07-09 16:03:13 +00:00
rescue Errno::ENOENT
puts "Missing service configuration file in test/service/configurations.yml"
{}
end
require "tmpdir"
2018-01-17 01:32:02 +00:00
ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir("active_storage_tests"))
2017-07-03 19:06:09 +00:00
ActiveStorage.logger = ActiveSupport::Logger.new(nil)
ActiveStorage.verifier = ActiveSupport::MessageVerifier.new("Testing")
2017-07-11 16:53:17 +00:00
2017-07-04 16:10:53 +00:00
class ActiveSupport::TestCase
self.file_fixture_path = File.expand_path("fixtures/files", __dir__)
2017-07-04 16:10:53 +00:00
private
def create_blob(data: "Hello world!", filename: "hello.txt", content_type: "text/plain")
ActiveStorage::Blob.create_after_upload! io: StringIO.new(data), filename: filename, content_type: content_type
2017-07-04 16:10:53 +00:00
end
def create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil)
ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata
end
def create_blob_before_direct_upload(filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain")
ActiveStorage::Blob.create_before_direct_upload! filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type
end
2017-09-28 20:43:37 +00:00
def read_image(blob_or_variant)
MiniMagick::Image.open blob_or_variant.service.send(:path_for, blob_or_variant.key)
end
2017-07-05 13:18:50 +00:00
end
require "global_id"
GlobalID.app = "ActiveStorageExampleApp"
ActiveRecord::Base.send :include, GlobalID::Identification
class User < ActiveRecord::Base
has_one_attached :avatar
has_one_attached :cover_photo, dependent: false
has_many_attached :highlights
end