spec/unit/attachment/base_spec.rb in bulldog-0.1.1 vs spec/unit/attachment/base_spec.rb in bulldog-0.2.0

- old
+ new

@@ -31,11 +31,11 @@ def png_path "#{temporary_directory}/png.png" end it "should return the path of the given style, interpolated from the path template" do - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') @thing.stubs(:id).returns(5) @thing.photo.path(:original).should == original_path @thing.photo.path(:small).should == small_path end @@ -43,11 +43,11 @@ before do spec = self Thing.attachment_reflections[:photo].configure do path "#{spec.temporary_directory}/:style.:extension" end - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') end it "should use the extension of the original file for the original style" do @thing.photo.path(:original).should == "#{temporary_directory}/original.jpg" end @@ -60,11 +60,11 @@ before do spec = self Thing.attachment_reflections[:photo].configure do path "#{spec.temporary_directory}/:style.xyz" end - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') end it "should use the extension of the path template for the original style" do @thing.photo.path(:original).should == "#{temporary_directory}/original.xyz" end @@ -80,11 +80,11 @@ style :small, {} default_style :small end it "should default to the attachment's default style" do - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') @thing.photo.path.should == "/tmp/small.jpg" end end end @@ -97,11 +97,11 @@ style :small style :png, :format => :png end it "should return the url of the given style, interpolated from the url template" do - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') @thing.photo.url(:original).should == "/assets/original.jpg" @thing.photo.url(:small).should == "/assets/small.jpg" end describe "when the :extension interpolation key is used" do @@ -109,11 +109,11 @@ spec = self Thing.attachment_reflections[:photo].configure do path "/tmp/:style.:extension" url "/assets/:style.:extension" end - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') end it "should use the extension of the original file for the original style" do @thing.photo.url(:original).should == "/assets/original.jpg" end @@ -128,11 +128,11 @@ spec = self Thing.attachment_reflections[:photo].configure do path "/tmp/:style.xyz" url "/assets/:style.xyz" end - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') end it "should use the extension of the url template for the original style" do @thing.photo.url(:original).should == "/assets/original.xyz" end @@ -148,11 +148,11 @@ style :small, {} default_style :small end it "should default to the attachment's default style" do - @thing.photo = test_image_file + @thing.photo = uploaded_file('test.jpg') @thing.photo.url.should == "/assets/small.jpg" end end end @@ -166,26 +166,16 @@ def original_path "#{temporary_directory}/#{@thing.id}.original.jpg" end - def with_temporary_file(path, content) - FileUtils.mkdir_p File.dirname(path) - open(path, 'w'){|f| f.print '...'} - begin - yield path - ensure - File.delete(path) - end - end - before do - @thing = Thing.new(:photo => test_image_file) + @thing = Thing.new(:photo => uploaded_file('test.jpg')) end it "should return the size of the file" do - @thing.photo.file_size.should == File.size(test_image_path) + @thing.photo.file_size.should == File.size("#{ROOT}/spec/data/test.jpg") end end describe "#file_name" do use_model_class(:Thing, :photo_file_name => :string) @@ -198,26 +188,16 @@ def original_path "#{temporary_directory}/#{@thing.id}.original.jpg" end - def with_temporary_file(path, content) - FileUtils.mkdir_p File.dirname(path) - open(path, 'w'){|f| f.print '...'} - begin - yield path - ensure - File.delete(path) - end - end - before do - @thing = Thing.new(:photo => test_image_file) + @thing = Thing.new(:photo => uploaded_file('test.jpg')) end it "should return the original base name of the file" do - @thing.photo.file_name.should == File.basename(test_image_path) + @thing.photo.file_name.should == File.basename("#{ROOT}/spec/data/test.jpg") end end def with_test_processor(options, &block) test_processor_class = Class.new(Processor::Base) do @@ -243,11 +223,11 @@ style :normal process :on => :test_event do context = self end end - thing = Thing.new(:photo => test_image_file) + thing = Thing.new(:photo => uploaded_file('test.jpg')) thing.photo.stubs(:default_processor_type).returns(:test) thing.photo.process(:test_event) context.should be_a(Processor::Test) end @@ -257,11 +237,11 @@ style :normal process :on => :test_event, :with => :test do context = self end end - thing = Thing.new(:photo => test_image_file) + thing = Thing.new(:photo => uploaded_file('test.jpg')) thing.photo.process(:test_event) context.should be_a(Processor::Test) end it "should not run any processors if no attachment is set" do @@ -284,11 +264,11 @@ style :large, :size => '1000x1000' process :on => :test_event, :styles => [:small], :with => :test do styles = self.styles end end - thing = Thing.new(:photo => test_image_file) + thing = Thing.new(:photo => uploaded_file('test.jpg')) thing.photo.process(:test_event) styles.should be_a(StyleSet) styles.map(&:name).should == [:small] end @@ -296,22 +276,22 @@ with_test_processor(:error => false) do Thing.has_attachment :attachment do style :one process(:on => :event, :with => :test){} end - thing = Thing.new(:attachment => test_empty_file) + thing = Thing.new(:attachment => uploaded_file('empty.txt')) thing.attachment.process(:event).should be_true end end it "should return false if an error was encountered" do with_test_processor(:error => true) do Thing.has_attachment :attachment do style :one process(:on => :event, :with => :test){} end - thing = Thing.new(:attachment => test_empty_file) + thing = Thing.new(:attachment => uploaded_file('empty.txt')) thing.attachment.process(:event).should be_false end end end @@ -322,22 +302,22 @@ with_test_processor(:error => true) do Thing.has_attachment :attachment do style :one process :on => :event, :with => :test end - thing = Thing.new(:attachment => test_empty_file) + thing = Thing.new(:attachment => uploaded_file('empty.txt')) lambda{thing.attachment.process!(:event)}.should raise_error(ActiveRecord::RecordInvalid) end end it "should not raise ActiveRecord::RecordInvalid if there are no errors present" do with_test_processor(:error => false) do Thing.has_attachment :attachment do style :one process :on => :event, :with => :test end - thing = Thing.new(:attachment => test_empty_file) + thing = Thing.new(:attachment => uploaded_file('empty.txt')) lambda{thing.attachment.process!(:event)}.should_not raise_error(ActiveRecord::RecordInvalid) end end it "should pass the given options to #process" do @@ -348,11 +328,11 @@ style :two process :on => :event, :with => :base do styles = self.styles.map(&:name) end end - thing = Thing.new(:attachment => test_empty_file) + thing = Thing.new(:attachment => uploaded_file('empty.txt')) thing.attachment.process!(:event, :styles => [:one]) styles.should == [:one] end end end @@ -363,24 +343,24 @@ :photo_file_size => :integer, :photo_content_type => :string) before do Thing.has_attachment :photo - @thing = Thing.new(:photo => uploaded_file_with_content('test.jpg', "\xff\xd8")) + @thing = Thing.new(:photo => uploaded_file('test.jpg')) end it "should set the stored attributes on assignment" do @thing.photo_file_name.should == 'test.jpg' - @thing.photo_file_size.should == 2 - @thing.photo_content_type.should =~ /image\/jpeg/ + @thing.photo_file_size.should == File.size('spec/data/test.jpg') + @thing.photo_content_type.should include('image/jpeg') end it "should successfully roundtrip the stored attributes" do warp_ahead 1.minute @thing.save @thing = Thing.find(@thing.id) @thing.photo_file_name.should == 'test.jpg' - @thing.photo_file_size.should == 2 - @thing.photo_content_type.should =~ /image\/jpeg/ + @thing.photo_file_size.should == File.size('spec/data/test.jpg') + @thing.photo_content_type.should include('image/jpeg') end end end