Repository: rails/rails
File: activestorage/test/jobs/transform_job_test.rb (Ruby)

Code before:
# frozen_string_literal: true

require "test_helper"
require "database/setup"

class ActiveStorage::TransformJobTest < ActiveJob::TestCase
  setup { @blob = create_file_blob }

  test "creates variant" do
    transformations = { resize_to_limit: [100, 100] }

    assert_changes -> { @blob.variant(transformations).send(:processed?) }, from: false, to: true do
      perform_enqueued_jobs do
        ActiveStorage::TransformJob.perform_later @blob, transformations
      end
    end
  end

  test "creates variant when untracked" do
    @was_tracking, ActiveStorage.track_variants = ActiveStorage.track_variants, false
    transformations = { resize_to_limit: [100, 100] }

    begin
      assert_changes -> { @blob.variant(transformations).send(:processed?) }, from: false, to: true do
        perform_enqueued_jobs do
          ActiveStorage::TransformJob.perform_later @blob, transformations
        end
      end
    ensure
      ActiveStorage.track_variants = @was_tracking
    end
  end
end


Review comment (performance):
hi @jonathanhefner @nicowenterodt 

thanks for your work.
 I would like to point out that this test does not test that a `variant` has been created. it tests that the preview image has been generated

What I mean by that is that each `ActiveStorage::Blob` has a `has_one_attached :preview_image` which is another blob

and right now `processed` only generates this blob, **but not the variant that's associated with `preview_image`**

in my production app, I had to do this to preprocess a preview with its variants: 
```ruby
ActiveStorage::Preview.new(blob, transformations).processed.key
```

Adding the `key` call is a bit of hack, but it will not only generate the `preview_image` but also the variant associated with which is, in my opinion, what's expected when we add the `preprocessed: true` ( the key that's returned is discarded, but it's the newly generated variant's key which i believe won't impact performance)

I will try to create a PR. I'm also considering changing the `processed` method implementation, but I feel it's a bit complex ( I need to further read on how to check if an image variant has already been processed for a set of transformations)

Code after:
# frozen_string_literal: true

require "test_helper"
require "database/setup"

class ActiveStorage::TransformJobTest < ActiveJob::TestCase
  setup { @blob = create_file_blob }

  test "creates variant" do
    transformations = { resize_to_limit: [100, 100] }

    assert_changes -> { @blob.variant(transformations).send(:processed?) }, from: false, to: true do
      perform_enqueued_jobs do
        ActiveStorage::TransformJob.perform_later @blob, transformations
      end
    end
  end

  test "creates variant for previewable file" do
    @blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
    transformations = { resize_to_limit: [100, 100] }

    assert_changes -> { @blob.reload.preview(transformations).send(:processed?) }, from: false, to: true do
      perform_enqueued_jobs do
        ActiveStorage::TransformJob.perform_later @blob, transformations
      end
    end
  end

  test "creates variant when untracked" do
    @was_tracking, ActiveStorage.track_variants = ActiveStorage.track_variants, false
    transformations = { resize_to_limit: [100, 100] }

    begin
      assert_changes -> { @blob.variant(transformations).send(:processed?) }, from: false, to: true do
        perform_enqueued_jobs do
          ActiveStorage::TransformJob.perform_later @blob, transformations
        end
      end
    ensure
      ActiveStorage.track_variants = @was_tracking
    end
  end
end