Skip to content

Commit

Permalink
cop
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaumik-Ashraf committed Aug 8, 2024
1 parent 2df72f3 commit d44d408
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 65 deletions.
7 changes: 3 additions & 4 deletions dev_suites/dev_validator_suite/validator_suite.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

module DevValidatorSuite # rubocop:disable Naming/ClassAndModuleCamelCase
module DevValidatorSuite
class ValidatorSuite < Inferno::TestSuite
title 'Validator Suite'
id :dev_validator
description "Inferno Core Developer Suite that makes calls to the HL7 Validator."
description 'Inferno Core Developer Suite that makes calls to the HL7 Validator.'

input :url,
title: 'FHIR Server Base Url'
Expand All @@ -19,7 +19,7 @@ class ValidatorSuite < Inferno::TestSuite
end

fhir_resource_validator do
url "http://localhost/hl7validatorapi"
url 'http://localhost/hl7validatorapi'
end

group do
Expand Down Expand Up @@ -54,6 +54,5 @@ class ValidatorSuite < Inferno::TestSuite
end
end
end

end
end
46 changes: 23 additions & 23 deletions lib/inferno/apps/cli/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
module Inferno
module CLI
class Execute

include ::Inferno::Utils::VerifyRunnable
include ::Inferno::Utils::PersistInputs

Expand All @@ -34,9 +33,9 @@ def self.suppress_output

# Inferno boot flow triggers migration and logger outputs it
# I would be allow this in verbose mode but definitely not for JSON output
suppress_output{ require_relative '../../../inferno' }
suppress_output { require_relative '../../../inferno' }

# TODO hijack logger and suppress or redirect its output
# TODO: hijack logger and suppress or redirect its output

COLOR = Pastel.new
CHECKMARK = "\u2713"
Expand All @@ -49,50 +48,49 @@ def self.suppress_output

attr_accessor :options


def run(options)
puts ''
puts '=========================================='
puts "Testing #{options[:suite]} Suite"
puts '=========================================='

self.options = options
verbose_puts "options:", self.options
verbose_puts 'options:', self.options

Inferno::Application.start(:suites)

suite = Inferno::Repositories::TestSuites.new.find(options[:suite])
raise StandardError, "Suite #{options[:suite]} not found" if suite.nil?

test_session = test_sessions_repo.create({test_suite_id: suite.id}) # TODO add suite options
test_session = test_sessions_repo.create({ test_suite_id: suite.id }) # TODO: add suite options

verify_runnable(
test_runs_repo.build_entity(create_params(test_session,suite)).runnable,
test_runs_repo.build_entity(create_params(test_session, suite)).runnable,
thor_hash_to_inputs_array(options[:inputs]),
test_session.suite_options
)

test_run = test_runs_repo.create(
create_params(test_session,suite).merge({status: 'queued'})
create_params(test_session, suite).merge({ status: 'queued' })
)

persist_inputs(create_params(test_session, suite), test_run)

puts "Running tests. This may take a while..." # TODO spinner/progress bar
puts 'Running tests. This may take a while...' # TODO: spinner/progress bar
Jobs.perform(Jobs::ExecuteTestRun, test_run.id, force_synchronous: true)

results = test_runs_repo.results_for_test_run(test_run.id).reverse
verbose_puts '=========================================='
verbose_puts "JSON Test Results:"
verbose_puts 'JSON Test Results:'
verbose_puts '=========================================='
verbose_puts serialize(results)
verbose_puts '=========================================='

puts '=========================================='
puts "Colored Test Results:"
puts 'Colored Test Results:'
puts '=========================================='
results.each do |result|
print format_id(result), ": "
print format_id(result), ': '
case result.result
when 'pass'
print COLOR.bold.green(CHECKMARK, ' pass')
Expand All @@ -110,9 +108,9 @@ def run(options)
when 'cancel'
print COLOR.red 'X cancel'
else
# TODO strict behavior or no?
#raise StandardError.new, "Unrecognized result #{result.result}" # strict
print '- unknown' # unstrict
# TODO: strict behavior or no?
# raise StandardError.new, "Unrecognized result #{result.result}" # strict
print '- unknown' # unstrict
end
puts ''
verbose_puts "\tsummary: ", result.result_message
Expand All @@ -123,7 +121,7 @@ def run(options)
end
puts '=========================================='

exit(0) if results.find{|result| result.test_suite_id == options[:suite_id]}.result == 'pass'
exit(0) if results.find { |result| result.test_suite_id == options[:suite_id] }.result == 'pass'

exit(1)
rescue Sequel::ValidationFailed => e
Expand All @@ -139,30 +137,32 @@ def run(options)
end

def thor_hash_to_inputs_array(hash)
hash.to_a.map{|pair| {name: pair[0], value: pair[1]}}
hash.to_a.map { |pair| { name: pair[0], value: pair[1] } }
end

def create_params(test_session, suite)
{
test_session_id: test_session.id,
test_suite_id: suite.id,
inputs: thor_hash_to_inputs_array(self.options[:inputs])
inputs: thor_hash_to_inputs_array(options[:inputs])
}
end

def serialize(entity)
case entity.class.to_s
when 'Array'
JSON.pretty_generate entity.map{ |item| JSON.parse serialize(item) }
when ->(x) { defined?(x.constantize) && defined?("Inferno::Web::Serializers::#{x.split('::').last}".constantize) }
JSON.pretty_generate entity.map { |item| JSON.parse serialize(item) }
when lambda { |x|
defined?(x.constantize) && defined?("Inferno::Web::Serializers::#{x.split('::').last}".constantize)
}
"Inferno::Web::Serializers::#{entity.class.to_s.split('::').last}".constantize.render(entity)
else
raise StandardError, "CLI does not know how to serialize #{entity.class}"
end
end

def verbose_print(*args)
print(COLOR.dim(*args)) if self.options[:verbose]
print(COLOR.dim(*args)) if options[:verbose]
end

def verbose_puts(*args)
Expand All @@ -176,7 +176,7 @@ def format_id(result)

def format_messages(result)
result.messages.map do |message|
"\n\t\t" + message.type + ": " + message.message
"\n\t\t" + message.type + ': ' + message.message
end.join('')
end

Expand All @@ -201,7 +201,7 @@ def format_outputs(result)

def print_error_and_exit(e, code)
# TODO: use Application Logger for stderr?
$stderr.puts COLOR.red "Error: #{e.full_message}"
warn COLOR.red "Error: #{e.full_message}"
exit(code)
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/inferno/apps/web/controllers/test_runs/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Web
module Controllers
module TestRuns
class Create < Controller

include ::Inferno::Utils::VerifyRunnable
include ::Inferno::Utils::PersistInputs

Expand Down
2 changes: 0 additions & 2 deletions lib/inferno/utils/persist_inputs.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Inferno
module Utils
module PersistInputs

def persist_inputs(params, test_run)
available_inputs = test_run.runnable.available_inputs
params[:inputs]&.each do |input_params|
Expand All @@ -25,7 +24,6 @@ def persist_inputs(params, test_run)
)
end
end

end
end
end
2 changes: 0 additions & 2 deletions lib/inferno/utils/verify_runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
module Inferno
module Utils
module VerifyRunnable

def verify_runnable(runnable, inputs, selected_suite_options)
missing_inputs = runnable&.missing_inputs(inputs, selected_suite_options)
user_runnable = runnable&.user_runnable?
raise Inferno::Exceptions::RequiredInputsNotFound, missing_inputs if missing_inputs&.any?
raise Inferno::Exceptions::NotUserRunnableException unless user_runnable
end

end
end
end
58 changes: 29 additions & 29 deletions spec/inferno/apps/cli/execute_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require_relative '../../../../lib/inferno/apps/cli/execute.rb'
require_relative '../../../../lib/inferno/apps/cli/execute'

## TODO REFACTOR ALL THESE TESTS WITH FACTORY BOT

RSpec.describe Inferno::CLI::Execute do # rubocop:disable RSpec/FilePath
let(:instance) { described_class.new }

describe '#thor_hash_to_inputs_array' do
let(:hash) { {url: 'https://example.com'} }
let(:hash) { { url: 'https://example.com' } }

it 'converts hash to array' do
result = instance.thor_hash_to_inputs_array(hash)
Expand All @@ -15,60 +15,60 @@

it 'returns proper inputs array' do
result = instance.thor_hash_to_inputs_array(hash)
expect(result).to eq([{name: :url, value: 'https://example.com'}])
expect(result).to eq([{ name: :url, value: 'https://example.com' }])
end
end

describe '#create_params' do
let(:test_suite) { BasicTestSuite::Suite }
let(:test_session) { create(:test_session) }
let(:inputs_hash) { {url: 'https://example.com'} }
let(:inputs_array) { [{name: :url, value: 'https://example.com'}] }
let(:inputs_hash) { { url: 'https://example.com' } }
let(:inputs_array) { [{ name: :url, value: 'https://example.com' }] }

it 'returns test run params' do
stubbed_instance = instance()
allow(stubbed_instance).to receive(:options).and_return({inputs: inputs_hash})
test_session_inst = test_session()
stubbed_instance = instance
allow(stubbed_instance).to receive(:options).and_return({ inputs: inputs_hash })
test_session_inst = test_session

result = stubbed_instance.create_params(test_session_inst, test_suite)
expect(result).to eq({test_session_id: test_session.id, test_suite_id: test_suite.id, inputs: inputs_array})
expect(result).to eq({ test_session_id: test_session.id, test_suite_id: test_suite.id, inputs: inputs_array })
end
end

describe '#serialize' do
let(:test_results) { create_list(:result, 2) }

it 'handles an array of test results without raising exception' do
expect { instance.serialize(test_results) }.not_to raise_error(StandardError)
expect { instance.serialize(test_results) }.to_not raise_error(StandardError)
end

it 'returns legit JSON' do
expect { JSON.parse(instance.serialize(test_results)) }.not_to raise_error(JSON::ParserError)
expect { JSON.parse(instance.serialize(test_results)) }.not_to raise_error(JSON::NestingError)
expect { JSON.parse(instance.serialize(test_results)) }.not_to raise_error(TypeError)
expect { JSON.parse(instance.serialize(test_results)) }.to_not raise_error(JSON::ParserError)
expect { JSON.parse(instance.serialize(test_results)) }.to_not raise_error(JSON::NestingError)
expect { JSON.parse(instance.serialize(test_results)) }.to_not raise_error(TypeError)
end
end

describe '#verbose_print' do
it 'outputs when verbose is true' do
stubbed_instance = instance()
allow(stubbed_instance).to receive(:options).and_return({verbose: true})
stubbed_instance = instance
allow(stubbed_instance).to receive(:options).and_return({ verbose: true })

expect { stubbed_instance.verbose_print('Lorem') }.to output(/Lorem/).to_stdout
end

it 'does not output when verbose is false' do
stubbed_instance = instance()
allow(stubbed_instance).to receive(:options).and_return({verbose: false})
stubbed_instance = instance
allow(stubbed_instance).to receive(:options).and_return({ verbose: false })

expect { stubbed_instance.verbose_print('Lorem') }.not_to output(/.+/).to_stdout
expect { stubbed_instance.verbose_print('Lorem') }.to_not output(/.+/).to_stdout
end
end

describe '#verbose_puts' do
it 'has output ending with \n with when verbose is true' do
stubbed_instance = instance()
allow(stubbed_instance).to receive(:options).and_return({verbose: true})
stubbed_instance = instance
allow(stubbed_instance).to receive(:options).and_return({ verbose: true })

expect { stubbed_instance.verbose_puts('Lorem') }.to output(/Lorem\n/).to_stdout
end
Expand All @@ -80,21 +80,21 @@
let(:test) { test_group.tests.first }

it 'returns suite id if test result belongs to suite' do
test_result = create(:result, runnable: {test_suite_id: test_suite.id})
test_result = create(:result, runnable: { test_suite_id: test_suite.id })

expect( instance.format_id(test_result) ).to eq( test_suite.id )
expect(instance.format_id(test_result)).to eq(test_suite.id)
end

it 'returns group id if test result belongs to group' do
test_result = create(:result, runnable: {test_group_id: test_group.id})
test_result = create(:result, runnable: { test_group_id: test_group.id })

expect( instance.format_id(test_result) ).to eq( test_group.id )
expect(instance.format_id(test_result)).to eq(test_group.id)
end

it 'returns test id if test result belongs to test' do
test_result = create(:result, runnable: {test_id: test.id});
test_result = create(:result, runnable: { test_id: test.id })

expect( instance.format_id(test_result) ).to eq( test.id )
expect(instance.format_id(test_result)).to eq(test.id)
end
end

Expand Down Expand Up @@ -126,19 +126,19 @@
end

describe '#format_inputs' do
let(:inputs) {[{name: :url, value: 'https://example.com'}]}
let(:inputs) { [{ name: :url, value: 'https://example.com' }] }
let(:test_result) { create(:result, input_json: JSON.generate(inputs)) }

it 'includes all values' do
formatted_string = instance.format_inputs(test_result)
inputs.each do |input_element|
expect(formatted_string).to include input_element[:value]
expect(formatted_string).to include input_element[:value]
end
end
end

describe '#format_outputs' do
let(:outputs) {[{name: :token, value: 'SAMPLE_OUTPUT'}]}
let(:outputs) { [{ name: :token, value: 'SAMPLE_OUTPUT' }] }
let(:test_result) { create(:result, output_json: JSON.generate(outputs)) }

it 'includes all values' do
Expand Down
3 changes: 0 additions & 3 deletions spec/inferno/utils/persist_inputs_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require_relative '../../../lib/inferno/utils/persist_inputs'

RSpec.describe Inferno::Utils::PersistInputs do

describe '#persist_inputs' do
it 'is defined' do
expect(described_class.method_defined?(:persist_inputs)).to eq(true)
end
end

end

2 changes: 1 addition & 1 deletion spec/inferno/utils/verify_runnable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../../../lib/inferno/utils/verify_runnable.rb'
require_relative '../../../lib/inferno/utils/verify_runnable'

RSpec.describe Inferno::Utils::VerifyRunnable do
describe '#verify_runnable' do
Expand Down

0 comments on commit d44d408

Please sign in to comment.