Module: PacioInfernoCore::ReadTest

Defined in:
lib/pacio_inferno_core/read_test.rb

Instance Method Summary collapse

Instance Method Details

#all_scratch_resourcesObject



3
4
5
# File 'lib/pacio_inferno_core/read_test.rb', line 3

def all_scratch_resources
  scratch_resources[:all] ||= []
end

#bad_resource_id_message(expected_id) ⇒ Object



95
96
97
# File 'lib/pacio_inferno_core/read_test.rb', line 95

def bad_resource_id_message(expected_id)
  "Expected resource to have id: `#{expected_id.inspect}`, but found `#{resource.id.inspect}`"
end

#no_resources_skip_messageObject



90
91
92
93
# File 'lib/pacio_inferno_core/read_test.rb', line 90

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

#perform_read_test(resources, _reply_handler = nil, delayed_reference: false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pacio_inferno_core/read_test.rb', line 7

def perform_read_test(resources, _reply_handler = nil, delayed_reference: false)
  skip_if resources.blank?, no_resources_skip_message

  resources_to_read = if delayed_reference
                        readable_references(resources)
                      else
                        readable_resources(resources)
                      end

  assert resources_to_read.present?, "No #{resource_type} id found."

  if config.options[:read_all_resources]
    if delayed_reference
      all_referencing_resources = referencing_resources(resources_to_read)
      info %(
        The #{resource_type} references used for this test were pulled from the following resources:
        #{all_referencing_resources}
      )

      resources_to_read.map! { |resource| resource[:reference] }
    end

    resources_to_read.each do |resource|
      read_and_validate(resource)
    end
  else
    first_resource = resources_to_read.first
    if delayed_reference.present?
      info %(
        The #{resource_type} reference used for this test was pulled from resource
        #{first_resource[:referencing_resource]}
      )
      first_resource = first_resource[:reference]
    end
    read_and_validate(first_resource)
  end
end

#read_and_validate(resource_to_read) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pacio_inferno_core/read_test.rb', line 72

def read_and_validate(resource_to_read)
  id = resource_id(resource_to_read)

  fhir_read resource_type, id

  assert_response_status(200)
  assert_resource_type(resource_type)
  assert resource.id.present? && resource.id == id, bad_resource_id_message(id)

  return unless resource_to_read.is_a? FHIR::Reference

  all_scratch_resources << resource
end

#readable_references(resources) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pacio_inferno_core/read_test.rb', line 51

def readable_references(resources)
  resources
    .filter_map do |resource|
      next unless resource[:reference].present? && resource[:reference].is_a?(FHIR::Reference)

      reference_id = resource[:reference].reference&.split('/')&.last
      next unless reference_id&.present?

      resource
    end
    .uniq { |resource| resource[:reference].reference.split('/').last }
end

#readable_resources(resources) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/pacio_inferno_core/read_test.rb', line 64

def readable_resources(resources)
  resources
    .select { |resource| resource.is_a?(resource_class) || resource.is_a?(FHIR::Reference) }
    .select { |resource| (resource.is_a?(FHIR::Reference) ? resource.reference.split('/').last : resource.id).present? }
    .compact
    .uniq { |resource| resource.is_a?(FHIR::Reference) ? resource.reference.split('/').last : resource.id }
end

#referencing_resources(readable_resources) ⇒ Object



45
46
47
48
49
# File 'lib/pacio_inferno_core/read_test.rb', line 45

def referencing_resources(readable_resources)
  readable_resources
    .map { |resource| resource[:referencing_resource] }
    .join(', ')
end

#resource_classObject



99
100
101
# File 'lib/pacio_inferno_core/read_test.rb', line 99

def resource_class
  FHIR.const_get(resource_type)
end

#resource_id(resource) ⇒ Object



86
87
88
# File 'lib/pacio_inferno_core/read_test.rb', line 86

def resource_id(resource)
  resource.is_a?(FHIR::Reference) ? resource.reference.split('/').last : resource.id
end