本文介绍了在rspec / cucumber运行后清除cloudinary文件上传的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fixture_file_upload 在我的 FactoryGirl 方法测试文件上传。问题是,在清理数据库后,所有这些上传的文件保留在 Cloudinary 上。

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.

我一直使用 Cloudinary :: Api.delete_resources 使用rake任务摆脱它们,我宁愿立即清理它们之前 DatabaseCleaner 删除所有相关的公共ID。

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.

我应该干扰 DatabaseCleaner 以从 Cloudinary

推荐答案

基于@ phoet的输入,并且考虑到cloudinary限制了API调用的数量在一天的时间,以及您可以清理在一个单一的调用图像的数量,我创建了一个类

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


$ b b

我使用如下:在我的工厂女孩​​文件,我打电话立即添加任何public_ids后创建一个广告:

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


$ b b env.rb中的

,我添加了

in env.rb, I added

at_exit do
  CleanupCloudinary.clean
end

以及spec_helper.rb

as well as in spec_helper.rb

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

这会导致在测试期间,在每100个云图像之后进行清理,并在测试后清理剩余的

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

这篇关于在rspec / cucumber运行后清除cloudinary文件上传的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:21