Class: DaVinciCRDTestKit::V201::ClientFHIRApiSearchTest

Inherits:
Inferno::Test
  • Object
show all
Defined in:
lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#successful_searchObject

Returns the value of attribute successful_search.



14
15
16
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 14

def successful_search
  @successful_search
end

Instance Method Details

#bad_resource_id_message(expected_id, actual_id) ⇒ Object



32
33
34
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 32

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

#check_id_search_result_entry(bundle_entry, search_id, entry_resource_type) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 60

def check_id_search_result_entry(bundle_entry, search_id, entry_resource_type)
  assert_resource_type(entry_resource_type, resource: bundle_entry)

  assert bundle_entry.id.present?, "Expected id field in returned #{entry_resource_type} resource"

  assert bundle_entry.id == search_id,
         bad_resource_id_message(search_id, bundle_entry.id)
end

#check_include_reference(base_resource_entry, include_resource_id, include_resource_type) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 86

def check_include_reference(base_resource_entry, include_resource_id, include_resource_type)
  base_resource_references = Array.wrap(get_reference_field(include_resource_type, base_resource_entry)).compact

  assert(base_resource_references.present?, %(
       #{resource_type} resource with id #{base_resource_entry.id} did not include the field that references a
       #{include_resource_type} resource}
       ))

  base_resource_reference_match_found = base_resource_references.any? do |base_resource_reference|
    base_resource_reference.reference_id == include_resource_id
  end

  assert(base_resource_reference_match_found, %(
  The #{resource_type} resource in search result bundle with id #{base_resource_entry.id} did not have a
  #{include_resource_type} reference with an id of `#{include_resource_id}`.`
))
end

#get_reference_field(reference_type, entry) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 143

def get_reference_field(reference_type, entry)
  case reference_type
  when 'patient'
    entry.beneficiary
  when 'practitioner'
    entry.practitioner
  when 'organization'
    if resource_type == 'Encounter'
      entry.serviceProvider
    else
      entry.organization
    end
  when 'location'
    locations = entry.location
    locations.map(&:location)
  end
end

#id_search_result_check(bundle, search_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 69

def id_search_result_check(bundle, search_id)
  warning do
    assert bundle.entry.any?,
           "Search result bundle is empty for #{resource_type} _id search with an id of `#{search_id}`"
  end
  return if bundle.entry.empty?

  self.successful_search = true

  bundle.entry
    .reject { |entry| entry&.resource&.resourceType == 'OperationOutcome' }
    .map(&:resource)
    .each do |resource|
      check_id_search_result_entry(resource, search_id, resource_type)
    end
end

#include_search_result_check(bundle, search_id, included_resource_type) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



104
105
106
107
108
109
110
111
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
139
140
141
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 104

def include_search_result_check(bundle, search_id, included_resource_type) # rubocop:disable Metrics/CyclomaticComplexity
  warning do
    assert bundle.entry.any?,
           "Search result bundle is empty for #{resource_type} _include #{search_type} search with an id
         of `#{search_id}`"
  end
  return if bundle.entry.empty?

  self.successful_search = true

  base_resource_entry_list = bundle.entry.select do |entry|
    entry.resource&.resourceType == resource_type
  end

  assert(base_resource_entry_list.length == 1, %(
  The #{included_resource_type} _include search for #{resource_type} resource with id #{search_id}
  should include exactly 1 #{resource_type} resource, instead got #{base_resource_entry_list.length}.
))

  base_resource_entry = base_resource_entry_list.first.resource

  bundle.entry
    .map(&:resource)
    .each do |resource|
      entry_resource_type = resource.resourceType

      if entry_resource_type == resource_type
        check_id_search_result_entry(resource, search_id, entry_resource_type)
      elsif entry_resource_type != 'OperationOutcome'
        entry_resource_type = included_resource_type.capitalize
        assert_resource_type(entry_resource_type, resource:)

        included_resource_id = resource.id
        assert included_resource_id.present?, "Expected id field in returned #{entry_resource_type} resource"
        check_include_reference(base_resource_entry, included_resource_id, included_resource_type)
      end
    end
end

#include_searchesObject



24
25
26
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 24

def include_searches
  ['organization_include', 'practitioner_include', 'location_include']
end

#perform_fhir_search(search_params, tags) ⇒ Object



36
37
38
39
40
41
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 36

def perform_fhir_search(search_params, tags)
  fhir_search(resource_type, params: search_params, tags:)
  assert_response_status(200)
  assert_resource_type(:bundle)
  resource
end

#reference_search_parametersObject



28
29
30
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 28

def reference_search_parameters
  ['organization', 'practitioner', 'patient']
end

#reference_search_result_check(bundle, reference_id, reference_type) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 161

def reference_search_result_check(bundle, reference_id, reference_type)
  warning do
    assert bundle.entry.any?, %(
         Search result bundle is empty for #{resource_type} #{reference_type} search with a #{reference_type} id
         `#{reference_id}`
  )
  end
  return if bundle.entry.empty?

  self.successful_search = true

  bundle.entry
    .reject { |entry| entry&.resource&.resourceType == 'OperationOutcome' }
    .map(&:resource)
    .each do |resource|
      assert_resource_type(resource_type, resource:)

      entry_reference_field = get_reference_field(reference_type, resource)
      assert(
        entry_reference_field.present?,
        %(
        #{resource_type} resource with id #{resource.id} did not include the field that references
        a #{reference_type} resource
      )
      )

      entry_reference_id = entry_reference_field.reference_id
      assert(
        entry_reference_id == reference_id,
        %(
        The #{resource_type} resource in search result bundle with id #{resource.id} should have a
        #{reference_type} reference with an id of `#{reference_id}`, instead got: `#{entry_reference_id}`
      )
      )
    end
end

#resource_typeObject



16
17
18
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 16

def resource_type
  config.options[:resource_type]
end

#search_typeObject



20
21
22
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 20

def search_type
  config.options[:search_type]
end

#status_search_result_check(bundle, status) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/davinci_crd_test_kit/client/v2.0.1/api/client_fhir_api_search_test.rb', line 43

def status_search_result_check(bundle, status)
  return if bundle.entry.empty?

  self.successful_search = true

  bundle.entry
    .reject { |entry| entry&.resource&.resourceType == 'OperationOutcome' }
    .map(&:resource)
    .each do |resource|
      assert_resource_type(resource_type, resource:)
      assert(resource.status == status, %(
      Each #{resource_type} resource in search result bundle should have a status of `#{status}`, instead got:
      `#{resource.status}` for resource with id: `#{resource.id}`
      ))
    end
end