File: processing_spec.rb

package info (click to toggle)
ruby-carrierwave 0.10.0%2Bgh-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,200 kB
  • ctags: 531
  • sloc: ruby: 7,180; sh: 26; makefile: 4
file content (162 lines) | stat: -rw-r--r-- 5,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# encoding: utf-8

require 'spec_helper'

describe CarrierWave::Uploader do

  before do
    @uploader_class = Class.new(CarrierWave::Uploader::Base)
    @uploader = @uploader_class.new
  end

  after do
    FileUtils.rm_rf(public_path)
  end

  describe '.process' do
    it "should add a single processor when a symbol is given" do
      @uploader_class.process :sepiatone
      @uploader.should_receive(:sepiatone)
      @uploader.process!
    end

    it "should add multiple processors when an array of symbols is given" do
      @uploader_class.process :sepiatone, :desaturate, :invert
      @uploader.should_receive(:sepiatone)
      @uploader.should_receive(:desaturate)
      @uploader.should_receive(:invert)
      @uploader.process!
    end

    it "should add a single processor with an argument when a hash is given" do
      @uploader_class.process :format => 'png'
      @uploader.should_receive(:format).with('png')
      @uploader.process!
    end

    it "should add a single processor with several argument when a hash is given" do
      @uploader_class.process :resize => [200, 300]
      @uploader.should_receive(:resize).with(200, 300)
      @uploader.process!
    end

    it "should add multiple processors when an hash with multiple keys is given" do
      @uploader_class.process :resize => [200, 300], :format => 'png'
      @uploader.should_receive(:resize).with(200, 300)
      @uploader.should_receive(:format).with('png')
      @uploader.process!
    end

    it "should call the processor if the condition method returns true" do
      @uploader_class.process :resize => [200, 300], :if => :true?
      @uploader_class.process :fancy, :if => :true?
      @uploader.should_receive(:true?).with("test.jpg").twice.and_return(true)
      @uploader.should_receive(:resize).with(200, 300)
      @uploader.should_receive(:fancy).with()
      @uploader.process!("test.jpg")
    end

    it "should not call the processor if the condition method returns false" do
      @uploader_class.process :resize => [200, 300], :if => :false?
      @uploader_class.process :fancy, :if => :false?
      @uploader.should_receive(:false?).with("test.jpg").twice.and_return(false)
      @uploader.should_not_receive(:resize)
      @uploader.should_not_receive(:fancy)
      @uploader.process!("test.jpg")
    end

    it "should call the processor if the condition block returns true" do
      @uploader_class.process :resize => [200, 300], :if => lambda{|record, args| record.true?(args[:file])}
      @uploader_class.process :fancy, :if => :true?
      @uploader.should_receive(:true?).with("test.jpg").twice.and_return(true)
      @uploader.should_receive(:resize).with(200, 300)
      @uploader.should_receive(:fancy).with()
      @uploader.process!("test.jpg")
    end

    it "should not call the processor if the condition block returns false" do
      @uploader_class.process :resize => [200, 300], :if => lambda{|record, args| record.false?(args[:file])}
      @uploader_class.process :fancy, :if => :false?
      @uploader.should_receive(:false?).with("test.jpg").twice.and_return(false)
      @uploader.should_not_receive(:resize)
      @uploader.should_not_receive(:fancy)
      @uploader.process!("test.jpg")
    end

    context "when using RMagick" do
      before do
        def @uploader.cover
          manipulate! { |frame, index| frame if index.zero? }
        end

        @uploader_class.send :include, CarrierWave::RMagick
      end

      after do
        @uploader.instance_eval { undef cover }
      end

      context "with a multi-page PDF" do
        before do
          @uploader.cache! File.open(file_path("multi_page.pdf"))
        end

        it "should successfully process" do
          @uploader_class.process :convert => 'jpg'
          @uploader.process!
        end

        it "should support page specific transformations" do
          @uploader_class.process :cover
          @uploader.process!
        end
      end

      context "with a simple image" do
        before do
          @uploader.cache! File.open(file_path("portrait.jpg"))
        end

        it "should still allow page specific transformations" do
          @uploader_class.process :cover
          @uploader.process!
        end
      end
    end

    context "with 'enable_processing' set to false" do
      it "should not do any processing" do
        @uploader_class.enable_processing = false
        @uploader_class.process :sepiatone, :desaturate, :invert
        @uploader.should_not_receive(:sepiatone)
        @uploader.should_not_receive(:desaturate)
        @uploader.should_not_receive(:invert)
        @uploader.process!
      end
    end
  end

  describe '#cache!' do
    before do
      CarrierWave.stub!(:generate_cache_id).and_return('1369894322-345-2255')
    end

    it "should trigger a process!" do
      @uploader.should_receive(:process!)
      @uploader.cache!(File.open(file_path('test.jpg')))
    end
  end

  describe '#recreate_versions!' do
    before do
      CarrierWave.stub!(:generate_cache_id).and_return('1369894322-345-2255')
    end

    it "should trigger a process!" do
      @uploader.store!(File.open(file_path('test.jpg')))
      @uploader.should_receive(:process!)
      @uploader.recreate_versions!
    end
  end

end