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

MedicationReference Checking #99

Merged
merged 8 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions lib/app/sequence_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ def self.test(name, &block)
result.message = e.message
result.details = e.details

rescue PassException => e
result.result = STATUS[:pass]
result.message = e.message

rescue TodoException => e
result.result = STATUS[:todo]
result.message = e.message
Expand Down Expand Up @@ -437,6 +441,10 @@ def todo(message = "")
raise TodoException.new message
end

def pass(message = "")
raise PassException.new message
end

def skip(message = "", details = nil)
raise SkipException.new message, details
end
Expand Down Expand Up @@ -528,12 +536,17 @@ def save_resource_ids_in_bundle(klass, reply)

def validate_read_reply(resource, klass)
assert !resource.nil?, "No #{klass.name.split(':').last} resources available from search."
id = resource.try(:id)
assert !id.nil?, "#{klass} id not returned"
read_response = @client.read(klass, id)
assert_response_ok read_response
assert !read_response.resource.nil?, "Expected valid #{klass} resource to be present"
assert read_response.resource.is_a?(klass), "Expected resource to be valid #{klass}"
if resource.is_a? FHIR::DSTU2::Reference
read_response = resource.read
else
id = resource.try(:id)
assert !id.nil?, "#{klass} id not returned"
read_response = @client.read(klass, id)
assert_response_ok read_response
read_response = read_response.resource
end
assert !read_response.nil?, "Expected valid #{klass} resource to be present"
assert read_response.is_a?(klass), "Expected resource to be valid #{klass}"
end

def validate_history_reply(resource, klass)
Expand Down Expand Up @@ -605,7 +618,29 @@ def test_resources_against_profile(resource_type, specified_profile=nil)
assert(all_errors.empty?, all_errors.join("<br/>\n"))
end

def check_resource_against_profile(resource, resource_type, specified_profile=nil)
assert resource.is_a?("FHIR::DSTU2::#{resource_type}".constantize),
"Expected resource to be of type #{resource_type}"

p = Inferno::ValidationUtil.guess_profile(resource)
if specified_profile
return unless p.url == specified_profile
end

if p
@profiles_encountered << p.url
@profiles_encountered.uniq!
errors = p.validate_resource(resource)
unless errors.empty?
errors.map!{|e| "#{resource_type}/#{resource.id}: #{e}"}
@profiles_failed[p.url] = [] unless @profiles_failed[p.url]
@profiles_failed[p.url].concat(errors)
end
else
errors = entry.resource.validate
end
assert(errors.empty?, errors.join("<br/>\n"))
end

# This is intended to be called on SequenceBase
# There is a test to ensure that this doesn't fall out of date
Expand Down
138 changes: 0 additions & 138 deletions lib/app/sequences/08k_argonaut_medication_sequence.rb

This file was deleted.

40 changes: 31 additions & 9 deletions lib/app/sequences/08l_argonaut_medication_order_sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ class ArgonautMedicationOrderSequence < SequenceBase
reply = get_resource_by_params(FHIR::DSTU2::MedicationOrder, patient: @instance.patient_id)
assert_bundle_response(reply)

@no_resources_found = false
resource_count = reply.try(:resource).try(:entry).try(:length) || 0
@no_resources_found = true if resource_count === 0
@no_resources_found = (resource_count == 0)

skip 'No resources appear to be available for this patient. Please use patients with more information.' if @no_resources_found

@medication_orders = reply&.resource&.entry.map do |med_order|
@medication_orders = reply&.resource&.entry&.map do |med_order|
med_order&.resource
end
validate_search_reply(FHIR::DSTU2::MedicationOrder, reply)
Expand Down Expand Up @@ -143,7 +142,7 @@ class ArgonautMedicationOrderSequence < SequenceBase
test_resources_against_profile('MedicationOrder')
end

test 'Referenced Medications conform to the Argonaut profile' do
test 'Referenced Medications support read interactions' do
metadata do
id '07'
link 'https://www.fhir.org/guides/argonaut/r2/StructureDefinition-argo-medication.html'
Expand All @@ -152,16 +151,39 @@ class ArgonautMedicationOrderSequence < SequenceBase
)
end

medication_references = @medication_orders&.select do |medication_order|
medication_order&.medicationReference unless medication_order.medicationReference.nil?
@medication_references = @medication_orders&.select do |medication_order|
!medication_order.medicationReference.nil?
end&.map do |ref|
ref.medicationReference
end

skip 'No medicationReferences available to test' if medication_references.empty?
pass 'Test passes because medication resource references are not used in any medication orders.' if @medication_references.nil? || @medication_references.empty?

not_contained_refs = @medication_references&.select {|ref| !ref.contained?}

medication_references&.each do |medication|
pass 'Test passes because all medication resource references are contained within the medication orders.' if not_contained_refs.empty?

not_contained_refs&.each do |medication|
validate_read_reply(medication, FHIR::DSTU2::Medication)
end
end

test 'Referenced Medications conform to the Argonaut profile' do
metadata do
id '08'
link 'https://www.fhir.org/guides/argonaut/r2/StructureDefinition-argo-medication.html'
desc %(
Medication resources must conform to the Argonaut profile
)
end

pass 'Test passes because medication resource references are not used in any medication orders.' if @medication_references.nil? || @medication_references.empty?

@medication_references&.each do |medication|
medication_resource = medication.read
check_resource_against_profile(medication_resource, 'Medication')
end
end
end
end
end
end
Loading