Skip to content

Commit

Permalink
Added a test with active_storage (issue ActsAsParanoid#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-ledorze committed Oct 27, 2021
1 parent 5ef7ff8 commit 429b4bb
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gemfiles/*.lock
.ruby-version
coverage/
log/
test/tmp
2 changes: 2 additions & 0 deletions gemfiles/active_record_52.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

source "https://rubygems.org"

gem "activejob", "~> 5.2.0", require: "active_job"
gem "activerecord", "~> 5.2.0", require: "active_record"
gem "activestorage", "~> 5.2.0"
gem "activesupport", "~> 5.2.0", require: "active_support"

# Development dependencies
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/active_record_60.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

source "https://rubygems.org"

gem "activejob", "~> 6.0.0", require: "active_job"
gem "activerecord", "~> 6.0.0", require: "active_record"
gem "activestorage", "~> 6.0.0"
gem "activesupport", "~> 6.0.0", require: "active_support"

# Development dependencies
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/active_record_61.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

source "https://rubygems.org"

gem "activejob", "~> 6.1.0", require: "active_job"
gem "activerecord", "~> 6.1.0", require: "active_record"
gem "activestorage", "~> 6.1.0"
gem "activesupport", "~> 6.1.0", require: "active_support"

# Development dependencies
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/active_record_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
source "https://rubygems.org"

github "rails/rails" do
gem "activejob", require: "active_job"
gem "activerecord", require: "active_record"
gem "activestorage"
gem "activesupport", require: "active_support"
end

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, world!
19 changes: 19 additions & 0 deletions test/test_active_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "test_helper"

class ParanoidActiveStorageTest < ParanoidBaseTest
def create_file_blob(filename: "hello.txt", content_type: "text/plain", metadata: nil)
args = { io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata }
if ActiveRecord::VERSION::MAJOR >= 6 && ActiveRecord::VERSION::MINOR >= 1
args[:service_name] = 'test'
end
ActiveStorage::Blob.respond_to?(:create_and_upload!) ? ActiveStorage::Blob.create_and_upload!(**args) : ActiveStorage::Blob.create_after_upload!(**args)
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
2 changes: 1 addition & 1 deletion test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def test_paranoid_destroy_with_update_callbacks
@paranoid_with_callback.destroy
end
ParanoidWithCallback.transaction do
@paranoid_with_callback.update_attributes(:name => "still paranoid")
@paranoid_with_callback.update(:name => "still paranoid")
end

assert_equal 1, @paranoid_with_callback.called_before_destroy
Expand Down
94 changes: 94 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@
require "minitest/autorun"
require "minitest/focus"

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"

require "active_job"
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)

if ActiveRecord::VERSION::MINOR == 0
module Rails
def self.autoloaders
Object.new.tap{|o| def o.zeitwerk_enabled?; false; end}
end
end
else
require "#{Gem.loaded_specs["activestorage"].full_gem_path}/app/models/active_storage/record" if File.exists?("#{Gem.loaded_specs["activestorage"].full_gem_path}/app/models/active_storage/record.rb")
if File.exists?("#{Gem.loaded_specs["activestorage"].full_gem_path}/app/models/active_storage/blob")
class ActiveStorage::Blob < ActiveStorage::Record; end if File.exists?("#{Gem.loaded_specs["activestorage"].full_gem_path}/app/models/active_storage/record.rb")
Dir.glob("#{Gem.loaded_specs["activestorage"].full_gem_path}/app/models/active_storage/blob/*").each{|f| require f}
end
end
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}
if ActiveStorage::Blob.respond_to?(:services=)
require 'active_storage/service/disk_service'
ActiveStorage::Blob.services = {
'test' => ActiveStorage::Service::DiskService.build(name: 'test', configurator: nil, root: 'test/tmp'),
}
end
require "#{Gem.loaded_specs["activestorage"].full_gem_path}/app/jobs/active_storage/base_job" if File.exists?("#{Gem.loaded_specs["activestorage"].full_gem_path}/app/jobs/active_storage/base_job.rb")
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

Expand Down Expand Up @@ -237,6 +287,34 @@ def setup_db

timestamps t
end

if ENABLE_ACTIVE_STORAGE
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.string :service_name
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

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false
t.datetime :created_at, null: false
t.index [:record_type, :record_id, :name, :blob_id], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table :active_storage_variant_records do |t|
t.belongs_to :blob, null: false, index: false
t.string :variation_digest, null: false
t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
end
end
end
end

Expand All @@ -252,6 +330,10 @@ def teardown_db
end
end

def clean_active_storage_attachments
Dir.glob("test/tmp/*").each{|f| FileUtils.rm_r(f)}
end

class ParanoidTime < ActiveRecord::Base
acts_as_paranoid

Expand All @@ -277,6 +359,13 @@ def ensure_destroyable

throw(:abort) unless destroyable
end

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
Expand Down Expand Up @@ -486,6 +575,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

Expand All @@ -501,6 +594,7 @@ def setup

def teardown
teardown_db
clean_active_storage_attachments
end

def assert_paranoid_deletion(model)
Expand Down

0 comments on commit 429b4bb

Please sign in to comment.