dm-paperclip-r3 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,361 @@
1
+ require 'test/helper'
2
+
3
+ class Dummy
4
+ # This is a dummy class
5
+ end
6
+
7
+ class AttachmentTest < Test::Unit::TestCase
8
+ context "Attachment default_options" do
9
+ setup do
10
+ rebuild_model
11
+ @old_default_options = Paperclip::Attachment.default_options.dup
12
+ @new_default_options = @old_default_options.merge({
13
+ :path => "argle/bargle",
14
+ :url => "fooferon",
15
+ :default_url => "not here.png"
16
+ })
17
+ end
18
+
19
+ teardown do
20
+ Paperclip::Attachment.default_options.merge! @old_default_options
21
+ end
22
+
23
+ should "be overrideable" do
24
+ Paperclip::Attachment.default_options.merge!(@new_default_options)
25
+ @new_default_options.keys.each do |key|
26
+ assert_equal @new_default_options[key],
27
+ Paperclip::Attachment.default_options[key]
28
+ end
29
+ end
30
+
31
+ context "without an Attachment" do
32
+ setup do
33
+ @dummy = Dummy.new
34
+ end
35
+
36
+ should "return false when asked exists?" do
37
+ assert !@dummy.avatar.exists?
38
+ end
39
+ end
40
+
41
+ context "on an Attachment" do
42
+ setup do
43
+ @dummy = Dummy.new
44
+ @attachment = @dummy.avatar
45
+ end
46
+
47
+ Paperclip::Attachment.default_options.keys.each do |key|
48
+ should "be the default_options for #{key}" do
49
+ assert_equal @old_default_options[key],
50
+ @attachment.instance_variable_get("@#{key}"),
51
+ key
52
+ end
53
+ end
54
+
55
+ context "when redefined" do
56
+ setup do
57
+ Paperclip::Attachment.default_options.merge!(@new_default_options)
58
+ @dummy = Dummy.new
59
+ @attachment = @dummy.avatar
60
+ end
61
+
62
+ Paperclip::Attachment.default_options.keys.each do |key|
63
+ should "be the new default_options for #{key}" do
64
+ assert_equal @new_default_options[key],
65
+ @attachment.instance_variable_get("@#{key}"),
66
+ key
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ context "An attachment with similarly named interpolations" do
74
+ setup do
75
+ rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
76
+ @dummy = Dummy.new
77
+ @dummy.stubs(:id).returns(1024)
78
+ @file = File.new(File.join(File.dirname(__FILE__),
79
+ "fixtures",
80
+ "5k.png"))
81
+ @dummy.avatar = @file
82
+ end
83
+
84
+ should "make sure that they are interpolated correctly" do
85
+ assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
86
+ end
87
+ end
88
+
89
+ context "An attachment with a :rails_env interpolation" do
90
+ setup do
91
+ @rails_env = "blah"
92
+ @id = 1024
93
+ rebuild_model :path => ":merb_env/:id.png"
94
+ @dummy = Dummy.new
95
+ @dummy.stubs(:id).returns(@id)
96
+ @file = File.new(File.join(File.dirname(__FILE__),
97
+ "fixtures",
98
+ "5k.png"))
99
+ @dummy.avatar = @file
100
+ end
101
+
102
+ should "return the proper path" do
103
+ temporary_env(@rails_env) {
104
+ assert_equal "#{Merb.env}/#{@id}.png", @dummy.avatar.path
105
+ }
106
+ end
107
+ end
108
+
109
+ context "An attachment with :convert_options" do
110
+ setup do
111
+ rebuild_model :styles => {
112
+ :thumb => "100x100",
113
+ :large => "400x400"
114
+ },
115
+ :convert_options => {
116
+ :all => "-do_stuff",
117
+ :thumb => "-thumbnailize"
118
+ }
119
+ @dummy = Dummy.new
120
+ end
121
+
122
+ should "report the correct options when sent #extra_options_for(:thumb)" do
123
+ assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
124
+ end
125
+
126
+ should "report the correct options when sent #extra_options_for(:large)" do
127
+ assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
128
+ end
129
+
130
+ context "when given a file" do
131
+ setup do
132
+ @file = File.new(File.join(File.dirname(__FILE__),
133
+ "fixtures",
134
+ "5k.png"))
135
+ Paperclip::Thumbnail.stubs(:make)
136
+ [:thumb, :large].each do |style|
137
+ @dummy.avatar.stubs(:extra_options_for).with(style)
138
+ end
139
+ end
140
+
141
+ should "call post_process" do
142
+ @dummy.avatar.expects(:post_process).once
143
+ @dummy.avatar = @file
144
+ end
145
+ end
146
+ end
147
+
148
+ context "Assigning an attachment" do
149
+ setup do
150
+ rebuild_model
151
+
152
+ @tempfile = mock
153
+ @tempfile.expects(:size).returns(10)
154
+
155
+ @not_file = mock
156
+ @not_file.stubs(:nil?).returns(false)
157
+ @not_file.expects(:to_tempfile).returns(@tempfile)
158
+ @not_file.expects(:original_filename).returns("filename.png\r\n")
159
+ @not_file.expects(:content_type).returns("image/png\r\n")
160
+ @not_file.expects(:size).returns(10)
161
+
162
+ @dummy = Dummy.new
163
+ @attachment = @dummy.avatar
164
+ @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
165
+ @attachment.expects(:queue_existing_for_delete)
166
+ @attachment.expects(:post_process)
167
+ @attachment.expects(:validate).twice
168
+ @dummy.avatar = @not_file
169
+ end
170
+
171
+ should "strip whitespace from original_filename field" do
172
+ assert_equal "filename.png", @dummy.avatar.original_filename
173
+ end
174
+
175
+ should "strip whitespace from content_type field" do
176
+ assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
177
+ end
178
+ end
179
+
180
+ context "Attachment with strange letters" do
181
+ setup do
182
+ rebuild_model
183
+
184
+ @tempfile = mock
185
+ @tempfile.expects(:size).returns(10)
186
+
187
+ @not_file = mock
188
+ @not_file.stubs(:nil?).returns(false)
189
+ @not_file.expects(:to_tempfile).returns(@tempfile)
190
+ @not_file.expects(:original_filename).returns("sheep_say_b_.png\r\n")
191
+ @not_file.expects(:content_type).returns("image/png\r\n")
192
+ @not_file.expects(:size).returns(10)
193
+
194
+ @dummy = Dummy.new
195
+ @attachment = @dummy.avatar
196
+ @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
197
+ @attachment.expects(:queue_existing_for_delete)
198
+ @attachment.expects(:post_process)
199
+ @attachment.expects(:validate).twice
200
+ @dummy.avatar = @not_file
201
+ end
202
+
203
+ should "remove strange letters and replace with underscore (_)" do
204
+ assert_equal "sheep_say_b_.png", @dummy.avatar.original_filename
205
+ end
206
+ end
207
+
208
+ context "An attachment" do
209
+ setup do
210
+ Paperclip::Attachment.default_options.merge!({
211
+ :path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
212
+ })
213
+ FileUtils.rm_rf("tmp")
214
+ rebuild_model
215
+ @instance = Dummy.new
216
+ @instance.stubs(:id).returns(1024)
217
+ @attachment = Paperclip::Attachment.new(:avatar, @instance)
218
+ @file = File.new(File.join(File.dirname(__FILE__),
219
+ "fixtures",
220
+ "5k.png"))
221
+ end
222
+
223
+ should "raise if there are not the correct columns when you try to assign" do
224
+ @other_attachment = Paperclip::Attachment.new(:not_here, @instance)
225
+ assert_raises(Paperclip::PaperclipError) do
226
+ @other_attachment.assign(@file)
227
+ end
228
+ end
229
+
230
+ should "return its default_url when no file assigned" do
231
+ assert @attachment.to_file.nil?
232
+ assert_equal "/avatars/original/missing.png", @attachment.url
233
+ assert_equal "/avatars/blah/missing.png", @attachment.url(:blah)
234
+ end
235
+
236
+ context "with a file assigned in the database" do
237
+ setup do
238
+ @instance.stubs(:avatar_file_name).returns('5k.png')
239
+ @instance.stubs(:avatar_content_type).returns("image/png")
240
+ @instance.stubs(:avatar_file_size).returns(12345)
241
+ @now = Time.now
242
+ Time.stubs(:now).returns(@now)
243
+ @instance.stubs(:avatar_updated_at).returns(Time.now)
244
+ end
245
+
246
+ should "return a correct url even if the file does not exist" do
247
+ assert_nil @attachment.to_file
248
+ assert_match %r{^/system/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah)
249
+ end
250
+
251
+ should "make sure the updated_at mtime is in the url if it is defined" do
252
+ assert_match %r{#{@now.to_i.to_s}$}, @attachment.url(:blah)
253
+ end
254
+
255
+ context "with the updated_at field removed" do
256
+ setup do
257
+ @instance.stubs(:[]).with(:avatar_updated_at).returns(nil)
258
+ end
259
+
260
+ should "only return the url without the updated_at when sent #url" do
261
+ assert_match "/avatars/#{@instance.id}/blah/5k.png", @attachment.url(:blah)
262
+ end
263
+ end
264
+
265
+ should "return the proper path when filename has a single .'s" do
266
+ assert_equal File.expand_path("./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.png"), File.expand_path(@attachment.path)
267
+ end
268
+
269
+ should "return the proper path when filename has multiple .'s" do
270
+ @instance.stubs(:avatar_file_name).returns("5k.old.png")
271
+ assert_equal File.expand_path("./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png"), File.expand_path(@attachment.path)
272
+ end
273
+
274
+ context "when expecting three styles" do
275
+ setup do
276
+ styles = {:styles => { :large => ["400x400", :png],
277
+ :medium => ["100x100", :gif],
278
+ :small => ["32x32#", :jpg]}}
279
+ @attachment = Paperclip::Attachment.new(:avatar,
280
+ @instance,
281
+ styles)
282
+ end
283
+
284
+ context "and assigned a file" do
285
+ setup do
286
+ now = Time.now
287
+ Time.stubs(:now).returns(now)
288
+ @attachment.assign(@file)
289
+ end
290
+
291
+ should "be dirty" do
292
+ assert @attachment.dirty?
293
+ end
294
+
295
+ context "and saved" do
296
+ setup do
297
+ @attachment.save
298
+ end
299
+
300
+ should "return the real url" do
301
+ assert @attachment.to_file
302
+ assert_match %r{^/system/avatars/#{@instance.id}/original/5k\.png}, @attachment.url
303
+ assert_match %r{^/system/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small)
304
+ end
305
+
306
+ should "commit the files to disk" do
307
+ [:large, :medium, :small].each do |style|
308
+ io = @attachment.to_file(style)
309
+ assert File.exists?(io)
310
+ assert ! io.is_a?(::Tempfile)
311
+ end
312
+ end
313
+
314
+ should "save the files as the right formats and sizes" do
315
+ [[:large, 400, 61, "PNG"],
316
+ [:medium, 100, 15, "GIF"],
317
+ [:small, 32, 32, "JPEG"]].each do |style|
318
+ cmd = "identify -format '%w %h %b %m' " +
319
+ "#{@attachment.path(style.first)}"
320
+ out = `#{cmd}`
321
+ width, height, size, format = out.split(" ")
322
+ assert_equal style[1].to_s, width.to_s
323
+ assert_equal style[2].to_s, height.to_s
324
+ assert_equal style[3].to_s, format.to_s
325
+ end
326
+ end
327
+
328
+ should "still have its #file attribute not be nil" do
329
+ assert ! @attachment.to_file.nil?
330
+ end
331
+
332
+ context "and deleted" do
333
+ setup do
334
+ @existing_names = @attachment.styles.keys.collect do |style|
335
+ @attachment.path(style)
336
+ end
337
+ @attachment.assign nil
338
+ @attachment.save
339
+ end
340
+
341
+ should "delete the files" do
342
+ @existing_names.each { |f| assert !File.exists?(f) }
343
+ end
344
+ end
345
+ end
346
+ end
347
+ end
348
+
349
+ end
350
+
351
+ context "when trying a nonexistant storage type" do
352
+ setup do
353
+ rebuild_model :storage => :not_here
354
+ end
355
+
356
+ should "not be able to find the module" do
357
+ assert_raise(NameError){ Dummy.new.avatar }
358
+ end
359
+ end
360
+ end
361
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ This is not an image.
File without changes
@@ -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).join
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
data/test/helper.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'tempfile'
6
+
7
+ require 'extlib'
8
+ require 'dm-core'
9
+ require 'dm-validations'
10
+ require 'dm-migrations'
11
+ begin
12
+ require 'ruby-debug'
13
+ rescue LoadError
14
+ puts "ruby-debug not loaded"
15
+ end
16
+
17
+ ROOT = File.join(File.dirname(__FILE__), '..')
18
+ RAILS_ROOT = ROOT
19
+ RAILS_ENV = ENV['RAILS_ENV']
20
+
21
+ Object.const_set("Merb", Class.new())
22
+ Merb.class_eval do
23
+ def self.root
24
+ "#{ROOT}"
25
+ end
26
+ def self.env(str=nil)
27
+ ENV['RAILS_ENV'] = str if str
28
+ ENV['RAILS_ENV']
29
+ end
30
+ end
31
+
32
+ $LOAD_PATH << File.join(ROOT, 'lib')
33
+ $LOAD_PATH << File.join(ROOT, 'lib', 'dm-paperclip')
34
+
35
+ require File.join(ROOT, 'lib', 'dm-paperclip.rb')
36
+
37
+ ENV['RAILS_ENV'] ||= 'test'
38
+
39
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
40
+ DataMapper.setup(:default, 'sqlite3::memory:')
41
+
42
+ unless defined?(Mash)
43
+ class Mash < Hash
44
+ end
45
+ end
46
+
47
+ Paperclip.configure do |config|
48
+ config.root = Merb.root # the application root to anchor relative urls (defaults to Dir.pwd)
49
+ config.env = Merb.env # server env support, defaults to ENV['RACK_ENV'] or 'development'
50
+ config.use_dm_validations = true # validate attachment sizes and such, defaults to false
51
+ end
52
+
53
+ def rebuild_model options = {}
54
+ Object.send(:remove_const, "Dummy") rescue nil
55
+ Object.const_set("Dummy", Class.new())
56
+ Dummy.class_eval do
57
+ include DataMapper::Resource
58
+ include DataMapper::Validate
59
+ include Paperclip::Resource
60
+ property :id, ::DataMapper::Types::Serial
61
+ property :other, String
62
+ has_attached_file :avatar, options
63
+ end
64
+ Dummy.auto_migrate!
65
+ end
66
+
67
+ def temporary_env(new_env)
68
+ old_env = Merb.env
69
+ Merb.env(new_env)
70
+ yield
71
+ Merb.env(old_env)
72
+ end