Skip to content

Commit

Permalink
Merge branch 'main' into FI-3261-markdown-descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyssaWang authored Dec 5, 2024
2 parents 3b18b6d + 15eb38c commit b72c706
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
26 changes: 26 additions & 0 deletions config/presets/dev_validator_preset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"title": "Preset for Validator Suite",
"id": "dev_validator_preset",
"test_suite_id": "dev_validator",
"inputs": [
{
"name": "url",
"value": "https://hapi.fhir.org/baseR4",
"_title": "FHIR Server Base Url",
"_type": "text"
},
{
"name": "access_token",
"value": null,
"_title": "Bearer/Access Token",
"_type": "text",
"_optional": true
},
{
"name": "patient_id",
"value": "1234321",
"_title": "Patient ID",
"_type": "text"
}
]
}
53 changes: 49 additions & 4 deletions lib/inferno/apps/cli/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ def run(options)

self.options = options

outputter.print_start_message(options)
outputter.print_start_message(self.options)

load_preset_file_and_set_preset_id

results = []
outputter.print_around_run(options) do
outputter.print_around_run(self.options) do
if all_selected_groups_and_tests.empty?
test_run = create_test_run(suite)
run_one(suite, test_run)
Expand Down Expand Up @@ -102,14 +104,26 @@ def outputter
@outputter ||= OUTPUTTERS[options[:outputter]].new
end

def load_preset_file_and_set_preset_id
return unless options[:preset_file]
raise StandardError, 'Cannot use `--preset-id` and `--preset-file` options together' if options[:preset_id]

raise StandardError, "File #{options[:preset_file]} not found" unless File.exist? options[:preset_file]

options[:preset_id] = JSON.parse(File.read(options[:preset_file]))['id']
raise StandardError, "Preset #{options[:preset_file]} is missing id" if options[:preset_id].nil?

presets_repo.insert_from_file(options[:preset_file])
end

def all_selected_groups_and_tests
@all_selected_groups_and_tests ||= runnables_by_short_id + groups + tests
end

def run_one(runnable, test_run)
verify_runnable(
runnable,
thor_hash_to_inputs_array(options[:inputs]),
thor_hash_to_inputs_array(inputs_and_preset),
test_session.suite_options
)

Expand All @@ -118,6 +132,33 @@ def run_one(runnable, test_run)
dispatch_job(test_run)
end

def inputs_and_preset
if preset
preset_inputs = preset.inputs.to_h do |preset_input|
[preset_input[:name], preset_input[:value]]
end

options.fetch(:inputs, {}).reverse_merge(preset_inputs)
else
options.fetch(:inputs, {})
end
end

def preset
return unless options[:preset_id]

@preset ||= presets_repo.find(options[:preset_id])

raise StandardError, "Preset #{options[:preset_id]} not found" if @preset.nil?

unless presets_repo.presets_for_suite(suite.id).include?(@preset)
raise StandardError,
"Preset #{options[:preset_id]} is incompatible with suite #{suite.id}"
end

@preset
end

def suite
@suite ||= Inferno::Repositories::TestSuites.new.find(options[:suite])

Expand Down Expand Up @@ -156,6 +197,10 @@ def session_data_repo
@session_data_repo ||= Inferno::Repositories::SessionData.new
end

def presets_repo
@presets_repo ||= Inferno::Repositories::Presets.new
end

def test_session
@test_session ||= test_sessions_repo.create({
test_suite_id: suite.id,
Expand All @@ -169,7 +214,7 @@ def create_params(test_session, runnable)
{
test_session_id: test_session.id,
runnable_id_key(runnable) => runnable.id,
inputs: thor_hash_to_inputs_array(options[:inputs])
inputs: thor_hash_to_inputs_array(inputs_and_preset)
}
end

Expand Down
12 changes: 10 additions & 2 deletions lib/inferno/apps/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ def version
option :inputs,
aliases: ['-i'],
type: :hash,
desc: 'Inputs (i.e: --inputs=foo:bar goo:baz)'
desc: 'Inputs (i.e: --inputs=foo:bar goo:baz); will merge and override preset inputs'
option :preset_id,
aliases: ['-P'],
type: :string,
desc: 'Inferno preset id; cannot be used with `--preset-file`'
option :preset_file,
aliases: ['-p'],
type: :string,
desc: 'Path to an Inferno preset file for inputs; cannot be used with `--preset-id`'
option :outputter,
aliases: ['-o'],
default: 'console',
Expand All @@ -142,7 +150,7 @@ def version
desc: 'Display this message'
def execute
Execute.boot_full_inferno
Execute.new.run(options)
Execute.new.run(options.dup) # dup to unfreeze Thor options
end

# https://github.com/rails/thor/issues/244 - Make Thor exit(1) on Errors/Exceptions
Expand Down
1 change: 1 addition & 0 deletions lib/inferno/config/boot/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
end

target_container.start :suites
target_container.start :presets
end
end
26 changes: 26 additions & 0 deletions spec/fixtures/dev_validator_preset_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"title": "Preset for Validator Suite in RSpec",
"id": "dev_validator_preset_2",
"test_suite_id": "dev_validator",
"inputs": [
{
"name": "url",
"value": "https://hapi.fhir.org/baseR4",
"_title": "FHIR Server Base Url",
"_type": "text"
},
{
"name": "access_token",
"value": null,
"_title": "Bearer/Access Token",
"_type": "text",
"_optional": true
},
{
"name": "patient_id",
"value": "1234321",
"_title": "Patient ID",
"_type": "text"
}
]
}
26 changes: 25 additions & 1 deletion spec/inferno/cli/execute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,16 @@
end

let(:inputs) { { 'url' => 'https://example.com', 'patient_id' => '1' } }
let(:preset_id) { 'dev_validator_preset' }
let(:preset_file) { Inferno::Application.root.join('spec/fixtures/dev_validator_preset_2.json').to_s }

it 'works on dev_validator suite' do
before do
stub_request(:post, "#{ENV.fetch('FHIR_RESOURCE_VALIDATOR_URL')}/validate")
.with(query: hash_including({}))
.to_return(status: 200, body: success_outcome.to_json)
end

it 'works on dev_validator suite' do
stub_request(:get, 'https://example.com/Patient/1')
.to_return(status: 200, body: FHIR::Patient.new({ name: { given: 'Smith' } }).to_json)

Expand All @@ -251,5 +255,25 @@
.to raise_error(an_instance_of(SystemExit).and(having_attributes(status: 0)))
end.to output(/.+/).to_stdout
end

it 'works with preset id' do
stub_request(:get, 'https://hapi.fhir.org/baseR4/Patient/1234321')
.to_return(status: 200, body: FHIR::Patient.new({ name: { given: 'Smith' } }).to_json)

expect do
expect { instance.run({ suite:, preset_id:, outputter: 'plain', verbose: true }) }
.to raise_error(an_instance_of(SystemExit).and(having_attributes(status: 0)))
end.to output(/.+/).to_stdout
end

it 'works with preset file' do
stub_request(:get, 'https://hapi.fhir.org/baseR4/Patient/1234321')
.to_return(status: 200, body: FHIR::Patient.new({ name: { given: 'Smith' } }).to_json)

expect do
expect { instance.run({ suite:, preset_file:, outputter: 'plain', verbose: true }) }
.to raise_error(an_instance_of(SystemExit).and(having_attributes(status: 0)))
end.to output(/.+/).to_stdout
end
end
end

0 comments on commit b72c706

Please sign in to comment.