resizing 1.0.1 → 1.0.3
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.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +8 -2
- data/.gitignore +6 -0
- data/Gemfile +6 -2
- data/README.md +59 -32
- data/lib/resizing/active_storage/service/resizing_service.rb +4 -2
- data/lib/resizing/carrier_wave/storage/file.rb +43 -10
- data/lib/resizing/carrier_wave.rb +15 -9
- data/lib/resizing/client.rb +24 -25
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +6 -16
- data/lib/resizing/http_clientable.rb +3 -3
- data/lib/resizing/mock_client.rb +6 -5
- data/lib/resizing/public_id.rb +5 -4
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +23 -12
- data/resizing.gemspec +5 -6
- data/test/resizing/carrier_wave_test.rb +336 -30
- data/test/resizing/client_test.rb +68 -12
- data/test/resizing/configuration_test.rb +2 -3
- data/test/resizing/error_test.rb +9 -9
- data/test/resizing/http_clientable_test.rb +1 -1
- data/test/resizing/public_id_test.rb +1 -1
- data/test/resizing_module_test.rb +88 -6
- data/test/resizing_test.rb +16 -0
- data/test/test_helper.rb +146 -9
- data/test/vcr/carrier_wave_test/update_image.yml +63 -0
- metadata +37 -19
- /data/{exe → bin}/console +0 -0
- /data/{exe → bin}/generate-changelog +0 -0
- /data/{exe → bin}/setup +0 -0
|
@@ -38,7 +38,7 @@ module Resizing
|
|
|
38
38
|
Resizing.configure = @configuration_template
|
|
39
39
|
|
|
40
40
|
client = Resizing::Client.new
|
|
41
|
-
VCR.use_cassette 'client/post', record: :once
|
|
41
|
+
VCR.use_cassette 'client/post', record: :once do
|
|
42
42
|
r = client.post('test/data/images/sample1.jpg', content_type: 'image/jpeg')
|
|
43
43
|
assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
|
|
44
44
|
assert_equal(r['project_id'], Resizing.configure.project_id)
|
|
@@ -56,7 +56,7 @@ module Resizing
|
|
|
56
56
|
Resizing.configure = @configuration_template
|
|
57
57
|
|
|
58
58
|
client = Resizing::Client.new
|
|
59
|
-
VCR.use_cassette 'client/post', record: :once
|
|
59
|
+
VCR.use_cassette 'client/post', record: :once do
|
|
60
60
|
assert_raises ArgumentError do
|
|
61
61
|
client.post('file_is_not_exists', content_type: 'image/jpeg')
|
|
62
62
|
end
|
|
@@ -67,7 +67,7 @@ module Resizing
|
|
|
67
67
|
Resizing.configure = @configuration_template
|
|
68
68
|
|
|
69
69
|
client = Resizing::Client.new
|
|
70
|
-
VCR.use_cassette 'client/post', record: :once
|
|
70
|
+
VCR.use_cassette 'client/post', record: :once do
|
|
71
71
|
f = File.open('test/data/images/sample1.jpg', 'r')
|
|
72
72
|
r = client.post(f, content_type: 'image/jpeg')
|
|
73
73
|
assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
|
|
@@ -122,7 +122,7 @@ module Resizing
|
|
|
122
122
|
f = File.open('test/data/images/sample1.jpg', 'r')
|
|
123
123
|
|
|
124
124
|
assert_raises Resizing::APIError do
|
|
125
|
-
|
|
125
|
+
_r = client.put(name, f, content_type: 'image/jpeg')
|
|
126
126
|
end
|
|
127
127
|
end
|
|
128
128
|
|
|
@@ -152,7 +152,7 @@ module Resizing
|
|
|
152
152
|
rescue Resizing::APIError => e
|
|
153
153
|
response = e.decoded_body
|
|
154
154
|
end
|
|
155
|
-
assert_equal response, {
|
|
155
|
+
assert_equal response, { 'error' => 'Magick::ImageMagickError', 'message' => 'invalid image format found' }
|
|
156
156
|
end
|
|
157
157
|
end
|
|
158
158
|
|
|
@@ -165,20 +165,76 @@ module Resizing
|
|
|
165
165
|
VCR.use_cassette 'client/metadata', record: :once do
|
|
166
166
|
name = '87263920-2081-498e-a107-9625f4fde01b'
|
|
167
167
|
r = client.metadata(name)
|
|
168
|
+
# r.body
|
|
169
|
+
# {
|
|
170
|
+
# "id":"87263920-2081-498e-a107-9625f4fde01b",
|
|
171
|
+
# "project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f",
|
|
172
|
+
# "content_type":"image/jpeg",
|
|
173
|
+
# "latest_version_id":"Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo",
|
|
174
|
+
# "latest_etag":"\"5766f95a7f28e6a53dd6fd179bf03a32\"",
|
|
175
|
+
# "size":848590,
|
|
176
|
+
# "created_at":"2020-10-11T05:02:25.912Z",
|
|
177
|
+
# "updated_at":"2020-10-11T05:02:25.912Z",
|
|
178
|
+
# "filename":"sample1.jpg",
|
|
179
|
+
# "width":4032,
|
|
180
|
+
# "height":3016,
|
|
181
|
+
# "format":"jpeg",
|
|
182
|
+
# "version":"Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo",
|
|
183
|
+
# "public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo"
|
|
184
|
+
# }
|
|
185
|
+
|
|
168
186
|
assert_equal(r['id'], name)
|
|
169
187
|
assert_equal(r['project_id'], Resizing.configure.project_id)
|
|
170
188
|
assert_equal(r['content_type'], 'image/jpeg')
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
189
|
+
assert_equal(r['latest_version_id'], 'Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo')
|
|
190
|
+
assert_equal(r['latest_etag'], '"5766f95a7f28e6a53dd6fd179bf03a32"')
|
|
191
|
+
assert_equal(r['created_at'], '2020-10-11T05:02:25.912Z')
|
|
192
|
+
assert_equal(r['updated_at'], '2020-10-11T05:02:25.912Z')
|
|
193
|
+
assert_equal(r['width'], 4032)
|
|
194
|
+
assert_equal(r['height'], 3016)
|
|
195
|
+
assert_equal(r['format'], 'jpeg')
|
|
196
|
+
assert_equal(r['version'], 'Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo')
|
|
177
197
|
assert_equal(
|
|
178
198
|
r['public_id'],
|
|
179
|
-
|
|
199
|
+
'/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo'
|
|
180
200
|
)
|
|
181
201
|
end
|
|
182
202
|
end
|
|
203
|
+
|
|
204
|
+
def test_get_raises_not_implemented_error
|
|
205
|
+
Resizing.configure = @configuration_template
|
|
206
|
+
client = Resizing::Client.new
|
|
207
|
+
|
|
208
|
+
assert_raises NotImplementedError do
|
|
209
|
+
client.get('some_image_id')
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def test_post_raises_error_without_content_type
|
|
214
|
+
Resizing.configure = @configuration_template
|
|
215
|
+
client = Resizing::Client.new
|
|
216
|
+
|
|
217
|
+
assert_raises ArgumentError do
|
|
218
|
+
client.post('test/data/images/sample1.jpg', {})
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def test_post_raises_error_with_invalid_io
|
|
223
|
+
Resizing.configure = @configuration_template
|
|
224
|
+
client = Resizing::Client.new
|
|
225
|
+
|
|
226
|
+
assert_raises ArgumentError do
|
|
227
|
+
client.post(12_345, content_type: 'image/jpeg')
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def test_put_raises_error_without_content_type
|
|
232
|
+
Resizing.configure = @configuration_template
|
|
233
|
+
client = Resizing::Client.new
|
|
234
|
+
|
|
235
|
+
assert_raises ArgumentError do
|
|
236
|
+
client.put('image_id', 'test/data/images/sample1.jpg', {})
|
|
237
|
+
end
|
|
238
|
+
end
|
|
183
239
|
end
|
|
184
240
|
end
|
|
@@ -23,7 +23,6 @@ module Resizing
|
|
|
23
23
|
template = @template.dup
|
|
24
24
|
template.delete(:image_host)
|
|
25
25
|
config = Resizing::Configuration.new template
|
|
26
|
-
assert_equal(config.host, Resizing::Configuration::DEFAULT_IMAGE_HOST)
|
|
27
26
|
assert_equal(config.image_host, Resizing::Configuration::DEFAULT_IMAGE_HOST)
|
|
28
27
|
end
|
|
29
28
|
|
|
@@ -38,7 +37,7 @@ module Resizing
|
|
|
38
37
|
template = @template.dup
|
|
39
38
|
template[:host] = 'need raise execption if host is presented'
|
|
40
39
|
assert_raises ConfigurationError do
|
|
41
|
-
|
|
40
|
+
_config = Resizing::Configuration.new template
|
|
42
41
|
end
|
|
43
42
|
end
|
|
44
43
|
|
|
@@ -154,7 +153,7 @@ module Resizing
|
|
|
154
153
|
|
|
155
154
|
assert_instance_of String, image_id
|
|
156
155
|
# UUID format: 8-4-4-4-12
|
|
157
|
-
assert_match
|
|
156
|
+
assert_match(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/, image_id)
|
|
158
157
|
end
|
|
159
158
|
|
|
160
159
|
def test_generate_image_id_returns_different_uuids
|
data/test/resizing/error_test.rb
CHANGED
|
@@ -18,29 +18,29 @@ module Resizing
|
|
|
18
18
|
|
|
19
19
|
def test_api_error_has_decoded_body_accessor
|
|
20
20
|
error = Resizing::APIError.new('test error')
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
assert_respond_to error, :decoded_body
|
|
23
23
|
assert_respond_to error, :decoded_body=
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def test_api_error_decoded_body_defaults_to_empty_hash
|
|
27
27
|
error = Resizing::APIError.new('test error')
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
assert_equal({}, error.decoded_body)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
def test_api_error_decoded_body_can_be_set_with_hash
|
|
33
33
|
error = Resizing::APIError.new('test error')
|
|
34
34
|
body = { 'error' => 'test', 'code' => 400 }
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
error.decoded_body = body
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
assert_equal body, error.decoded_body
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
def test_api_error_decoded_body_raises_argument_error_for_non_hash
|
|
42
42
|
error = Resizing::APIError.new('test error')
|
|
43
|
-
|
|
43
|
+
|
|
44
44
|
assert_raises ArgumentError do
|
|
45
45
|
error.decoded_body = 'not a hash'
|
|
46
46
|
end
|
|
@@ -48,9 +48,9 @@ module Resizing
|
|
|
48
48
|
|
|
49
49
|
def test_api_error_decoded_body_raises_argument_error_for_array
|
|
50
50
|
error = Resizing::APIError.new('test error')
|
|
51
|
-
|
|
51
|
+
|
|
52
52
|
assert_raises ArgumentError do
|
|
53
|
-
error.decoded_body = [
|
|
53
|
+
error.decoded_body = %w[not a hash]
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
|
|
@@ -58,7 +58,7 @@ module Resizing
|
|
|
58
58
|
error = assert_raises Resizing::ConfigurationError do
|
|
59
59
|
raise Resizing::ConfigurationError, 'test configuration error'
|
|
60
60
|
end
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
assert_equal 'test configuration error', error.message
|
|
63
63
|
end
|
|
64
64
|
|
|
@@ -66,7 +66,7 @@ module Resizing
|
|
|
66
66
|
error = assert_raises Resizing::APIError do
|
|
67
67
|
raise Resizing::APIError, 'test api error'
|
|
68
68
|
end
|
|
69
|
-
|
|
69
|
+
|
|
70
70
|
assert_equal 'test api error', error.message
|
|
71
71
|
end
|
|
72
72
|
end
|
|
@@ -30,7 +30,7 @@ module Resizing
|
|
|
30
30
|
|
|
31
31
|
def test_expect_equal_identifier
|
|
32
32
|
public_id = Resizing::PublicId.new @public_id_as_string
|
|
33
|
-
assert_equal @public_id_as_string.gsub(
|
|
33
|
+
assert_equal @public_id_as_string.gsub(%r{/v.*$}, ''), public_id.identifier
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def test_expect_equal_public_id
|
|
@@ -5,16 +5,16 @@ require 'test_helper'
|
|
|
5
5
|
class ResizingModuleTest < Minitest::Test
|
|
6
6
|
def setup
|
|
7
7
|
# Reset configure before each test to ensure clean state
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
return unless Resizing.instance_variable_defined?(:@configure)
|
|
9
|
+
|
|
10
|
+
Resizing.remove_instance_variable(:@configure)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def teardown
|
|
14
14
|
# Reset configure after each test
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
return unless Resizing.instance_variable_defined?(:@configure)
|
|
16
|
+
|
|
17
|
+
Resizing.remove_instance_variable(:@configure)
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def test_configure_raises_error_when_not_initialized
|
|
@@ -121,4 +121,86 @@ class ResizingModuleTest < Minitest::Test
|
|
|
121
121
|
assert_includes url, 'image456/v789/'
|
|
122
122
|
assert_includes url, 'w_100'
|
|
123
123
|
end
|
|
124
|
+
|
|
125
|
+
def test_generate_identifier_returns_identifier_string
|
|
126
|
+
Resizing.configure = {
|
|
127
|
+
image_host: 'https://img.example.com',
|
|
128
|
+
project_id: 'project123',
|
|
129
|
+
secret_token: 'token123'
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
identifier = Resizing.generate_identifier
|
|
133
|
+
|
|
134
|
+
assert_instance_of String, identifier
|
|
135
|
+
assert_includes identifier, 'project123'
|
|
136
|
+
assert_match %r{/projects/project123/upload/images/}, identifier
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def test_client_returns_mock_client_when_enable_mock_is_true
|
|
140
|
+
Resizing.configure = {
|
|
141
|
+
image_host: 'https://img.example.com',
|
|
142
|
+
project_id: 'project123',
|
|
143
|
+
secret_token: 'token123',
|
|
144
|
+
enable_mock: true
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
client = Resizing.client
|
|
148
|
+
|
|
149
|
+
assert_instance_of Resizing::MockClient, client
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def test_client_returns_real_client_when_enable_mock_is_false
|
|
153
|
+
Resizing.configure = {
|
|
154
|
+
image_host: 'https://img.example.com',
|
|
155
|
+
project_id: 'project123',
|
|
156
|
+
secret_token: 'token123',
|
|
157
|
+
enable_mock: false
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
client = Resizing.client
|
|
161
|
+
|
|
162
|
+
assert_instance_of Resizing::Client, client
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def test_put_delegates_to_client
|
|
166
|
+
Resizing.configure = {
|
|
167
|
+
image_host: 'https://img.example.com',
|
|
168
|
+
project_id: 'project123',
|
|
169
|
+
secret_token: 'token123',
|
|
170
|
+
enable_mock: true
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
result = Resizing.put('image_id', 'dummy', content_type: 'image/jpeg')
|
|
174
|
+
|
|
175
|
+
assert_instance_of Hash, result
|
|
176
|
+
assert_equal 'image_id', result['id']
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def test_delete_delegates_to_client
|
|
180
|
+
Resizing.configure = {
|
|
181
|
+
image_host: 'https://img.example.com',
|
|
182
|
+
project_id: 'project123',
|
|
183
|
+
secret_token: 'token123',
|
|
184
|
+
enable_mock: true
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
result = Resizing.delete('image_id')
|
|
188
|
+
|
|
189
|
+
assert_instance_of Hash, result
|
|
190
|
+
assert_equal 'image_id', result['id']
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def test_metadata_delegates_to_client
|
|
194
|
+
Resizing.configure = {
|
|
195
|
+
image_host: 'https://img.example.com',
|
|
196
|
+
project_id: 'project123',
|
|
197
|
+
secret_token: 'token123',
|
|
198
|
+
enable_mock: true
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
result = Resizing.metadata('image_id', {})
|
|
202
|
+
|
|
203
|
+
assert_instance_of Hash, result
|
|
204
|
+
assert_equal 'image_id', result['id']
|
|
205
|
+
end
|
|
124
206
|
end
|
data/test/resizing_test.rb
CHANGED
|
@@ -6,4 +6,20 @@ class ResizingTest < Minitest::Test
|
|
|
6
6
|
def test_that_it_has_a_version_number
|
|
7
7
|
refute_nil ::Resizing::VERSION
|
|
8
8
|
end
|
|
9
|
+
|
|
10
|
+
def test_file_part_class_returns_valid_class
|
|
11
|
+
klass = Resizing.file_part_class
|
|
12
|
+
# Should return either Faraday::Multipart::FilePart (Faraday 2.x) or Faraday::UploadIO (Faraday 1.x)
|
|
13
|
+
assert [Faraday::Multipart::FilePart, Faraday::UploadIO].include?(klass),
|
|
14
|
+
"Expected Faraday::Multipart::FilePart or Faraday::UploadIO, got #{klass}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_file_part_class_returns_faraday_multipart_file_part_when_defined
|
|
18
|
+
# Faraday::Multipart::FilePart is defined when faraday-multipart gem is loaded
|
|
19
|
+
if defined?(Faraday::Multipart::FilePart)
|
|
20
|
+
assert_equal Faraday::Multipart::FilePart, Resizing.file_part_class
|
|
21
|
+
else
|
|
22
|
+
assert_equal Faraday::UploadIO, Resizing.file_part_class
|
|
23
|
+
end
|
|
24
|
+
end
|
|
9
25
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
require 'simplecov'
|
|
3
4
|
require 'simplecov-cobertura'
|
|
4
5
|
|
|
5
6
|
SimpleCov.start do
|
|
6
|
-
add_filter
|
|
7
|
+
add_filter '/test/'
|
|
7
8
|
|
|
8
9
|
if ENV['CI']
|
|
9
10
|
formatter SimpleCov::Formatter::CoberturaFormatter
|
|
@@ -28,6 +29,7 @@ require 'rails'
|
|
|
28
29
|
require 'active_record'
|
|
29
30
|
require 'fog-aws'
|
|
30
31
|
require 'carrierwave'
|
|
32
|
+
require 'carrierwave/orm/activerecord'
|
|
31
33
|
require 'resizing'
|
|
32
34
|
require 'pry-byebug'
|
|
33
35
|
|
|
@@ -40,11 +42,93 @@ VCR.configure do |c|
|
|
|
40
42
|
c.allow_http_connections_when_no_cassette = false
|
|
41
43
|
|
|
42
44
|
# raise Faraday::TimeoutError, when project_id is timeout_project_id
|
|
43
|
-
c.before_http_request(
|
|
45
|
+
c.before_http_request(->(r) { URI(r.uri).path.match? %r{/projects/timeout_project_id} }) do
|
|
44
46
|
raise Faraday::TimeoutError
|
|
45
47
|
end
|
|
46
48
|
end
|
|
47
49
|
|
|
50
|
+
# VCRカセットのリクエストが実際に使用されたかを検証するヘルパー
|
|
51
|
+
module VCRRequestAssertions
|
|
52
|
+
# VCRカセット内でブロックを実行し、カセットのインタラクションがすべて使用されたことを確認
|
|
53
|
+
#
|
|
54
|
+
# @param cassette_name [String] VCRカセット名
|
|
55
|
+
# @param options [Hash] VCR.use_cassetteに渡すオプション
|
|
56
|
+
# @yield 実行するブロック
|
|
57
|
+
# @return [void]
|
|
58
|
+
#
|
|
59
|
+
# @example
|
|
60
|
+
# assert_vcr_requests_made 'carrier_wave_test/remove_resizing_picture' do
|
|
61
|
+
# model.remove_resizing_picture!
|
|
62
|
+
# model.save!
|
|
63
|
+
# end
|
|
64
|
+
def assert_vcr_requests_made(cassette_name, options = {})
|
|
65
|
+
options = { record: :none }.merge(options)
|
|
66
|
+
|
|
67
|
+
VCR.use_cassette(cassette_name, options) do |cassette|
|
|
68
|
+
interaction_list = cassette.http_interactions
|
|
69
|
+
initial_count = interaction_list.remaining_unused_interaction_count
|
|
70
|
+
|
|
71
|
+
assert initial_count.positive?,
|
|
72
|
+
"Cassette '#{cassette_name}' should have at least 1 interaction"
|
|
73
|
+
|
|
74
|
+
yield cassette if block_given?
|
|
75
|
+
|
|
76
|
+
remaining_count = interaction_list.remaining_unused_interaction_count
|
|
77
|
+
used_count = initial_count - remaining_count
|
|
78
|
+
|
|
79
|
+
assert_equal 0, remaining_count,
|
|
80
|
+
"Expected all #{initial_count} cassette interactions to be used, " \
|
|
81
|
+
"but #{remaining_count} remain unused (#{used_count} were used)"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# VCRカセット内でブロックを実行し、指定した数のインタラクションが使用されたことを確認
|
|
86
|
+
#
|
|
87
|
+
# @param cassette_name [String] VCRカセット名
|
|
88
|
+
# @param expected_count [Integer] 使用されるべきインタラクション数
|
|
89
|
+
# @param options [Hash] VCR.use_cassetteに渡すオプション
|
|
90
|
+
# @yield 実行するブロック
|
|
91
|
+
# @return [void]
|
|
92
|
+
#
|
|
93
|
+
# @example
|
|
94
|
+
# assert_vcr_requests_count 'client/post', 1 do
|
|
95
|
+
# Resizing.post(file)
|
|
96
|
+
# end
|
|
97
|
+
def assert_vcr_requests_count(cassette_name, expected_count, options = {})
|
|
98
|
+
options = { record: :none }.merge(options)
|
|
99
|
+
|
|
100
|
+
VCR.use_cassette(cassette_name, options) do |cassette|
|
|
101
|
+
interaction_list = cassette.http_interactions
|
|
102
|
+
initial_count = interaction_list.remaining_unused_interaction_count
|
|
103
|
+
|
|
104
|
+
yield cassette if block_given?
|
|
105
|
+
|
|
106
|
+
remaining_count = interaction_list.remaining_unused_interaction_count
|
|
107
|
+
used_count = initial_count - remaining_count
|
|
108
|
+
|
|
109
|
+
assert_equal expected_count, used_count,
|
|
110
|
+
"Expected #{expected_count} cassette interactions to be used, " \
|
|
111
|
+
"but #{used_count} were used"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# VCRカセット内でブロックを実行し、リクエストが発行されないことを確認
|
|
116
|
+
#
|
|
117
|
+
# @param cassette_name [String] VCRカセット名
|
|
118
|
+
# @param options [Hash] VCR.use_cassetteに渡すオプション
|
|
119
|
+
# @yield 実行するブロック
|
|
120
|
+
# @return [void]
|
|
121
|
+
#
|
|
122
|
+
# @example
|
|
123
|
+
# assert_vcr_no_requests 'carrier_wave_test/remove_resizing_picture' do
|
|
124
|
+
# model.remove_resizing_picture = true
|
|
125
|
+
# # save!を呼ばないのでリクエストは発行されない
|
|
126
|
+
# end
|
|
127
|
+
def assert_vcr_no_requests(cassette_name, options = {}, &block)
|
|
128
|
+
assert_vcr_requests_count(cassette_name, 0, options, &block)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
48
132
|
ActiveRecord::Base.establish_connection(
|
|
49
133
|
adapter: 'mysql2',
|
|
50
134
|
host: '127.0.0.1',
|
|
@@ -58,7 +142,7 @@ ActiveRecord::Base.establish_connection(
|
|
|
58
142
|
ActiveRecord::Schema.define do
|
|
59
143
|
self.verbose = false
|
|
60
144
|
|
|
61
|
-
%i
|
|
145
|
+
%i[test_models test_jpg_models test_model_with_default_urls].each do |model_name|
|
|
62
146
|
connection.execute "drop table if exists #{model_name}"
|
|
63
147
|
|
|
64
148
|
create_table model_name do |t|
|
|
@@ -103,19 +187,72 @@ class ResizingUploaderWithDefaultURL < CarrierWave::Uploader::Base
|
|
|
103
187
|
end
|
|
104
188
|
|
|
105
189
|
class TestModel < ::ActiveRecord::Base
|
|
106
|
-
extend CarrierWave::Mount
|
|
107
|
-
|
|
108
190
|
mount_uploader :resizing_picture, ResizingUploader
|
|
109
191
|
end
|
|
110
192
|
|
|
111
193
|
class TestJPGModel < ::ActiveRecord::Base
|
|
112
|
-
extend CarrierWave::Mount
|
|
113
|
-
|
|
114
194
|
mount_uploader :resizing_picture, ResizingJPGUploader
|
|
115
195
|
end
|
|
116
196
|
|
|
117
197
|
class TestModelWithDefaultURL < ::ActiveRecord::Base
|
|
118
|
-
extend CarrierWave::Mount
|
|
119
|
-
|
|
120
198
|
mount_uploader :resizing_picture, ResizingUploaderWithDefaultURL
|
|
121
199
|
end
|
|
200
|
+
|
|
201
|
+
# コールバックのテスト用モデル
|
|
202
|
+
# mount_uploader が登録する各種コールバックが正しく動作するかをテストするため
|
|
203
|
+
# カスタムコールバックを追加して呼び出しを追跡する
|
|
204
|
+
class TestModelWithCallbackTracking < ::ActiveRecord::Base
|
|
205
|
+
self.table_name = 'test_models'
|
|
206
|
+
|
|
207
|
+
mount_uploader :resizing_picture, ResizingUploader
|
|
208
|
+
|
|
209
|
+
attr_accessor :callback_log
|
|
210
|
+
|
|
211
|
+
# before_save / after_save コールバック
|
|
212
|
+
before_save :track_before_save
|
|
213
|
+
after_save :track_after_save
|
|
214
|
+
|
|
215
|
+
# before_destroy / after_destroy コールバック
|
|
216
|
+
before_destroy :track_before_destroy
|
|
217
|
+
after_destroy :track_after_destroy
|
|
218
|
+
|
|
219
|
+
# after_commit コールバック
|
|
220
|
+
after_commit :track_create_commit, on: :create
|
|
221
|
+
after_commit :track_update_commit, on: :update
|
|
222
|
+
after_commit :track_destroy_commit, on: :destroy
|
|
223
|
+
|
|
224
|
+
def initialize(*args)
|
|
225
|
+
super
|
|
226
|
+
@callback_log = []
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
private
|
|
230
|
+
|
|
231
|
+
def track_before_save
|
|
232
|
+
@callback_log << :before_save
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def track_after_save
|
|
236
|
+
@callback_log << :after_save
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def track_before_destroy
|
|
240
|
+
@callback_log << :before_destroy
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def track_after_destroy
|
|
244
|
+
@callback_log << :after_destroy
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def track_create_commit
|
|
248
|
+
@callback_log << :create_commit
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def track_update_commit
|
|
252
|
+
@callback_log << :update_commit
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def track_destroy_commit
|
|
256
|
+
@callback_log << :destroy_commit
|
|
257
|
+
end
|
|
258
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
---
|
|
2
|
+
http_interactions:
|
|
3
|
+
- request:
|
|
4
|
+
method: post
|
|
5
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/
|
|
6
|
+
body:
|
|
7
|
+
encoding: ASCII-8BIT
|
|
8
|
+
string: ''
|
|
9
|
+
headers:
|
|
10
|
+
User-Agent:
|
|
11
|
+
- Faraday v1.0.1
|
|
12
|
+
response:
|
|
13
|
+
status:
|
|
14
|
+
code: 200
|
|
15
|
+
message: OK
|
|
16
|
+
headers:
|
|
17
|
+
content-type:
|
|
18
|
+
- application/json; charset=utf-8
|
|
19
|
+
body:
|
|
20
|
+
encoding: UTF-8
|
|
21
|
+
string: '{"id":"old-image-id-1111-1111-111111111111","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v1","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old-image-id-1111-1111-111111111111/v1"}'
|
|
22
|
+
recorded_at: Sun, 11 Oct 2020 04:56:48 GMT
|
|
23
|
+
- request:
|
|
24
|
+
method: post
|
|
25
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/
|
|
26
|
+
body:
|
|
27
|
+
encoding: ASCII-8BIT
|
|
28
|
+
string: ''
|
|
29
|
+
headers:
|
|
30
|
+
User-Agent:
|
|
31
|
+
- Faraday v1.0.1
|
|
32
|
+
response:
|
|
33
|
+
status:
|
|
34
|
+
code: 200
|
|
35
|
+
message: OK
|
|
36
|
+
headers:
|
|
37
|
+
content-type:
|
|
38
|
+
- application/json; charset=utf-8
|
|
39
|
+
body:
|
|
40
|
+
encoding: UTF-8
|
|
41
|
+
string: '{"id":"new-image-id-2222-2222-222222222222","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v2","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/new-image-id-2222-2222-222222222222/v2"}'
|
|
42
|
+
recorded_at: Sun, 11 Oct 2020 04:56:49 GMT
|
|
43
|
+
- request:
|
|
44
|
+
method: delete
|
|
45
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old-image-id-1111-1111-111111111111
|
|
46
|
+
body:
|
|
47
|
+
encoding: US-ASCII
|
|
48
|
+
string: ''
|
|
49
|
+
headers:
|
|
50
|
+
User-Agent:
|
|
51
|
+
- Faraday v1.0.1
|
|
52
|
+
response:
|
|
53
|
+
status:
|
|
54
|
+
code: 200
|
|
55
|
+
message: OK
|
|
56
|
+
headers:
|
|
57
|
+
content-type:
|
|
58
|
+
- application/json; charset=utf-8
|
|
59
|
+
body:
|
|
60
|
+
encoding: UTF-8
|
|
61
|
+
string: '{"id":"old-image-id-1111-1111-111111111111","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f"}'
|
|
62
|
+
recorded_at: Sun, 11 Oct 2020 04:56:50 GMT
|
|
63
|
+
recorded_with: VCR 6.0.0
|