Skip to content

Commit

Permalink
Add create and update tests for QuestionnaireResponse and Task
Browse files Browse the repository at this point in the history
  • Loading branch information
elsaperelli committed Nov 23, 2024
1 parent 3f9d03a commit 9b34e4e
Show file tree
Hide file tree
Showing 11 changed files with 1,247 additions and 0 deletions.
20 changes: 20 additions & 0 deletions config/presets/inferno_dtr_server_suite.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions lib/davinci_dtr_test_kit/create_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module DaVinciDTRTestKit
module CreateTest
def perform_create_test(create_resources, resource_type)
assert_valid_json(create_resources)
create_resources_list = JSON.parse(create_resources)
skip_if(!create_resources_list.is_a?(Array), 'Resources to create not inputted in list format, skipping test.')

valid_create_resources =
create_resources_list
.compact_blank
.map { |resource| FHIR.from_contents(resource.to_json) }
.select { |resource| resource.resourceType == resource_type }
.select { |resource| resource_is_valid?(resource:) }

skip_if(valid_create_resources.blank?,
%(No valid #{resource_type} resources were provided to send in Create requests, skipping test.))

valid_create_resources.each do |create_resource|
fhir_create(create_resource)
assert_response_status(201)
end
end
end
end
8 changes: 8 additions & 0 deletions lib/davinci_dtr_test_kit/dtr_light_ehr_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require_relative 'profiles/questionnaire_response/questionnaire_response_context_search'
require_relative 'profiles/questionnaire_response/questionnaire_response_read'
require_relative 'profiles/questionnaire_response/questionnaire_response_validation'
require_relative 'profiles/questionnaire_response/questionnaire_response_create'
require_relative 'profiles/questionnaire_response/questionnaire_response_update'
require_relative 'profiles/coverage/coverage_read'
require_relative 'profiles/coverage/coverage_validation'
require_relative 'profiles/communication_request/communication_request_read'
Expand All @@ -25,6 +27,8 @@
require_relative 'profiles/service_request/service_request_validation'
require_relative 'profiles/task/task_read'
require_relative 'profiles/task/task_validation'
require_relative 'profiles/task/task_create'
require_relative 'profiles/task/task_update'
require_relative 'profiles/vision_prescription/vision_prescription_read'
require_relative 'profiles/vision_prescription/vision_prescription_validation'
require 'smart_app_launch/smart_stu1_suite'
Expand Down Expand Up @@ -136,6 +140,8 @@ class DTRLightEHRSuite < Inferno::TestSuite
test from: :questionnaire_response_context_search
test from: :questionnaire_response_read
test from: :questionnaire_response_validation
test from: :questionnaire_response_create
test from: :questionnaire_response_update
end

group do
Expand Down Expand Up @@ -216,6 +222,8 @@ class DTRLightEHRSuite < Inferno::TestSuite

test from: :task_read
test from: :task_validation
test from: :task_create
test from: :task_update
end

group do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../../create_test'

module DaVinciDTRTestKit
class QuestionnaireResponseCreateTest < Inferno::Test
include DaVinciDTRTestKit::CreateTest

title 'Server is capable of creating a QuestionnaireResponse resource from QuestionnaireResponse create interaction'
description 'A sever SHALL support the QuestionnaireResponse create interaction'

id :questionnaire_response_create
input :create_questionnaire_resources

def resource_type
'QuestionnaireResponse'
end

run do
perform_create_test(create_questionnaire_resources, resource_type)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../../update_test'

module DaVinciDTRTestKit
class QuestionnaireResponseUpdateTest < Inferno::Test
include DaVinciDTRTestKit::UpdateTest

title 'Server is capable of updating a QuestionnaireResponse resource from QuestionnaireResponse update interaction'
description 'A server SHALL support the QuestionnaireResponse update interaction'

id :questionnaire_response_update
input :update_questionnaire_resources

def resource_type
'QuestionnaireResponse'
end

run do
perform_update_test(update_questionnaire_resources, resource_type)
end
end
end
21 changes: 21 additions & 0 deletions lib/davinci_dtr_test_kit/profiles/task/task_create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../../create_test'

module DaVinciDTRTestKit
class TaskCreateTest < Inferno::Test
include DaVinciDTRTestKit::CreateTest

title 'Server is capable of creating a Task resource from Task create interaction'
description 'A server SHOULD support the Task create interaction'

id :task_create
input :create_task_resources

def resource_type
'Task'
end

run do
perform_create_test(create_task_resources, resource_type)
end
end
end
21 changes: 21 additions & 0 deletions lib/davinci_dtr_test_kit/profiles/task/task_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../../update_test'

module DaVinciDTRTestKit
class TaskUpdateTest < Inferno::Test
include DaVinciDTRTestKit::UpdateTest

title 'Server is capable of updating a Task resource from Task update interaction'
description 'A server SHOULD support the Task update interaction'

id :task_update
input :update_task_resources

def resource_type
'Task'
end

run do
perform_update_test(update_task_resources, resource_type)
end
end
end
24 changes: 24 additions & 0 deletions lib/davinci_dtr_test_kit/update_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module DaVinciDTRTestKit
module UpdateTest
def perform_update_test(update_resources, resource_type)
assert_valid_json(update_resources)
update_resources_list = JSON.parse(update_resources)
skip_if(!update_resources_list.is_a?(Array), 'Resources to update not inputted in list format, skipping test.')

valid_update_resources =
update_resources_list
.compact_blank
.map { |resource| FHIR.from_contents(resource.to_json) }
.select { |resource| resource.resourceType == resource_type }
.select { |resource| resource_is_valid?(resource:) }

skip_if(valid_update_resources.blank?,
%(No valid #{resource_type} resources were provided to send in Update requests, skipping test.))

valid_update_resources.each do |update_resource|
fhir_update(update_resource, update_resource.id)
assert_response_status([200, 201])
end
end
end
end
175 changes: 175 additions & 0 deletions spec/davinci_dtr_test_kit/create_test_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
RSpec.describe DaVinciDTRTestKit::CreateTest do
let(:validator_url) { ENV.fetch('FHIR_RESOURCE_VALIDATOR_URL') }
let(:suite) { Inferno::Repositories::TestSuites.new.find('dtr_light_ehr') }
let(:session_data_repo) { Inferno::Repositories::SessionData.new }
let(:test_session) { repo_create(:test_session, test_suite_id: suite.id) }
let(:server_endpoint) { 'http://example.com' }

def run(runnable, inputs = {})
test_run_params = { test_session_id: test_session.id }.merge(runnable.reference_hash)
test_run = Inferno::Repositories::TestRuns.new.create(test_run_params)
inputs.each do |name, value|
session_data_repo.save(
test_session_id: test_session.id,
name:,
value:,
type: runnable.config.input_type(name)
)
end
Inferno::TestRunner.new(test_session:, test_run:).run(runnable)
end

describe 'Behavior of create test' do
let(:create_test) do
Class.new(Inferno::Test) do
include DaVinciDTRTestKit::CreateTest

fhir_resource_validator do
url ENV.fetch('FHIR_RESOURCE_VALIDATOR_URL', 'http://hl7_validator_service:3500')

cli_context do
txServer nil
displayWarnings true
disableDefaultResourceFetcher true
end

igs('hl7.fhir.us.davinci-dtr', 'hl7.fhir.us.core')
end

fhir_client do
url :server_endpoint
end

input :server_endpoint
input :create_resources

def resource_type
'QuestionnaireResponse'
end

run do
perform_create_test(create_resources, resource_type)
end
end
end

let(:operation_outcome_success) do
{
outcomes: [{
issues: []
}],
sessionId: 'b8cf5547-1dc7-4714-a797-dc2347b93fe2'
}
end

let(:operation_outcome_failure) do
{
outcomes: [{
issues: [{
level: 'ERROR'
}]
}],
sessionId: 'b8cf5547-1dc7-4714-a797-dc2347b93fe2'
}
end

let(:create_resources) do
JSON.parse(
File.read(File.join(
__dir__, '..', 'fixtures', 'questionnaire_response_create_update_example.json'
))
)
end

let(:encounter_resources) do
[FHIR::Encounter.new(
status: 'finished',
period: {
start: '2021-12-08T16:35:11.000Z',
end: '2022-02-07T17:51:00.000Z'
},
subject: {
reference: 'Patient/pat001'
}
)].to_json
end

before do
Inferno::Repositories::Tests.new.insert(create_test)
end

it 'passes if a 201 is received' do
validation_request = stub_request(:post, "#{validator_url}/validate")
.to_return(status: 200, body: operation_outcome_success.to_json)
questionnaire_response_create_request = stub_request(:post, "#{server_endpoint}/QuestionnaireResponse")
.to_return(status: 201, body: create_resources.to_json)

result = run(create_test, create_resources: create_resources.to_json, server_endpoint:)
expect(result.result).to eq('pass')
expect(validation_request).to have_been_made
expect(questionnaire_response_create_request).to have_been_made
end

it 'fails if the json is invalid' do
result = run(create_test, create_resources: '[[', server_endpoint:)
expect(result.result).to eq('fail')
end

it 'skips if the create_resources input is not an Array' do
result = run(create_test, create_resources: create_resources[0].to_json, server_endpoint:)
expect(result.result).to eq('skip')
expect(result.result_message).to eq(
'Resources to create not inputted in list format, skipping test.'
)
end

it 'skips if create_resources input is empty' do
result = run(create_test, create_resources: [], server_endpoint:)
expect(result.result).to eq('skip')
expect(result.result_message).to eq(
"Input 'create_resources' is nil, skipping test."
)
end

it 'skips if empty resource json is inputted' do
result = run(create_test, create_resources: [{}].to_json, server_endpoint:)
expect(result.result).to eq('skip')
expect(result.result_message).to eq(
'No valid QuestionnaireResponse resources were provided to send in Create requests, skipping test.'
)
end

it 'skips if inputted resource is the wrong resource type' do
result = run(create_test, create_resources: encounter_resources, server_endpoint:)
expect(result.result).to eq('skip')
expect(result.result_message).to eq(
'No valid QuestionnaireResponse resources were provided to send in Create requests, skipping test.'
)
end

it 'skips if passed in QuestionnaireResponse resource is invalid' do
validation_request = stub_request(:post, "#{validator_url}/validate")
.to_return(status: 200, body: operation_outcome_failure.to_json)

result = run(create_test, create_resources: create_resources.to_json, server_endpoint:)
expect(result.result).to eq('skip')
expect(result.result_message).to eq(
'No valid QuestionnaireResponse resources were provided to send in Create requests, skipping test.'
)
expect(validation_request).to have_been_made
end

it 'fails if QuestionnaireResponse creation interaction returns non 201' do
validation_request = stub_request(:post, "#{validator_url}/validate")
.to_return(status: 200, body: operation_outcome_success.to_json)
questionnaire_response_create_request = stub_request(:post, "#{server_endpoint}/QuestionnaireResponse")
.to_return(status: 400)

result = run(create_test, create_resources: create_resources.to_json, server_endpoint:)
expect(result.result).to eq('fail')
expect(result.result_message).to eq('Unexpected response status: expected 201, but received 400')
expect(validation_request).to have_been_made
expect(questionnaire_response_create_request).to have_been_made
end
end
end
Loading

0 comments on commit 9b34e4e

Please sign in to comment.