uploadcolumn 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,120 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ gem 'activerecord'
4
+ require 'active_record'
5
+
6
+ require File.join(File.dirname(__FILE__), '../lib/upload_column')
7
+
8
+ ActiveRecord::Base.send(:include, UploadColumn)
9
+
10
+ class Entry < ActiveRecord::Base; end # setup a basic AR class for testing
11
+
12
+ describe "UploadColumn::MagicColumns.set_upload_column_with_magic_columns" do
13
+
14
+ include UploadColumnSpecHelper
15
+
16
+ before(:each) do
17
+ setup_standard_mocking
18
+ UploadColumn::UploadedFile.should_receive(:upload).with(@file, @entry, :avatar, @options).and_return(@uploaded_file)
19
+ end
20
+
21
+ it "should assign methods from the uploaded file to database columns" do
22
+ Entry.should_receive(:column_names).and_return([ 'monkey', 'llama', 'avatar_path', 'avatar_size'])
23
+
24
+ @uploaded_file.stub!(:path).and_return('/path/to/my/file')
25
+ @uploaded_file.stub!(:size).and_return(9999)
26
+
27
+ @entry.avatar = @file
28
+
29
+ @entry.avatar_path.should == '/path/to/my/file'
30
+ @entry.avatar_size.should == 9999
31
+ end
32
+
33
+ it "should do nothing when the column names do not exist on the object" do
34
+ Entry.should_receive(:column_names).and_return([ 'monkey', 'llama', 'avatar_monkey', 'avatar_size'])
35
+
36
+ @uploaded_file.stub!(:size).and_return(9999)
37
+
38
+ @entry.avatar = @file
39
+
40
+ @entry.avatar_monkey.should be_nil
41
+ @entry.avatar_size.should == 9999
42
+ end
43
+ end
44
+
45
+ describe "UploadColumn::MagicColumns.set_upload_column_temp_with_magic_columns" do
46
+
47
+ include UploadColumnSpecHelper
48
+
49
+ before(:each) do
50
+ setup_standard_mocking
51
+
52
+ @temp_value = '12345.1234.12345/somewhere.png'
53
+
54
+ @retrieved_file = mock('a retrieved file')
55
+ @retrieved_file.should_receive(:actual_filename).and_return('walruss.png')
56
+
57
+ UploadColumn::UploadedFile.should_receive(:retrieve_temp).with(@temp_value, @entry, :avatar, @options).and_return(@retrieved_file)
58
+ @entry.should_receive(:[]=).with(:avatar, 'walruss.png')
59
+ end
60
+
61
+ it "should assign methods from the uploaded file to database columns" do
62
+ Entry.stub!(:column_names).and_return([ 'monkey', 'llama', 'avatar_path', 'avatar_size'])
63
+
64
+ @retrieved_file.stub!(:path).and_return('/path/to/my/file')
65
+ @retrieved_file.stub!(:size).and_return(9999)
66
+
67
+ @entry.avatar_temp = @temp_value
68
+
69
+ @entry.avatar_path.should == '/path/to/my/file'
70
+ @entry.avatar_size.should == 9999
71
+ end
72
+
73
+ it "should do nothing when the column names do not exist on the object" do
74
+ Entry.stub!(:column_names).and_return([ 'monkey', 'llama', 'avatar_monkey', 'avatar_size'])
75
+
76
+ @retrieved_file.stub!(:size).and_return(9999)
77
+
78
+ @entry.avatar_temp = @temp_value
79
+
80
+ @entry.avatar_monkey.should be_nil
81
+ @entry.avatar_size.should == 9999
82
+ end
83
+ end
84
+
85
+ describe "UploadColumn::MagicColumns.save_uploaded_files_with_magic_columns" do
86
+
87
+ include UploadColumnSpecHelper
88
+
89
+ before(:each) do
90
+ setup_standard_mocking
91
+ UploadColumn::UploadedFile.should_receive(:upload).with(@file, @entry, :avatar, @options).and_return(@uploaded_file)
92
+ @entry.avatar = @file
93
+ @uploaded_file.stub!(:tempfile?).and_return(false)
94
+ end
95
+
96
+ it "should reevaluate magic columns" do
97
+ Entry.stub!(:column_names).and_return([ 'monkey', 'llama', 'avatar_path', 'avatar_size'])
98
+
99
+ @uploaded_file.stub!(:path).and_return('/path/to/my/file')
100
+ @uploaded_file.stub!(:size).and_return(9999)
101
+
102
+ @entry.send(:save_uploaded_files)
103
+
104
+ @entry.avatar_path.should == '/path/to/my/file'
105
+ @entry.avatar_size.should == 9999
106
+ end
107
+
108
+ it "should do nothing when the column names do not exist on the object" do
109
+ Entry.stub!(:column_names).and_return([ 'monkey', 'llama', 'avatar_monkey', 'avatar_size'])
110
+
111
+ @uploaded_file.stub!(:size).and_return(9999)
112
+
113
+ @entry.send(:save_uploaded_files)
114
+
115
+ @entry.avatar_monkey.should be_nil
116
+ @entry.avatar_size.should == 9999
117
+ end
118
+
119
+ end
120
+
@@ -0,0 +1,186 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '../lib/upload_column/manipulators/rmagick')
3
+
4
+ describe UploadColumn::Manipulators::RMagick, "#manipulate!" do
5
+
6
+ before(:each) do
7
+ @uploaded_file = class << self; self end # this is a singleton object
8
+ @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
9
+ @uploaded_file.load_manipulator_dependencies
10
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
11
+ end
12
+
13
+ it "should yield the first frame of the image and then save the result, for a single-framed image" do
14
+ a_frame = mock('a frame')
15
+ Magick::Image.should_receive(:read).with('/some_path.png').and_return( [a_frame] )
16
+
17
+ @uploaded_file.manipulate! do |img|
18
+ img.should == a_frame
19
+ img.should_receive(:write).with('/some_path.png')
20
+ img
21
+ end
22
+ end
23
+
24
+ it "should yield all frames and save the result, for a multi-framed image" do
25
+ image = Magick::Image.read(file_path('netscape.gif'))
26
+ Magick::Image.should_receive(:read).with('/some_path.png').and_return( image )
27
+
28
+ imagelist = Magick::ImageList.new
29
+ Magick::ImageList.should_receive(:new).and_return(imagelist)
30
+
31
+ imagelist.should_receive(:<<).with(image[0]).exactly(:once).ordered
32
+ imagelist.should_receive(:<<).with(image[1]).exactly(:once).ordered
33
+
34
+ image[0].should_receive(:solarize)
35
+ image[1].should_receive(:solarize)
36
+
37
+ imagelist.should_receive(:write).with('/some_path.png')
38
+
39
+ @uploaded_file.manipulate! do |img|
40
+ img.solarize
41
+ img
42
+ end
43
+
44
+ end
45
+
46
+ it "should raise an more meaningful error if something goes wrong" do
47
+ Magick::Image.should_receive(:read).and_raise(Magick::ImageMagickError.new('arrggh'))
48
+
49
+ lambda do
50
+ @uploaded_file.manipulate! do |img|
51
+ img
52
+ end
53
+ end.should raise_error( UploadColumn::ManipulationError, "Failed to manipulate with rmagick, maybe it is not an image? Original Error: arrggh" )
54
+
55
+ end
56
+
57
+ end
58
+
59
+ describe UploadColumn::Manipulators::RMagick, "#resize!" do
60
+
61
+ before(:each) do
62
+ @uploaded_file = class << self; self end
63
+ @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
64
+ @uploaded_file.load_manipulator_dependencies
65
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
66
+ end
67
+
68
+ it "should use rmagick to resize the image to the appropriate size" do
69
+
70
+ img = mock('an image frame')
71
+ @uploaded_file.should_receive(:manipulate!).and_yield(img)
72
+
73
+ geometry_img = mock('image returned by change_geometry')
74
+
75
+ img.should_receive(:change_geometry).with("200x200").and_yield(20, 40, geometry_img)
76
+
77
+ geometry_img.should_receive(:resize).with(20, 40)
78
+
79
+ @uploaded_file.resize!("200x200")
80
+ end
81
+
82
+ end
83
+
84
+
85
+ describe UploadColumn::Manipulators::RMagick, "#crop_resized!" do
86
+
87
+ before(:each) do
88
+ @uploaded_file = class << self; self end
89
+ @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
90
+ @uploaded_file.load_manipulator_dependencies
91
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
92
+ end
93
+
94
+ it "should use rmagick to resize and crop the image to the appropriate size" do
95
+
96
+ img = mock('an image frame')
97
+ @uploaded_file.should_receive(:manipulate!).and_yield(img)
98
+
99
+ img.should_receive(:crop_resized).with(200, 200)
100
+
101
+ @uploaded_file.crop_resized!("200x200")
102
+ end
103
+
104
+ end
105
+
106
+ describe UploadColumn::Manipulators::RMagick, "#convert!" do
107
+
108
+ before(:each) do
109
+ @uploaded_file = class << self; self end
110
+ @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
111
+ @uploaded_file.load_manipulator_dependencies
112
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
113
+ end
114
+
115
+ it "should use rmagick to change the image format" do
116
+
117
+ img = mock('an image frame')
118
+ @uploaded_file.should_receive(:manipulate!).and_yield(img)
119
+
120
+ img.should_receive(:format=).with("PNG")
121
+
122
+ @uploaded_file.convert!(:png)
123
+ end
124
+
125
+ end
126
+
127
+ describe UploadColumn::Manipulators::RMagick, "#process!" do
128
+
129
+ before(:each) do
130
+ @uploaded_file = class << self; self end
131
+ @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
132
+ @uploaded_file.load_manipulator_dependencies
133
+ @uploaded_file.stub!(:path).and_return('/some_path.png')
134
+ end
135
+
136
+ it "should resize the image if a string like '333x444' is passed" do
137
+ @uploaded_file.should_receive(:resize!).with('333x444')
138
+ @uploaded_file.process!('333x444')
139
+ end
140
+
141
+ it "should crop and resize the image if a string like 'c333x444' is passed" do
142
+ @uploaded_file.should_receive(:crop_resized!).with('333x444')
143
+ @uploaded_file.process!('c333x444')
144
+ end
145
+
146
+ it "should pass on a proc to manipulate!" do
147
+ img_frame = mock('an image frame')
148
+ proc = proc { |img| img.solarize }
149
+ img_frame.should_receive(:solarize)
150
+
151
+ @uploaded_file.should_receive(:manipulate!).and_yield(img_frame)
152
+
153
+ @uploaded_file.process!(proc)
154
+ end
155
+
156
+ it "should yield to manipulate! if a block is given" do
157
+ img_frame = mock('an image frame')
158
+ img_frame.should_receive(:solarize)
159
+
160
+ @uploaded_file.should_receive(:manipulate!).and_yield(img_frame)
161
+
162
+ @uploaded_file.process! do |img|
163
+ img.solarize
164
+ end
165
+ end
166
+
167
+ it "should resize first and then yield to manipulate! if both a block and a size string are given" do
168
+ img_frame = mock('an image frame')
169
+ img_frame.should_receive(:solarize)
170
+
171
+ @uploaded_file.should_receive(:resize!).with('200x200').ordered
172
+ @uploaded_file.should_receive(:manipulate!).ordered.and_yield(img_frame)
173
+
174
+ @uploaded_file.process!('200x200') do |img|
175
+ img.solarize
176
+ end
177
+ end
178
+
179
+ it "should do nothing if :none is passed" do
180
+ @uploaded_file.should_not_receive(:manipulate!)
181
+ @uploaded_file.process!(:none)
182
+ end
183
+
184
+ end
185
+
186
+