rails/activestorage/test/fixture_set_test.rb
Sean Doyle 948e9d74e3
Fix Flaky ActiveStorage test (#41225)
Fixes a flaky Active Storage test introduced by [rails/rails#41065][],
and improves the documentation.

It seems that the test is covering the backwards compatibility of an
older interface for retrieving records through
`ActiveStorage::Record#find_signed!`. The test itself would pass
unpredictably. To isolate the failure and reproduce it consistently, a
see value was found after some trial and error:

```
SEED=59729 bin/test test/fixture_set_test.rb test/models/attachment_test.rb
```

This _used_ to pass consistently because [rails/rails][#41065]
introduced a call to `fixtures :all`, which introduces more variation in
the database's ID generation sequence. Without that line, `id` values
start at `1`, so the fact that calls to
`ActiveStorage::Attached::One#id` and `ActiveStorage::Blob#id` **both
return `1`** is purely coincidence.

The proposed resolution changes the test slightly. Prior to this change,
the identifier used during retrieval and verification fetched from
`@user.avatar.id`, where `@user.avatar` is an instance of
`ActiveStorage::Attached::One`. The verifier/retriever combination in
that test expected a signed identifier for an `ActiveStorage::Blob`
instance. The change involved retrieving an instance through
`@user.avatar.blob`.

To better emphasize how global the `fixtures :all` declaration is, move
it from the `test/fixture_set_test.rb` file to the `test/test_helper.rb`
file.

[rails/rails#41065]: https://github.com/rails/rails/pull/41065
2021-01-24 18:29:11 +01:00

39 lines
935 B
Ruby

# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::FixtureSetTest < ActiveSupport::TestCase
fixtures :all
def test_active_storage_blob
user = users(:first)
avatar = user.avatar
assert_equal avatar.blob.content_type, "image/jpeg+override"
assert_equal avatar.blob.filename.to_s, "racecar.jpg"
assert_equal avatar.blob.service.name, :local
avatar.blob.open { |file| assert FileUtils.identical?(file, file_fixture("racecar.jpg")) }
end
def test_active_storage_attachment
user = users(:first)
avatar = user.avatar
assert_not_predicate avatar, :blank?
assert_predicate avatar, :attached?
assert_predicate avatar.attachment, :present?
end
def test_active_storage_metadata
user = users(:first)
avatar = user.avatar.tap(&:analyze)
assert avatar.metadata["identified"]
assert avatar.metadata["analyzed"]
end
end