diff options
Diffstat (limited to 'activestorage/test/models')
-rw-r--r-- | activestorage/test/models/attached/many_test.rb | 59 | ||||
-rw-r--r-- | activestorage/test/models/attached/one_test.rb | 31 |
2 files changed, 78 insertions, 12 deletions
diff --git a/activestorage/test/models/attached/many_test.rb b/activestorage/test/models/attached/many_test.rb index fc89dae658..82be88af08 100644 --- a/activestorage/test/models/attached/many_test.rb +++ b/activestorage/test/models/attached/many_test.rb @@ -106,6 +106,17 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase assert_equal "video.mp4", @user.highlights.second.filename.to_s end + test "attaching existing blobs to an existing record one at a time" do + @user.highlights.attach create_blob(filename: "funky.jpg") + @user.highlights.attach create_blob(filename: "town.jpg") + assert_equal "funky.jpg", @user.highlights.first.filename.to_s + assert_equal "town.jpg", @user.highlights.second.filename.to_s + + @user.reload + assert_equal "funky.jpg", @user.highlights.first.filename.to_s + assert_equal "town.jpg", @user.highlights.second.filename.to_s + end + test "updating an existing record to attach existing blobs" do @user.update! highlights: [ create_file_blob(filename: "racecar.jpg"), create_file_blob(filename: "video.mp4") ] assert_equal "racecar.jpg", @user.highlights.first.filename.to_s @@ -138,6 +149,40 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase assert_not ActiveStorage::Blob.service.exist?(@user.highlights.second.key) end + test "replacing existing, dependent attachments on an existing record via assign and attach" do + [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |old_blobs| + @user.highlights.attach old_blobs + + @user.highlights = [] + assert_not @user.highlights.attached? + + perform_enqueued_jobs do + @user.highlights.attach create_blob(filename: "whenever.jpg"), create_blob(filename: "wherever.jpg") + end + + assert_equal "whenever.jpg", @user.highlights.first.filename.to_s + assert_equal "wherever.jpg", @user.highlights.second.filename.to_s + assert_not ActiveStorage::Blob.exists?(old_blobs.first.id) + assert_not ActiveStorage::Blob.exists?(old_blobs.second.id) + assert_not ActiveStorage::Blob.service.exist?(old_blobs.first.key) + assert_not ActiveStorage::Blob.service.exist?(old_blobs.second.key) + end + end + + test "replacing existing, independent attachments on an existing record via assign and attach" do + @user.vlogs.attach create_blob(filename: "funky.mp4"), create_blob(filename: "town.mp4") + + @user.vlogs = [] + assert_not @user.vlogs.attached? + + assert_no_enqueued_jobs only: ActiveStorage::PurgeJob do + @user.vlogs.attach create_blob(filename: "whenever.mp4"), create_blob(filename: "wherever.mp4") + end + + assert_equal "whenever.mp4", @user.vlogs.first.filename.to_s + assert_equal "wherever.mp4", @user.vlogs.second.filename.to_s + end + test "successfully updating an existing record to replace existing, dependent attachments" do [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |old_blobs| @user.highlights.attach old_blobs @@ -195,21 +240,21 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase end end - test "successfully updating an existing record to remove dependent attachments" do + test "updating an existing record to remove dependent attachments" do [ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs| @user.highlights.attach blobs - perform_enqueued_jobs do - @user.update! highlights: [] + assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.first ] do + assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.second ] do + @user.update! highlights: [] + end end assert_not @user.highlights.attached? - assert_not ActiveStorage::Blob.service.exist?(blobs.first.key) - assert_not ActiveStorage::Blob.service.exist?(blobs.second.key) end end - test "successfully updating an existing record to remove independent attachments" do + test "updating an existing record to remove independent attachments" do [ create_blob(filename: "funky.mp4"), create_blob(filename: "town.mp4") ].tap do |blobs| @user.vlogs.attach blobs @@ -218,8 +263,6 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase end assert_not @user.vlogs.attached? - assert ActiveStorage::Blob.service.exist?(blobs.first.key) - assert ActiveStorage::Blob.service.exist?(blobs.second.key) end end diff --git a/activestorage/test/models/attached/one_test.rb b/activestorage/test/models/attached/one_test.rb index 33f9122644..01caaf0b55 100644 --- a/activestorage/test/models/attached/one_test.rb +++ b/activestorage/test/models/attached/one_test.rb @@ -195,11 +195,35 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase end end - test "successfully updating an existing record to remove a dependent attachment" do + test "removing a dependent attachment from an existing record" do create_blob(filename: "funky.jpg").tap do |blob| @user.avatar.attach blob - perform_enqueued_jobs do + assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blob ] do + @user.avatar.attach nil + end + + assert_not @user.avatar.attached? + end + end + + test "removing an independent attachment from an existing record" do + create_blob(filename: "funky.jpg").tap do |blob| + @user.cover_photo.attach blob + + assert_no_enqueued_jobs only: ActiveStorage::PurgeJob do + @user.cover_photo.attach nil + end + + assert_not @user.cover_photo.attached? + end + end + + test "updating an existing record to remove a dependent attachment" do + create_blob(filename: "funky.jpg").tap do |blob| + @user.avatar.attach blob + + assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blob ] do @user.update! avatar: nil end @@ -207,7 +231,7 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase end end - test "successfully updating an existing record to remove an independent attachment" do + test "updating an existing record to remove an independent attachment" do create_blob(filename: "funky.jpg").tap do |blob| @user.cover_photo.attach blob @@ -216,7 +240,6 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase end assert_not @user.cover_photo.attached? - assert ActiveStorage::Blob.service.exist?(blob.key) end end |