Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #408 from onc-healthit/fi-567-add-include-medicati…
Browse files Browse the repository at this point in the history
…on-test

FI-567: Add _include Medication test
  • Loading branch information
arscan authored Jan 15, 2020
2 parents 5096942 + 58c015a commit cdec3c4
Show file tree
Hide file tree
Showing 34 changed files with 217 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe '#test_medication_inclusion' do
before do
@sequence = @sequence_class.new(@instance, @client)
@request_with_code = FHIR::MedicationRequest.new(medicationCodeableConcept: { coding: [{ code: 'abc' }] })
@request_with_internal_reference = FHIR::MedicationRequest.new(medicationReference: { reference: '#456' })
@request_with_external_reference = FHIR::MedicationRequest.new(medicationReference: { reference: 'Medication/789' })
@search_params = {
patient: @instance.patient_id,
intent: 'order'
}
@search_params_with_medication = @search_params.merge(_include: 'MedicationRequest:medication')
end

it 'succeeds if no external Medication references are used' do
@sequence.test_medication_inclusion([@request_with_code, @request_with_internal_reference], @search_params)
end

it 'fails if the search including Medications returns a non-success response' do
stub_request(:get, "#{@base_url}/MedicationRequest")
.with(query: @search_params_with_medication)
.to_return(status: 400)

exception = assert_raises(Inferno::AssertionException) do
@sequence.test_medication_inclusion([@request_with_external_reference], @search_params)
end

assert_equal 'Bad response code: expected 200, 201, but found 400. ', exception.message
end

it 'fails if the search including Medications does not return a Bundle' do
stub_request(:get, "#{@base_url}/MedicationRequest")
.with(query: @search_params_with_medication)
.to_return(status: 200, body: FHIR::MedicationRequest.new.to_json)

exception = assert_raises(Inferno::AssertionException) do
@sequence.test_medication_inclusion([@request_with_external_reference], @search_params)
end

assert_equal 'Expected FHIR Bundle but found: MedicationRequest', exception.message
end

it 'fails if no Medications are present in the search results' do
stub_request(:get, "#{@base_url}/MedicationRequest")
.with(query: @search_params_with_medication)
.to_return(status: 200, body: wrap_resources_in_bundle(FHIR::MedicationRequest.new).to_json)

exception = assert_raises(Inferno::AssertionException) do
@sequence.test_medication_inclusion([@request_with_external_reference], @search_params)
end

assert_equal 'No Medications were included in the search results', exception.message
end

it 'succeeds if Medications are present in the search results' do
stub_request(:get, "#{@base_url}/MedicationRequest")
.with(query: @search_params_with_medication)
.to_return(status: 200, body: wrap_resources_in_bundle([@request_with_external_reference, FHIR::Medication.new]).to_json)

@sequence.test_medication_inclusion([@request_with_external_reference], @search_params)
end
end
8 changes: 8 additions & 0 deletions generator/uscore/us_core_unit_test_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def generate(sequence, path, module_name)
class_name = sequence[:class_name]
return if tests[class_name].blank?

if sequence[:resource] == 'MedicationRequest'
tests[class_name] << ERB.new(
File.read(
File.join(__dir__, 'templates', 'unit_tests', 'medication_inclusion_unit_test.rb.erb')
)
).result
end

unit_tests = template.result_with_hash(
class_name: class_name,
tests: tests[class_name],
Expand Down
33 changes: 32 additions & 1 deletion generator/uscore/uscore_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ def create_search_test(sequence, search_param)
including support for these #{param} comparators: #{comparators.keys.join(', ')})
end

if sequence[:resource] == 'MedicationRequest'
search_test[:description] += %(
If any MedicationRequest resources use external references to
Medications, the search will be repeated with
_include=MedicationRequest:medication.
)
end

is_first_search = search_param == find_first_search(sequence)

search_test[:test_code] =
Expand All @@ -282,7 +290,7 @@ def create_search_test(sequence, search_param)
#{get_search_params(search_param[:names], sequence)}
reply = get_resource_by_params(versioned_resource_class('#{sequence[:resource]}'), search_params)
validate_search_reply(versioned_resource_class('#{sequence[:resource]}'), reply, search_params)
assert_response_ok(reply)
#{'test_medication_inclusion(reply.resource.entry.map(&:resource), search_params)' if sequence[:resource] == 'MedicationRequest'}
)
end
comparator_search_code = get_comparator_searches(search_param[:names], sequence)
Expand Down Expand Up @@ -608,6 +616,7 @@ def get_first_search_with_fixed_values(sequence, search_parameters, save_resourc
save_resource_ids_in_bundle(#{save_resource_ids_in_bundle_arguments})
save_delayed_sequence_references(@#{sequence[:resource].underscore}_ary)
validate_search_reply(versioned_resource_class('#{sequence[:resource]}'), reply, search_params)
#{'test_medication_inclusion(@medication_request_ary, search_params)' if sequence[:resource] == 'MedicationRequest'}
break#{' if values_found == 2' if find_two_values}
end
#{skip_if_not_found(sequence)})
Expand Down Expand Up @@ -765,6 +774,28 @@ def validate_resource_item(resource, property, value)
)
end

if sequence[:resource] == 'MedicationRequest'
validate_function += %(
def test_medication_inclusion(medication_requests, search_params)
requests_with_external_references =
medication_requests
.select { |request| request&.medicationReference&.present? }
.reject { |request| request&.medicationReference&.reference&.start_with? '#' }
return if requests_with_external_references.blank?
search_params.merge!(_include: 'MedicationRequest:medication')
response = get_resource_by_params(FHIR::MedicationRequest, search_params)
assert_response_ok(response)
assert_bundle_response(response)
requests_with_medications = fetch_all_bundled_resources(response.resource)
medications = requests_with_medications.select { |resource| resource.resourceType == 'Medication' }
assert medications.present?, 'No Medications were included in the search results'
end
)
end

validate_function
end

Expand Down
5 changes: 1 addition & 4 deletions lib/modules/uscore_v3.1.0/bodyheight_sequence.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/modules/uscore_v3.1.0/bodytemp_sequence.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/modules/uscore_v3.1.0/bodyweight_sequence.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/modules/uscore_v3.1.0/bp_sequence.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/modules/uscore_v3.1.0/headcircum_sequence.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/modules/uscore_v3.1.0/heartrate_sequence.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdec3c4

Please sign in to comment.