public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
rsanheim (author)
Fri Mar 14 11:13:27 -0700 2008
technoweenie (committer)
Fri Mar 14 11:17:48 -0700 2008
attachment_fu / test / processors / rmagick_test.rb
100644 253 lines (219 sloc) 11.104 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
 
class RmagickTest < Test::Unit::TestCase
  attachment_model Attachment
 
  if Object.const_defined?(:Magick)
    def test_should_create_image_from_uploaded_file
      assert_created do
        attachment = upload_file :filename => '/files/rails.png'
        assert_valid attachment
        assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
        assert attachment.image?
        assert !attachment.size.zero?
        #assert_equal 1784, attachment.size
        assert_equal 50, attachment.width
        assert_equal 64, attachment.height
        assert_equal '50x64', attachment.image_size
      end
    end
    
    def test_should_create_image_from_uploaded_file_with_custom_content_type
      assert_created do
        attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
        assert_valid attachment
        assert !attachment.image?
        assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
        assert !attachment.size.zero?
        #assert_equal 1784, attachment.size
        assert_nil attachment.width
        assert_nil attachment.height
        assert_equal [], attachment.thumbnails
      end
    end
    
    def test_should_create_thumbnail
      attachment = upload_file :filename => '/files/rails.png'
      
      assert_created do
        basename, ext = attachment.filename.split '.'
        thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', 50, 50)
        assert_valid thumbnail
        assert !thumbnail.size.zero?
        #assert_in_delta 4673, thumbnail.size, 2
        assert_equal 50, thumbnail.width
        assert_equal 50, thumbnail.height
        assert_equal [thumbnail.id], attachment.thumbnails.collect(&:id)
        assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
        assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
      end
    end
    
    def test_should_create_thumbnail_with_geometry_string
      attachment = upload_file :filename => '/files/rails.png'
      
      assert_created do
        basename, ext = attachment.filename.split '.'
        thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', 'x50')
        assert_valid thumbnail
        assert !thumbnail.size.zero?
        #assert_equal 3915, thumbnail.size
        assert_equal 39, thumbnail.width
        assert_equal 50, thumbnail.height
        assert_equal [thumbnail], attachment.thumbnails
        assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
        assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
      end
    end
    
    def test_should_resize_image(klass = ImageAttachment)
      attachment_model klass
      assert_equal [50, 50], attachment_model.attachment_options[:resize_to]
      attachment = upload_file :filename => '/files/rails.png'
      assert_valid attachment
      assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
      assert attachment.image?
      assert !attachment.size.zero?
      #assert_in_delta 4673, attachment.size, 2
      assert_equal 50, attachment.width
      assert_equal 50, attachment.height
    end
    
    test_against_subclass :test_should_resize_image, ImageAttachment
    
    def test_should_resize_image_with_geometry(klass = ImageOrPdfAttachment)
      attachment_model klass
      assert_equal 'x50', attachment_model.attachment_options[:resize_to]
      attachment = upload_file :filename => '/files/rails.png'
      assert_valid attachment
      assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
      assert attachment.image?
      assert !attachment.size.zero?
      #assert_equal 3915, attachment.size
      assert_equal 39, attachment.width
      assert_equal 50, attachment.height
    end
    
    test_against_subclass :test_should_resize_image_with_geometry, ImageOrPdfAttachment
    
    def test_should_give_correct_thumbnail_filenames(klass = ImageWithThumbsFileAttachment)
      attachment_model klass
      assert_created 3 do
        attachment = upload_file :filename => '/files/rails.png'
        thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
        geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
        
        [attachment, thumb, geo].each { |record| assert_valid record }
    
        assert_match /rails\.png$/, attachment.full_filename
        assert_match /rails_geometry\.png$/, attachment.full_filename(:geometry)
        assert_match /rails_thumb\.png$/, attachment.full_filename(:thumb)
      end
    end
    
    test_against_subclass :test_should_give_correct_thumbnail_filenames, ImageWithThumbsFileAttachment
    
    def test_should_automatically_create_thumbnails(klass = ImageWithThumbsAttachment)
      attachment_model klass
      assert_created 3 do
        attachment = upload_file :filename => '/files/rails.png'
        assert_valid attachment
        assert !attachment.size.zero?
        #assert_equal 1784, attachment.size
        assert_equal 55, attachment.width
        assert_equal 55, attachment.height
        assert_equal 2, attachment.thumbnails.length
        assert_equal 1.0, attachment.aspect_ratio
        
        thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
        assert !thumb.new_record?, thumb.errors.full_messages.join("\n")
        assert !thumb.size.zero?
        #assert_in_delta 4673, thumb.size, 2
        assert_equal 50, thumb.width
        assert_equal 50, thumb.height
        assert_equal 1.0, thumb.aspect_ratio
        
        geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
        assert !geo.new_record?, geo.errors.full_messages.join("\n")
        assert !geo.size.zero?
        #assert_equal 3915, geo.size
        assert_equal 50, geo.width
        assert_equal 50, geo.height
        assert_equal 1.0, geo.aspect_ratio
      end
    end
    
    test_against_subclass :test_should_automatically_create_thumbnails, ImageWithThumbsAttachment
    
    # same as above method, but test it on a file model
    test_against_class :test_should_automatically_create_thumbnails, ImageWithThumbsFileAttachment
    test_against_subclass :test_should_automatically_create_thumbnails_on_class, ImageWithThumbsFileAttachment
    
    def test_should_use_thumbnail_subclass(klass = ImageWithThumbsClassFileAttachment)
      attachment_model klass
      attachment = nil
      assert_difference ImageThumbnail, :count do
        attachment = upload_file :filename => '/files/rails.png'
        assert_valid attachment
      end
      assert_kind_of ImageThumbnail, attachment.thumbnails.first
      assert_equal attachment.id, attachment.thumbnails.first.parent.id
      assert_kind_of FileAttachment, attachment.thumbnails.first.parent
      assert_equal 'rails_thumb.png', attachment.thumbnails.first.filename
      assert_equal attachment.thumbnails.first.full_filename, attachment.full_filename(attachment.thumbnails.first.thumbnail),
        "#full_filename does not use thumbnail class' path."
      assert_equal attachment.destroy, attachment
    end
    
    test_against_subclass :test_should_use_thumbnail_subclass, ImageWithThumbsClassFileAttachment
    
    def test_should_remove_old_thumbnail_files_when_updating(klass = ImageWithThumbsFileAttachment)
      attachment_model klass
      attachment = nil
      assert_created 3 do
        attachment = upload_file :filename => '/files/rails.png'
      end
    
      old_filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
    
      assert_not_created do
        use_temp_file "files/rails.png" do |file|
          attachment.filename = 'rails2.png'
          attachment.temp_path = File.join(fixture_path, file)
          attachment.save
          new_filenames = [attachment.reload.full_filename] + attachment.thumbnails.collect { |t| t.reload.full_filename }
          new_filenames.each { |f| assert File.exists?(f), "#{f} does not exist" }
          old_filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
        end
      end
    end
    
    test_against_subclass :test_should_remove_old_thumbnail_files_when_updating, ImageWithThumbsFileAttachment
    
    def test_should_delete_file_when_in_file_system_when_attachment_record_destroyed(klass = ImageWithThumbsFileAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'
      filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
      filenames.each { |f| assert File.exists?(f), "#{f} never existed to delete on destroy" }
      attachment.destroy
      filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
    end
    
    test_against_subclass :test_should_delete_file_when_in_file_system_when_attachment_record_destroyed, ImageWithThumbsFileAttachment
    
    def test_should_have_full_filename_method(klass = FileAttachment)
      attachment_model klass
      attachment = upload_file :filename => '/files/rails.png'
      assert_respond_to attachment, :full_filename
    end
    
    test_against_subclass :test_should_have_full_filename_method, FileAttachment
    
    def test_should_overwrite_old_thumbnail_records_when_updating(klass = ImageWithThumbsAttachment)
      attachment_model klass
      attachment = nil
      assert_created 3 do
        attachment = upload_file :filename => '/files/rails.png'
      end
      assert_not_created do # no new db_file records
        use_temp_file "files/rails.png" do |file|
          attachment.filename = 'rails2.png'
          # The above test (#test_should_have_full_filename_method) to pass before be can set the temp_path below --
          # #temp_path calls #full_filename, which is not getting mixed into the attachment. Maybe we don't need to
          # set temp_path at all?
          #
          # attachment.temp_path = File.join(fixture_path, file)
          attachment.save!
        end
      end
    end
    
    test_against_subclass :test_should_overwrite_old_thumbnail_records_when_updating, ImageWithThumbsAttachment
    
    def test_should_overwrite_old_thumbnail_records_when_renaming(klass = ImageWithThumbsAttachment)
      attachment_model klass
      attachment = nil
      assert_created 3 do
        attachment = upload_file :class => klass, :filename => '/files/rails.png'
      end
      assert_not_created do # no new db_file records
        attachment.filename = 'rails2.png'
        attachment.save
        assert !attachment.reload.size.zero?
        assert_equal 'rails2.png', attachment.filename
      end
    end
    
    test_against_subclass :test_should_overwrite_old_thumbnail_records_when_renaming, ImageWithThumbsAttachment
  else
    def test_flunk
      puts "RMagick not installed, no tests running"
    end
  end
end