From d44d4085aacae065f980f4b59b91e9b8c0597a7f Mon Sep 17 00:00:00 2001 From: Shaumik-Ashraf Date: Thu, 8 Aug 2024 16:48:38 -0400 Subject: [PATCH] cop --- .../dev_validator_suite/validator_suite.rb | 7 +-- lib/inferno/apps/cli/execute.rb | 46 +++++++-------- .../apps/web/controllers/test_runs/create.rb | 1 - lib/inferno/utils/persist_inputs.rb | 2 - lib/inferno/utils/verify_runnable.rb | 2 - spec/inferno/apps/cli/execute_spec.rb | 58 +++++++++---------- spec/inferno/utils/persist_inputs_spec.rb | 3 - spec/inferno/utils/verify_runnable_spec.rb | 2 +- 8 files changed, 56 insertions(+), 65 deletions(-) diff --git a/dev_suites/dev_validator_suite/validator_suite.rb b/dev_suites/dev_validator_suite/validator_suite.rb index d562ef16f..edc848b00 100644 --- a/dev_suites/dev_validator_suite/validator_suite.rb +++ b/dev_suites/dev_validator_suite/validator_suite.rb @@ -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' @@ -19,7 +19,7 @@ class ValidatorSuite < Inferno::TestSuite end fhir_resource_validator do - url "http://localhost/hl7validatorapi" + url 'http://localhost/hl7validatorapi' end group do @@ -54,6 +54,5 @@ class ValidatorSuite < Inferno::TestSuite end end end - end end diff --git a/lib/inferno/apps/cli/execute.rb b/lib/inferno/apps/cli/execute.rb index b4df9c878..f03aaaa73 100644 --- a/lib/inferno/apps/cli/execute.rb +++ b/lib/inferno/apps/cli/execute.rb @@ -10,7 +10,6 @@ module Inferno module CLI class Execute - include ::Inferno::Utils::VerifyRunnable include ::Inferno::Utils::PersistInputs @@ -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" @@ -49,7 +48,6 @@ def self.suppress_output attr_accessor :options - def run(options) puts '' puts '==========================================' @@ -57,42 +55,42 @@ def run(options) 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') @@ -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 @@ -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 @@ -139,22 +137,24 @@ 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}" @@ -162,7 +162,7 @@ def serialize(entity) 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) @@ -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 @@ -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 diff --git a/lib/inferno/apps/web/controllers/test_runs/create.rb b/lib/inferno/apps/web/controllers/test_runs/create.rb index 5913c5742..c72f40a57 100644 --- a/lib/inferno/apps/web/controllers/test_runs/create.rb +++ b/lib/inferno/apps/web/controllers/test_runs/create.rb @@ -6,7 +6,6 @@ module Web module Controllers module TestRuns class Create < Controller - include ::Inferno::Utils::VerifyRunnable include ::Inferno::Utils::PersistInputs diff --git a/lib/inferno/utils/persist_inputs.rb b/lib/inferno/utils/persist_inputs.rb index 74177f602..dbfe345c5 100644 --- a/lib/inferno/utils/persist_inputs.rb +++ b/lib/inferno/utils/persist_inputs.rb @@ -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| @@ -25,7 +24,6 @@ def persist_inputs(params, test_run) ) end end - end end end diff --git a/lib/inferno/utils/verify_runnable.rb b/lib/inferno/utils/verify_runnable.rb index ab217e87c..df8622a60 100644 --- a/lib/inferno/utils/verify_runnable.rb +++ b/lib/inferno/utils/verify_runnable.rb @@ -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 diff --git a/spec/inferno/apps/cli/execute_spec.rb b/spec/inferno/apps/cli/execute_spec.rb index 895deb129..b68a93834 100644 --- a/spec/inferno/apps/cli/execute_spec.rb +++ b/spec/inferno/apps/cli/execute_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../../../lib/inferno/apps/cli/execute.rb' +require_relative '../../../../lib/inferno/apps/cli/execute' ## TODO REFACTOR ALL THESE TESTS WITH FACTORY BOT @@ -6,7 +6,7 @@ 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) @@ -15,23 +15,23 @@ 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 @@ -39,36 +39,36 @@ 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 @@ -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 @@ -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 diff --git a/spec/inferno/utils/persist_inputs_spec.rb b/spec/inferno/utils/persist_inputs_spec.rb index 31d8089cb..9c8b3fc36 100644 --- a/spec/inferno/utils/persist_inputs_spec.rb +++ b/spec/inferno/utils/persist_inputs_spec.rb @@ -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 - diff --git a/spec/inferno/utils/verify_runnable_spec.rb b/spec/inferno/utils/verify_runnable_spec.rb index 68a03c0d4..e201c94f3 100644 --- a/spec/inferno/utils/verify_runnable_spec.rb +++ b/spec/inferno/utils/verify_runnable_spec.rb @@ -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