resizing 1.1.0.pre → 1.2.1

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +17 -2
  3. data/Gemfile +6 -2
  4. data/README.md +59 -32
  5. data/lib/resizing/active_storage/service/resizing_service.rb +6 -2
  6. data/lib/resizing/active_storage/service.rb +9 -0
  7. data/lib/resizing/active_storage.rb +7 -0
  8. data/lib/resizing/carrier_wave/storage/file.rb +35 -16
  9. data/lib/resizing/carrier_wave/storage/remote.rb +7 -3
  10. data/lib/resizing/carrier_wave.rb +10 -11
  11. data/lib/resizing/client.rb +40 -24
  12. data/lib/resizing/configurable.rb +1 -1
  13. data/lib/resizing/configuration.rb +6 -16
  14. data/lib/resizing/http_clientable.rb +3 -3
  15. data/lib/resizing/mock_client.rb +6 -5
  16. data/lib/resizing/public_id.rb +5 -4
  17. data/lib/resizing/version.rb +1 -1
  18. data/lib/resizing.rb +15 -12
  19. data/resizing.gemspec +5 -8
  20. data/test/resizing/active_storage_service_test.rb +98 -0
  21. data/test/resizing/carrier_wave/storage/file_test.rb +149 -8
  22. data/test/resizing/carrier_wave/storage/remote_test.rb +75 -0
  23. data/test/resizing/carrier_wave_test.rb +99 -37
  24. data/test/resizing/client_test.rb +96 -11
  25. data/test/resizing/configurable_test.rb +82 -0
  26. data/test/resizing/configuration_test.rb +118 -2
  27. data/test/resizing/constants_test.rb +25 -0
  28. data/test/resizing/error_test.rb +73 -0
  29. data/test/resizing/http_clientable_test.rb +84 -0
  30. data/test/resizing/mock_client_test.rb +75 -0
  31. data/test/resizing/public_id_test.rb +1 -1
  32. data/test/resizing_module_test.rb +206 -0
  33. data/test/test_helper.rb +89 -9
  34. metadata +33 -47
  35. data/lib/resizing/video/client.rb +0 -116
  36. data/lib/resizing/video.rb +0 -8
  37. data/test/resizing/video/client_test.rb +0 -158
  38. data/test/vcr/video/metadata/success.yml +0 -47
  39. data/test/vcr/video/prepare/success.yml +0 -47
  40. data/test/vcr/video/upload_completed/success.yml +0 -47
  41. /data/{exe → bin}/console +0 -0
  42. /data/{exe → bin}/generate-changelog +0 -0
  43. /data/{exe → bin}/setup +0 -0
@@ -4,6 +4,8 @@ require 'test_helper'
4
4
 
5
5
  module Resizing
6
6
  class CarrierWaveTest < Minitest::Test
7
+ include VCRRequestAssertions
8
+
7
9
  def setup
8
10
  TestModel.delete_all
9
11
  TestJPGModel.delete_all
@@ -23,24 +25,65 @@ module Resizing
23
25
 
24
26
  def test_remove_resizing_picture
25
27
  model = prepare_model TestModel
28
+ model.save!
29
+
30
+ # VCRカセットのDELETEリクエストが実際に発行されたことを確認
31
+ assert_vcr_requests_made 'carrier_wave_test/remove_resizing_picture' do
32
+ model.remove_resizing_picture!
33
+ model.save!
34
+ end
35
+
36
+ # DELETEが成功した結果、URLがnilになることを確認
37
+ assert_nil model.resizing_picture_url
38
+ end
39
+
40
+ def test_remove_calls_delete_immediately
41
+ model = prepare_model TestModel
42
+ model.save!
43
+
44
+ # remove!を呼んだとき即時にDELETEリクエストがとぶことを確認する
45
+ assert_vcr_requests_made 'carrier_wave_test/remove_resizing_picture' do
46
+ model.resizing_picture.remove!
47
+ end
48
+ end
49
+
50
+ def test_blank_returns_true_after_remove
51
+ model = prepare_model TestModel
52
+ model.save!
26
53
 
27
- VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
28
- SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
29
- model.remove_resizing_picture!
54
+ refute model.resizing_picture.blank?, 'resizing_picture should not be blank before remove'
30
55
 
31
- assert_nil model.resizing_picture_url
32
- end
56
+ # DELETEリクエストが発行されたことを確認
57
+ assert_vcr_requests_made 'carrier_wave_test/remove_resizing_picture' do
58
+ model.remove_resizing_picture!
59
+ model.save!
33
60
  end
61
+
62
+ assert model.resizing_picture.blank?, 'resizing_picture should be blank after remove'
63
+ assert_nil model.resizing_picture_url
34
64
  end
35
65
 
36
- def test_do_not_raise_if_empty_column_is_removed
66
+ def test_blank_returns_true_for_new_record
67
+ model = TestModel.new
68
+ assert model.resizing_picture.blank?, 'resizing_picture should be blank for new record'
69
+ end
70
+
71
+ def test_remove_resizing_picture_with_flag_and_save
37
72
  model = prepare_model TestModel
73
+ model.save!
74
+
75
+ refute model.resizing_picture.blank?, 'resizing_picture should not be blank before remove'
76
+
77
+ model.remove_resizing_picture = true
38
78
 
39
- VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
40
- SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
41
- model.resizing_picture.remove!
42
- end
79
+ # save時にDELETEリクエストが発行されたことを確認
80
+ assert_vcr_requests_made 'carrier_wave_test/remove_resizing_picture' do
81
+ model.save!
43
82
  end
83
+
84
+ # DELETEが成功した結果、blank?がtrueになることを確認
85
+ assert model.resizing_picture.blank?, 'resizing_picture should be blank after save'
86
+ assert_nil model.resizing_picture_url
44
87
  end
45
88
 
46
89
  def test_picture_url_return_correct_value_and_when_model_reloaded
@@ -84,42 +127,61 @@ module Resizing
84
127
  end
85
128
 
86
129
  def test_is_successful
87
- model = prepare_model_with_tempfile TestModel
130
+ model = TestModel.new
131
+
132
+ # POSTリクエストが発行されたことを確認
133
+ assert_vcr_requests_made 'carrier_wave_test/save' do
134
+ file = File.open('test/data/images/sample1.jpg', 'r')
135
+ model.resizing_picture = file
136
+ end
137
+
138
+ assert_equal('http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e/', model.resizing_picture_url)
139
+ end
140
+
141
+ def test_file_returns_same_instance_on_multiple_calls
142
+ model = prepare_model TestModel
88
143
  model.save!
89
- # assert_equal('http://192.168.56.101:5000/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850__tqNsnoGWWUibtIBZ5NgjV45M/', model.resizing_picture_url)
144
+
145
+ file1 = model.resizing_picture.file
146
+ file2 = model.resizing_picture.file
147
+
148
+ assert_same file1, file2
90
149
  end
91
150
 
92
- def expect_url
93
- 'http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/'+
94
- 'upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
151
+ def test_file_returns_nil_for_blank_identifier
152
+ model = TestModel.new
153
+ assert_nil model.resizing_picture.file
95
154
  end
96
155
 
97
- def prepare_model model
98
- VCR.use_cassette 'carrier_wave_test/save', record: :once do
99
- SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
100
- model = model.new
101
- file = File.open('test/data/images/sample1.jpg', 'r')
102
- uploaded_file = ActionDispatch::Http::UploadedFile.new(
103
- filename: File.basename(file.path),
104
- type: 'image/jpeg',
105
- tempfile: file
106
- )
107
-
108
- model.resizing_picture = uploaded_file
109
- return model
110
- end
111
- end
156
+ def test_file_uses_read_column_when_identifier_nil
157
+ model = prepare_model TestModel
158
+ model.save!
159
+ model.reload
160
+
161
+ file = model.resizing_picture.file
162
+ refute_nil file
163
+ assert_instance_of Resizing::CarrierWave::Storage::File, file
164
+ end
165
+
166
+ def expect_url
167
+ 'http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/' \
168
+ 'upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
112
169
  end
113
170
 
114
- def prepare_model_with_tempfile model
171
+ def prepare_model(model)
172
+ result = nil
115
173
  VCR.use_cassette 'carrier_wave_test/save', record: :once do
116
- SecureRandom.stub :uuid, '28c49144-c00d-4cb5-8619-98ce95977b9c' do
117
- model = model.new
118
- file = File.open('test/data/images/sample1.jpg', 'r')
119
- model.resizing_picture = file
120
- return model
121
- end
174
+ result = model.new
175
+ file = File.open('test/data/images/sample1.jpg', 'r')
176
+ uploaded_file = ActionDispatch::Http::UploadedFile.new(
177
+ filename: File.basename(file.path),
178
+ type: 'image/jpeg',
179
+ tempfile: file
180
+ )
181
+
182
+ result.resizing_picture = uploaded_file
122
183
  end
184
+ result
123
185
  end
124
186
  end
125
187
  end
@@ -34,11 +34,40 @@ module Resizing
34
34
  assert_equal(client.config, config)
35
35
  end
36
36
 
37
- def test_is_postable_file
37
+ def test_is_postable_with_filename
38
38
  Resizing.configure = @configuration_template
39
39
 
40
40
  client = Resizing::Client.new
41
- VCR.use_cassette 'client/post', record: :once do
41
+ VCR.use_cassette 'client/post', record: :once do
42
+ r = client.post('test/data/images/sample1.jpg', content_type: 'image/jpeg')
43
+ assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
44
+ assert_equal(r['project_id'], Resizing.configure.project_id)
45
+ assert_equal(r['content_type'], 'image/jpeg')
46
+ assert(!r['latest_version_id'].nil?)
47
+ assert(!r['latest_etag'].nil?)
48
+ assert(!r['created_at'].nil?)
49
+ assert(!r['updated_at'].nil?)
50
+ assert_equal(r['public_id'], '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo')
51
+ assert_equal(r['filename'], 'sample1.jpg')
52
+ end
53
+ end
54
+
55
+ def test_is_unpostable_with_filename
56
+ Resizing.configure = @configuration_template
57
+
58
+ client = Resizing::Client.new
59
+ VCR.use_cassette 'client/post', record: :once do
60
+ assert_raises ArgumentError do
61
+ client.post('file_is_not_exists', content_type: 'image/jpeg')
62
+ end
63
+ end
64
+ end
65
+
66
+ def test_is_postable_with_file
67
+ Resizing.configure = @configuration_template
68
+
69
+ client = Resizing::Client.new
70
+ VCR.use_cassette 'client/post', record: :once do
42
71
  f = File.open('test/data/images/sample1.jpg', 'r')
43
72
  r = client.post(f, content_type: 'image/jpeg')
44
73
  assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
@@ -93,7 +122,7 @@ module Resizing
93
122
  f = File.open('test/data/images/sample1.jpg', 'r')
94
123
 
95
124
  assert_raises Resizing::APIError do
96
- r = client.put(name, f, content_type: 'image/jpeg')
125
+ _r = client.put(name, f, content_type: 'image/jpeg')
97
126
  end
98
127
  end
99
128
 
@@ -123,7 +152,7 @@ module Resizing
123
152
  rescue Resizing::APIError => e
124
153
  response = e.decoded_body
125
154
  end
126
- assert_equal response, {"error"=>"Magick::ImageMagickError", "message"=>"invalid image format found"}
155
+ assert_equal response, { 'error' => 'Magick::ImageMagickError', 'message' => 'invalid image format found' }
127
156
  end
128
157
  end
129
158
 
@@ -136,20 +165,76 @@ module Resizing
136
165
  VCR.use_cassette 'client/metadata', record: :once do
137
166
  name = '87263920-2081-498e-a107-9625f4fde01b'
138
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
+
139
186
  assert_equal(r['id'], name)
140
187
  assert_equal(r['project_id'], Resizing.configure.project_id)
141
188
  assert_equal(r['content_type'], 'image/jpeg')
142
- assert(r['latest_version_id'] != nil)
143
- assert(r['latest_etag'] != nil)
144
- assert(r['created_at'] != nil)
145
- assert(r['updated_at'] != nil)
146
- assert(r['height'] != nil)
147
- assert(r['width'] != nil)
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')
148
197
  assert_equal(
149
198
  r['public_id'],
150
- "/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo"
199
+ '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo'
151
200
  )
152
201
  end
153
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
154
239
  end
155
240
  end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class ConfigurableTest < Minitest::Test
7
+ class TestConfigurable
8
+ include Configurable
9
+
10
+ def initialize(config = nil)
11
+ initialize_config(config)
12
+ end
13
+ end
14
+
15
+ def setup
16
+ # NOP
17
+ end
18
+
19
+ def teardown
20
+ # NOP
21
+ end
22
+
23
+ def test_initialize_config_with_configuration_object
24
+ config = Configuration.new(
25
+ image_host: 'https://test.example.com',
26
+ project_id: 'test_id',
27
+ secret_token: 'test_token'
28
+ )
29
+ obj = TestConfigurable.new(config)
30
+
31
+ assert_equal config, obj.config
32
+ end
33
+
34
+ def test_initialize_config_with_nil_uses_global_configure
35
+ Resizing.configure = Configuration.new(
36
+ image_host: 'https://global.example.com',
37
+ project_id: 'global_id',
38
+ secret_token: 'global_token'
39
+ )
40
+
41
+ obj = TestConfigurable.new(nil)
42
+
43
+ assert_equal Resizing.configure.image_host, obj.config.image_host
44
+ end
45
+
46
+ def test_initialize_config_with_hash
47
+ config_hash = {
48
+ image_host: 'https://hash.example.com',
49
+ project_id: 'hash_id',
50
+ secret_token: 'hash_token'
51
+ }
52
+ obj = TestConfigurable.new(config_hash)
53
+
54
+ assert_equal 'https://hash.example.com', obj.config.image_host
55
+ assert_equal 'hash_id', obj.config.project_id
56
+ end
57
+
58
+ def test_config_is_accessible
59
+ config = Configuration.new(
60
+ image_host: 'https://test.example.com',
61
+ project_id: 'test_id',
62
+ secret_token: 'test_token'
63
+ )
64
+ obj = TestConfigurable.new(config)
65
+
66
+ assert_instance_of Configuration, obj.config
67
+ end
68
+
69
+ def test_attr_reader_config_is_defined
70
+ config = Configuration.new(
71
+ image_host: 'https://test.example.com',
72
+ project_id: 'test_id',
73
+ secret_token: 'test_token'
74
+ )
75
+ obj = TestConfigurable.new(config)
76
+
77
+ # Verify that config is accessible as a reader
78
+ assert obj.respond_to?(:config)
79
+ assert !obj.respond_to?(:config=)
80
+ end
81
+ end
82
+ 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
- config = Resizing::Configuration.new template
40
+ _config = Resizing::Configuration.new template
42
41
  end
43
42
  end
44
43
 
@@ -147,5 +146,122 @@ module Resizing
147
146
  config = Resizing::Configuration.new @template
148
147
  assert_match %r{/projects/#{config.project_id}/upload/images/[\da-z-]}, config.generate_identifier
149
148
  end
149
+
150
+ def test_generate_image_id_returns_uuid
151
+ config = Resizing::Configuration.new @template
152
+ image_id = config.generate_image_id
153
+
154
+ assert_instance_of String, image_id
155
+ # UUID format: 8-4-4-4-12
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)
157
+ end
158
+
159
+ def test_generate_image_id_returns_different_uuids
160
+ config = Resizing::Configuration.new @template
161
+ id1 = config.generate_image_id
162
+ id2 = config.generate_image_id
163
+
164
+ refute_equal id1, id2
165
+ end
166
+
167
+ def test_equality_returns_true_for_same_configurations
168
+ config1 = Resizing::Configuration.new @template
169
+ config2 = Resizing::Configuration.new @template
170
+
171
+ assert_equal config1, config2
172
+ end
173
+
174
+ def test_equality_returns_false_for_different_image_host
175
+ config1 = Resizing::Configuration.new @template
176
+ template2 = @template.dup
177
+ template2[:image_host] = 'http://different.host'
178
+ config2 = Resizing::Configuration.new template2
179
+
180
+ refute_equal config1, config2
181
+ end
182
+
183
+ def test_equality_returns_false_for_different_project_id
184
+ config1 = Resizing::Configuration.new @template
185
+ template2 = @template.dup
186
+ template2[:project_id] = 'different-project-id'
187
+ config2 = Resizing::Configuration.new template2
188
+
189
+ refute_equal config1, config2
190
+ end
191
+
192
+ def test_equality_returns_false_for_different_class
193
+ config = Resizing::Configuration.new @template
194
+
195
+ refute_equal config, 'not a configuration'
196
+ refute_equal config, @template
197
+ end
198
+
199
+ def test_transformation_path_with_multiple_transforms
200
+ config = Resizing::Configuration.new @template
201
+ transforms = [
202
+ { w: 100, h: 200 },
203
+ { f: 'webp', q: 80 }
204
+ ]
205
+
206
+ path = config.transformation_path(transforms)
207
+
208
+ assert_equal 'w_100,h_200/f_webp,q_80', path
209
+ end
210
+
211
+ def test_transformation_path_with_single_hash
212
+ config = Resizing::Configuration.new @template
213
+ transform = { w: 300, h: 300, c: 'fill' }
214
+
215
+ path = config.transformation_path(transform)
216
+
217
+ assert_equal 'w_300,h_300,c_fill', path
218
+ end
219
+
220
+ def test_transformation_path_ignores_unknown_options
221
+ config = Resizing::Configuration.new @template
222
+ transform = { w: 100, unknown_option: 'ignored', h: 200 }
223
+
224
+ path = config.transformation_path(transform)
225
+
226
+ assert_equal 'w_100,h_200', path
227
+ refute_includes path, 'unknown_option'
228
+ end
229
+
230
+ def test_transformation_path_with_empty_array
231
+ config = Resizing::Configuration.new @template
232
+ path = config.transformation_path([])
233
+
234
+ assert_equal '', path
235
+ end
236
+
237
+ def test_generate_identifier_includes_project_id
238
+ config = Resizing::Configuration.new @template
239
+ identifier = config.generate_identifier
240
+
241
+ assert_includes identifier, config.project_id
242
+ end
243
+
244
+ def test_generate_identifier_has_correct_format
245
+ config = Resizing::Configuration.new @template
246
+ identifier = config.generate_identifier
247
+
248
+ assert_match %r{\A/projects/[\da-z-]+/upload/images/[\da-z-]+\z}, identifier
249
+ end
250
+
251
+ def test_enable_mock_defaults_to_false
252
+ template = @template.dup
253
+ template.delete(:enable_mock)
254
+ config = Resizing::Configuration.new template
255
+
256
+ assert_equal false, config.enable_mock
257
+ end
258
+
259
+ def test_enable_mock_can_be_set_to_true
260
+ template = @template.dup
261
+ template[:enable_mock] = true
262
+ config = Resizing::Configuration.new template
263
+
264
+ assert_equal true, config.enable_mock
265
+ end
150
266
  end
151
267
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class ConstantsTest < Minitest::Test
7
+ def test_http_status_ok_is_defined
8
+ assert_equal 200, Resizing::Constants::HTTP_STATUS_OK
9
+ end
10
+
11
+ def test_http_status_created_is_defined
12
+ assert_equal 201, Resizing::Constants::HTTP_STATUS_CREATED
13
+ end
14
+
15
+ def test_http_status_not_found_is_defined
16
+ assert_equal 404, Resizing::Constants::HTTP_STATUS_NOT_FOUND
17
+ end
18
+
19
+ def test_constants_are_integers
20
+ assert_instance_of Integer, Resizing::Constants::HTTP_STATUS_OK
21
+ assert_instance_of Integer, Resizing::Constants::HTTP_STATUS_CREATED
22
+ assert_instance_of Integer, Resizing::Constants::HTTP_STATUS_NOT_FOUND
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module Resizing
6
+ class ErrorTest < Minitest::Test
7
+ def test_error_is_standard_error_subclass
8
+ assert Resizing::Error < StandardError
9
+ end
10
+
11
+ def test_configuration_error_is_error_subclass
12
+ assert Resizing::ConfigurationError < Resizing::Error
13
+ end
14
+
15
+ def test_api_error_is_error_subclass
16
+ assert Resizing::APIError < Resizing::Error
17
+ end
18
+
19
+ def test_api_error_has_decoded_body_accessor
20
+ error = Resizing::APIError.new('test error')
21
+
22
+ assert_respond_to error, :decoded_body
23
+ assert_respond_to error, :decoded_body=
24
+ end
25
+
26
+ def test_api_error_decoded_body_defaults_to_empty_hash
27
+ error = Resizing::APIError.new('test error')
28
+
29
+ assert_equal({}, error.decoded_body)
30
+ end
31
+
32
+ def test_api_error_decoded_body_can_be_set_with_hash
33
+ error = Resizing::APIError.new('test error')
34
+ body = { 'error' => 'test', 'code' => 400 }
35
+
36
+ error.decoded_body = body
37
+
38
+ assert_equal body, error.decoded_body
39
+ end
40
+
41
+ def test_api_error_decoded_body_raises_argument_error_for_non_hash
42
+ error = Resizing::APIError.new('test error')
43
+
44
+ assert_raises ArgumentError do
45
+ error.decoded_body = 'not a hash'
46
+ end
47
+ end
48
+
49
+ def test_api_error_decoded_body_raises_argument_error_for_array
50
+ error = Resizing::APIError.new('test error')
51
+
52
+ assert_raises ArgumentError do
53
+ error.decoded_body = %w[not a hash]
54
+ end
55
+ end
56
+
57
+ def test_configuration_error_can_be_raised_with_message
58
+ error = assert_raises Resizing::ConfigurationError do
59
+ raise Resizing::ConfigurationError, 'test configuration error'
60
+ end
61
+
62
+ assert_equal 'test configuration error', error.message
63
+ end
64
+
65
+ def test_api_error_can_be_raised_with_message
66
+ error = assert_raises Resizing::APIError do
67
+ raise Resizing::APIError, 'test api error'
68
+ end
69
+
70
+ assert_equal 'test api error', error.message
71
+ end
72
+ end
73
+ end