Skip to content

Commit

Permalink
extract #normalize_kws_for_model_assertion for easier RSpec
Browse files Browse the repository at this point in the history
integration.
  • Loading branch information
apotonick committed Jan 20, 2025
1 parent a69025d commit e5164d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/trailblazer/test/assertion/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def assert_fail?(*args, **kws, &block)
module Assert
module_function

# @semi-public (for rspec-trailblazer).
def normalize_for(params_fragment, **kws)
kws = normalize_kws(**kws)
ctx = ctx_for_params_fragment(params_fragment, **kws)
Expand All @@ -49,9 +50,9 @@ def normalize_for(params_fragment, **kws)

#@public
def assert_pass(params_fragment, deep_merge: true, assertion:, test:, expected_attributes_to_merge:, **kws) # TODO: remove kws.
ctx, kws = normalize_for(params_fragment, test: test, **kws)
ctx, kws = normalize_for(params_fragment, test: test, **kws) # compute input.

expected_attributes = expected_attributes_for(expected_attributes_to_merge, **kws)
expected_attributes = expected_attributes_for(expected_attributes_to_merge, **kws) # compute "output", expected model attributes.

activity = kws[:operation] # FIXME.

Expand All @@ -65,7 +66,7 @@ def assert_fail(params_fragment, expected_errors, assertion:, **kws)
assertion.(activity, ctx, expected_errors, **kws)
end

#@private
# @semi-public used in rspec-trailblazer.
def ctx_for_params_fragment(params_fragment, key_in_params:, default_ctx:, **)
return params_fragment if params_fragment.kind_of?(Trailblazer::Test::Context)
# If {:key_in_params} is given, key the {params_fragment} with it, e.g. {params: {transaction: {.. params_fragment ..}}}
Expand All @@ -76,31 +77,40 @@ def ctx_for_params_fragment(params_fragment, key_in_params:, default_ctx:, **)

# @private
# Gather all test case configuration. This involves reading all test `let` directives.
def normalize_kws(user_block:, test:, operation: test.operation, expected_attributes: test.expected_attributes, contract_name: "default", model_at: :model, invoke: Assertion.method(:invoke_operation), **options)
def normalize_kws(user_block:, test:, operation: test.operation, contract_name: "default", invoke: Assertion.method(:invoke_operation), **options)
kws = {
operation: operation,
expected_attributes: expected_attributes,
test: test,
contract_name: contract_name,
model_at: model_at,
user_block: user_block,
invoke: invoke,

**normalize_kws_for_ctx(test: test, **options)
**normalize_kws_for_ctx(test: test, **options),
**normalize_kws_for_model_assertion(test: test, **options),
}

return kws
end

def normalize_kws_for_ctx(test:, key_in_params: test.key_in_params, default_ctx: test.default_ctx)
# @semi-public used in rspec-trailblazer.
# Used when building the incoming {ctx}, e.g. in {#run}.
def normalize_kws_for_ctx(test:, key_in_params: test.key_in_params, default_ctx: test.default_ctx, **)
{
default_ctx: default_ctx,
key_in_params: key_in_params,
}
end

def normalize_kws_for_model_assertion(test:, expected_attributes: test.expected_attributes, model_at: :model, **)
{
expected_attributes: expected_attributes,
model_at: model_at,
}
end

# FIXME: when {deep_merge: true} the result hash contains subclassed AR classes instead of the original ones.
# when we got this sorted we can allows deep merging here, too.
# @semi-public (for rspec-trailblazer).
def expected_attributes_for(expected_attributes_to_merge, expected_attributes:, deep_merge: false, **)
_expected_attributes = merge_for(expected_attributes, expected_attributes_to_merge, deep_merge)
end
Expand Down
1 change: 1 addition & 0 deletions lib/trailblazer/test/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Update < Create
end
end

# TODO: 2BRM.
Song = Struct.new(:band, :title, :duration) do
def save()
@save = true
Expand Down

0 comments on commit e5164d9

Please sign in to comment.