Skip to content

Commit

Permalink
test plan assertions running via iteration interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 20, 2024
1 parent b0c3d0d commit 1fb6271
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 56 deletions.
36 changes: 11 additions & 25 deletions lib/trailblazer/workflow/test/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ module Trailblazer
module Workflow
module Test
module Assertions
def assert_positions(asserted_positions, expected_tuples, lanes_cfg:, test_plan:)
expected_positions =
expected_tuples.collect do |position_row|
test_plan.position_from_tuple(*position_row[:tuple])
end

# sorting_block = ->(a, b) { a.activity.object_id <=> b.activity.object_id }

# sorted_asserted_positions = asserted_positions.to_a.sort(&sorting_block)
# sorted_expected_positions = expected_positions.sort(&sorting_block)

def assert_positions(asserted_positions, expected_positions, lanes_cfg:, test_plan:)
# FIXME: make 100% sure that row_position etc are always sorted.

expected_positions_ary = expected_positions.to_a

asserted_positions.to_a.collect.with_index do |position, index|
assert_equal position, expected_positions[index],
Assertions.error_message_for(position, expected_positions[index], lanes_cfg: lanes_cfg)
assert_equal position, expected_positions_ary[index],
Assertions.error_message_for(position, expected_positions_ary[index], lanes_cfg: lanes_cfg)
end

# FIXME: figure out why we can't just compare the entire array!
Expand All @@ -27,8 +19,8 @@ def assert_positions(asserted_positions, expected_tuples, lanes_cfg:, test_plan:
# Compile error message when the expected lane position doesn't match the actual one.
def self.error_message_for(position, expected_position, lanes_cfg:) # TODO: test me.
# TODO: make the labels use UTF8 icons etc, as in the CLI rendering code.
expected_label = Test::Plan::Structure.serialize_suspend_position(*position.to_a, lanes_cfg: lanes_cfg)[:comment]
actual_label = Test::Plan::Structure.serialize_suspend_position(*expected_position.to_a, lanes_cfg: lanes_cfg)[:comment]
expected_label = Discovery::Present.readable_name_for_suspend_or_terminus(*position.to_a, lanes_cfg: lanes_cfg)
actual_label = Discovery::Present.readable_name_for_suspend_or_terminus(*expected_position.to_a, lanes_cfg: lanes_cfg)

lane_label = Discovery::Present.lane_options_for(*position.to_a, lanes_cfg: lanes_cfg)[:label]

Expand All @@ -38,19 +30,13 @@ def self.error_message_for(position, expected_position, lanes_cfg:) # TODO: test
# Grab the start_position and expected_lane_positions from the discovered plan, run
# the collaboration from there and check if it actually reached the expected configuration.
def assert_advance(event_label, test_plan:, lanes_cfg:, schema:, message_flow:, expected_ctx:, ctx: {seq: []}, **) # TODO: allow {ctx}
testing_row = test_plan[event_label]
iteration = test_plan.to_a.find { |iteration| iteration.event_label == event_label } or raise

start_position = testing_row[:start_position]
start_tuple = start_position[:tuple]
start_position = test_plan.position_from_tuple(*start_tuple)
start_position = iteration.start_task_position

# current position.
#DISCUSS: here, we could also ask the State layer for the start configuration, on a different level.
lane_positions = testing_row[:start_configuration].collect do |row|
test_plan.position_from_tuple(*row[:tuple])
end

lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(lane_positions)
lane_positions = iteration.start_positions

ctx_for_advance = ctx

Expand All @@ -65,7 +51,7 @@ def assert_advance(event_label, test_plan:, lanes_cfg:, schema:, message_flow:,
message_flow: message_flow,
)

assert_positions configuration[:lane_positions], testing_row[:suspend_configuration], lanes_cfg: lanes_cfg, test_plan: test_plan
assert_positions configuration[:lane_positions], iteration.suspend_positions, lanes_cfg: lanes_cfg, test_plan: test_plan

# assert_equal ctx.inspect, expected_ctx.inspect
ctx
Expand Down
27 changes: 3 additions & 24 deletions lib/trailblazer/workflow/test/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ module Plan
module_function

# Code fragment with assertions for the discovered/configured test plan.
def for(discovered_states, input:, **options)
code_string = discovered_states.collect do |row|

# TODO: introduce a "test structure" that computes event label, and then links to the specific row of the {discovered_states}.
# that way, we only compute the "ID" once.
event_label = CommentHeader.start_position_label(row[:positions_before][1], row, **options)
def for(iteration_set, input:, **options)
code_string = iteration_set.to_a.collect do |iteration|
event_label = iteration.event_label

%(
# test: #{event_label}
Expand All @@ -20,24 +17,6 @@ def for(discovered_states, input:, **options)
end
end

# Render a test plan JSON structure that can be checked in so the assertions
# don't change with code modifications.
module Structure
module_function

def serialize(discovered_states, **options)
discovered_states.collect do |row|
{
event_label: CommentHeader.start_position_label(row[:positions_before][1], row, **options),
start_position: serialize_position(*row[:positions_before][1].to_a, **options),
start_configuration: serialize_configuration(row[:positions_before][0], **options),
suspend_configuration: serialize_configuration(row[:suspend_configuration].lane_positions, **options),
outcome: row[:outcome],
}
end
end
end

def render_comment_header(discovered_states, **options)
CommentHeader.(discovered_states, **options)
end
Expand Down
18 changes: 11 additions & 7 deletions test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ def assert_position_after(actual_configuration, expected_ids, lanes:)
it "{#render_cli_state_table}" do
states, lanes_sorted, lanes_cfg = self.class.states()

cli_state_table = Trailblazer::Workflow::Discovery::Present::StateTable.(states, lanes_cfg: lanes_cfg)
iteration_set = Trailblazer::Workflow::Iteration::Set.from_discovered_states(states, lanes_cfg: lanes_cfg)

cli_state_table = Trailblazer::Workflow::Discovery::Present::StateTable.(iteration_set, lanes_cfg: lanes_cfg)
puts cli_state_table
assert_equal cli_state_table,
%(+---------------------------------+----------------------------------------+
Expand Down Expand Up @@ -371,16 +373,18 @@ class TestPlanExecutionTest < Minitest::Spec
it "run test plan" do
states, lanes_sorted, lanes_cfg, schema, message_flow = DiscoveryTest.states()

iteration_set = Trailblazer::Workflow::Iteration::Set.from_discovered_states(states, lanes_cfg: lanes_cfg)

#@ Test plan
puts Trailblazer::Workflow::Test::Plan.for(states, lanes_cfg: lanes_cfg, input: {})
# FIXME: properly test this output!
puts Trailblazer::Workflow::Test::Plan.for(iteration_set, lanes_cfg: lanes_cfg, input: {})


# TODO: encapsulate
plan_structure = Trailblazer::Workflow::Test::Plan::Structure.serialize(states, lanes_cfg: lanes_cfg)
testing_json = JSON.pretty_generate(plan_structure)
test_plan_structure = Trailblazer::Workflow::Test::Plan::Structure.deserialize(plan_structure, lanes_cfg: lanes_cfg)


# plan_structure = Trailblazer::Workflow::Test::Plan::Structure.serialize(states, lanes_cfg: lanes_cfg)
# testing_json = JSON.pretty_generate(plan_structure)
# test_plan_structure = Trailblazer::Workflow::Test::Plan::Structure.deserialize(plan_structure, lanes_cfg: lanes_cfg)
test_plan_structure = iteration_set

# test: ☝ ⏵︎Create form
ctx = assert_advance "☝ ⏵︎Create form", expected_ctx: {}, test_plan: test_plan_structure, lanes_cfg: lanes_cfg, schema: schema, message_flow: message_flow
Expand Down

0 comments on commit 1fb6271

Please sign in to comment.