Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autocreate / autodelete #4

Closed
wants to merge 8 commits into from
Prev Previous commit
Add autocreate and autodelete
  • Loading branch information
jhlee-mitre committed May 25, 2022
commit e61555a9aa65cc12fb079b8f83bc1adbae3e93c3
8 changes: 4 additions & 4 deletions TestScripts/general_test_script.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@
"fixture": [
{
"id": "fixture-patient-create",
"autocreate": false,
"autodelete": false,
"autocreate": true,
"autodelete": true,
"resource": {
"reference": "Patient/example.json",
"display": "Peter Chalmers"
}
},
{
"id": "fixture-patient-minimum",
"autocreate": false,
"autodelete": false,
"autocreate": true,
"autodelete": true,
"resource": {
"reference": "Patient/example.json",
"display": "Peter Chalmers (minimum)"
Expand Down
3 changes: 3 additions & 0 deletions lib/TestScriptEngine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def execute_runnables runnable_id = nil
if runnables[runnable_id]
puts "\nBeggining execution of #{runnable_id}.\n\n"
reports[runnable_id] = runnable.execute client
runnable.postprocess
puts "\nFinished execution of #{runnable_id}. Score: #{reports[runnable_id].score} \n"
else
FHIR.logger.info "[.execute_runnables] No runnable stored with id [#{runnable_id}]."
Expand All @@ -95,9 +96,11 @@ def execute_runnables runnable_id = nil
runnables.each do |id, runnable|
puts "\nBeggining execution of #{id}.\n\n"
reports[id] = runnable.run client
runnable.postprocess
puts "\nFinished execution of #{id}. Score: #{reports[id].score} \n"
end
end

end

# @path [String] Optional, specifies the path to the folder which the
Expand Down
55 changes: 33 additions & 22 deletions lib/TestScriptRunnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,29 @@ def load_fixtures

hash[fixture.id] = resource
type = resource.resourceType

# Need to consider where to put this code, such as a separate method before setup execition.
if fixture.autocreate
FHIR.logger.info "[.load_fixture] Autocreate Fixture: #{fixture.id}"
script.setup.action.each do |ac|
next if ac.operation == nil
if ac.operation.sourceId == fixture.id
FHIR.logger.warn "Possible duplication of fixture creation (autocreation and setup section)"
FHIR.logger.warn "Check setup in TestScript. Operation type: #{ac.operation.type.code} , sourceId: #{ac.operation.sourceId}"
end
end

script.setup.action.unshift action_create(fixture.id, type) if fixture.autocreate
script.teardown.action << action_delete(fixture.id, type) if fixture.autodelete
begin
client.create(get_resource_from_ref(fixture.resource))
rescue StandardError => e
log_error e.message
report.error e.message
throw :exit
end
end

# script.setup.action.unshift action_create(fixture.id, type) if fixture.autocreate
# script.teardown.action << action_delete(fixture.id, type) if fixture.autodelete
end
end

Expand All @@ -80,25 +100,16 @@ def get_resource_from_ref reference
end
end

def action_delete(sourceId, type)
FHIR::TestScript::Setup::Action.new({
operation: FHIR::TestScript::Setup::Action::Operation.new({
sourceId: sourceId,
resource: type,
local_method: 'delete'
})
})
end

def action_create(sourceId, type)
FHIR::TestScript::Setup::Action.new({
operation: FHIR::TestScript::Setup::Action::Operation.new({
sourceId: sourceId,
resource: type,
local_method: 'delete'
})
})
end
# Failure of postprocessing (autodelete) won't fail test as opposed to failure of autocreate will.
def postprocess
script.fixture.each_with_object({}) do |fixture|
if fixture.autodelete
FHIR.logger.info "[.load_fixture] Autodelete Fixture: #{fixture.id}"
resource = get_resource_from_ref(fixture.resource)
reply = client.destroy(resource.class, resource.id)
end
end
end

def run client = nil
client client
Expand All @@ -117,7 +128,7 @@ def run client = nil
def execute op
return unless op

FHIR.logger.info "[.execute]: #{op.description}"
FHIR.logger.info "[.execute] #{op.description}"

catch :exit do
throw :exit, report.fail('noClient') unless client
Expand Down
5 changes: 3 additions & 2 deletions lib/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
require './TestScriptRunnable' # TODO: Remove

default_engine = TestScriptEngine.new 'http:https://hapi.fhir.org/baseR4'
# 'http:https://test.fhir.org/r4/'
# Load and execute all TestScript in /TestScripts
default_engine.load_scripts
# default_engine.load_scripts
# Specify a TestScript to execute
# default_engine.load_scripts(nil, 'general_test_script.json')
default_engine.load_scripts(nil, 'general_test_script.json')
default_engine.make_runnables
default_engine.execute_runnables
default_engine.write_reports
Expand Down