trailblazer-test 0.1.1 → 1.0.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.
@@ -1,42 +0,0 @@
1
- module Trailblazer::Test
2
- module Deprecation
3
- module Operation
4
- module Assertions
5
- include Trailblazer::Test::Operation::Assertions
6
-
7
- # @needs default_params
8
- # @needs default_options
9
-
10
- def params(default_params: self.default_params, deep_merge: true, **new_params)
11
- [merge_for(default_params, new_params, deep_merge), {}]
12
- end
13
-
14
- def ctx(new_params, *options)
15
- # need *options to allow user to do something like:
16
- # ctx({yeah: 'nah'}, "current_user" => Object, some: 'other' )
17
-
18
- # this is not greate but seems necessary using *options
19
- new_options = options.first || {}
20
- deep_merge = new_options[:deep_merge].nil? ? true : deep_merge
21
- new_options.delete(:deep_merge)
22
-
23
- ctx = merge_for(_default_options, options.first || {}, deep_merge)
24
- [merge_for(params[0], new_params, deep_merge), ctx]
25
- end
26
-
27
- def _default_options(options: default_options)
28
- options
29
- end
30
-
31
- # compatibility call for TRB 2.0
32
- def _call_operation(operation_class, *args)
33
- operation_class.(args[0][0], args[0][1])
34
- end
35
-
36
- def _model(result)
37
- result["model"]
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,28 +0,0 @@
1
- module Trailblazer::Test
2
- module Deprecation
3
- module Operation
4
- module Helper
5
- def call(operation_class, *args, &block)
6
- call!(operation_class, args, &block)
7
- end
8
-
9
- def factory(operation_class, *args, &block)
10
- call!(operation_class, args, raise_on_failure: true, &block)
11
- end
12
-
13
- # @private
14
- def call!(operation_class, args, raise_on_failure: false)
15
- operation_class.(*args).tap do |result|
16
- unless result.success?
17
- yield result if block_given?
18
-
19
- raise OperationFailedError, "factory(#{operation_class}) failed." if raise_on_failure
20
- end
21
- end
22
- end
23
- end
24
- end
25
-
26
- class OperationFailedError < RuntimeError; end
27
- end
28
- end
@@ -1,89 +0,0 @@
1
- require "hashie"
2
-
3
- module Trailblazer::Test::Operation
4
- module Assertions
5
- # @needs default_params
6
- # @needs default_options
7
- # @needs expected_attrs
8
-
9
- def params(default_params: self.default_params, deep_merge: true, **new_params)
10
- {params: merge_for(default_params, new_params, deep_merge)}
11
- end
12
-
13
- def ctx(new_params, default_options: self.default_options, deep_merge: true, **options)
14
- new_params = merge_for(params[:params], new_params, deep_merge)
15
- new_options = merge_for(default_options, options, deep_merge)
16
-
17
- {params: new_params, **new_options}
18
- end
19
-
20
- def assert_pass(operation_class, operation_inputs, expected_attributes, default_attributes: expected_attrs, deep_merge: true, &block)
21
- expected_attributes = merge_for(default_attributes, expected_attributes, deep_merge)
22
-
23
- assert_pass_with_model(operation_class, operation_inputs, expected_model_attributes: expected_attributes, &block)
24
- end
25
-
26
- def assert_fail(operation_class, operation_inputs, expected_errors: nil, contract_name: "default", &block)
27
- assert_fail_with_model(operation_class, operation_inputs, expected_errors: expected_errors, contract_name: contract_name, &block)
28
- end
29
-
30
- # @private
31
- # TODO: test expected_attributes default param and explicit!
32
- def assert_pass_with_model(operation_class, operation_inputs, expected_model_attributes: {}, &user_block)
33
- _assert_call(operation_class, operation_inputs, user_block: user_block) do |result|
34
- assert_equal true, result.success?
35
- assert_exposes(_model(result), expected_model_attributes)
36
- end
37
- end
38
-
39
- # @private
40
- def assert_fail_with_model(operation_class, operation_inputs, expected_errors: nil, contract_name: raise, &user_block)
41
- _assert_call(operation_class, operation_inputs, user_block: user_block) do |result|
42
- assert_equal true, result.failure?
43
-
44
- raise ExpectedErrorsTypeError, "expected_errors has to be an Array" unless expected_errors.is_a?(Array)
45
-
46
- # only test _if_ errors are present, not the content.
47
- errors = result["contract.#{contract_name}"].errors.messages # TODO: this will soon change with the operation Errors object.
48
-
49
- assert_equal expected_errors.sort, errors.keys.sort
50
- end
51
- end
52
-
53
- # @private
54
- def _assert_call(operation_class, operation_inputs, user_block: raise)
55
- result = _call_operation(operation_class, operation_inputs)
56
-
57
- return user_block.call(result) if user_block # DISCUSS: result or model?
58
-
59
- yield(result)
60
-
61
- result
62
- end
63
-
64
- # @private
65
- class ExpectedErrorsTypeError < RuntimeError; end
66
-
67
- # @private
68
- class CtxHash < Hash
69
- include Hashie::Extensions::DeepMerge
70
- end
71
-
72
- # @private
73
- def merge_for(dest, source, deep_merge)
74
- return dest.merge(source) unless deep_merge
75
-
76
- CtxHash[dest].deep_merge(CtxHash[source])
77
- end
78
-
79
- # @private
80
- def _call_operation(operation_class, operation_inputs)
81
- operation_class.(operation_inputs)
82
- end
83
-
84
- # @private
85
- def _model(result)
86
- result[:model]
87
- end
88
- end
89
- end
@@ -1,54 +0,0 @@
1
- module Trailblazer::Test::Operation
2
- module Helper
3
- def call(operation_class, **args, &block)
4
- call!(operation_class, args, &block)
5
- end
6
-
7
- def factory(operation_class, **args, &block)
8
- call!(operation_class, args.merge(raise_on_failure: true), &block)
9
- end
10
-
11
- def mock_step(operation_class, id:, subprocess: nil, subprocess_path: nil)
12
- raise ArgumentError, "Missing block: `mock_step` requires a block." unless block_given?
13
-
14
- block = ->(ctx, **) { yield(ctx) }
15
-
16
- # If deeply nested step needs to be mocked, use the `patch` provided by Subprocess
17
- if subprocess
18
- return Class.new(operation_class) do
19
- patch = { subprocess_path => ->() { step block, replace: id, id: id } }
20
- step Subprocess(subprocess, patch: patch), replace: subprocess, id: subprocess
21
- end
22
- end
23
-
24
- Class.new(operation_class) { step block, replace: id, id: id }
25
- end
26
-
27
- # @private
28
- def call!(operation_class, raise_on_failure: false, **args)
29
- operation_class.trace(**args).tap do |result|
30
- unless result.success?
31
-
32
- msg = "factory(#{operation_class}) has failed"
33
-
34
- unless result["contract.default"].nil? # should we allow to change contract name?
35
- if result["contract.default"].errors.messages.any?
36
- msg += " due to validation errors: #{result["contract.default"].errors.messages}"
37
- end
38
- end
39
-
40
- if raise_on_failure
41
- result.wtf?
42
- raise OperationFailedError, msg
43
- end
44
- end
45
-
46
- yield result if block_given?
47
-
48
- result
49
- end
50
- end
51
-
52
- class OperationFailedError < RuntimeError; end
53
- end
54
- end
@@ -1,13 +0,0 @@
1
- module Trailblazer::Test::Operation
2
- module PolicyAssertions
3
- include Assertions
4
- # @needs params_pass
5
- # @needs options_pass
6
- def assert_policy_fail(operation_class, ctx, policy_name: "default")
7
- _assert_call(operation_class, ctx, user_block: nil) do |result|
8
- assert_equal true, result.failure?
9
- assert_equal true, result["result.policy.#{policy_name}"].failure?
10
- end
11
- end
12
- end
13
- end