jcnetdev-paperclip 1.0.20080704

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', '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,331 @@
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.find(: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
+ @file = File.new(File.join(File.dirname(__FILE__),
26
+ "fixtures",
27
+ "5k.png"))
28
+ @dummy.avatar = @file
29
+ assert @dummy.save
30
+ end
31
+
32
+ should "create its thumbnails properly" do
33
+ assert_match /\b50x50\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
34
+ end
35
+
36
+ context "redefining its attachment styles" do
37
+ setup do
38
+ Dummy.class_eval do
39
+ has_attached_file :avatar, :styles => { :thumb => "150x25#" }
40
+ end
41
+ @d2 = Dummy.find(@dummy.id)
42
+ @d2.avatar.reprocess!
43
+ @d2.save
44
+ end
45
+
46
+ should "create its thumbnails properly" do
47
+ assert_match /\b150x25\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
48
+ end
49
+ end
50
+ end
51
+
52
+ context "A model with no attachment validation" do
53
+ setup do
54
+ rebuild_model :styles => { :large => "300x300>",
55
+ :medium => "100x100",
56
+ :thumb => ["32x32#", :gif] },
57
+ :default_style => :medium,
58
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
59
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
60
+ @dummy = Dummy.new
61
+ end
62
+
63
+ should "have its definition return false when asked about whiny_thumbnails" do
64
+ assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
65
+ end
66
+
67
+ context "when validates_attachment_thumbnails is called" do
68
+ setup do
69
+ Dummy.validates_attachment_thumbnails :avatar
70
+ end
71
+
72
+ should "have its definition return true when asked about whiny_thumbnails" do
73
+ assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
74
+ end
75
+ end
76
+
77
+ context "redefined to have attachment validations" do
78
+ setup do
79
+ rebuild_model :styles => { :large => "300x300>",
80
+ :medium => "100x100",
81
+ :thumb => ["32x32#", :gif] },
82
+ :whiny_thumbnails => true,
83
+ :default_style => :medium,
84
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
85
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
86
+ end
87
+
88
+ should "have its definition return true when asked about whiny_thumbnails" do
89
+ assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
90
+ end
91
+ end
92
+ end
93
+
94
+ context "A model with a filesystem attachment" do
95
+ setup do
96
+ rebuild_model :styles => { :large => "300x300>",
97
+ :medium => "100x100",
98
+ :thumb => ["32x32#", :gif] },
99
+ :whiny_thumbnails => true,
100
+ :default_style => :medium,
101
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
102
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
103
+ @dummy = Dummy.new
104
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
105
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
106
+
107
+ assert @dummy.avatar = @file
108
+ assert @dummy.valid?
109
+ assert @dummy.save
110
+ end
111
+
112
+ should "write and delete its files" do
113
+ [["434x66", :original],
114
+ ["300x46", :large],
115
+ ["100x15", :medium],
116
+ ["32x32", :thumb]].each do |geo, style|
117
+ cmd = %Q[identify -format "%wx%h" #{@dummy.avatar.to_file(style).path}]
118
+ assert_equal geo, `#{cmd}`.chomp, cmd
119
+ end
120
+
121
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
122
+
123
+ @d2 = Dummy.find(@dummy.id)
124
+ assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file.path}`.chomp
125
+ assert_equal "434x66", `identify -format "%wx%h" #{@d2.avatar.to_file(:original).path}`.chomp
126
+ assert_equal "300x46", `identify -format "%wx%h" #{@d2.avatar.to_file(:large).path}`.chomp
127
+ assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file(:medium).path}`.chomp
128
+ assert_equal "32x32", `identify -format "%wx%h" #{@d2.avatar.to_file(:thumb).path}`.chomp
129
+
130
+ @dummy.avatar = "not a valid file but not nil"
131
+ assert_equal File.basename(@file.path), @dummy.avatar_file_name
132
+ assert @dummy.valid?
133
+ assert @dummy.save
134
+
135
+ saved_paths.each do |p|
136
+ assert File.exists?(p)
137
+ end
138
+
139
+ @dummy.avatar = nil
140
+ assert_nil @dummy.avatar_file_name
141
+ assert @dummy.valid?
142
+ assert @dummy.save
143
+
144
+ saved_paths.each do |p|
145
+ assert ! File.exists?(p)
146
+ end
147
+
148
+ @d2 = Dummy.find(@dummy.id)
149
+ assert_nil @d2.avatar_file_name
150
+ end
151
+
152
+ should "work exactly the same when new as when reloaded" do
153
+ @d2 = Dummy.find(@dummy.id)
154
+
155
+ assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
156
+ [:thumb, :medium, :large, :original].each do |style|
157
+ assert_equal @dummy.avatar.to_file(style).path, @d2.avatar.to_file(style).path
158
+ end
159
+
160
+ saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
161
+
162
+ @d2.avatar = nil
163
+ assert @d2.save
164
+
165
+ saved_paths.each do |p|
166
+ assert ! File.exists?(p)
167
+ end
168
+ end
169
+
170
+ should "know the difference between good files, bad files, not files, and nil" do
171
+ expected = @dummy.avatar.to_file
172
+ @dummy.avatar = "not a file"
173
+ assert @dummy.valid?
174
+ assert_equal expected.path, @dummy.avatar.to_file.path
175
+
176
+ @dummy.avatar = @bad_file
177
+ assert ! @dummy.valid?
178
+ @dummy.avatar = nil
179
+ assert @dummy.valid?
180
+ end
181
+
182
+ should "know the difference between good files, bad files, not files, and nil when validating" do
183
+ Dummy.validates_attachment_presence :avatar
184
+ @d2 = Dummy.find(@dummy.id)
185
+ @d2.avatar = @file
186
+ assert @d2.valid?
187
+ @d2.avatar = @bad_file
188
+ assert ! @d2.valid?
189
+ @d2.avatar = nil
190
+ assert ! @d2.valid?
191
+ end
192
+
193
+ should "be able to reload without saving an not have the file disappear" do
194
+ @dummy.avatar = @file
195
+ assert @dummy.save
196
+ @dummy.avatar = nil
197
+ assert_nil @dummy.avatar_file_name
198
+ @dummy.reload
199
+ assert_equal "5k.png", @dummy.avatar_file_name
200
+ end
201
+ end
202
+
203
+ if ENV['S3_TEST_BUCKET']
204
+ def s3_files_for attachment
205
+ [:thumb, :medium, :large, :original].inject({}) do |files, style|
206
+ data = `curl '#{attachment.url(style)}' 2>/dev/null`.chomp
207
+ t = Tempfile.new("paperclip-test")
208
+ t.write(data)
209
+ t.rewind
210
+ files[style] = t
211
+ files
212
+ end
213
+ end
214
+
215
+ context "A model with an S3 attachment" do
216
+ setup do
217
+ rebuild_model :styles => { :large => "300x300>",
218
+ :medium => "100x100",
219
+ :thumb => ["32x32#", :gif] },
220
+ :storage => :s3,
221
+ :whiny_thumbnails => true,
222
+ # :s3_options => {:logger => Logger.new(StringIO.new)},
223
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
224
+ :default_style => :medium,
225
+ :bucket => ENV['S3_TEST_BUCKET'],
226
+ :path => ":class/:attachment/:id/:style/:basename.:extension"
227
+ @dummy = Dummy.new
228
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
229
+ @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
230
+
231
+ assert @dummy.avatar = @file
232
+ assert @dummy.valid?
233
+ assert @dummy.save
234
+
235
+ @files_on_s3 = s3_files_for @dummy.avatar
236
+ end
237
+
238
+ should "write and delete its files" do
239
+ [["434x66", :original],
240
+ ["300x46", :large],
241
+ ["100x15", :medium],
242
+ ["32x32", :thumb]].each do |geo, style|
243
+ cmd = %Q[identify -format "%wx%h" #{@files_on_s3[style].path}]
244
+ assert_equal geo, `#{cmd}`.chomp, cmd
245
+ end
246
+
247
+ @d2 = Dummy.find(@dummy.id)
248
+ @d2_files = s3_files_for @d2.avatar
249
+ [["434x66", :original],
250
+ ["300x46", :large],
251
+ ["100x15", :medium],
252
+ ["32x32", :thumb]].each do |geo, style|
253
+ cmd = %Q[identify -format "%wx%h" #{@d2_files[style].path}]
254
+ assert_equal geo, `#{cmd}`.chomp, cmd
255
+ end
256
+
257
+ @dummy.avatar = "not a valid file but not nil"
258
+ assert_equal File.basename(@file.path), @dummy.avatar_file_name
259
+ assert @dummy.valid?
260
+ assert @dummy.save
261
+
262
+ saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
263
+
264
+ saved_keys.each do |key|
265
+ assert key.exists?
266
+ end
267
+
268
+ @dummy.avatar = nil
269
+ assert_nil @dummy.avatar_file_name
270
+ assert @dummy.valid?
271
+ assert @dummy.save
272
+
273
+ saved_keys.each do |key|
274
+ assert ! key.exists?
275
+ end
276
+
277
+ @d2 = Dummy.find(@dummy.id)
278
+ assert_nil @d2.avatar_file_name
279
+ end
280
+
281
+ should "work exactly the same when new as when reloaded" do
282
+ @d2 = Dummy.find(@dummy.id)
283
+
284
+ assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
285
+ [:thumb, :medium, :large, :original].each do |style|
286
+ assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
287
+ end
288
+
289
+ saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
290
+
291
+ @d2.avatar = nil
292
+ assert @d2.save
293
+
294
+ saved_keys.each do |key|
295
+ assert ! key.exists?
296
+ end
297
+ end
298
+
299
+ should "know the difference between good files, bad files, not files, and nil" do
300
+ expected = @dummy.avatar.to_file
301
+ @dummy.avatar = "not a file"
302
+ assert @dummy.valid?
303
+ assert_equal expected.full_name, @dummy.avatar.to_file.full_name
304
+
305
+ @dummy.avatar = @bad_file
306
+ assert ! @dummy.valid?
307
+ @dummy.avatar = nil
308
+ assert @dummy.valid?
309
+
310
+ Dummy.validates_attachment_presence :avatar
311
+ @d2 = Dummy.find(@dummy.id)
312
+ @d2.avatar = @file
313
+ assert @d2.valid?
314
+ @d2.avatar = @bad_file
315
+ assert ! @d2.valid?
316
+ @d2.avatar = nil
317
+ assert ! @d2.valid?
318
+ end
319
+
320
+ should "be able to reload without saving an not have the file disappear" do
321
+ @dummy.avatar = @file
322
+ assert @dummy.save
323
+ @dummy.avatar = nil
324
+ assert_nil @dummy.avatar_file_name
325
+ @dummy.reload
326
+ assert_equal "5k.png", @dummy.avatar_file_name
327
+ end
328
+ end
329
+ end
330
+ end
331
+