Module: USQualityCoreTestKit::ReferenceResolutionTest

Extended by:
Forwardable
Includes:
Inferno::DSL::FHIRResourceNavigation
Included in:
USQualityCoreV050::AdverseEventReferenceResolutionTest, USQualityCoreV050::AllergyIntoleranceReferenceResolutionTest, USQualityCoreV050::CarePlanReferenceResolutionTest, USQualityCoreV050::CareTeamReferenceResolutionTest, USQualityCoreV050::ConditionEncounterDiagnosisReferenceResolutionTest, USQualityCoreV050::ConditionProblemsHealthConcernsReferenceResolutionTest, USQualityCoreV050::CoverageReferenceResolutionTest, USQualityCoreV050::DevicerequestReferenceResolutionTest, USQualityCoreV050::DiagnosticReportLabReferenceResolutionTest, USQualityCoreV050::DiagnosticReportNoteReferenceResolutionTest, USQualityCoreV050::DocumentReferenceReferenceResolutionTest, USQualityCoreV050::EncounterReferenceResolutionTest, USQualityCoreV050::GoalReferenceResolutionTest, USQualityCoreV050::HeadOccipitalFrontalCircumferencePercentileReferenceResolutionTest, USQualityCoreV050::ImmunizationReferenceResolutionTest, USQualityCoreV050::ImmunizationnotdoneReferenceResolutionTest, USQualityCoreV050::LocationReferenceResolutionTest, USQualityCoreV050::MedicationadministrationReferenceResolutionTest, USQualityCoreV050::MedicationdispenseReferenceResolutionTest, USQualityCoreV050::MedicationdispensedeclinedReferenceResolutionTest, USQualityCoreV050::MedicationnotrequestedReferenceResolutionTest, USQualityCoreV050::MedicationrequestReferenceResolutionTest, USQualityCoreV050::ObservationClinicalResultReferenceResolutionTest, USQualityCoreV050::ObservationLabReferenceResolutionTest, USQualityCoreV050::ObservationScreeningAssessmentReferenceResolutionTest, USQualityCoreV050::PediatricBmiForAgeReferenceResolutionTest, USQualityCoreV050::PediatricWeightForHeightReferenceResolutionTest, USQualityCoreV050::PractitionerRoleReferenceResolutionTest, USQualityCoreV050::ProcedureReferenceResolutionTest, USQualityCoreV050::ProcedurenotdoneReferenceResolutionTest, USQualityCoreV050::ProvenanceReferenceResolutionTest, USQualityCoreV050::RelatedPersonReferenceResolutionTest, USQualityCoreV050::ServicenotrequestedReferenceResolutionTest, USQualityCoreV050::ServicerequestReferenceResolutionTest, USQualityCoreV050::SimpleObservationReferenceResolutionTest, USQualityCoreV050::SpecimenReferenceResolutionTest, USQualityCoreV050::TaskReferenceResolutionTest, USQualityCoreV050::TaskrejectedReferenceResolutionTest, USQualityCoreV050::UsCoreBloodPressureReferenceResolutionTest, USQualityCoreV050::UsCoreBmiReferenceResolutionTest, USQualityCoreV050::UsCoreBodyHeightReferenceResolutionTest, USQualityCoreV050::UsCoreBodyTemperatureReferenceResolutionTest, USQualityCoreV050::UsCoreBodyWeightReferenceResolutionTest, USQualityCoreV050::UsCoreHeartRateReferenceResolutionTest, USQualityCoreV050::UsCoreObservationOccupationReferenceResolutionTest, USQualityCoreV050::UsCoreObservationPregnancyintentReferenceResolutionTest, USQualityCoreV050::UsCoreObservationPregnancystatusReferenceResolutionTest, USQualityCoreV050::UsCorePulseOximetryReferenceResolutionTest, USQualityCoreV050::UsCoreRespiratoryRateReferenceResolutionTest, USQualityCoreV050::UsCoreSmokingstatusReferenceResolutionTest
Defined in:
lib/us_quality_core_test_kit/reference_resolution_test.rb

Instance Method Summary collapse

Instance Method Details

#must_support_referencesObject



62
63
64
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 62

def must_support_references
  .must_supports[:elements].select { |element_definition| element_definition[:types]&.include?('Reference') }
end

#must_support_references_with_target_profileObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 66

def must_support_references_with_target_profile
  # mapping array of target_profiles to array of {path, target_profile} pair
  must_support_references.map do |element_definition|
    (element_definition[:target_profiles] || ['']).map do |target_profile|
      {
        path: element_definition[:path],
        target_profile: target_profile
      }
    end
  end.flatten
end

#no_resources_skip_messageObject



57
58
59
60
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 57

def no_resources_skip_message
  "No #{resource_type} resources appear to be available. " \
    'Please use patients with more information.'
end

#perform_reference_resolution_test(resources) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 12

def perform_reference_resolution_test(resources)
  skip_if resources.blank?, no_resources_skip_message

  pass if unresolved_references(resources).empty?

  skip "Could not resolve and validate any Must Support references for #{unresolved_references_strings.join(', ')}"
end

#record_resolved_reference(reference, target_profile) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 28

def record_resolved_reference(reference, target_profile)
  saved_reference = resolved_references.find { |item| item[:reference] == reference.reference }

  if saved_reference.present?
    saved_reference[:profiles] << target_profile if target_profile.present? && !saved_reference[:profiles].include?(target_profile)
  else
    saved_reference = {
      reference: reference.reference,
      profiles: []
    }

    saved_reference[:profiles] << target_profile if target_profile.present?
    resolved_references.add(saved_reference)
  end
end

#reference_resolved?(reference, target_profile) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 44

def reference_resolved?(reference, target_profile)
  resolved_references.any? do |item|
    item[:reference] == reference.reference &&
      (
        target_profile.blank? || item[:profiles].include?(target_profile)
      )
  end
end

#resolve_reference(reference) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 140

def resolve_reference(reference)
  reference_type = reference.resource_type
  reference_id = reference.reference_id

  begin
    if reference.relative?
      begin
        reference.resource_class
      rescue NameError
        return nil
      end

      fhir_read(reference_type, reference_id)&.resource
    elsif reference.base_uri.chomp('/') == fhir_client.instance_variable_get(:@base_service_url).chomp('/')
      fhir_read(reference_type, reference_id)&.resource
    else
      get(reference.reference)&.resource
    end
  rescue StandardError => e
    Inferno::Application['logger'].error("Unable to resolve reference #{reference.reference}")
    Inferno::Application['logger'].error(e.full_message)
    nil
  end
end

#resolved_referencesObject



53
54
55
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 53

def resolved_references
  scratch[:resolved_references] ||= Set.new
end

#resource_is_valid_with_target_profile?(resource, target_profile) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
168
169
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 165

def resource_is_valid_with_target_profile?(resource, target_profile)
  return true if target_profile.blank?

  resource_is_valid?(resource:, profile_url: target_profile, add_messages_to_runnable: false)
end

#unresolved_references(resources = []) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 78

def unresolved_references(resources = [])
  @unresolved_references ||=
    must_support_references_with_target_profile.select do |reference_path_profile_pair|
      path = reference_path_profile_pair[:path]
      target_profile = reference_path_profile_pair[:target_profile]

      found_one_reference = false

      resolve_one_reference = resources.any? do |resource|
        value_found = resolve_path(resource, path).select { |value| value.is_a?(FHIR::Reference) }
        next if value_found.empty?

        found_one_reference = true

        value_found.any? do |reference|
          validate_reference_resolution(resource, reference, target_profile)
        end
      end

      found_one_reference && !resolve_one_reference
    end

  if .must_supports[:choices].present?
    @unresolved_references.delete_if do |reference|
      choice_profiles = .must_supports[:choices].find { |choice| choice[:target_profiles]&.include?(reference[:target_profile]) }

      choice_profiles.present? &&
        choice_profiles[:target_profiles]&.any? { |profile| @unresolved_references.none? { |element| element[:target_profile] == profile } }
    end
  end

  @unresolved_references
end

#unresolved_references_stringsObject



20
21
22
23
24
25
26
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 20

def unresolved_references_strings
  unresolved_reference_hash =
    unresolved_references.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |missing, hash|
      hash[missing[:path]] << missing[:target_profile]
    end
  unresolved_reference_hash.map { |path, profiles| "#{path} element: Reference#{"(#{profiles.join('|')})" unless profiles.first.empty?}" }
end

#validate_reference_resolution(resource, reference, target_profile) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/us_quality_core_test_kit/reference_resolution_test.rb', line 112

def validate_reference_resolution(resource, reference, target_profile)
  return true if reference_resolved?(reference, target_profile)

  if reference.contained?
    # if reference_id is blank it is referring to itself, so we know it exists
    return true if reference.reference_id.blank?

    return resource.contained.any? do |contained_resource|
      contained_resource&.id == reference.reference_id &&
      resource_is_valid_with_target_profile?(contained_resource, target_profile)
    end
  end

  reference_type = reference.resource_type
  reference_id = reference.reference_id

  resolved_resource = resolve_reference(reference)

  return false if resolved_resource.nil?

  return false unless resolved_resource.resourceType == reference_type && resolved_resource.id == reference_id

  return false unless resource_is_valid_with_target_profile?(resolved_resource, target_profile)

  record_resolved_reference(reference, target_profile)
  true
end