Module: Inferno::DSL::Assertions

Defined in:
lib/inferno/dsl/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert(test, message = '') ⇒ Object

Make an assertion

Parameters:

  • test

    a value whose truthiness will determine whether the assertion passes or fails

  • message (String) (defaults to: '')

    failure message

Raises:



11
12
13
# File 'lib/inferno/dsl/assertions.rb', line 11

def assert(test, message = '')
  raise Exceptions::AssertionException, message unless test
end

#assert_resource_type(resource_type, resource: self.resource) ⇒ Object

Check a FHIR resource's type

Examples:

# The resource type can be a symbol, String, or FHIR::Model class
assert_resource_type(:capability_statement)
assert_resource_type('CapabilityStatement')
assert_resource_type(FHIR::CapabilityStatement)

Parameters:

  • resource_type (String, Symbol, Class)
  • resource (FHIR::Model) (defaults to: self.resource)


43
44
45
46
47
48
# File 'lib/inferno/dsl/assertions.rb', line 43

def assert_resource_type(resource_type, resource: self.resource)
  resource_type_name = normalize_resource_type(resource_type)

  assert resource&.resourceType == resource_type_name,
         bad_resource_type_message(resource_type_name, resource&.resourceType)
end

#assert_response_content_type(type, request: self.request) ⇒ Object



162
163
164
165
166
167
# File 'lib/inferno/dsl/assertions.rb', line 162

def assert_response_content_type(type, request: self.request)
  header = request.response_header('Content-Type')
  assert header.present?, no_content_type_message

  assert header.value.start_with?(type), bad_content_type_message(type, header.value)
end

#assert_response_status(status, response: self.response) ⇒ Object

Check an response's status

Parameters:

  • status (Integer, Array<Integer>)

    a single integer or an array of integer status codes

  • response (Hash) (defaults to: self.response)


25
26
27
# File 'lib/inferno/dsl/assertions.rb', line 25

def assert_response_status(status, response: self.response)
  assert Array.wrap(status).include?(response[:status]), bad_response_status_message(status, response[:status])
end

#assert_valid_bundle_entries(bundle: resource, resource_types: {}) ⇒ Object

Validate each entry of a Bundle

[String,Symbol,FHIR::Model,Array<String,Symbol,FHIR::Model>,Hash] If a string, symbol, or FHIR::Model is provided, only that resource type will be validated. If an array of strings is provided, only those resource types will be validated. If a hash is provided with resource types as keys and profile urls (or nil) as values, only those resource types will be validated against the provided profile url or the base resource if nil.

Examples:

# Only validate Patient bundle entries
assert_valid_bundle_entries(resource_types: 'Patient')

# Only valiadte Patient and Condition bundle entries
assert_valid_bundle_entries(resource_types: ['Patient', 'Condition'])

# Only validate Patient and Condition bundle entries. Validate Patient
# resources against the given profile, and Codition resources against the
# base FHIR Condition resource.
assert_valid_bundle_entries(
  resource_types: {
    'Patient': 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient',
    'Condition': nil
  }
)

Parameters:

  • bundle (FHIR::Bundle) (defaults to: resource)
  • resource_types (defaults to: {})


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/inferno/dsl/assertions.rb', line 92

def assert_valid_bundle_entries(bundle: resource, resource_types: {})
  assert_resource_type('Bundle', resource: bundle)

  types_to_check = normalize_types_to_check(resource_types)

  invalid_resources =
    bundle
      .entry
      .map(&:resource)
      .select { |resource| types_to_check.empty? || types_to_check.include?(resource.resourceType) }
      .reject do |resource|
        validation_params = { resource: resource }
        profile = types_to_check[resource.resourceType]
        validation_params[:profile_url] = profile if profile

        resource_is_valid?(**validation_params)
      end

  assert invalid_resources.empty?, invalid_bundle_entries_message(invalid_resources)
end

#assert_valid_http_uri(uri, message = '') ⇒ Object

Check for a valid http/https uri

Parameters:

  • uri (String)
  • message (String) (defaults to: '')

    custom failure message



157
158
159
160
# File 'lib/inferno/dsl/assertions.rb', line 157

def assert_valid_http_uri(uri, message = '')
  error_message = message.presence || "\"#{uri}\" is not a valid URI"
  assert uri =~ /\A#{URI::DEFAULT_PARSER.make_regexp(['http', 'https'])}\z/, error_message
end

#assert_valid_json(maybe_json_string, message = '') ⇒ Object

Check for valid JSON

Parameters:

  • maybe_json_string (String)
  • message (String) (defaults to: '')

    extra failure message



147
148
149
150
151
# File 'lib/inferno/dsl/assertions.rb', line 147

def assert_valid_json(maybe_json_string, message = '')
  assert JSON.parse(maybe_json_string)
rescue JSON::ParserError
  assert false, "Invalid JSON. #{message}"
end

#assert_valid_resource(resource: self.resource, profile_url: nil) ⇒ Object

Validate a FHIR resource

Parameters:

  • resource (FHIR::Model) (defaults to: self.resource)
  • profile_url (String) (defaults to: nil)

    url of the profile to validate against, defaults to validating against the base FHIR resource



60
61
62
63
# File 'lib/inferno/dsl/assertions.rb', line 60

def assert_valid_resource(resource: self.resource, profile_url: nil)
  assert resource_is_valid?(resource: resource, profile_url: profile_url),
         invalid_resource_message(profile_url)
end

#bad_content_type_message(expected, received) ⇒ Object



173
174
175
# File 'lib/inferno/dsl/assertions.rb', line 173

def bad_content_type_message(expected, received)
  "Expected `Content-Type` to be `#{expected}`, but found `#{received}`"
end

#no_content_type_messageObject



169
170
171
# File 'lib/inferno/dsl/assertions.rb', line 169

def no_content_type_message
  'Response did not contain a `Content-Type` header.'
end