diff options
Diffstat (limited to 'activestorage/test/models')
-rw-r--r-- | activestorage/test/models/attached/many_test.rb | 72 | ||||
-rw-r--r-- | activestorage/test/models/attachment_test.rb | 53 |
2 files changed, 73 insertions, 52 deletions
diff --git a/activestorage/test/models/attached/many_test.rb b/activestorage/test/models/attached/many_test.rb index e826109874..39ddecb041 100644 --- a/activestorage/test/models/attached/many_test.rb +++ b/activestorage/test/models/attached/many_test.rb @@ -269,44 +269,22 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase end end - test "analyzing a new blob from an uploaded file after attaching it to an existing record" do - perform_enqueued_jobs do - @user.highlights.attach fixture_file_upload("racecar.jpg") - end - - assert @user.highlights.reload.first.analyzed? - assert_equal 4104, @user.highlights.first.metadata[:width] - assert_equal 2736, @user.highlights.first.metadata[:height] - end - - test "analyzing a new blob from an uploaded file after attaching it to an existing record via update" do - perform_enqueued_jobs do - @user.update! highlights: [ fixture_file_upload("racecar.jpg") ] - end - - assert @user.highlights.reload.first.analyzed? - assert_equal 4104, @user.highlights.first.metadata[:width] - assert_equal 2736, @user.highlights.first.metadata[:height] - end + test "updating an existing record with attachments when appending on assign" do + append_on_assign do + @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") - test "analyzing a directly-uploaded blob after attaching it to an existing record" do - perform_enqueued_jobs do - @user.highlights.attach directly_upload_file_blob(filename: "racecar.jpg") - end + assert_difference -> { @user.reload.highlights.count }, +2 do + @user.update! highlights: [ create_blob(filename: "whenever.jpg"), create_blob(filename: "wherever.jpg") ] + end - assert @user.highlights.reload.first.analyzed? - assert_equal 4104, @user.highlights.first.metadata[:width] - assert_equal 2736, @user.highlights.first.metadata[:height] - end + assert_no_difference -> { @user.reload.highlights.count } do + @user.update! highlights: [ ] + end - test "analyzing a directly-uploaded blob after attaching it to an existing record via update" do - perform_enqueued_jobs do - @user.update! highlights: [ directly_upload_file_blob(filename: "racecar.jpg") ] + assert_no_difference -> { @user.reload.highlights.count } do + @user.update! highlights: nil + end end - - assert @user.highlights.reload.first.analyzed? - assert_equal 4104, @user.highlights.first.metadata[:width] - assert_equal 2736, @user.highlights.first.metadata[:height] end test "attaching existing blobs to a new record" do @@ -422,24 +400,6 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase assert_equal "Could not find or build blob: expected attachable, got :foo", error.message end - test "analyzing a new blob from an uploaded file after attaching it to a new record" do - perform_enqueued_jobs do - user = User.create!(name: "Jason", highlights: [ fixture_file_upload("racecar.jpg") ]) - assert user.highlights.reload.first.analyzed? - assert_equal 4104, user.highlights.first.metadata[:width] - assert_equal 2736, user.highlights.first.metadata[:height] - end - end - - test "analyzing a directly-uploaded blob after attaching it to a new record" do - perform_enqueued_jobs do - user = User.create!(name: "Jason", highlights: [ directly_upload_file_blob(filename: "racecar.jpg") ]) - assert user.highlights.reload.first.analyzed? - assert_equal 4104, user.highlights.first.metadata[:width] - assert_equal 2736, user.highlights.first.metadata[:height] - end - end - test "detaching" do [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs| @user.highlights.attach blobs @@ -596,4 +556,12 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase User.remove_method :highlights end end + + private + def append_on_assign + ActiveStorage.replace_on_assign_to_many, previous = false, ActiveStorage.replace_on_assign_to_many + yield + ensure + ActiveStorage.replace_on_assign_to_many = previous + end end diff --git a/activestorage/test/models/attachment_test.rb b/activestorage/test/models/attachment_test.rb new file mode 100644 index 0000000000..94f354d116 --- /dev/null +++ b/activestorage/test/models/attachment_test.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "test_helper" +require "database/setup" + +class ActiveStorage::AttachmentTest < ActiveSupport::TestCase + include ActiveJob::TestHelper + + setup do + @user = User.create!(name: "Josh") + end + + teardown { ActiveStorage::Blob.all.each(&:delete) } + + test "analyzing a directly-uploaded blob after attaching it" do + blob = directly_upload_file_blob(filename: "racecar.jpg") + assert_not blob.analyzed? + + perform_enqueued_jobs do + @user.highlights.attach(blob) + end + + assert blob.reload.analyzed? + assert_equal 4104, blob.metadata[:width] + assert_equal 2736, blob.metadata[:height] + end + + test "mirroring a directly-uploaded blob after attaching it" do + previous_service, ActiveStorage::Blob.service = ActiveStorage::Blob.service, build_mirror_service + + blob = directly_upload_file_blob + assert_not ActiveStorage::Blob.service.mirrors.second.exist?(blob.key) + + perform_enqueued_jobs do + @user.highlights.attach(blob) + end + + assert ActiveStorage::Blob.service.mirrors.second.exist?(blob.key) + ensure + ActiveStorage::Blob.service = previous_service + end + + private + def build_mirror_service + ActiveStorage::Service::MirrorService.new \ + primary: build_disk_service("primary"), + mirrors: 3.times.collect { |i| build_disk_service("mirror_#{i + 1}") } + end + + def build_disk_service(purpose) + ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir("active_storage_tests_#{purpose}")) + end +end |