How to test direct upload

Hi all,

I noticed that in Shrine Testing Guide, there is no mention to testing direct upload.
I wonder if it’s the best practice to upload a file before testing.
I’d like to do unit test of server side (more specifically, a form object that accepts files from direct upload), not acceptance test.

You can test it by uploading to cache directory and passing uploaded file data to the attachment attribute, e.g:

cached_file = FileUploader.upload(some_file, :cache)
record.attachment = cached_file.to_json

It worked, thanks!
I found it helpful to have this in documentation. If you agree, I’m happy to send a PR!

Yes, please send one :+1:

Hi, uploading to cache directly worked with FileSystem storage, but didn’t work with Memory storage.
More specifically, when assigning a JSON of a cache file to a ActiveRecord model, I got an error like below:

Shrine::FileNotFound:
        file "a20db4b853eaee20d7610580c88e9e37.png" not found on storage
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/storage/memory.rb:28:in `rescue in open'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/storage/memory.rb:21:in `open'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/uploaded_file.rb:265:in `_open'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/uploaded_file.rb:93:in `open'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/plugins/refresh_metadata.rb:16:in `refresh_metadata!'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/plugins/restore_cached_data.rb:21:in `cached'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/attacher.rb:97:in `attach_cached'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/attacher.rb:77:in `assign'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/plugins/rack_file.rb:31:in `assign'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/plugins/model.rb:114:in `model_assign'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/plugins/model.rb:41:in `block in define_model_methods'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activemodel-6.0.3.4/lib/active_model/attribute_assignment.rb:50:in `public_send'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activemodel-6.0.3.4/lib/active_model/attribute_assignment.rb:50:in `_assign_attribute'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activemodel-6.0.3.4/lib/active_model/attribute_assignment.rb:43:in `block in _assign_attributes'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activemodel-6.0.3.4/lib/active_model/attribute_assignment.rb:42:in `each'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activemodel-6.0.3.4/lib/active_model/attribute_assignment.rb:42:in `_assign_attributes'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activerecord-6.0.3.4/lib/active_record/attribute_assignment.rb:21:in `_assign_attributes'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/activemodel-6.0.3.4/lib/active_model/attribute_assignment.rb:35:in `assign_attributes'
      # ./app/forms/profile_registration_forms/user_form.rb:128:in `assign_attributes_to_profile'
      # ./app/forms/profile_registration_forms/user_form.rb:67:in `save'
      # ./spec/forms/profile_registration_forms/user_form_spec.rb:82:in `block (4 levels) in <top (required)>'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:124:in `block in run'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:110:in `loop'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:110:in `run'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/rspec-retry-0.6.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
      # /Users/okuramasafumi/.gem/ruby/2.6.6/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:37:in `block (2 levels) in setup'
      # ------------------
      # --- Caused by: ---
      # KeyError:
      #   key not found: "a20db4b853eaee20d7610580c88e9e37.png"
      #   /Users/okuramasafumi/.gem/ruby/2.6.6/gems/shrine-3.3.0/lib/shrine/storage/memory.rb:24:in `fetch'

Here’s the list of plugins used here:

  • activerecord
  • derivatives
  • backgrounding
  • determine_mime_type
  • cached_attachment_data
  • restore_cached_data

If this is a known issue, I’ll add a note to the documentation.
If not, I’d like to know if it’s by design or intentional.
I’m not very happy with using minio for testing because of setup effort…

The issue might be that the thread/process that uploaded the file has different storage from the thread/process reading the file. This might be the case in Rails system tests, though I’m not sure. It’s definitely the case if the app you’re testing is running in a different process.