test/attachment_test.rb in thoughtbot-paperclip-2.1.5 vs test/attachment_test.rb in thoughtbot-paperclip-2.2.0
- old
+ new
@@ -79,10 +79,12 @@
"fixtures",
"5k.png"), 'rb')
@dummy.avatar = @file
end
+ teardown { @file.close }
+
should "make sure that they are interpolated correctly" do
assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
end
end
@@ -97,10 +99,12 @@
"fixtures",
"5k.png"), 'rb')
@dummy.avatar = @file
end
+ teardown { @file.close }
+
should "return the proper path" do
temporary_rails_env(@rails_env) {
assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
}
end
@@ -115,62 +119,143 @@
:convert_options => {
:all => "-do_stuff",
:thumb => "-thumbnailize"
}
@dummy = Dummy.new
+ @dummy.avatar
end
should "report the correct options when sent #extra_options_for(:thumb)" do
assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
end
should "report the correct options when sent #extra_options_for(:large)" do
assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
end
- context "when given a file" do
- setup do
- @file = File.new(File.join(File.dirname(__FILE__),
- "fixtures",
- "5k.png"), 'rb')
- Paperclip::Thumbnail.stubs(:make)
- [:thumb, :large].each do |style|
- @dummy.avatar.stubs(:extra_options_for).with(style)
- end
+ before_should "call extra_options_for(:thumb/:large)" do
+ Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb)
+ Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large)
+ end
+ end
+
+ context "An attachment with both 'normal' and hash-style styles" do
+ setup do
+ rebuild_model :styles => {
+ :normal => ["50x50#", :png],
+ :hash => { :geometry => "50x50#", :format => :png }
+ }
+ @dummy = Dummy.new
+ @attachment = @dummy.avatar
+ end
+
+ [:processors, :whiny, :convert_options, :geometry, :format].each do |field|
+ should "have the same #{field} field" do
+ assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
end
+ end
+ end
- [:thumb, :large].each do |style|
- should "call extra_options_for(#{style})" do
- @dummy.avatar.expects(:extra_options_for).with(style)
- @dummy.avatar = @file
- end
+ context "An attachment with multiple processors" do
+ setup do
+ class Paperclip::Test < Paperclip::Processor; end
+ @style_params = { :once => {:one => 1, :two => 2} }
+ rebuild_model :processors => [:thumbnail, :test], :styles => @style_params
+ @dummy = Dummy.new
+ @file = StringIO.new("...")
+ @file.stubs(:to_tempfile).returns(@file)
+ Paperclip::Test.stubs(:make).returns(@file)
+ Paperclip::Thumbnail.stubs(:make).returns(@file)
+ end
+
+ context "when assigned" do
+ setup { @dummy.avatar = @file }
+
+ before_should "call #make on all specified processors" do
+ expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => nil, :convert_options => ""})
+ Paperclip::Thumbnail.expects(:make).with(@file, expected_params).returns(@file)
+ Paperclip::Test.expects(:make).with(@file, expected_params).returns(@file)
end
end
end
+ context "Assigning an attachment with post_process hooks" do
+ setup do
+ rebuild_model :styles => { :something => "100x100#" }
+ Dummy.class_eval do
+ before_avatar_post_process :do_before_avatar
+ after_avatar_post_process :do_after_avatar
+ before_post_process :do_before_all
+ after_post_process :do_after_all
+ def do_before_avatar; end
+ def do_after_avatar; end
+ def do_before_all; end
+ def do_after_all; end
+ end
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
+ @file.stubs(:to_tempfile).returns(@file)
+ @dummy = Dummy.new
+ Paperclip::Thumbnail.stubs(:make).returns(@file)
+ @attachment = @dummy.avatar
+ end
+
+ should "call the defined callbacks when assigned" do
+ @dummy.expects(:do_before_avatar).with()
+ @dummy.expects(:do_after_avatar).with()
+ @dummy.expects(:do_before_all).with()
+ @dummy.expects(:do_after_all).with()
+ Paperclip::Thumbnail.expects(:make).returns(@file)
+ @dummy.avatar = @file
+ end
+
+ should "not cancel the processing if a before_post_process returns nil" do
+ @dummy.expects(:do_before_avatar).with().returns(nil)
+ @dummy.expects(:do_after_avatar).with()
+ @dummy.expects(:do_before_all).with().returns(nil)
+ @dummy.expects(:do_after_all).with()
+ Paperclip::Thumbnail.expects(:make).returns(@file)
+ @dummy.avatar = @file
+ end
+
+ should "cancel the processing if a before_post_process returns false" do
+ @dummy.expects(:do_before_avatar).never
+ @dummy.expects(:do_after_avatar).never
+ @dummy.expects(:do_before_all).with().returns(false)
+ @dummy.expects(:do_after_all).never
+ Paperclip::Thumbnail.expects(:make).never
+ @dummy.avatar = @file
+ end
+
+ should "cancel the processing if a before_avatar_post_process returns false" do
+ @dummy.expects(:do_before_avatar).with().returns(false)
+ @dummy.expects(:do_after_avatar).never
+ @dummy.expects(:do_before_all).with().returns(true)
+ @dummy.expects(:do_after_all).never
+ Paperclip::Thumbnail.expects(:make).never
+ @dummy.avatar = @file
+ end
+ end
+
context "Assigning an attachment" do
setup do
- rebuild_model
-
- @not_file = mock
- @not_file.stubs(:nil?).returns(false)
- @not_file.expects(:to_tempfile).returns(self)
- @not_file.expects(:original_filename).returns("filename.png\r\n")
- @not_file.expects(:content_type).returns("image/png\r\n")
- @not_file.expects(:size).returns(10).times(2)
-
+ rebuild_model :styles => { :something => "100x100#" }
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
+ @file.expects(:original_filename).returns("5k.png\n\n")
+ @file.expects(:content_type).returns("image/png\n\n")
+ @file.stubs(:to_tempfile).returns(@file)
@dummy = Dummy.new
+ Paperclip::Thumbnail.expects(:make).returns(@file)
+ @dummy.expects(:run_callbacks).with(:before_avatar_post_process, {:original => @file})
+ @dummy.expects(:run_callbacks).with(:before_post_process, {:original => @file})
+ @dummy.expects(:run_callbacks).with(:after_avatar_post_process, {:original => @file, :something => @file})
+ @dummy.expects(:run_callbacks).with(:after_post_process, {:original => @file, :something => @file})
@attachment = @dummy.avatar
- @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
- @attachment.expects(:queue_existing_for_delete)
- @attachment.expects(:post_process)
- @attachment.expects(:validate)
- @dummy.avatar = @not_file
+ @dummy.avatar = @file
end
should "strip whitespace from original_filename field" do
- assert_equal "filename.png", @dummy.avatar.original_filename
+ assert_equal "5k.png", @dummy.avatar.original_filename
end
should "strip whitespace from content_type field" do
assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
end
@@ -190,10 +275,11 @@
@dummy = Dummy.new
@attachment = @dummy.avatar
@attachment.expects(:valid_assignment?).with(@not_file).returns(true)
@attachment.expects(:queue_existing_for_delete)
@attachment.expects(:post_process)
+ @attachment.expects(:valid?).returns(true)
@attachment.expects(:validate)
@dummy.avatar = @not_file
end
should "remove strange letters and replace with underscore (_)" do
@@ -214,10 +300,12 @@
@file = File.new(File.join(File.dirname(__FILE__),
"fixtures",
"5k.png"), 'rb')
end
+ teardown { @file.close }
+
should "raise if there are not the correct columns when you try to assign" do
@other_attachment = Paperclip::Attachment.new(:not_here, @instance)
assert_raises(Paperclip::PaperclipError) do
@other_attachment.assign(@file)
end
@@ -245,16 +333,20 @@
@attachment.stubs(:instance_read).with(:updated_at).returns(Time.now)
end
should "return a correct url even if the file does not exist" do
assert_nil @attachment.to_file
- assert_match %r{^/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah)
+ assert_match %r{^/system/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah)
end
should "make sure the updated_at mtime is in the url if it is defined" do
assert_match %r{#{Time.now.to_i}$}, @attachment.url(:blah)
end
+
+ should "make sure the updated_at mtime is NOT in the url if false is passed to the url method" do
+ assert_no_match %r{#{Time.now.to_i}$}, @attachment.url(:blah, false)
+ end
context "with the updated_at field removed" do
setup do
@attachment.stubs(:instance_read).with(:updated_at).returns(nil)
end
@@ -300,12 +392,12 @@
end
should "return the real url" do
file = @attachment.to_file
assert file
- assert_match %r{^/avatars/#{@instance.id}/original/5k\.png}, @attachment.url
- assert_match %r{^/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small)
+ assert_match %r{^/system/avatars/#{@instance.id}/original/5k\.png}, @attachment.url
+ assert_match %r{^/system/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small)
file.close
end
should "commit the files to disk" do
[:large, :medium, :small].each do |style|
@@ -375,9 +467,11 @@
end
rebuild_class
@dummy = Dummy.new
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
end
+
+ teardown { @file.close }
should "not error when assigned an attachment" do
assert_nothing_raised { @dummy.avatar = @file }
end