ripta-dm-paperclip 2.2.9.2

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,358 @@
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
+ @not_file = mock
153
+ @not_file.stubs(:nil?).returns(false)
154
+ @not_file.expects(:to_tempfile).returns(self)
155
+ @not_file.expects(:original_filename).returns("filename.png\r\n")
156
+ @not_file.expects(:content_type).returns("image/png\r\n")
157
+ @not_file.expects(:size).returns(10)
158
+
159
+ @dummy = Dummy.new
160
+ @attachment = @dummy.avatar
161
+ @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
162
+ @attachment.expects(:queue_existing_for_delete)
163
+ @attachment.expects(:post_process)
164
+ @attachment.expects(:validate).twice
165
+ @dummy.avatar = @not_file
166
+ end
167
+
168
+ should "strip whitespace from original_filename field" do
169
+ assert_equal "filename.png", @dummy.avatar.original_filename
170
+ end
171
+
172
+ should "strip whitespace from content_type field" do
173
+ assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
174
+ end
175
+ end
176
+
177
+ context "Attachment with strange letters" do
178
+ setup do
179
+ rebuild_model
180
+
181
+ @not_file = mock
182
+ @not_file.stubs(:nil?).returns(false)
183
+ @not_file.expects(:to_tempfile).returns(self)
184
+ @not_file.expects(:original_filename).returns("sheep_say_b�.png\r\n")
185
+ @not_file.expects(:content_type).returns("image/png\r\n")
186
+ @not_file.expects(:size).returns(10)
187
+
188
+ @dummy = Dummy.new
189
+ @attachment = @dummy.avatar
190
+ @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
191
+ @attachment.expects(:queue_existing_for_delete)
192
+ @attachment.expects(:post_process)
193
+ @attachment.expects(:validate).twice
194
+ @dummy.avatar = @not_file
195
+ end
196
+
197
+ should "remove strange letters and replace with underscore (_)" do
198
+ assert_equal "sheep_say_b_.png", @dummy.avatar.original_filename
199
+ end
200
+ end
201
+
202
+ context "An attachment" do
203
+ setup do
204
+ Paperclip::Attachment.default_options.merge!({
205
+ :path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
206
+ })
207
+ FileUtils.rm_rf("tmp")
208
+ rebuild_model
209
+ @instance = Dummy.new
210
+ @instance.stubs(:id).returns(1024)
211
+ @attachment = Paperclip::Attachment.new(:avatar, @instance)
212
+ @file = File.new(File.join(File.dirname(__FILE__),
213
+ "fixtures",
214
+ "5k.png"))
215
+ end
216
+
217
+ should "raise if there are not the correct columns when you try to assign" do
218
+ @other_attachment = Paperclip::Attachment.new(:not_here, @instance)
219
+ assert_raises(Paperclip::PaperclipError) do
220
+ @other_attachment.assign(@file)
221
+ end
222
+ end
223
+
224
+ should "return its default_url when no file assigned" do
225
+ assert @attachment.to_file.nil?
226
+ assert_equal "/avatars/original/missing.png", @attachment.url
227
+ assert_equal "/avatars/blah/missing.png", @attachment.url(:blah)
228
+ end
229
+
230
+ context "with a file assigned in the database" do
231
+ setup do
232
+ @instance.stubs(:attribute_get).with(:avatar_file_name).returns('5k.png')
233
+ @instance.stubs(:attribute_get).with(:avatar_content_type).returns("image/png")
234
+ @instance.stubs(:attribute_get).with(:avatar_file_size).returns(12345)
235
+ @now = Time.now
236
+ Time.stubs(:now).returns(@now)
237
+ @instance.stubs(:attribute_get).with(:avatar_updated_at).returns(Time.now)
238
+ end
239
+
240
+ should "return a correct url even if the file does not exist" do
241
+ assert_nil @attachment.to_file
242
+ assert_match %r{^/system/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah)
243
+ end
244
+
245
+ should "make sure the updated_at mtime is in the url if it is defined" do
246
+ assert_match %r{#{@now.to_i.to_s}$}, @attachment.url(:blah)
247
+ end
248
+
249
+ context "with the updated_at field removed" do
250
+ setup do
251
+ @instance.stubs(:[]).with(:avatar_updated_at).returns(nil)
252
+ end
253
+
254
+ should "only return the url without the updated_at when sent #url" do
255
+ assert_match "/avatars/#{@instance.id}/blah/5k.png", @attachment.url(:blah)
256
+ end
257
+ end
258
+
259
+ should "return the proper path when filename has a single .'s" do
260
+ assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.png", @attachment.path
261
+ end
262
+
263
+ should "return the proper path when filename has multiple .'s" do
264
+ @instance.stubs(:attribute_get).with(:avatar_file_name).returns("5k.old.png")
265
+ assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png", @attachment.path
266
+ end
267
+
268
+ context "when expecting three styles" do
269
+ setup do
270
+ styles = {:styles => { :large => ["400x400", :png],
271
+ :medium => ["100x100", :gif],
272
+ :small => ["32x32#", :jpg]}}
273
+ @attachment = Paperclip::Attachment.new(:avatar,
274
+ @instance,
275
+ styles)
276
+ end
277
+
278
+ context "and assigned a file" do
279
+ setup do
280
+ now = Time.now
281
+ Time.stubs(:now).returns(now)
282
+ @attachment.assign(@file)
283
+ end
284
+
285
+ should "be dirty" do
286
+ assert @attachment.dirty?
287
+ end
288
+
289
+ context "and saved" do
290
+ setup do
291
+ @attachment.save
292
+ end
293
+
294
+ should "return the real url" do
295
+ assert @attachment.to_file
296
+ assert_match %r{^/system/avatars/#{@instance.id}/original/5k\.png}, @attachment.url
297
+ assert_match %r{^/system/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small)
298
+ end
299
+
300
+ should "commit the files to disk" do
301
+ [:large, :medium, :small].each do |style|
302
+ io = @attachment.to_io(style)
303
+ assert File.exists?(io)
304
+ assert ! io.is_a?(::Tempfile)
305
+ end
306
+ end
307
+
308
+ should "save the files as the right formats and sizes" do
309
+ [[:large, 400, 61, "PNG"],
310
+ [:medium, 100, 15, "GIF"],
311
+ [:small, 32, 32, "JPEG"]].each do |style|
312
+ cmd = "identify -format '%w %h %b %m' " +
313
+ "#{@attachment.to_io(style.first).path}"
314
+ out = `#{cmd}`
315
+ width, height, size, format = out.split(" ")
316
+ assert_equal style[1].to_s, width.to_s
317
+ assert_equal style[2].to_s, height.to_s
318
+ assert_equal style[3].to_s, format.to_s
319
+ end
320
+ end
321
+
322
+ should "still have its #file attribute not be nil" do
323
+ assert ! @attachment.to_file.nil?
324
+ end
325
+
326
+ context "and deleted" do
327
+ setup do
328
+ @existing_names = @attachment.styles.keys.collect do |style|
329
+ @attachment.path(style)
330
+ end
331
+ @instance.expects(:attributes=).with({ :avatar_file_name => nil,
332
+ :avatar_content_type => nil,
333
+ :avatar_file_size => nil })
334
+ @attachment.assign nil
335
+ @attachment.save
336
+ end
337
+
338
+ should "delete the files" do
339
+ @existing_names.each{|f| assert !File.exists?(f) }
340
+ end
341
+ end
342
+ end
343
+ end
344
+ end
345
+
346
+ end
347
+
348
+ context "when trying a nonexistant storage type" do
349
+ setup do
350
+ rebuild_model :storage => :not_here
351
+ end
352
+
353
+ should "not be able to find the module" do
354
+ assert_raise(NameError){ Dummy.new.avatar }
355
+ end
356
+ end
357
+ end
358
+ 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).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
data/test/helper.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'tempfile'
6
+
7
+ require 'dm-core'
8
+ require 'dm-validations'
9
+ require 'dm-migrations'
10
+ begin
11
+ require 'ruby-debug'
12
+ rescue LoadError
13
+ puts "ruby-debug not loaded"
14
+ end
15
+
16
+ ROOT = File.join(File.dirname(__FILE__), '..')
17
+ RAILS_ROOT = ROOT
18
+ RAILS_ENV = ENV['RAILS_ENV']
19
+
20
+ Object.const_set("Merb", Class.new())
21
+ Merb.class_eval do
22
+ def self.root
23
+ "#{ROOT}"
24
+ end
25
+ def self.env
26
+ ENV['RAILS_ENV']
27
+ end
28
+ end
29
+
30
+ $LOAD_PATH << File.join(ROOT, 'lib')
31
+ $LOAD_PATH << File.join(ROOT, 'lib', 'dm-paperclip')
32
+
33
+ require File.join(ROOT, 'lib', 'dm-paperclip.rb')
34
+
35
+ ENV['RAILS_ENV'] ||= 'test'
36
+
37
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
38
+ DataMapper.setup(:default, 'sqlite3::memory:')
39
+
40
+ unless defined?(Mash)
41
+ class Mash < Hash
42
+ end
43
+ end
44
+
45
+ def rebuild_model options = {}
46
+ Object.send(:remove_const, "Dummy") rescue nil
47
+ Object.const_set("Dummy", Class.new())
48
+ Dummy.class_eval do
49
+ include DataMapper::Resource
50
+ include DataMapper::Validate
51
+ include Paperclip::Resource
52
+ property :id, Integer, :serial => true
53
+ property :other, String
54
+ has_attached_file :avatar, options
55
+ end
56
+ Dummy.auto_migrate!
57
+ end
58
+
59
+ def temporary_env(new_env)
60
+ old_env = defined?(RAILS_ENV) ? RAILS_ENV : nil
61
+ Object.const_set("RAILS_ENV", new_env)
62
+ yield
63
+ Object.const_set("RAILS_ENV", old_env)
64
+ end