trailblazer-test 0.1.0 → 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.
data/.rubocop.yml DELETED
@@ -1,17 +0,0 @@
1
- inherit_from:
2
- - https://raw.githubusercontent.com/trailblazer/meta/master/rubocop.yml
3
-
4
- Style/SignalException:
5
- Exclude:
6
- - lib/trailblazer/test/operation/assertions.rb
7
- - lib/trailblazer/test/operation/helper.rb
8
- - lib/trailblazer/test/deprecation/operation/helper.rb
9
-
10
- Metrics/ParameterLists:
11
- Exclude:
12
- - lib/trailblazer/test/operation/assertions.rb
13
-
14
- Metrics/LineLength:
15
- Exclude:
16
- - lib/trailblazer/test/operation/assertions.rb
17
-
data/.travis.yml DELETED
@@ -1,16 +0,0 @@
1
- language: ruby
2
- before_install:
3
- - gem install bundler
4
- matrix:
5
- include:
6
- - rvm: 2.1
7
- gemfile: Gemfile
8
- - rvm: 2.2
9
- gemfile: Gemfile
10
- - rvm: 2.3.1
11
- gemfile: Gemfile
12
- - rvm: 2.4.1
13
- gemfile: Gemfile
14
- - rvm: 2.5.0
15
- gemfile: Gemfile
16
- script: bundle exec rake test && rake rubocop
@@ -1,37 +0,0 @@
1
- # module MiniTest::Assertions
2
- module Trailblazer
3
- module Test
4
- # Evaluate value if it's a lambda, and let the caller know whether we need an
5
- # assert_equal or an assert.
6
- def self.expected(asserted, value, actual)
7
- value.is_a?(Proc) ? [value.(actual: actual, asserted: asserted), false] : [value, true]
8
- end
9
-
10
- # Read the actual value from the asserted object (e.g. a model).
11
- def self.actual(asserted, reader, name)
12
- reader ? asserted.public_send(reader, name) : asserted.public_send(name)
13
- end
14
-
15
- module Assertions
16
- module_function
17
-
18
- # tuples = defaults.merge(overrides) # FIXME: merge with above!
19
-
20
- # Test if all `tuples` values on `asserted` match the expected values.
21
- # @param asserted Object Object that exposes attributes to test
22
- # @param tuples Hash Key/value attribute pairs to test
23
- # @param options Hash Default :reader is `asserted.{name}`,
24
- # TODO: test err msgs!
25
- def assert_exposes(asserted, tuples, reader: nil)
26
- tuples.each do |k, v|
27
- actual = Test.actual(asserted, reader, k)
28
- expected, is_eq = Test.expected(asserted, v, actual)
29
-
30
- is_eq ? assert_equal(expected, actual, "Property [#{k}] mismatch") : assert(expected, "Actual: #{actual.inspect}.")
31
- end
32
- end
33
- end
34
- end
35
- end
36
- # Trailblazer::Operation::Result.infect_an_assertion :assert_result_matches, :must_match, :do_not_flip
37
- # Object.infect_an_assertion :assert_exposes, :must_expose, :do_not_flip
@@ -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,38 +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
- # @private
12
- def call!(operation_class, raise_on_failure: false, **args)
13
- operation_class.trace(**args).tap do |result|
14
- unless result.success?
15
-
16
- msg = "factory(#{operation_class}) has failed"
17
-
18
- unless result["contract.default"].nil? # should we allow to change contract name?
19
- if result["contract.default"].errors.messages.any?
20
- msg += " due to validation errors: #{result["contract.default"].errors.messages}"
21
- end
22
- end
23
-
24
- if raise_on_failure
25
- result.wtf?
26
- raise OperationFailedError, msg
27
- end
28
- end
29
-
30
- yield result if block_given?
31
-
32
- result
33
- end
34
- end
35
-
36
- class OperationFailedError < RuntimeError; end
37
- end
38
- 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