kt-delayed_paperclip 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe DelayedPaperclip::ClassMethods do
4
+ before :each do
5
+ reset_dummy(with_processed: false)
6
+ end
7
+
8
+ describe ".process_in_background" do
9
+ it "is empty to start" do
10
+ Dummy.paperclip_definitions.should == { :image => {} }
11
+ end
12
+
13
+ it "adds basics to paperclip_definitions" do
14
+ Dummy.process_in_background(:image)
15
+ Dummy.paperclip_definitions.should == { :image => {
16
+ :delayed => {
17
+ :priority => 0,
18
+ :only_process => [],
19
+ :url_with_processing => true,
20
+ :processing_image_url => nil,
21
+ :queue => "paperclip"}
22
+ }
23
+ }
24
+ end
25
+
26
+ it "allows to set queue name" do
27
+ Dummy.process_in_background(:image, :queue => "custom")
28
+ expect(Dummy.paperclip_definitions).to eq({ :image => {
29
+ :delayed => {
30
+ :priority => 0,
31
+ :only_process => [],
32
+ :url_with_processing => true,
33
+ :processing_image_url => nil,
34
+ :queue => "custom"}
35
+ }
36
+ })
37
+ end
38
+
39
+ context "with processing_image_url" do
40
+ before :each do
41
+ Dummy.process_in_background(:image, processing_image_url: "/processing/url")
42
+ end
43
+
44
+ it "incorporates processing url" do
45
+ Dummy.paperclip_definitions.should == { :image => {
46
+ :delayed => {
47
+ :priority => 0,
48
+ :only_process => [],
49
+ :url_with_processing => true,
50
+ :processing_image_url => "/processing/url",
51
+ :queue => "paperclip"}
52
+ }
53
+ }
54
+ end
55
+ end
56
+
57
+ context "inherits only_process options" do
58
+ before :each do
59
+ reset_class("Dummy", paperclip: { only_process: [:small, :large] } )
60
+ Dummy.process_in_background(:image)
61
+ end
62
+
63
+ it "incorporates processing url" do
64
+ Dummy.paperclip_definitions.should == { :image => {
65
+ :only_process => [:small, :large],
66
+ :delayed => {
67
+ :priority => 0,
68
+ :only_process => [:small, :large],
69
+ :url_with_processing => true,
70
+ :processing_image_url => nil,
71
+ :queue => "paperclip"}
72
+ }
73
+ }
74
+ end
75
+ end
76
+
77
+ context "sets callback" do
78
+ context "commit" do
79
+ it "sets after_commit callback" do
80
+ Dummy.expects(:after_commit).with(:enqueue_delayed_processing)
81
+ Dummy.process_in_background(:image)
82
+ end
83
+ end
84
+
85
+ context "save" do
86
+ it "sets after_save callback" do
87
+ Dummy.stubs(:respond_to?).with(:attachment_definitions).returns(true)
88
+ Dummy.stubs(:respond_to?).with(:after_commit).returns(false)
89
+ Dummy.expects(:after_save).with(:enqueue_delayed_processing)
90
+ Dummy.process_in_background(:image)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe DelayedPaperclip::InstanceMethods do
4
+ before :each do
5
+ reset_dummy
6
+ end
7
+
8
+ let(:dummy) { Dummy.create }
9
+
10
+ describe "#enqueue_delayed_processing" do
11
+ it "marks enqueue_delayed_processing" do
12
+ dummy.expects(:mark_enqueue_delayed_processing)
13
+ dummy.enqueue_delayed_processing
14
+ end
15
+
16
+ it "enqueues post processing for all enqueued" do
17
+ dummy.instance_variable_set(:@_enqued_for_processing, ['image'])
18
+ dummy.expects(:enqueue_post_processing_for).with('image')
19
+ dummy.enqueue_delayed_processing
20
+ end
21
+
22
+ it "clears instance variables" do
23
+ dummy.instance_variable_set(:@_enqued_for_processing, ['foo'])
24
+ dummy.instance_variable_set(:@_enqued_for_processing_with_processing, ['image'])
25
+ dummy.expects(:enqueue_post_processing_for).with('foo')
26
+ dummy.enqueue_delayed_processing
27
+ dummy.instance_variable_get(:@_enqued_for_processing).should == []
28
+ dummy.instance_variable_get(:@_enqued_for_processing_with_processing).should == []
29
+ end
30
+ end
31
+
32
+ describe "#mark_enqueue_delayed_processing" do
33
+ it "updates columns of _processing" do
34
+ dummy.image_processing.should be_falsey
35
+ dummy.instance_variable_set(:@_enqued_for_processing_with_processing, ['image'])
36
+ dummy.mark_enqueue_delayed_processing
37
+ dummy.reload.image_processing.should be_truthy
38
+ end
39
+
40
+ it "does nothing if instance variable not set" do
41
+ dummy.image_processing.should be_falsey
42
+ dummy.mark_enqueue_delayed_processing
43
+ dummy.reload.image_processing.should be_falsey
44
+ end
45
+ end
46
+
47
+ describe "#enqueue_post_processing_for" do
48
+ it "enqueues the instance and image" do
49
+ DelayedPaperclip.expects(:enqueue).with("Dummy", dummy.id, :image)
50
+ dummy.enqueue_post_processing_for("image")
51
+ end
52
+ end
53
+
54
+ describe "#prepare_enqueueing_for" do
55
+ it "updates processing column to true" do
56
+ dummy.image_processing.should be_falsey
57
+ dummy.prepare_enqueueing_for("image")
58
+ dummy.image_processing.should be_truthy
59
+ end
60
+
61
+ it "sets instance variables for column updating" do
62
+ dummy.prepare_enqueueing_for("image")
63
+ dummy.instance_variable_get(:@_enqued_for_processing_with_processing).should == ["image"]
64
+ end
65
+
66
+ it "sets instance variables for processing" do
67
+ dummy.prepare_enqueueing_for("image")
68
+ dummy.instance_variable_get(:@_enqued_for_processing).should == ["image"]
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,273 @@
1
+ require 'spec_helper'
2
+
3
+ describe DelayedPaperclip::UrlGenerator do
4
+ before :each do
5
+ reset_dummy({})
6
+ end
7
+
8
+ def make_attachment(options = {})
9
+ reset_dummy(options)
10
+ Dummy.create.image
11
+ end
12
+
13
+ describe "for" do
14
+ before :each do
15
+ attachment.stubs(:original_filename).returns "12k.png"
16
+ end
17
+
18
+ context "with split processing" do
19
+ let(:attachment) do
20
+ make_attachment(
21
+ {
22
+ paperclip: {
23
+ styles: {
24
+ online: "400x400x",
25
+ background: "600x600x"
26
+ },
27
+ only_process: [:online]
28
+ },
29
+
30
+ only_process: [:background]
31
+ }
32
+ )
33
+ end
34
+
35
+ context "processing" do
36
+ before :each do
37
+ attachment.stubs(:processing?).returns true
38
+ end
39
+
40
+ it "returns the default_url when the style is still being processed" do
41
+ expect(attachment.url(:background)).to eql "/images/background/missing.png"
42
+ end
43
+
44
+ it "returns the attachment url when the style is not set for background processing" do
45
+ expect(attachment.url(:online)).to eql "/system/dummies/images/000/000/001/online/12k.png"
46
+ end
47
+ end
48
+
49
+ context "not processing" do
50
+ before :each do
51
+ attachment.stubs(:processing?).returns false
52
+ end
53
+
54
+ it "returns the attachment url even when the style is set for background processing" do
55
+ expect(attachment.url(:background)).to eql "/system/dummies/images/000/000/001/background/12k.png"
56
+ end
57
+
58
+ it "returns the generated url when the style is not set for background processing" do
59
+ expect(attachment.url(:online)).to eql "/system/dummies/images/000/000/001/online/12k.png"
60
+ end
61
+ end
62
+
63
+ context "should be able to escape (, ), [, and ]." do
64
+ def generate(expected, updated_at = nil)
65
+ attachment = make_attachment(
66
+ {
67
+ paperclip: {
68
+ styles: {
69
+ online: "400x400x",
70
+ background: "600x600x"
71
+ },
72
+ only_process: [:online],
73
+ interpolator: MockInterpolator.new(result: expected)
74
+ },
75
+
76
+ only_process: [:background]
77
+ }
78
+ )
79
+ url_generator = DelayedPaperclip::UrlGenerator.new(attachment, {})
80
+ attachment.stubs(:updated_at).returns updated_at
81
+ url_generator.for(:style_name, {escape: true, timestamp: !!updated_at})
82
+ end
83
+
84
+ it "interpolates correctly without timestamp" do
85
+ expect(
86
+ "the%28expected%29result%5B%5D"
87
+ ).to eq(generate("the(expected)result[]"))
88
+ end
89
+
90
+ it "does not interpolate timestamp" do
91
+ expected = "the(expected)result[]"
92
+ updated_at = 1231231234
93
+
94
+ expect(
95
+ "the%28expected%29result%5B%5D?#{updated_at}"
96
+ ).to eq(generate(expected, updated_at))
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ describe "#most_appropriate_url" do
103
+ context "without delayed_default_url" do
104
+ let(:attachment) { make_attachment(paperclip: {url: "/blah/url.jpg"}) }
105
+
106
+ subject { DelayedPaperclip::UrlGenerator.new(attachment) }
107
+
108
+ before :each do
109
+ subject.stubs(:delayed_default_url?).returns false
110
+ end
111
+
112
+ context "with original file name" do
113
+ it "returns options url" do
114
+ attachment.stubs(:original_filename).returns "blah"
115
+ subjec = DelayedPaperclip::UrlGenerator.new(attachment)
116
+ subjec.stubs(:delayed_default_url?).returns false
117
+ subjec.most_appropriate_url.should == "/blah/url.jpg"
118
+ end
119
+ end
120
+
121
+ context "without original_filename" do
122
+ before :each do
123
+ attachment.stubs(:original_filename).returns nil
124
+ end
125
+
126
+ context "without delayed_options" do
127
+ before :each do
128
+ attachment.stubs(:delayed_options).returns nil
129
+ end
130
+
131
+ it "gets default url" do
132
+ subject.expects(:default_url)
133
+ subject.most_appropriate_url
134
+ end
135
+ end
136
+
137
+ context "with delayed_options" do
138
+ before :each do
139
+ attachment.stubs(:delayed_options).returns(some: 'thing')
140
+ end
141
+
142
+ context "without processing_image_url" do
143
+ before :each do
144
+ attachment.stubs(:processing_image_url).returns nil
145
+ end
146
+
147
+ it "gets default url" do
148
+ subject.expects(:default_url)
149
+ subject.most_appropriate_url
150
+ end
151
+ end
152
+
153
+ context "with processing_image_url" do
154
+ before :each do
155
+ attachment.stubs(:processing_image_url).returns "/processing/image.jpg"
156
+ end
157
+
158
+ context "and is processing" do
159
+ before :each do
160
+ attachment.stubs(:processing?).returns true
161
+ end
162
+
163
+ it "gets processing url" do
164
+ subject.most_appropriate_url.should == "/processing/image.jpg"
165
+ end
166
+ end
167
+
168
+ context "and is not processing" do
169
+ it "gets default url" do
170
+ subject.expects(:default_url)
171
+ subject.most_appropriate_url
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+
180
+ describe "#timestamp_possible?" do
181
+ let(:attachment) { make_attachment }
182
+ subject { DelayedPaperclip::UrlGenerator.new(attachment) }
183
+
184
+ context "with delayed_default_url" do
185
+ before :each do
186
+ subject.stubs(:delayed_default_url?).returns true
187
+ end
188
+
189
+ it "is false" do
190
+ subject.timestamp_possible?.should be_falsey
191
+ end
192
+ end
193
+
194
+ context "without delayed_default_url" do
195
+ before :each do
196
+ subject.stubs(:delayed_default_url?).returns false
197
+ end
198
+
199
+ it "goes up the chain" do
200
+ subject.class.superclass.any_instance.expects(:timestamp_possible?)
201
+ subject.timestamp_possible?
202
+ end
203
+ end
204
+ end
205
+
206
+ describe "#delayed_default_url?" do
207
+ let(:attachment) { make_attachment }
208
+ subject { DelayedPaperclip::UrlGenerator.new(attachment) }
209
+
210
+ before :each do
211
+ attachment.stubs(:job_is_processing).returns false
212
+ attachment.stubs(:dirty?).returns false
213
+ attachment.delayed_options[:url_with_processing] = true
214
+ attachment.instance.stubs(:respond_to?).with(:image_processing?).returns true
215
+ attachment.stubs(:processing?).returns true
216
+ attachment.stubs(:processing_style?).with(anything).returns true
217
+ end
218
+
219
+ it "has all false, delayed_default_url returns true" do
220
+ subject.delayed_default_url?.should be_truthy
221
+ end
222
+
223
+ context "job is processing" do
224
+ before :each do
225
+ attachment.stubs(:job_is_processing).returns true
226
+ end
227
+
228
+ it "returns true" do
229
+ subject.delayed_default_url?.should be_falsey
230
+ end
231
+ end
232
+
233
+ context "attachment is dirty" do
234
+ before :each do
235
+ attachment.stubs(:dirty?).returns true
236
+ end
237
+
238
+ it "returns true" do
239
+ subject.delayed_default_url?.should be_falsey
240
+ end
241
+ end
242
+
243
+ context "attachment has delayed_options without url_with_processing" do
244
+ before :each do
245
+ attachment.delayed_options[:url_with_processing] = false
246
+ end
247
+
248
+ it "returns true" do
249
+ subject.delayed_default_url?.should be_falsey
250
+ end
251
+ end
252
+
253
+ context "attachment does not responds to name_processing and is not processing" do
254
+ before :each do
255
+ attachment.instance.stubs(:respond_to?).with(:image_processing?).returns false
256
+ attachment.stubs(:processing?).returns false
257
+ end
258
+
259
+ it "returns true" do
260
+ subject.delayed_default_url?.should be_falsey
261
+ end
262
+ end
263
+
264
+ context "style is provided and is being processed" do
265
+ let(:style) { :main }
266
+ before :each do
267
+ attachment.stubs(:processing_style?).with(style).returns(true)
268
+ end
269
+
270
+ specify { expect(subject.delayed_default_url?(style)).to be }
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe DelayedPaperclip do
4
+ before :each do
5
+ reset_dummy
6
+ end
7
+
8
+ describe ".options" do
9
+ it ".options returns basic options" do
10
+ DelayedPaperclip.options.should == {:background_job_class => DelayedPaperclip::ProcessJob,
11
+ :url_with_processing => true,
12
+ :processing_image_url => nil,
13
+ :queue => "paperclip"}
14
+ end
15
+ end
16
+
17
+ describe ".processor" do
18
+ it ".processor returns processor" do
19
+ DelayedPaperclip.processor.should == DelayedPaperclip::ProcessJob
20
+ end
21
+ end
22
+
23
+ describe ".enqueue" do
24
+ it "delegates to processor" do
25
+ DelayedPaperclip::ProcessJob.expects(:enqueue_delayed_paperclip).with("Dummy", 1, :image)
26
+ DelayedPaperclip.enqueue("Dummy", 1, :image)
27
+ end
28
+ end
29
+
30
+ describe ".process_job" do
31
+ let(:dummy) { Dummy.create! }
32
+
33
+ it "returns early on invalid id" do
34
+ dummy_stub = stub
35
+ dummy_stub.expects(:where).with(id: dummy.id).returns([])
36
+ Dummy.expects(:unscoped).returns(dummy_stub)
37
+ dummy.image.expects(:process_delayed!).never
38
+ DelayedPaperclip.process_job("Dummy", dummy.id, :image)
39
+ end
40
+
41
+ it "finds dummy and calls #process_delayed!" do
42
+ dummy_stub = stub
43
+ dummy_stub.expects(:where).with(id: dummy.id).returns([dummy])
44
+ Dummy.expects(:unscoped).returns(dummy_stub)
45
+ dummy.image.expects(:process_delayed!)
46
+ DelayedPaperclip.process_job("Dummy", dummy.id, :image)
47
+ end
48
+ end
49
+
50
+ describe "paperclip definitions" do
51
+ before :each do
52
+ reset_dummy :paperclip => { styles: { thumbnail: "25x25"} }
53
+ end
54
+
55
+ it "returns paperclip options regardless of version" do
56
+ expect(Dummy.paperclip_definitions).to eq({:image => { :styles => { :thumbnail => "25x25" },
57
+ :delayed => { :priority => 0,
58
+ :only_process => [],
59
+ :url_with_processing => true,
60
+ :processing_image_url => nil,
61
+ :queue => "paperclip"}
62
+ }
63
+ })
64
+ end
65
+ end
66
+ end