dm-paperclip 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,142 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'geometry.rb')
6
+
7
+ class GeometryTest < Test::Unit::TestCase
8
+ context "Paperclip::Geometry" do
9
+ should "correctly report its given dimensions" do
10
+ assert @geo = Paperclip::Geometry.new(1024, 768)
11
+ assert_equal 1024, @geo.width
12
+ assert_equal 768, @geo.height
13
+ end
14
+
15
+ should "correctly create a square if the height dimension is missing" do
16
+ assert @geo = Paperclip::Geometry.new(1024)
17
+ assert_equal 1024, @geo.width
18
+ assert_equal 1024, @geo.height
19
+ end
20
+
21
+ should "correctly create a square if the width dimension is missing" do
22
+ assert @geo = Paperclip::Geometry.new(nil, 768)
23
+ assert_equal 768, @geo.width
24
+ assert_equal 768, @geo.height
25
+ end
26
+
27
+ should "be generated from a WxH-formatted string" do
28
+ assert @geo = Paperclip::Geometry.parse("800x600")
29
+ assert_equal 800, @geo.width
30
+ assert_equal 600, @geo.height
31
+ end
32
+
33
+ should "be generated from a xH-formatted string" do
34
+ assert @geo = Paperclip::Geometry.parse("x600")
35
+ assert_equal 600, @geo.width
36
+ assert_equal 600, @geo.height
37
+ end
38
+
39
+ should "be generated from a Wx-formatted string" do
40
+ assert @geo = Paperclip::Geometry.parse("800x")
41
+ assert_equal 800, @geo.width
42
+ assert_equal 800, @geo.height
43
+ end
44
+
45
+ should "ensure the modifier is nil if only one dimension present" do
46
+ assert @geo = Paperclip::Geometry.parse("123x")
47
+ assert_nil @geo.modifier
48
+ end
49
+
50
+ should "ensure the modifier is nil if not present" do
51
+ assert @geo = Paperclip::Geometry.parse("123x456")
52
+ assert_nil @geo.modifier
53
+ end
54
+
55
+ ['>', '<', '#', '@', '%', '^', '!'].each do |mod|
56
+ should "ensure the modifier #{mod} is preserved" do
57
+ assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
58
+ assert_equal mod, @geo.modifier
59
+ end
60
+ end
61
+
62
+ should "make sure the modifier gets passed during transformation_to" do
63
+ assert @src = Paperclip::Geometry.parse("123x456")
64
+ assert @dst = Paperclip::Geometry.parse("123x456>")
65
+ assert_equal "123x456>", @src.transformation_to(@dst).to_s
66
+ end
67
+
68
+ should "be generated from a file" do
69
+ file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
70
+ file = File.new(file)
71
+ assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
72
+ assert @geo.height > 0
73
+ assert @geo.width > 0
74
+ end
75
+
76
+ should "be generated from a file path" do
77
+ file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
78
+ assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
79
+ assert @geo.height > 0
80
+ assert @geo.width > 0
81
+ end
82
+
83
+ should "not generate from a bad file" do
84
+ file = "/home/This File Does Not Exist.omg"
85
+ assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
86
+ end
87
+
88
+ [['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
89
+ ['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333],
90
+ ['square', 100, 100, false, false, true, 100, 100, 1]].each do |args|
91
+ context "performing calculations on a #{args[0]} viewport" do
92
+ setup do
93
+ @geo = Paperclip::Geometry.new(args[1], args[2])
94
+ end
95
+
96
+ should "#{args[3] ? "" : "not"} be vertical" do
97
+ assert_equal args[3], @geo.vertical?
98
+ end
99
+
100
+ should "#{args[4] ? "" : "not"} be horizontal" do
101
+ assert_equal args[4], @geo.horizontal?
102
+ end
103
+
104
+ should "#{args[5] ? "" : "not"} be square" do
105
+ assert_equal args[5], @geo.square?
106
+ end
107
+
108
+ should "report that #{args[6]} is the larger dimension" do
109
+ assert_equal args[6], @geo.larger
110
+ end
111
+
112
+ should "report that #{args[7]} is the smaller dimension" do
113
+ assert_equal args[7], @geo.smaller
114
+ end
115
+
116
+ should "have an aspect ratio of #{args[8]}" do
117
+ assert_in_delta args[8], @geo.aspect, 0.0001
118
+ end
119
+ end
120
+ end
121
+
122
+ [[ [1000, 100], [64, 64], "x64", "64x64+288+0" ],
123
+ [ [100, 1000], [50, 950], "x950", "50x950+22+0" ],
124
+ [ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args|
125
+ context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do
126
+ setup do
127
+ @geo = Paperclip::Geometry.new(*args[0])
128
+ @dst = Paperclip::Geometry.new(*args[1])
129
+ @scale, @crop = @geo.transformation_to @dst, true
130
+ end
131
+
132
+ should "be able to return the correct scaling transformation geometry #{args[2]}" do
133
+ assert_equal args[2], @scale
134
+ end
135
+
136
+ should "be able to return the correct crop transformation geometry #{args[3]}" do
137
+ assert_equal args[3], @crop
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,334 @@
1
+ require 'test/helper.rb'
2
+
3
+ class IntegrationTest < Test::Unit::TestCase
4
+ context "Many models at once" do
5
+ setup do
6
+ rebuild_model
7
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
8
+ 300.times do |i|
9
+ Dummy.create! :avatar => @file
10
+ end
11
+ end
12
+
13
+ should "not exceed the open file limit" do
14
+ assert_nothing_raised do
15
+ dummies = Dummy.all
16
+ dummies.each { |dummy| dummy.avatar }
17
+ end
18
+ end
19
+ end
20
+
21
+ context "An attachment" do
22
+ setup do
23
+ rebuild_model :styles => { :thumb => "50x50#" }
24
+ @dummy = Dummy.new
25
+ @dummy.id = 1
26
+ @file = File.new(File.join(File.dirname(__FILE__),
27
+ "fixtures",
28
+ "5k.png"))
29
+ @dummy.avatar = @file
30
+ assert @dummy.save
31
+ end
32
+
33
+ should "create its thumbnails properly" do
34
+ assert_match /\b50x50\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
35
+ end
36
+
37
+ context "redefining its attachment styles" do
38
+ setup do
39
+ Dummy.class_eval do
40
+ has_attached_file :avatar, :styles => { :thumb => "150x25#" }
41
+ end
42
+ @d2 = Dummy[@dummy.id]
43
+ @d2.avatar.reprocess!
44
+ @d2.save
45
+ end
46
+
47
+ should "create its thumbnails properly" do
48
+ assert_match /\b150x25\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
49
+ end
50
+ end
51
+ end
52
+
53
+ context "A model with no attachment validation" do
54
+ setup do
55
+ rebuild_model :styles => { :large => "300x300>",
56
+ :medium => "100x100",
57
+ :thumb => ["32x32#", :gif] },
58
+ :default_style => :medium,
59
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
60
+ :path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
61
+ @dummy = Dummy.new
62
+ end
63
+
64
+ should "have its definition return false when asked about whiny_thumbnails" do
65
+ assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
66
+ end
67
+
68
+ context "when validates_attachment_thumbnails is called" do
69
+ setup do
70
+ Dummy.validates_attachment_thumbnails :avatar
71
+ end
72
+
73
+ should "have its definition return true when asked about whiny_thumbnails" do
74
+ assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
75
+ end
76
+ end
77
+
78
+ context "redefined to have attachment validations" do
79
+ setup do
80
+ rebuild_model :styles => { :large => "300x300>",
81
+ :medium => "100x100",
82
+ :thumb => ["32x32#", :gif] },
83
+ :whiny_thumbnails => true,
84
+ :default_style => :medium,
85
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
86
+ :path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
87
+ end
88
+
89
+ should "have its definition return true when asked about whiny_thumbnails" do
90
+ assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
91
+ end
92
+ end
93
+ end
94
+
95
+ context "A model with a filesystem attachment" do
96
+ setup do
97
+ rebuild_model :styles => { :large => "300x300>",
98
+ :medium => "100x100",
99
+ :thumb => ["32x32#", :gif] },
100
+ :whiny_thumbnails => true,
101
+ :default_style => :medium,
102
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
103
+ :path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
104
+ @dummy = Dummy.new
105
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
106
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
107
+
108
+ @dummy.id = 1
109
+ assert @dummy.avatar = @file
110
+ assert @dummy.valid?
111
+ assert @dummy.save
112
+ end
113
+
114
+ should "write and delete its files" do
115
+ [["434x66", :original],
116
+ ["300x46", :large],
117
+ ["100x15", :medium],
118
+ ["32x32", :thumb]].each do |geo, style|
119
+ cmd = %Q[identify -format "%wx%h" #{@dummy.avatar.to_file(style).path}]
120
+ assert_equal geo, `#{cmd}`.chomp, cmd
121
+ end
122
+
123
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
124
+
125
+ @d2 = Dummy[@dummy.id]
126
+ assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file.path}`.chomp
127
+ assert_equal "434x66", `identify -format "%wx%h" #{@d2.avatar.to_file(:original).path}`.chomp
128
+ assert_equal "300x46", `identify -format "%wx%h" #{@d2.avatar.to_file(:large).path}`.chomp
129
+ assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file(:medium).path}`.chomp
130
+ assert_equal "32x32", `identify -format "%wx%h" #{@d2.avatar.to_file(:thumb).path}`.chomp
131
+
132
+ @dummy.avatar = "not a valid file but not nil"
133
+ assert_equal File.basename(@file.path), @dummy.avatar_file_name
134
+ assert @dummy.valid?
135
+ @dummy.other = 'dirty' # must have a change in order to save
136
+ assert @dummy.save
137
+
138
+ saved_paths.each do |p|
139
+ assert File.exists?(p)
140
+ end
141
+
142
+ @dummy.avatar = nil
143
+ assert_nil @dummy.avatar_file_name
144
+ assert @dummy.valid?
145
+ assert @dummy.save
146
+
147
+ saved_paths.each do |p|
148
+ assert ! File.exists?(p)
149
+ end
150
+
151
+ @d2 = Dummy[@dummy.id]
152
+ assert_nil @d2.avatar_file_name
153
+ end
154
+
155
+ should "work exactly the same when new as when reloaded" do
156
+ @d2 = Dummy[@dummy.id]
157
+
158
+ assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
159
+ [:thumb, :medium, :large, :original].each do |style|
160
+ assert_equal @dummy.avatar.to_file(style).path, @d2.avatar.to_file(style).path
161
+ end
162
+
163
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
164
+
165
+ @d2.avatar = nil
166
+ assert @d2.save
167
+
168
+ saved_paths.each do |p|
169
+ assert ! File.exists?(p)
170
+ end
171
+ end
172
+
173
+ should "know the difference between good files, bad files, not files, and nil" do
174
+ expected = @dummy.avatar.to_file
175
+ @dummy.avatar = "not a file"
176
+ assert @dummy.valid?
177
+ assert_equal expected.path, @dummy.avatar.to_file.path
178
+
179
+ @dummy.avatar = @bad_file
180
+ assert ! @dummy.valid?
181
+ @dummy.avatar = nil
182
+ assert @dummy.valid?
183
+ end
184
+
185
+ should "know the difference between good files, bad files, not files, and nil when validating" do
186
+ Dummy.validates_attachment_presence :avatar
187
+ @d2 = Dummy[@dummy.id]
188
+ @d2.avatar = @file
189
+ assert @d2.valid?
190
+ @d2.avatar = @bad_file
191
+ assert ! @d2.valid?
192
+ @d2.avatar = nil
193
+ assert ! @d2.valid?
194
+ end
195
+
196
+ should "be able to reload without saving an not have the file disappear" do
197
+ @dummy.avatar = @file
198
+ assert @dummy.save
199
+ @dummy.avatar = nil
200
+ assert_nil @dummy.avatar_file_name
201
+ @dummy.reload
202
+ assert_equal "5k.png", @dummy.avatar_file_name
203
+ end
204
+ end
205
+
206
+ if ENV['S3_TEST_BUCKET']
207
+ def s3_files_for attachment
208
+ [:thumb, :medium, :large, :original].inject({}) do |files, style|
209
+ data = `curl '#{attachment.url(style)}' 2>/dev/null`.chomp
210
+ t = Tempfile.new("paperclip-test")
211
+ t.write(data)
212
+ t.rewind
213
+ files[style] = t
214
+ files
215
+ end
216
+ end
217
+
218
+ context "A model with an S3 attachment" do
219
+ setup do
220
+ rebuild_model :styles => { :large => "300x300>",
221
+ :medium => "100x100",
222
+ :thumb => ["32x32#", :gif] },
223
+ :storage => :s3,
224
+ :whiny_thumbnails => true,
225
+ # :s3_options => {:logger => Logger.new(StringIO.new)},
226
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
227
+ :default_style => :medium,
228
+ :bucket => ENV['S3_TEST_BUCKET'],
229
+ :path => ":class/:attachment/:id/:style/:basename.:extension"
230
+ @dummy = Dummy.new
231
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
232
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
233
+
234
+ assert @dummy.avatar = @file
235
+ assert @dummy.valid?
236
+ assert @dummy.save
237
+
238
+ @files_on_s3 = s3_files_for @dummy.avatar
239
+ end
240
+
241
+ should "write and delete its files" do
242
+ [["434x66", :original],
243
+ ["300x46", :large],
244
+ ["100x15", :medium],
245
+ ["32x32", :thumb]].each do |geo, style|
246
+ cmd = %Q[identify -format "%wx%h" #{@files_on_s3[style].path}]
247
+ assert_equal geo, `#{cmd}`.chomp, cmd
248
+ end
249
+
250
+ @d2 = Dummy[@dummy.id]
251
+ @d2_files = s3_files_for @d2.avatar
252
+ [["434x66", :original],
253
+ ["300x46", :large],
254
+ ["100x15", :medium],
255
+ ["32x32", :thumb]].each do |geo, style|
256
+ cmd = %Q[identify -format "%wx%h" #{@d2_files[style].path}]
257
+ assert_equal geo, `#{cmd}`.chomp, cmd
258
+ end
259
+
260
+ @dummy.avatar = "not a valid file but not nil"
261
+ assert_equal File.basename(@file.path), @dummy.avatar_file_name
262
+ assert @dummy.valid?
263
+ assert @dummy.save
264
+
265
+ saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
266
+
267
+ saved_keys.each do |key|
268
+ assert key.exists?
269
+ end
270
+
271
+ @dummy.avatar = nil
272
+ assert_nil @dummy.avatar_file_name
273
+ assert @dummy.valid?
274
+ assert @dummy.save
275
+
276
+ saved_keys.each do |key|
277
+ assert ! key.exists?
278
+ end
279
+
280
+ @d2 = Dummy[@dummy.id]
281
+ assert_nil @d2.avatar_file_name
282
+ end
283
+
284
+ should "work exactly the same when new as when reloaded" do
285
+ @d2 = Dummy[@dummy.id]
286
+
287
+ assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
288
+ [:thumb, :medium, :large, :original].each do |style|
289
+ assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
290
+ end
291
+
292
+ saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
293
+
294
+ @d2.avatar = nil
295
+ assert @d2.save
296
+
297
+ saved_keys.each do |key|
298
+ assert ! key.exists?
299
+ end
300
+ end
301
+
302
+ should "know the difference between good files, bad files, not files, and nil" do
303
+ expected = @dummy.avatar.to_file
304
+ @dummy.avatar = "not a file"
305
+ assert @dummy.valid?
306
+ assert_equal expected.full_name, @dummy.avatar.to_file.full_name
307
+
308
+ @dummy.avatar = @bad_file
309
+ assert ! @dummy.valid?
310
+ @dummy.avatar = nil
311
+ assert @dummy.valid?
312
+
313
+ Dummy.validates_attachment_presence :avatar
314
+ @d2 = Dummy[@dummy.id]
315
+ @d2.avatar = @file
316
+ assert @d2.valid?
317
+ @d2.avatar = @bad_file
318
+ assert ! @d2.valid?
319
+ @d2.avatar = nil
320
+ assert ! @d2.valid?
321
+ end
322
+
323
+ should "be able to reload without saving an not have the file disappear" do
324
+ @dummy.avatar = @file
325
+ assert @dummy.save
326
+ @dummy.avatar = nil
327
+ assert_nil @dummy.avatar_file_name
328
+ @dummy.reload
329
+ assert_equal "5k.png", @dummy.avatar_file_name
330
+ end
331
+ end
332
+ end
333
+ end
334
+