mardi 3 juillet 2018

How would you write a test for zip file generation?

I'm working on a service/workflow that takes a number of data files associated with a Project and creates a zip of them. These files are managed by Carrierwave. I am using the native Rails TestCase/Minitest and was wondering what is the best way to test the generation of the zip file?

At the moment, the code that would grab the files and write them to a zip looks something like:

def generate_zip(project)
  project_tracks = project.project_tracks

  # base temp dir
  temp_dir = Dir.mktmpdir

  # path for zip we are about to create
  zip_path = File.join(temp_dir, "#{project.name}.zip")

  Zip::ZipOutputStream.open(zip_path) do |zos|
    project_tracks.each do |project_track|
      path = project_track.track.path
      zos.put_next_entry(path)
      zos.write project_track.track.file.read
    end
  end
end

Initially my thought was to create the zip files and compare the bytes? Was wondering if this was a good idea or if there is a better/faster way to test this logic.

Aucun commentaire:

Enregistrer un commentaire