Commit 204c917b

mo khan <mo@mokhan.ca>
2015-01-21 04:21:33
add test to re-process an uploaded image.
1 parent e828e53
app/jobs/re_process_photo_job.rb
@@ -3,9 +3,17 @@ class ReProcessPhotoJob < ActiveJob::Base
 
   def perform(photo)
     key = OriginalVersion.new(photo).create_key
-    @storage.download(key) do |file|
-      temp_file = move_to_temporary_storage(file.path, File.basename(key))
+    blob_storage.download(key) do |file|
+      temp_file = file_storage.store(file)
       ProcessPhotoJob.perform_later(photo, temp_file)
     end
   end
+
+  def file_storage
+    TemporaryStorage.new
+  end
+
+  def blob_storage
+    Spank::IOC.resolve(:blob_storage)
+  end
 end
spec/jobs/re_process_photo_job_spec.rb
@@ -1,5 +1,29 @@
-require 'rails_helper'
+require "rails_helper"
 
-RSpec.describe ReProcessPhotoJob, :type => :job do
-  pending "add some examples to (or delete) #{__FILE__}"
+describe ReProcessPhotoJob, type: :job do
+  subject { ReProcessPhotoJob.new }
+
+  describe "#perform" do
+    let(:file_storage) { double(store: true) }
+    let(:blob_storage) { double(download: true) }
+    let(:photo) { create(:photo) }
+    let(:key) { "uploads/photo/image/#{photo.id}/#{photo.image}" }
+    let(:file) { double }
+    let(:temp_file) { double }
+
+    before :each do
+      allow(subject).to receive(:file_storage).and_return(file_storage)
+      allow(subject).to receive(:blob_storage).and_return(blob_storage)
+    end
+
+    it "processes the original file" do
+      allow(blob_storage).to receive(:download).with(key).and_yield(file)
+      allow(file_storage).to receive(:store).with(file).and_return(temp_file)
+      allow(ProcessPhotoJob).to receive(:perform_later)
+
+      subject.perform(photo)
+
+      expect(ProcessPhotoJob).to have_received(:perform_later).with(photo, temp_file)
+    end
+  end
 end