Module: GenevaDrive::TestHelpers
- Defined in:
- lib/geneva_drive/test_helpers.rb
Overview
Test helpers for testing workflows in your application.
Include this module in your test classes to get access to workflow testing utilities.
Instance Method Summary collapse
-
#assert_step_executed(workflow, step_name, state: "completed") ⇒ void
Asserts that a workflow has executed a specific step.
-
#assert_workflow_state(workflow, expected_state) ⇒ void
Asserts that a workflow is in a specific state.
-
#perform_next_step(workflow) ⇒ GenevaDrive::StepExecution?
Executes only the next pending step of a workflow.
-
#perform_step_inline(workflow, step_name = nil) ⇒ GenevaDrive::StepExecution
Executes a step inline, bypassing normal workflow progression.
-
#speedrun_workflow(workflow, max_iterations: 100) ⇒ GenevaDrive::Workflow
Executes all steps of a workflow synchronously for testing.
Instance Method Details
#assert_step_executed(workflow, step_name, state: "completed") ⇒ void
This method returns an undefined value.
Asserts that a workflow has executed a specific step.
148 149 150 151 152 153 154 155 156 |
# File 'lib/geneva_drive/test_helpers.rb', line 148 def assert_step_executed(workflow, step_name, state: "completed") step_execution = workflow.step_executions.find_by( step_name: step_name.to_s, state: state ) assert step_execution, "Expected step '#{step_name}' to be #{state}, but it was not found" end |
#assert_workflow_state(workflow, expected_state) ⇒ void
This method returns an undefined value.
Asserts that a workflow is in a specific state.
168 169 170 171 172 |
# File 'lib/geneva_drive/test_helpers.rb', line 168 def assert_workflow_state(workflow, expected_state) workflow.reload assert_equal expected_state.to_s, workflow.state, "Expected workflow to be #{expected_state}, but was #{workflow.state}" end |
#perform_next_step(workflow) ⇒ GenevaDrive::StepExecution?
Executes only the next pending step of a workflow.
Useful for testing step-by-step behavior and inspecting state between steps.
76 77 78 79 80 81 82 83 84 |
# File 'lib/geneva_drive/test_helpers.rb', line 76 def perform_next_step(workflow) workflow.reload step_execution = workflow.current_execution return nil unless step_execution step_execution.execute! workflow.reload step_execution end |
#perform_step_inline(workflow, step_name = nil) ⇒ GenevaDrive::StepExecution
Executes a step inline, bypassing normal workflow progression.
Creates a step execution for the given step and executes it immediately. This is a testing shortcut that allows you to test individual steps without running through the entire flow.
When called without a step name, executes the current step.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/geneva_drive/test_helpers.rb', line 107 def perform_step_inline(workflow, step_name = nil) workflow.reload step_name ||= workflow.next_step_name step_def = workflow.class.steps.find { |s| s.name == step_name.to_s } unless step_def available = workflow.class.steps.map(&:name).join(", ") raise ArgumentError, "Step '#{step_name}' is not defined in #{workflow.class.name}. " \ "Available steps: #{available}" end # Cancel any existing scheduled step executions to satisfy uniqueness constraint workflow.step_executions.where(state: "scheduled").update_all( state: "canceled", outcome: "canceled", canceled_at: Time.current ) step_execution = workflow.step_executions.create!( step_name: step_def.name, state: "scheduled", scheduled_for: Time.current ) step_execution.execute! workflow.reload step_execution end |
#speedrun_workflow(workflow, max_iterations: 100) ⇒ GenevaDrive::Workflow
Executes all steps of a workflow synchronously for testing.
This method runs through all scheduled steps, executing each one immediately regardless of wait times. Useful for testing complete workflow behavior.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/geneva_drive/test_helpers.rb', line 39 def speedrun_workflow(workflow, max_iterations: 100) iterations = 0 loop do workflow.reload break if %w[finished canceled paused].include?(workflow.state) step_execution = workflow.current_execution break unless step_execution step_execution.execute! iterations += 1 if iterations >= max_iterations raise "speedrun_workflow exceeded max_iterations (#{max_iterations}). " \ "Workflow may be in an infinite loop." end end workflow.reload end |