Where to cleanup cloudinary file uploads after rsp

2019-07-09 03:57发布

I use fixture_file_upload in my FactoryGirl methods to test file uploads. Problem is that after cleaning the database, all these uploaded files remain on Cloudinary.

I've been using Cloudinary::Api.delete_resources using a rake task to get rid of them, but I'd rather immediately clean them up before DatabaseCleaner removes all related public id's.

Where should I interfere with DatabaseCleaner as to remove these files from Cloudinary?

2条回答
Melony?
2楼-- · 2019-07-09 04:48

i would have two ways of doing things here.

firstly, i would not upload anything to cloudinary unless it is a integration test. i would use a mock, stub or test-double.

secondly, if you really really really need to upload the files for whatever reason, i would write a hook that does automatic cleanup in an after_all hook of you tests.

查看更多
可以哭但决不认输i
3楼-- · 2019-07-09 04:58

Based on @phoet's input, and given the fact that cloudinary limits the amount of API calls you can do on a single day, as well as the amount of images you can cleanup in a single call, I created a class

class CleanupCloudinary
  @@public_ids = []

  def self.add_public_ids
    Attachinary::File.all.each do |image|
      @@public_ids << image.public_id

      clean if @@public_ids.count == 100
    end
  end

  def self.clean
    Cloudinary::Api.delete_resources(@@public_ids) if @@public_ids.count > 0

    @@public_ids = []
  end
end

which I use as follows: in my factory girl file, I make a call to immediately add any public_ids after creating an advertisement:

after(:build, :create) do 
  CleanupCloudinary.add_public_ids
end

in env.rb, I added

at_exit do
  CleanupCloudinary.clean
end

as well as in spec_helper.rb

config.after(:suite) do
  CleanupCloudinary.clean
end

This results in, during testing, cleanup after each 100 cloudinary images, and after testing, to clean up the remainder

查看更多
登录 后发表回答