subroutine 4.4.0 → 4.5.0
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/lib/subroutine/op.rb +1 -1
- data/lib/subroutine/version.rb +1 -1
- data/subroutine.gemspec +5 -2
- metadata +6 -53
- data/.bundler-version +0 -1
- data/.github/CODEOWNERS +0 -1
- data/.github/dependabot.yml +0 -24
- data/.github/workflows/build.yml +0 -27
- data/.gitignore +0 -16
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/Appraisals +0 -26
- data/CHANGELOG.MD +0 -189
- data/Gemfile +0 -8
- data/LICENSE.txt +0 -22
- data/README.md +0 -88
- data/Rakefile +0 -10
- data/gemfiles/rails_6.1.gemfile +0 -10
- data/gemfiles/rails_6.1.gemfile.lock +0 -177
- data/gemfiles/rails_7.0.gemfile +0 -10
- data/gemfiles/rails_7.0.gemfile.lock +0 -174
- data/gemfiles/rails_7.1.gemfile +0 -9
- data/gemfiles/rails_7.1.gemfile.lock +0 -189
- data/gemfiles/rails_7.2.gemfile +0 -9
- data/gemfiles/rails_7.2.gemfile.lock +0 -191
- data/gemfiles/rails_8.0.gemfile +0 -9
- data/gemfiles/rails_8.0.gemfile.lock +0 -204
- data/test/subroutine/association_test.rb +0 -364
- data/test/subroutine/auth_test.rb +0 -148
- data/test/subroutine/base_test.rb +0 -328
- data/test/subroutine/fields_test.rb +0 -218
- data/test/subroutine/outputs_test.rb +0 -162
- data/test/subroutine/type_caster_test.rb +0 -510
- data/test/support/ops.rb +0 -516
- data/test/test_helper.rb +0 -17
@@ -1,328 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test_helper"
|
4
|
-
|
5
|
-
module Subroutine
|
6
|
-
class OpTest < TestCase
|
7
|
-
|
8
|
-
def test_simple_fields_definition
|
9
|
-
op = ::SignupOp.new
|
10
|
-
assert_equal %i[email password], op.field_configurations.keys.sort
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_inherited_fields
|
14
|
-
op = ::AdminSignupOp.new
|
15
|
-
assert_equal %i[email password privileges], op.field_configurations.keys.sort
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_class_attribute_usage
|
19
|
-
assert ::AdminSignupOp < ::SignupOp
|
20
|
-
|
21
|
-
sid = ::SignupOp.field_configurations.object_id
|
22
|
-
bid = ::AdminSignupOp.field_configurations.object_id
|
23
|
-
|
24
|
-
refute_equal sid, bid
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_fields_from_inherited_fields_without_inheriting_from_the_class
|
28
|
-
refute ::BusinessSignupOp < ::SignupOp
|
29
|
-
|
30
|
-
user_fields = ::SignupOp.field_configurations.keys
|
31
|
-
biz_fields = ::BusinessSignupOp.field_configurations.keys
|
32
|
-
|
33
|
-
user_fields.each do |field|
|
34
|
-
assert_includes biz_fields, field
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_fields_from_ignores_except_fields
|
39
|
-
op = ::ExceptFooBarOp.new
|
40
|
-
refute op.field_configurations.key?(:foo)
|
41
|
-
refute op.field_configurations.key?(:bar)
|
42
|
-
assert_equal [:baz], op.field_configurations.keys.sort
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_fields_from_ignores_except_association_fields
|
46
|
-
op = ::ExceptAssociationOp.new
|
47
|
-
refute op.field_configurations.key?(:admin)
|
48
|
-
refute op.field_configurations.key?(:admin_id)
|
49
|
-
refute op.field_configurations.key?(:admin_type)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_fields_from_only_fields
|
53
|
-
op = ::OnlyFooBarOp.new
|
54
|
-
assert op.field_configurations.key?(:foo)
|
55
|
-
assert op.field_configurations.key?(:bar)
|
56
|
-
refute_equal [:baz], op.field_configurations.keys.sort
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_fields_from_only_association_fields
|
60
|
-
op = ::OnlyAssociationOp.new
|
61
|
-
assert op.field_configurations.key?(:admin)
|
62
|
-
assert op.field_configurations.key?(:admin_type)
|
63
|
-
assert op.field_configurations.key?(:admin_id)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_defaults_declaration_options
|
67
|
-
op = ::DefaultsOp.new
|
68
|
-
assert_equal "foo", op.foo
|
69
|
-
assert_equal "bar", op.bar
|
70
|
-
assert_equal false, op.baz
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_inherited_defaults_override_correctly
|
74
|
-
op = ::InheritedDefaultsOp.new
|
75
|
-
assert_equal "barstool", op.bar
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_accessors_are_created
|
79
|
-
op = ::SignupOp.new
|
80
|
-
|
81
|
-
assert_respond_to op, :email
|
82
|
-
assert_respond_to op, :email=
|
83
|
-
|
84
|
-
assert_respond_to op, :password
|
85
|
-
assert_respond_to op, :password=
|
86
|
-
|
87
|
-
refute_respond_to ::SignupOp, :email
|
88
|
-
refute_respond_to ::SignupOp, :email=
|
89
|
-
refute_respond_to ::SignupOp, :password
|
90
|
-
refute_respond_to ::SignupOp, :password=
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_defaults_are_applied_to_new_instances
|
94
|
-
op = ::SignupOp.new
|
95
|
-
|
96
|
-
assert_nil op.email
|
97
|
-
assert_nil op.password
|
98
|
-
|
99
|
-
op = ::AdminSignupOp.new
|
100
|
-
|
101
|
-
assert_nil op.email
|
102
|
-
assert_nil op.password
|
103
|
-
assert_equal "min", op.privileges
|
104
|
-
|
105
|
-
op.privileges = "max"
|
106
|
-
assert_equal "max", op.privileges
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_validations_are_evaluated_before_perform_is_invoked
|
110
|
-
op = ::SignupOp.new
|
111
|
-
|
112
|
-
refute op.submit
|
113
|
-
|
114
|
-
refute op.perform_called
|
115
|
-
|
116
|
-
assert_equal ["can't be blank"], op.errors[:email]
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_validation_errors_can_be_inherited_and_transformed
|
120
|
-
op = ::AdminSignupOp.new(email: "foo@bar.com", password: "password123")
|
121
|
-
|
122
|
-
refute op.submit
|
123
|
-
|
124
|
-
assert op.perform_called
|
125
|
-
refute op.perform_finished
|
126
|
-
|
127
|
-
assert_equal ["has gotta be @admin.com"], op.errors[:email]
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_validation_errors_can_be_inherited_and_prefixed
|
131
|
-
op = PrefixedInputsOp.new(user_email_address: "foo@bar.com")
|
132
|
-
refute op.submit
|
133
|
-
|
134
|
-
assert_equal ["has gotta be @admin.com"], op.errors[:user_email_address]
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_when_valid_perform_completes_it_returns_control
|
138
|
-
op = ::SignupOp.new(email: "foo@bar.com", password: "password123")
|
139
|
-
op.submit!
|
140
|
-
|
141
|
-
assert op.perform_called
|
142
|
-
assert op.perform_finished
|
143
|
-
|
144
|
-
u = op.created_user
|
145
|
-
|
146
|
-
assert_equal "foo@bar.com", u.email_address
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_instance_and_class_submit_bang_return_instance
|
150
|
-
assert ::SignupOp.new(email: "foo@bar.com", password: "password123").submit!.is_a?(::SignupOp)
|
151
|
-
assert ::SignupOp.submit!(email: "foo@bar.com", password: "password123").is_a?(::SignupOp)
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_it_raises_an_error_when_used_with_a_bang_and_performing_or_validation_fails
|
155
|
-
op = ::SignupOp.new(email: "foo@bar.com")
|
156
|
-
|
157
|
-
err = assert_raises ::Subroutine::Failure do
|
158
|
-
op.submit!
|
159
|
-
end
|
160
|
-
|
161
|
-
assert_equal "Password can't be blank", err.message
|
162
|
-
end
|
163
|
-
|
164
|
-
def test_uses_failure_class_to_raise_error
|
165
|
-
op = ::CustomFailureClassOp.new
|
166
|
-
|
167
|
-
err = assert_raises ::CustomFailureClassOp::Failure do
|
168
|
-
op.submit!
|
169
|
-
end
|
170
|
-
|
171
|
-
assert_equal "Will never work", err.message
|
172
|
-
end
|
173
|
-
|
174
|
-
def test_the_result_of_perform_doesnt_matter
|
175
|
-
op = ::FalsePerformOp.new
|
176
|
-
assert op.submit!
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_it_allows_submission_from_the_class
|
180
|
-
op = SignupOp.submit
|
181
|
-
assert_equal ["can't be blank"], op.errors[:email]
|
182
|
-
|
183
|
-
assert_raises ::Subroutine::Failure do
|
184
|
-
SignupOp.submit!
|
185
|
-
end
|
186
|
-
|
187
|
-
op = SignupOp.submit! email: "foo@bar.com", password: "password123"
|
188
|
-
assert_equal "foo@bar.com", op.created_user.email_address
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_it_sets_the_params_and_defaults_immediately
|
192
|
-
op = ::AdminSignupOp.new(email: "foo")
|
193
|
-
assert_equal({
|
194
|
-
"email" => "foo",
|
195
|
-
}, op.params)
|
196
|
-
|
197
|
-
assert_equal({
|
198
|
-
"privileges" => "min",
|
199
|
-
}, op.defaults)
|
200
|
-
|
201
|
-
assert_equal({
|
202
|
-
"email" => "foo",
|
203
|
-
"privileges" => "min",
|
204
|
-
}, op.params_with_defaults)
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_it_allows_defaults_to_be_overridden
|
208
|
-
op = ::AdminSignupOp.new(email: "foo", privileges: nil)
|
209
|
-
|
210
|
-
assert_equal({
|
211
|
-
"email" => "foo",
|
212
|
-
"privileges" => nil,
|
213
|
-
}, op.params)
|
214
|
-
|
215
|
-
assert_equal({ "privileges" => "min" }, op.defaults)
|
216
|
-
end
|
217
|
-
|
218
|
-
def test_it_overriding_default_does_not_alter_default
|
219
|
-
op = ::AdminSignupOp.new(email: "foo")
|
220
|
-
op.privileges << "bangbang"
|
221
|
-
|
222
|
-
op = ::AdminSignupOp.new(email: "foo", privileges: nil)
|
223
|
-
|
224
|
-
assert_equal({
|
225
|
-
"email" => "foo",
|
226
|
-
"privileges" => nil,
|
227
|
-
}, op.params)
|
228
|
-
|
229
|
-
assert_equal({
|
230
|
-
"privileges" => "min",
|
231
|
-
}, op.defaults)
|
232
|
-
end
|
233
|
-
|
234
|
-
def test_it_overrides_defaults_with_nils
|
235
|
-
op = ::AdminSignupOp.new(email: "foo", privileges: nil)
|
236
|
-
assert_equal({
|
237
|
-
"privileges" => nil,
|
238
|
-
"email" => "foo",
|
239
|
-
}, op.params)
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_it_casts_params_on_the_way_in
|
243
|
-
op = ::TypeCastOp.new(integer_input: "25")
|
244
|
-
assert_equal(25, op.params["integer_input"])
|
245
|
-
|
246
|
-
op.decimal_input = "25.3"
|
247
|
-
assert_equal(BigDecimal("25.3"), op.params["decimal_input"])
|
248
|
-
end
|
249
|
-
|
250
|
-
def test_it_allow_retrival_of_outputs
|
251
|
-
op = ::SignupOp.submit!(email: "foo@bar.com", password: "password123")
|
252
|
-
u = op.created_user
|
253
|
-
|
254
|
-
assert_equal "foo@bar.com", u.email_address
|
255
|
-
end
|
256
|
-
|
257
|
-
# Hash#with_indifferent_access was changing output objects
|
258
|
-
def test_outputs_are_not_mutated
|
259
|
-
out = { "foo" => "bar" }
|
260
|
-
op = ::SignupOp.new
|
261
|
-
op.send :output, :perform_called, out
|
262
|
-
value = op.perform_called
|
263
|
-
assert_equal out, value
|
264
|
-
assert_equal Hash, value.class
|
265
|
-
end
|
266
|
-
|
267
|
-
def test_it_does_not_omit_the_backtrace_from_the_original_error
|
268
|
-
op = ::ErrorTraceOp.new
|
269
|
-
begin
|
270
|
-
op.submit!
|
271
|
-
rescue Exception => e
|
272
|
-
found = e.backtrace.detect do |msg|
|
273
|
-
msg =~ %r{test/support/ops\.rb:[\d]+.+foo}
|
274
|
-
end
|
275
|
-
|
276
|
-
refute_nil found, "Expected backtrace to include original caller of foo"
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
def test_a_block_is_accepted_on_instantiation
|
281
|
-
op = ::SignupOp.new do |o|
|
282
|
-
o.email = "foo@bar.com"
|
283
|
-
o.password = "password123!"
|
284
|
-
end
|
285
|
-
|
286
|
-
assert_equal "foo@bar.com", op.email
|
287
|
-
assert_equal "password123!", op.password
|
288
|
-
|
289
|
-
assert_equal true, op.field_provided?(:email)
|
290
|
-
assert_equal true, op.field_provided?(:password)
|
291
|
-
end
|
292
|
-
|
293
|
-
def test_a_block_is_not_accepted_with_submit
|
294
|
-
assert_raises ::ArgumentError do
|
295
|
-
::SignupOp.submit! do |o|
|
296
|
-
o.email = "foo@bar.com"
|
297
|
-
o.password = "password123!"
|
298
|
-
end
|
299
|
-
end
|
300
|
-
|
301
|
-
assert_raises ::ArgumentError do
|
302
|
-
::SignupOp.submit do |o|
|
303
|
-
o.email = "foo@bar.com"
|
304
|
-
o.password = "password123!"
|
305
|
-
end
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
def test_actioncontroller_parameters_can_be_provided
|
310
|
-
raw_params = { email: "foo@bar.com", password: "password123!" }.with_indifferent_access
|
311
|
-
params = ::ActionController::Parameters.new(raw_params)
|
312
|
-
op = SignupOp.new(params)
|
313
|
-
op.submit!
|
314
|
-
|
315
|
-
assert_equal "foo@bar.com", op.email
|
316
|
-
assert_equal "password123!", op.password
|
317
|
-
|
318
|
-
assert_equal raw_params, op.params
|
319
|
-
end
|
320
|
-
|
321
|
-
def test_inspect_is_pretty
|
322
|
-
op = SignupOp.new({ email: "foo@bar.com", password: "password123!" })
|
323
|
-
oid = format('%x', (op.object_id << 1))
|
324
|
-
assert_equal "#<SignupOp:0x#{oid} email: \"foo@bar.com\", password: \"password123!\">", op.inspect
|
325
|
-
end
|
326
|
-
|
327
|
-
end
|
328
|
-
end
|
@@ -1,218 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test_helper"
|
4
|
-
require "subroutine/fields"
|
5
|
-
|
6
|
-
module Subroutine
|
7
|
-
class FieldsTest < TestCase
|
8
|
-
|
9
|
-
class Whatever
|
10
|
-
|
11
|
-
include Subroutine::Fields
|
12
|
-
|
13
|
-
string :foo, default: "foo"
|
14
|
-
string :qux, default: "qux"
|
15
|
-
string :protekted, mass_assignable: false
|
16
|
-
|
17
|
-
integer :bar, default: -> { 3 }, group: :sekret
|
18
|
-
string :protekted_group_input, group: :sekret
|
19
|
-
|
20
|
-
date :baz, group: :the_bazzes
|
21
|
-
|
22
|
-
def initialize(options = {})
|
23
|
-
setup_fields(options)
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
class WhateverWithDefaultsIncluded < Whatever
|
29
|
-
self.include_defaults_in_params = true
|
30
|
-
end
|
31
|
-
|
32
|
-
class MutationBase
|
33
|
-
include Subroutine::Fields
|
34
|
-
|
35
|
-
field :foo, group: :three_letter
|
36
|
-
field :bar, group: :three_letter
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
class MutationChild < MutationBase
|
41
|
-
|
42
|
-
field :qux, group: :three_letter
|
43
|
-
field :food, group: :four_letter
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_fields_are_configured
|
48
|
-
assert_equal 6, Whatever.field_configurations.size
|
49
|
-
assert_equal :string, Whatever.field_configurations[:foo][:type]
|
50
|
-
assert_equal :string, Whatever.field_configurations[:qux][:type]
|
51
|
-
assert_equal :integer, Whatever.field_configurations[:bar][:type]
|
52
|
-
assert_equal :date, Whatever.field_configurations[:baz][:type]
|
53
|
-
assert_equal :string, Whatever.field_configurations[:protekted][:type]
|
54
|
-
assert_equal :string, Whatever.field_configurations[:protekted_group_input][:type]
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_field_defaults_are_handled
|
58
|
-
instance = Whatever.new
|
59
|
-
assert_equal "foo", instance.foo
|
60
|
-
assert_equal 3, instance.bar
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_fields_can_be_provided
|
64
|
-
instance = Whatever.new(foo: "abc", bar: nil)
|
65
|
-
assert_equal "abc", instance.foo
|
66
|
-
assert_nil instance.bar
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_field_provided
|
70
|
-
instance = Whatever.new(foo: "abc")
|
71
|
-
assert_equal true, instance.field_provided?(:foo)
|
72
|
-
assert_equal false, instance.field_provided?(:bar)
|
73
|
-
|
74
|
-
instance = DefaultsOp.new
|
75
|
-
assert_equal false, instance.field_provided?(:foo)
|
76
|
-
|
77
|
-
instance = DefaultsOp.new(foo: "foo")
|
78
|
-
assert_equal true, instance.field_provided?(:foo)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_field_provided_include_manual_assigned_fields
|
82
|
-
instance = Whatever.new
|
83
|
-
instance.foo = "bar"
|
84
|
-
|
85
|
-
assert_equal true, instance.field_provided?(:foo)
|
86
|
-
assert_equal false, instance.field_provided?(:bar)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_invalid_typecast
|
90
|
-
assert_raises "Error for field `baz`: invalid date" do
|
91
|
-
Whatever.new(baz: "2015-13-01")
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_params_does_not_include_defaults
|
96
|
-
instance = Whatever.new(foo: "abc")
|
97
|
-
assert_equal({ "foo" => "abc" }, instance.provided_params)
|
98
|
-
assert_equal({ "foo" => "foo", "bar" => 3, "qux" => "qux" }, instance.defaults)
|
99
|
-
assert_equal({ "foo" => "abc" }, instance.params)
|
100
|
-
assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.params_with_defaults)
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_params_include_defaults_if_globally_configured
|
104
|
-
Subroutine.stubs(:include_defaults_in_params?).returns(true)
|
105
|
-
instance = Whatever.new(foo: "abc")
|
106
|
-
assert Whatever.include_defaults_in_params?
|
107
|
-
assert_equal({ "foo" => "abc" }, instance.provided_params)
|
108
|
-
assert_equal({ "foo" => "foo", "bar" => 3, "qux" => "qux" }, instance.defaults)
|
109
|
-
assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.params)
|
110
|
-
assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.params_with_defaults)
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_params_includes_defaults_if_opted_into
|
114
|
-
refute Subroutine.include_defaults_in_params?
|
115
|
-
instance = WhateverWithDefaultsIncluded.new(foo: "abc")
|
116
|
-
assert_equal({ "foo" => "abc" }, instance.provided_params)
|
117
|
-
assert_equal({ "foo" => "foo", "bar" => 3, "qux" => "qux" }, instance.defaults)
|
118
|
-
assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.params)
|
119
|
-
assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.params_with_defaults)
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_named_params_do_not_include_defaults_unless_asked_for
|
123
|
-
instance = Whatever.new(foo: "abc")
|
124
|
-
assert_equal({}, instance.sekret_provided_params)
|
125
|
-
assert_equal({}, instance.sekret_params)
|
126
|
-
assert_equal({ "bar" => 3 }, instance.sekret_params_with_defaults)
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_named_params_include_defaults_if_configured
|
130
|
-
instance = WhateverWithDefaultsIncluded.new(foo: "abc")
|
131
|
-
assert_equal({}, instance.sekret_provided_params)
|
132
|
-
assert_equal({ "bar" => 3 }, instance.sekret_params)
|
133
|
-
assert_equal({ "bar" => 3 }, instance.sekret_params_with_defaults)
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_fields_can_opt_out_of_mass_assignment
|
137
|
-
assert_raises Subroutine::Fields::MassAssignmentError do
|
138
|
-
Whatever.new(foo: "abc", protekted: "foo")
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_non_mass_assignment_fields_can_be_individually_assigned
|
143
|
-
instance = Whatever.new(foo: "abc")
|
144
|
-
instance.protekted = "bar"
|
145
|
-
|
146
|
-
assert_equal "bar", instance.protekted
|
147
|
-
assert_equal true, instance.field_provided?(:protekted)
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_get_field
|
151
|
-
instance = Whatever.new
|
152
|
-
|
153
|
-
assert_equal "foo", instance.get_field(:foo)
|
154
|
-
end
|
155
|
-
|
156
|
-
def test_set_field
|
157
|
-
instance = Whatever.new
|
158
|
-
instance.set_field(:foo, "bar")
|
159
|
-
|
160
|
-
assert_equal "bar", instance.foo
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_set_field_adds_to_provided_params
|
164
|
-
instance = Whatever.new
|
165
|
-
instance.set_field(:foo, "bar")
|
166
|
-
assert_equal true, instance.provided_params.key?(:foo)
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_set_field_can_add_to_the_default_params
|
170
|
-
instance = Whatever.new
|
171
|
-
instance.set_field(:foo, "bar", group_type: :default)
|
172
|
-
assert_equal false, instance.provided_params.key?(:foo)
|
173
|
-
assert_equal "bar", instance.default_params[:foo]
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_group_fields_are_accessible_at_the_class
|
177
|
-
fields = Whatever.fields_by_group[:sekret].sort
|
178
|
-
assert_equal %i[bar protekted_group_input], fields
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_groups_fields_are_accessible
|
182
|
-
op = Whatever.new(foo: "bar", protekted_group_input: "pgi", bar: 8)
|
183
|
-
assert_equal({ protekted_group_input: "pgi", bar: 8 }.with_indifferent_access, op.sekret_params)
|
184
|
-
assert_equal({ protekted_group_input: "pgi", foo: "bar", bar: 8 }.with_indifferent_access, op.params)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_fields_from_allows_merging_of_config
|
188
|
-
op = GroupedDefaultsOp.new(foo: "foo")
|
189
|
-
assert_equal({ foo: "foo" }.with_indifferent_access, op.params)
|
190
|
-
assert_equal({ foo: "foo" }.with_indifferent_access, op.inherited_params)
|
191
|
-
assert_equal({ foo: "foo", bar: "bar", baz: false }.with_indifferent_access, op.params_with_defaults)
|
192
|
-
assert_equal({ foo: "foo", bar: "bar", baz: false }.with_indifferent_access, op.inherited_params_with_defaults)
|
193
|
-
assert_equal({}.with_indifferent_access, op.without_inherited_params)
|
194
|
-
end
|
195
|
-
|
196
|
-
def test_fields_are_inherited_to_subclasses
|
197
|
-
assert_equal(%i[amount_cents], ParentInheritanceOp.field_configurations.keys.sort)
|
198
|
-
assert_equal(%i[debit_cents], ParentInheritanceOp::EarlyInheritanceOp.field_configurations.keys.sort)
|
199
|
-
assert_equal(%i[amount_cents debit_cents], ParentInheritanceOp::LateInheritanceOp.field_configurations.keys.sort)
|
200
|
-
assert_equal(%i[amount_cents credit_cents], OuterInheritanceOp.field_configurations.keys.sort)
|
201
|
-
end
|
202
|
-
|
203
|
-
def test_field_definitions_are_not_mutated_by_subclasses
|
204
|
-
assert_equal(%i[bar foo], MutationBase.field_configurations.keys.sort)
|
205
|
-
assert_equal(%i[bar foo food qux], MutationChild.field_configurations.keys.sort)
|
206
|
-
end
|
207
|
-
|
208
|
-
def test_group_fields_are_not_mutated_by_subclasses
|
209
|
-
assert_equal(%i[three_letter], MutationBase.fields_by_group.keys.sort)
|
210
|
-
assert_equal(%i[four_letter three_letter], MutationChild.fields_by_group.keys.sort)
|
211
|
-
|
212
|
-
assert_equal(%i[bar foo], MutationBase.fields_by_group[:three_letter].sort)
|
213
|
-
assert_equal(%i[bar foo qux], MutationChild.fields_by_group[:three_letter].sort)
|
214
|
-
assert_equal(%i[food], MutationChild.fields_by_group[:four_letter].sort)
|
215
|
-
end
|
216
|
-
|
217
|
-
end
|
218
|
-
end
|
@@ -1,162 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module Subroutine
|
6
|
-
class OutputsTest < TestCase
|
7
|
-
class MissingOutputOp < ::Subroutine::Op
|
8
|
-
def perform
|
9
|
-
output :foo, 'bar'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class LazyOutputOp < ::Subroutine::Op
|
14
|
-
outputs :foo, lazy: true
|
15
|
-
outputs :baz, lazy: true, type: String
|
16
|
-
|
17
|
-
def perform
|
18
|
-
output :foo, -> { call_me }
|
19
|
-
output :baz, -> { call_baz }
|
20
|
-
end
|
21
|
-
|
22
|
-
def call_me; end
|
23
|
-
|
24
|
-
def call_baz; end
|
25
|
-
end
|
26
|
-
|
27
|
-
class MissingOutputSetOp < ::Subroutine::Op
|
28
|
-
outputs :foo
|
29
|
-
def perform
|
30
|
-
true
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class OutputNotRequiredOp < ::Subroutine::Op
|
35
|
-
outputs :foo, required: false
|
36
|
-
def perform
|
37
|
-
true
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class NoOutputNoSuccessOp < ::Subroutine::Op
|
42
|
-
outputs :foo
|
43
|
-
|
44
|
-
def perform
|
45
|
-
errors.add(:foo, 'bar')
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class OutputWithTypeValidationNotRequired < ::Subroutine::Op
|
50
|
-
outputs :value, type: String, required: false
|
51
|
-
|
52
|
-
def perform; end
|
53
|
-
end
|
54
|
-
|
55
|
-
class OutputWithTypeValidationRequired < ::Subroutine::Op
|
56
|
-
outputs :value, type: String, required: true
|
57
|
-
|
58
|
-
def perform; end
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_it_raises_an_error_if_an_output_is_not_defined_but_is_set
|
62
|
-
op = MissingOutputOp.new
|
63
|
-
assert_raises ::Subroutine::Outputs::UnknownOutputError do
|
64
|
-
op.submit
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_it_raises_an_error_if_not_all_outputs_were_set
|
69
|
-
op = MissingOutputSetOp.new
|
70
|
-
assert_raises ::Subroutine::Outputs::OutputNotSetError do
|
71
|
-
op.submit
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_it_does_not_raise_an_error_if_output_is_not_set_and_is_not_required
|
76
|
-
op = OutputNotRequiredOp.new
|
77
|
-
op.submit
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_it_does_not_raise_an_error_if_the_perform_is_not_a_success
|
81
|
-
op = NoOutputNoSuccessOp.new
|
82
|
-
refute op.submit
|
83
|
-
end
|
84
|
-
|
85
|
-
###################
|
86
|
-
# type validation #
|
87
|
-
###################
|
88
|
-
|
89
|
-
def test_it_does_not_raise_an_error_if_output_is_set_to_the_right_type
|
90
|
-
op = OutputWithTypeValidationNotRequired.new
|
91
|
-
op.send(:output, :value, 'foo')
|
92
|
-
assert op.submit
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_it_raises_an_error_if_output_is_not_set_to_the_right_type
|
96
|
-
op = OutputWithTypeValidationNotRequired.new
|
97
|
-
op.send(:output, :value, 1)
|
98
|
-
assert_raises ::Subroutine::Outputs::InvalidOutputTypeError do
|
99
|
-
op.submit
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_it_does_not_raise_an_error_if_output_is_set_to_nil_when_there_is_type_validation_and_not_required
|
104
|
-
op = OutputWithTypeValidationNotRequired.new
|
105
|
-
op.send(:output, :value, nil)
|
106
|
-
op.submit
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_it_raises_an_error_if_output_is_set_to_nil_when_there_is_type_validation_and_is_required
|
110
|
-
op = OutputWithTypeValidationRequired.new
|
111
|
-
op.send(:output, :value, nil)
|
112
|
-
assert_raises ::Subroutine::Outputs::InvalidOutputTypeError do
|
113
|
-
op.submit
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
################
|
118
|
-
# lazy outputs #
|
119
|
-
################
|
120
|
-
|
121
|
-
def test_it_does_not_call_lazy_output_values_if_not_accessed
|
122
|
-
op = LazyOutputOp.new
|
123
|
-
op.expects(:call_me).never
|
124
|
-
op.submit!
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_it_calls_lazy_output_values_if_accessed
|
128
|
-
op = LazyOutputOp.new
|
129
|
-
op.expects(:call_me).once
|
130
|
-
op.submit!
|
131
|
-
op.foo
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_it_validates_type_when_lazy_output_is_accessed
|
135
|
-
op = LazyOutputOp.new
|
136
|
-
op.expects(:call_baz).once.returns("a string")
|
137
|
-
op.submit!
|
138
|
-
assert_silent do
|
139
|
-
op.baz
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_it_raises_error_on_invalid_type_when_lazy_output_is_accessed
|
144
|
-
op = LazyOutputOp.new
|
145
|
-
op.expects(:call_baz).once.returns(10)
|
146
|
-
op.submit!
|
147
|
-
error = assert_raises(Subroutine::Outputs::InvalidOutputTypeError) do
|
148
|
-
op.baz
|
149
|
-
end
|
150
|
-
assert_match(/Invalid output type for 'baz' expected String but got Integer/, error.message)
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_it_returns_outputs
|
154
|
-
op = LazyOutputOp.new
|
155
|
-
op.expects(:call_me).once.returns(1)
|
156
|
-
op.expects(:call_baz).once.returns("a string")
|
157
|
-
op.submit!
|
158
|
-
assert_equal({ foo: 1, baz: "a string" }, op.outputs)
|
159
|
-
end
|
160
|
-
|
161
|
-
end
|
162
|
-
end
|