1 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
3 class RmagickTest < Test::Unit::TestCase
4 attachment_model Attachment
6 if Object.const_defined?(:Magick)
7 def test_should_create_image_from_uploaded_file
9 attachment = upload_file :filename => '/files/rails.png'
10 assert_valid attachment
11 assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
12 assert attachment.image?
13 assert !attachment.size.zero?
14 #assert_equal 1784, attachment.size
15 assert_equal 50, attachment.width
16 assert_equal 64, attachment.height
17 assert_equal '50x64', attachment.image_size
21 def test_should_create_image_from_uploaded_file_with_custom_content_type
23 attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
24 assert_valid attachment
25 assert !attachment.image?
26 assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
27 assert !attachment.size.zero?
28 #assert_equal 1784, attachment.size
29 assert_nil attachment.width
30 assert_nil attachment.height
31 assert_equal [], attachment.thumbnails
35 def test_should_create_thumbnail
36 attachment = upload_file :filename => '/files/rails.png'
39 basename, ext = attachment.filename.split '.'
40 thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', 50, 50)
41 assert_valid thumbnail
42 assert !thumbnail.size.zero?
43 #assert_in_delta 4673, thumbnail.size, 2
44 assert_equal 50, thumbnail.width
45 assert_equal 50, thumbnail.height
46 assert_equal [thumbnail.id], attachment.thumbnails.collect(&:id)
47 assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
48 assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
52 def test_should_create_thumbnail_with_geometry_string
53 attachment = upload_file :filename => '/files/rails.png'
56 basename, ext = attachment.filename.split '.'
57 thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', 'x50')
58 assert_valid thumbnail
59 assert !thumbnail.size.zero?
60 #assert_equal 3915, thumbnail.size
61 assert_equal 39, thumbnail.width
62 assert_equal 50, thumbnail.height
63 assert_equal [thumbnail], attachment.thumbnails
64 assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
65 assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
69 def test_should_resize_image(klass = ImageAttachment)
70 attachment_model klass
71 assert_equal [50, 50], attachment_model.attachment_options[:resize_to]
72 attachment = upload_file :filename => '/files/rails.png'
73 assert_valid attachment
74 assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
75 assert attachment.image?
76 assert !attachment.size.zero?
77 #assert_in_delta 4673, attachment.size, 2
78 assert_equal 50, attachment.width
79 assert_equal 50, attachment.height
82 test_against_subclass :test_should_resize_image, ImageAttachment
84 def test_should_resize_image_with_geometry(klass = ImageOrPdfAttachment)
85 attachment_model klass
86 assert_equal 'x50', attachment_model.attachment_options[:resize_to]
87 attachment = upload_file :filename => '/files/rails.png'
88 assert_valid attachment
89 assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
90 assert attachment.image?
91 assert !attachment.size.zero?
92 #assert_equal 3915, attachment.size
93 assert_equal 39, attachment.width
94 assert_equal 50, attachment.height
97 test_against_subclass :test_should_resize_image_with_geometry, ImageOrPdfAttachment
99 def test_should_give_correct_thumbnail_filenames(klass = ImageWithThumbsFileAttachment)
100 attachment_model klass
102 attachment = upload_file :filename => '/files/rails.png'
103 thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
104 geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
106 [attachment, thumb, geo].each { |record| assert_valid record }
108 assert_match /rails\.png$/, attachment.full_filename
109 assert_match /rails_geometry\.png$/, attachment.full_filename(:geometry)
110 assert_match /rails_thumb\.png$/, attachment.full_filename(:thumb)
114 test_against_subclass :test_should_give_correct_thumbnail_filenames, ImageWithThumbsFileAttachment
116 def test_should_automatically_create_thumbnails(klass = ImageWithThumbsAttachment)
117 attachment_model klass
119 attachment = upload_file :filename => '/files/rails.png'
120 assert_valid attachment
121 assert !attachment.size.zero?
122 #assert_equal 1784, attachment.size
123 assert_equal 55, attachment.width
124 assert_equal 55, attachment.height
125 assert_equal 2, attachment.thumbnails.length
126 assert_equal 1.0, attachment.aspect_ratio
128 thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
129 assert !thumb.new_record?, thumb.errors.full_messages.join("\n")
130 assert !thumb.size.zero?
131 #assert_in_delta 4673, thumb.size, 2
132 assert_equal 50, thumb.width
133 assert_equal 50, thumb.height
134 assert_equal 1.0, thumb.aspect_ratio
136 geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
137 assert !geo.new_record?, geo.errors.full_messages.join("\n")
138 assert !geo.size.zero?
139 #assert_equal 3915, geo.size
140 assert_equal 50, geo.width
141 assert_equal 50, geo.height
142 assert_equal 1.0, geo.aspect_ratio
146 test_against_subclass :test_should_automatically_create_thumbnails, ImageWithThumbsAttachment
148 # same as above method, but test it on a file model
149 test_against_class :test_should_automatically_create_thumbnails, ImageWithThumbsFileAttachment
150 test_against_subclass :test_should_automatically_create_thumbnails_on_class, ImageWithThumbsFileAttachment
152 def test_should_use_thumbnail_subclass(klass = ImageWithThumbsClassFileAttachment)
153 attachment_model klass
155 assert_difference ImageThumbnail, :count do
156 attachment = upload_file :filename => '/files/rails.png'
157 assert_valid attachment
159 assert_kind_of ImageThumbnail, attachment.thumbnails.first
160 if attachment.thumbnails.first.respond_to?(:parent)
161 assert_equal attachment.id, attachment.thumbnails.first.parent.id
162 assert_kind_of FileAttachment, attachment.thumbnails.first.parent
164 assert_equal 'rails_thumb.png', attachment.thumbnails.first.filename
165 assert_equal attachment.thumbnails.first.full_filename, attachment.full_filename(attachment.thumbnails.first.thumbnail),
166 "#full_filename does not use thumbnail class' path."
167 assert_equal attachment.destroy, attachment
170 test_against_subclass :test_should_use_thumbnail_subclass, ImageWithThumbsClassFileAttachment
172 def test_should_remove_old_thumbnail_files_when_updating(klass = ImageWithThumbsFileAttachment)
173 attachment_model klass
176 attachment = upload_file :filename => '/files/rails.png'
179 old_filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
181 assert_not_created do
182 use_temp_file "files/rails.png" do |file|
183 attachment.filename = 'rails2.png'
184 attachment.temp_paths.unshift File.join(fixture_path, file)
186 new_filenames = [attachment.reload.full_filename] + attachment.thumbnails.collect { |t| t.reload.full_filename }
187 new_filenames.each { |f| assert File.exists?(f), "#{f} does not exist" }
188 old_filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
193 test_against_subclass :test_should_remove_old_thumbnail_files_when_updating, ImageWithThumbsFileAttachment
195 def test_should_delete_file_when_in_file_system_when_attachment_record_destroyed(klass = ImageWithThumbsFileAttachment)
196 attachment_model klass
197 attachment = upload_file :filename => '/files/rails.png'
198 filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
199 filenames.each { |f| assert File.exists?(f), "#{f} never existed to delete on destroy" }
201 filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
204 test_against_subclass :test_should_delete_file_when_in_file_system_when_attachment_record_destroyed, ImageWithThumbsFileAttachment
206 def test_should_have_full_filename_method(klass = FileAttachment)
207 attachment_model klass
208 attachment = upload_file :filename => '/files/rails.png'
209 assert_respond_to attachment, :full_filename
212 test_against_subclass :test_should_have_full_filename_method, FileAttachment
214 def test_should_overwrite_old_thumbnail_records_when_updating(klass = ImageWithThumbsAttachment)
215 attachment_model klass
218 attachment = upload_file :filename => '/files/rails.png'
220 assert_not_created do # no new db_file records
221 use_temp_file "files/rails.png" do |file|
222 attachment.filename = 'rails2.png'
223 # The above test (#test_should_have_full_filename_method) to pass before be can set the temp_path below --
224 # #temp_path calls #full_filename, which is not getting mixed into the attachment. Maybe we don't need to
225 # set temp_path at all?
227 # attachment.temp_paths.unshift File.join(fixture_path, file)
233 test_against_subclass :test_should_overwrite_old_thumbnail_records_when_updating, ImageWithThumbsAttachment
235 def test_should_overwrite_old_thumbnail_records_when_renaming(klass = ImageWithThumbsAttachment)
236 attachment_model klass
239 attachment = upload_file :class => klass, :filename => '/files/rails.png'
241 assert_not_created do # no new db_file records
242 attachment.filename = 'rails2.png'
244 assert !attachment.reload.size.zero?
245 assert_equal 'rails2.png', attachment.filename
249 test_against_subclass :test_should_overwrite_old_thumbnail_records_when_renaming, ImageWithThumbsAttachment
252 puts "RMagick not installed, no tests running"