From e8e2c3c4a7e065cb6ff943cd426856b0f55d2465 Mon Sep 17 00:00:00 2001 From: Aymeric Le Dorze Date: Wed, 1 Aug 2018 14:29:47 +0200 Subject: [PATCH] Added a test with active_storage (issue #103) --- .gitignore | 1 + gemfiles/active_record_52.gemfile | 2 + gemfiles/active_record_edge.gemfile | 2 + test/fixtures/hello.txt | 1 + test/test_active_storage.rb | 15 +++++++ test/test_helper.rb | 67 +++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 test/fixtures/hello.txt create mode 100644 test/test_active_storage.rb diff --git a/.gitignore b/.gitignore index da570124..cb165a55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ pkg .DS_Store Gemfile.lock gemfiles/*.lock +test/tmp diff --git a/gemfiles/active_record_52.gemfile b/gemfiles/active_record_52.gemfile index c1195c58..26cd7e0c 100644 --- a/gemfiles/active_record_52.gemfile +++ b/gemfiles/active_record_52.gemfile @@ -1,8 +1,10 @@ source 'https://rubygems.org' github 'rails/rails', branch: '5-2-stable' do + gem 'activejob', require: 'active_job' gem 'activerecord', require: 'active_record' gem 'activesupport', require: 'active_support' + gem 'activestorage' end # Development dependencies diff --git a/gemfiles/active_record_edge.gemfile b/gemfiles/active_record_edge.gemfile index 1a044ea5..3e9884f7 100644 --- a/gemfiles/active_record_edge.gemfile +++ b/gemfiles/active_record_edge.gemfile @@ -1,8 +1,10 @@ source 'https://rubygems.org' github 'rails/rails' do + gem 'activejob', require: 'active_job' gem 'activerecord', require: 'active_record' gem 'activesupport', require: 'active_support' + gem 'activestorage' end # Development dependencies diff --git a/test/fixtures/hello.txt b/test/fixtures/hello.txt new file mode 100644 index 00000000..5dd01c17 --- /dev/null +++ b/test/fixtures/hello.txt @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file diff --git a/test/test_active_storage.rb b/test/test_active_storage.rb new file mode 100644 index 00000000..99069a69 --- /dev/null +++ b/test/test_active_storage.rb @@ -0,0 +1,15 @@ +require 'test_helper' + +class ParanoidActiveStorageTest < ParanoidBaseTest + def create_file_blob(filename: "hello.txt", content_type: "text/plain", metadata: nil) + ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata + end + + def test_paranoid_active_storage + unless ENABLE_ACTIVE_STORAGE + skip 'ActiveStorage is only available for Rails >= 5.2' + end + pt = ParanoidTime.create(:main_file => create_file_blob, :files => [create_file_blob, create_file_blob], :undependent_main_file => create_file_blob, :undependent_files => [create_file_blob, create_file_blob]) + pt.destroy + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 956968ae..7f4a4edc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,6 +10,34 @@ require 'acts_as_paranoid' require 'minitest/autorun' +ENABLE_ACTIVE_STORAGE = ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 2) +if ENABLE_ACTIVE_STORAGE + # load ActiveStorage + require 'global_id' + ActiveRecord::Base.include(GlobalID::Identification) + GlobalID.app = 'ActsAsParanoid' + + ActiveJob::Base.queue_adapter = :test + + require 'active_support/cache' + + require 'active_storage' + require 'active_storage/attached' + require 'active_storage/service/disk_service' + if ActiveRecord::VERSION::MAJOR >= 6 + require 'active_storage/reflection' + ActiveRecord::Base.include(ActiveStorage::Reflection::ActiveRecordExtensions) + ActiveRecord::Reflection.singleton_class.prepend(ActiveStorage::Reflection::ReflectionExtension) + ActiveRecord::Base.include(ActiveStorage::Attached::Model) + else + ActiveRecord::Base.extend(ActiveStorage::Attached::Macros) + end + $: << "#{Gem.loaded_specs['activestorage'].full_gem_path}/app/models/" + Dir.glob("#{Gem.loaded_specs['activestorage'].full_gem_path}/app/models/active_storage/*").each{|f| require f} + Dir.glob("#{Gem.loaded_specs['activestorage'].full_gem_path}/app/jobs/active_storage/*").each{|f| require f} + ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: 'test/tmp') +end + # Silence deprecation halfway through the test I18n.enforce_available_locales = true @@ -208,6 +236,29 @@ def setup_db timestamps t end + + if ENABLE_ACTIVE_STORAGE + create_table :active_storage_attachments do |t| + t.string :name, :null => false + t.string :record_type, :null => false + t.bigint :record_id, :null => false + t.bigint :blob_id, :null => false + t.datetime :created_at, :null => false + t.index [:blob_id], :name => "index_active_storage_attachments_on_blob_id" + t.index [:record_type, :record_id, :name, :blob_id], :name => "index_active_storage_attachments_uniqueness", :unique => true + end + + create_table :active_storage_blobs do |t| + t.string :key, :null => false + t.string :filename, :null => false + t.string :content_type + t.text :metadata + t.bigint :byte_size, :null => false + t.string :checksum, :null => false + t.datetime :created_at, :null => false + t.index [:key], :name => "index_active_storage_blobs_on_key", :unique => true + end + end end end @@ -225,6 +276,10 @@ def teardown_db tables.each { |table| ActiveRecord::Base.connection.drop_table(table) } end +def clean_active_storage_attachments + Dir.glob('test/tmp/*').each{|f| FileUtils.rm_r(f)} +end + class ParanoidTime < ActiveRecord::Base acts_as_paranoid @@ -238,6 +293,13 @@ class ParanoidTime < ActiveRecord::Base has_one :has_one_not_paranoid, :dependent => :destroy belongs_to :not_paranoid, :dependent => :destroy + + if ENABLE_ACTIVE_STORAGE + has_one_attached :main_file + has_many_attached :files + has_one_attached :undependent_main_file, :dependent => false + has_many_attached :undependent_files, :dependent => false + end end class ParanoidBoolean < ActiveRecord::Base @@ -393,6 +455,10 @@ class ParanoidHasManyAsParent < ActiveRecord::Base end class ParanoidBaseTest < ActiveSupport::TestCase + if ENABLE_ACTIVE_STORAGE + self.file_fixture_path = 'test/fixtures' + end + def setup setup_db @@ -408,6 +474,7 @@ def setup def teardown teardown_db + clean_active_storage_attachments end def assert_empty(collection)