Skip to content

Commit

Permalink
Refactored logic for custom result block
Browse files Browse the repository at this point in the history
Signed-off-by: Vanessa Fotso <[email protected]>
  • Loading branch information
vanessuniq committed Aug 9, 2024
1 parent f7d7ce9 commit 9004016
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 237 deletions.
74 changes: 42 additions & 32 deletions dev_suites/dev_custom_results/custom_result_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ class PassingCustomResultGroup < Inferno::TestGroup
title 'Passing Group'
description 'Criteria: Passes if test 1 passes, or test 2 and test 3 pass'

customize_passing_result do |results|
test1_result = results[tests.first.id].result
test2_result = results[tests[1].id].result
test3_result = results[tests.last.id].result

test1_result == 'pass' || (test2_result == 'pass' && test3_result == 'pass')
end

test 'Test 1: passing' do
run { assert true }
end
Expand All @@ -27,6 +19,16 @@ class PassingCustomResultGroup < Inferno::TestGroup

run { assert false }
end

run do
test1_result = child_results[tests.first.id].result
pass_if test1_result == 'pass', 'Test 1 passed'

test2_result = child_results[tests[1].id].result
test3_result = child_results[tests.last.id].result

assert test2_result == 'pass' && test3_result == 'pass', 'Either test 1, or test 2 and test 3 must pass'
end
end

class NonPassingCustomResultGroup < Inferno::TestGroup
Expand All @@ -36,14 +38,6 @@ class NonPassingCustomResultGroup < Inferno::TestGroup

optional

customize_passing_result do |results|
test1_result = results[tests.first.id].result
test2_result = results[tests[1].id].result
test3_result = results[tests.last.id].result

test1_result == 'pass' || (test2_result == 'pass' && test3_result == 'pass')
end

test 'Test 1: failing' do
run { assert false }
end
Expand All @@ -59,6 +53,17 @@ class NonPassingCustomResultGroup < Inferno::TestGroup

run { skip }
end

run do
test1_result = child_results[tests.first.id].result
pass_if test1_result == 'pass', 'Test 1 passed'
add_message('info', 'Test 1 did not pass')

test2_result = child_results[tests[1].id].result
test3_result = child_results[tests.last.id].result

assert test2_result == 'pass' && test3_result == 'pass', 'Either test 1, or test 2 and test 3 must pass'
end
end

class PassingCustomResultGroupWithNestedGroups < Inferno::TestGroup
Expand All @@ -68,14 +73,6 @@ class PassingCustomResultGroupWithNestedGroups < Inferno::TestGroup

optional

customize_passing_result do |results|
group1_result = results[groups.first.id].result
group2_result = results[groups[1].id].result
group3_result = results[groups.last.id].result

group1_result == 'pass' || (group2_result == 'pass' && group3_result == 'pass')
end

group 'Inner Group 1: failing' do
test do
run { assert false }
Expand All @@ -97,23 +94,36 @@ class PassingCustomResultGroupWithNestedGroups < Inferno::TestGroup
run { assert true }
end
end

run do
group1_result = child_results[groups.first.id].result
pass_if group1_result == 'pass', 'Inner Group 1 passed.'
add_message('info', 'Inner Group 1 did not pass.')

group2_result = child_results[groups[1].id].result
group3_result = child_results[groups.last.id].result

assert group2_result == 'pass' && group3_result == 'pass',
'Either inner group 1, or inner group 2 and inner group 3 must pass'
end
end

class Suite < Inferno::TestSuite
id :custom_result_suite
title 'Custom Result Suite'
description 'Criteria: Passes if `Passing Group` passes and any of the other groups passes.'

customize_passing_result do |results|
group1_result = results[:passing_custom_result_group]
other_groups_pass = results.any? do |result|
result.test_group_id != group1_result.test_group_id && result.result == 'pass'
end
group1_result.result == 'pass' && other_groups_pass
end

group from: :passing_custom_result_group
group from: :non_passing_custom_result_group
group from: :passing_custom_result_group_with_nested_groups

run do
group1_result = child_results[:passing_custom_result_group]
other_groups_pass = child_results.any? do |result|
result.test_group_id != group1_result.test_group_id && result.result == 'pass'
end
assert group1_result.result == 'pass' && other_groups_pass,
'`Passing Group` and any of the other groups must pass.'
end
end
end
4 changes: 3 additions & 1 deletion lib/inferno/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative 'dsl/results'
require_relative 'dsl/runnable'
require_relative 'dsl/suite_endpoint'
require_relative 'dsl/messages'

module Inferno
# The DSL for writing tests.
Expand All @@ -16,7 +17,8 @@ module DSL
HTTPClient,
Results,
FHIRValidation,
FHIRResourceValidation
FHIRResourceValidation,
Messages
].freeze

EXTENDABLE_DSL_MODULES = [
Expand Down
68 changes: 68 additions & 0 deletions lib/inferno/dsl/messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Inferno
module DSL
# This module contains methods to add meessages to runnable results
module Messages
# @private
def messages
@messages ||= []
end

# Add a message to the result.
#
# @param type [String] error, warning, or info
# @param message [String]
# @return [void]
def add_message(type, message)
messages << { type: type.to_s, message: format_markdown(message) }
end

# Add an informational message to the results of a test. If passed a
# block, a failed assertion will become an info message and test execution
# will continue.
#
# @param message [String]
# @return [void]
# @example
# # Add an info message
# info 'This message will be added to the test results'
#
# # The message for the failed assertion will be treated as an info
# # message. Test exection will continue.
# info { assert false == true }
def info(message = nil)
unless block_given?
add_message('info', message) unless message.nil?
return
end

yield
rescue Exceptions::AssertionException => e
add_message('info', e.message)
end

# Add a warning message to the results of a test. If passed a block, a
# failed assertion will become a warning message and test execution will
# continue.
#
# @param message [String]
# @return [void]
# @example
# # Add a warning message
# warning 'This message will be added to the test results'
#
# # The message for the failed assertion will be treated as a warning
# # message. Test exection will continue.
# warning { assert false == true }
def warning(message = nil)
unless block_given?
add_message('warning', message) unless message.nil?
return
end

yield
rescue Exceptions::AssertionException => e
add_message('warning', e.message)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/inferno/dsl/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ def default_id
to_s
end

# Set/Get the block that is executed when a runnable is run
#
# @param block [Proc]
# @return [Proc] the block that is executed when a runnable is run
def block(&block)
return @block unless block_given?

@block = block
end

alias run block

# @private
def all_children
@all_children ||= []
Expand Down
74 changes: 0 additions & 74 deletions lib/inferno/entities/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ def initialize(**params)
@suite_options = params[:suite_options].presence || {}
end

# @private
def messages
@messages ||= []
end

# Add a message to the result.
#
# @param type [String] error, warning, or info
# @param message [String]
# @return [void]
def add_message(type, message)
messages << { type: type.to_s, message: format_markdown(message) }
end

# Set output values. Once set, these values will be available to any
# subsequent tests.
#
Expand All @@ -59,54 +45,6 @@ def outputs_to_persist
@outputs_to_persist ||= {}
end

# Add an informational message to the results of a test. If passed a
# block, a failed assertion will become an info message and test execution
# will continue.
#
# @param message [String]
# @return [void]
# @example
# # Add an info message
# info 'This message will be added to the test results'
#
# # The message for the failed assertion will be treated as an info
# # message. Test exection will continue.
# info { assert false == true }
def info(message = nil)
unless block_given?
add_message('info', message) unless message.nil?
return
end

yield
rescue Exceptions::AssertionException => e
add_message('info', e.message)
end

# Add a warning message to the results of a test. If passed a block, a
# failed assertion will become a warning message and test execution will
# continue.
#
# @param message [String]
# @return [void]
# @example
# # Add a warning message
# warning 'This message will be added to the test results'
#
# # The message for the failed assertion will be treated as a warning
# # message. Test exection will continue.
# warning { assert false == true }
def warning(message = nil)
unless block_given?
add_message('warning', message) unless message.nil?
return
end

yield
rescue Exceptions::AssertionException => e
add_message('warning', e.message)
end

# @private
def method_missing(name, *args, &)
parent_instance = self.class.parent&.new
Expand Down Expand Up @@ -167,18 +105,6 @@ def repository
Inferno::Repositories::Tests.new
end

# Set/Get the block that is executed when a Test is run
#
# @param block [Proc]
# @return [Proc] the block that is executed when a Test is run
def block(&block)
return @block unless block_given?

@block = block
end

alias run block

def short_id
@short_id ||= begin
prefix = parent.respond_to?(:short_id) ? "#{parent.short_id}." : ''
Expand Down
Loading

0 comments on commit 9004016

Please sign in to comment.