Module: Inferno::DSL::Assertions
- Included in:
- Entities::TestGroup, Entities::TestSuite
- Defined in:
- lib/inferno/dsl/assertions.rb
Overview
This module contains the assertions used within tests to verify the behavior of the systems under test. Failing an assertion causes a test to immediately stop execution and receive a ‘fail` result. Additional assertions added to this module will be available in all tests.
Instance Method Summary collapse
-
#assert(test, message = '') ⇒ void
Make an assertion.
-
#assert_must_support_elements_present(resources, profile_url, validator_name: :default, metadata: nil, requirement_extension: nil) {|Metadata| ... } ⇒ void
Check that all Must Support elements defined on the given profile are present in the given resources.
-
#assert_resource_type(resource_type, resource: self.resource) ⇒ void
Check a FHIR resource’s type.
-
#assert_response_content_type(type, request: self.request) ⇒ void
Check the Content-Type header of a response.
-
#assert_response_status(status, request: self.request, response: nil) ⇒ void
Check a response’s status.
-
#assert_valid_bundle_entries(bundle: resource, resource_types: {}) ⇒ void
Validate each entry of a Bundle.
-
#assert_valid_http_uri(uri, message = '') ⇒ void
Check for a valid http/https uri.
-
#assert_valid_json(maybe_json_string, message = '') ⇒ void
Check for valid JSON.
-
#assert_valid_resource(resource: self.resource, profile_url: nil, validator: :default) ⇒ void
Validate a FHIR resource.
- #bad_content_type_message(expected, received) ⇒ Object
- #bad_resource_type_message(expected, received) ⇒ Object
- #bad_response_status_message(expected, received) ⇒ Object
- #invalid_bundle_entries_message(invalid_resources) ⇒ Object
- #invalid_resource_message(resource, profile_url) ⇒ Object
- #missing_must_support_elements_message(missing_elements, resources) ⇒ Object
- #no_content_type_message ⇒ Object
- #normalize_resource_type(resource_type) ⇒ Object
- #normalize_types_to_check(resource_types) ⇒ Object
Instance Method Details
#assert(test, message = '') ⇒ void
This method returns an undefined value.
Make an assertion
16 17 18 |
# File 'lib/inferno/dsl/assertions.rb', line 16 def assert(test, = '') raise Exceptions::AssertionException, unless test end |
#assert_must_support_elements_present(resources, profile_url, validator_name: :default, metadata: nil, requirement_extension: nil) {|Metadata| ... } ⇒ void
This method returns an undefined value.
Check that all Must Support elements defined on the given profile are present in the given resources. Must Support elements are identified on the profile StructureDefinition and pre-parsed into metadata, which may be customized prior to the check by passing a block. Alternate metadata may be provided directly. Set test suite config flag debug_must_support_metadata: true to log the metadata to a file for debugging.
215 216 217 218 219 220 |
# File 'lib/inferno/dsl/assertions.rb', line 215 def assert_must_support_elements_present(resources, profile_url, validator_name: :default, metadata: nil, requirement_extension: nil, &) missing_elements = missing_must_support_elements(resources, profile_url, validator_name:, metadata:, requirement_extension:, &) assert missing_elements.empty?, (missing_elements, resources) end |
#assert_resource_type(resource_type, resource: self.resource) ⇒ void
This method returns an undefined value.
Check a FHIR resource’s type
52 53 54 55 56 57 |
# File 'lib/inferno/dsl/assertions.rb', line 52 def assert_resource_type(resource_type, resource: self.resource) resource_type_name = normalize_resource_type(resource_type) assert resource&.resourceType == resource_type_name, (resource_type_name, resource&.resourceType) end |
#assert_response_content_type(type, request: self.request) ⇒ void
This method returns an undefined value.
Check the Content-Type header of a response. This assertion will fail if the response’s content type does not begin with the provided type.
185 186 187 188 189 190 |
# File 'lib/inferno/dsl/assertions.rb', line 185 def assert_response_content_type(type, request: self.request) header = request.response_header('Content-Type') assert header.present?, assert header.value.start_with?(type), (type, header.value) end |
#assert_response_status(status, request: self.request, response: nil) ⇒ void
This method returns an undefined value.
Check a response’s status
32 33 34 35 |
# File 'lib/inferno/dsl/assertions.rb', line 32 def assert_response_status(status, request: self.request, response: nil) response ||= request&.response assert Array.wrap(status).include?(response[:status]), (status, response[:status]) end |
#assert_valid_bundle_entries(bundle: resource, resource_types: {}) ⇒ void
This method returns an undefined value.
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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/inferno/dsl/assertions.rb', line 107 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: } profile = types_to_check[resource.resourceType] validation_params[:profile_url] = profile if profile resource_is_valid?(**validation_params) end assert invalid_resources.empty?, (invalid_resources) end |
#assert_valid_http_uri(uri, message = '') ⇒ void
This method returns an undefined value.
Check for a valid http/https uri
174 175 176 177 |
# File 'lib/inferno/dsl/assertions.rb', line 174 def assert_valid_http_uri(uri, = '') = .presence || "\"#{uri}\" is not a valid URI" assert uri =~ /\A#{URI::DEFAULT_PARSER.make_regexp(['http', 'https'])}\z/, end |
#assert_valid_json(maybe_json_string, message = '') ⇒ void
This method returns an undefined value.
Check for valid JSON
163 164 165 166 167 |
# File 'lib/inferno/dsl/assertions.rb', line 163 def assert_valid_json(maybe_json_string, = '') assert JSON.parse(maybe_json_string) rescue JSON::ParserError assert false, "Invalid JSON. #{}" end |
#assert_valid_resource(resource: self.resource, profile_url: nil, validator: :default) ⇒ void
This method returns an undefined value.
Validate a FHIR resource
74 75 76 77 |
# File 'lib/inferno/dsl/assertions.rb', line 74 def assert_valid_resource(resource: self.resource, profile_url: nil, validator: :default) assert resource_is_valid?(resource:, profile_url:, validator:), (resource, profile_url) end |
#bad_content_type_message(expected, received) ⇒ Object
198 199 200 |
# File 'lib/inferno/dsl/assertions.rb', line 198 def (expected, received) "Expected `Content-Type` to be `#{expected}`, but found `#{received}`" end |
#bad_resource_type_message(expected, received) ⇒ Object
38 39 40 |
# File 'lib/inferno/dsl/assertions.rb', line 38 def (expected, received) "Unexpected resource type: expected #{expected}, but received #{received}" end |
#bad_response_status_message(expected, received) ⇒ Object
21 22 23 |
# File 'lib/inferno/dsl/assertions.rb', line 21 def (expected, received) "Unexpected response status: expected #{Array.wrap(expected).join(', ')}, but received #{received}" end |
#invalid_bundle_entries_message(invalid_resources) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/inferno/dsl/assertions.rb', line 129 def (invalid_resources) identifier_strings = invalid_resources .map { |resource| "#{resource.resourceType}##{resource.id}" } .join(', ') "The following bundle entries are invalid: #{identifier_strings}" end |
#invalid_resource_message(resource, profile_url) ⇒ Object
60 61 62 63 64 |
# File 'lib/inferno/dsl/assertions.rb', line 60 def (resource, profile_url) return "Resource does not conform to the profile: #{profile_url}" if profile_url.present? "Resource does not conform to the base #{resource&.resourceType} profile." end |
#missing_must_support_elements_message(missing_elements, resources) ⇒ Object
223 224 225 226 |
# File 'lib/inferno/dsl/assertions.rb', line 223 def (missing_elements, resources) "Could not find #{missing_elements.join(', ')} in the #{resources.length} " \ 'provided resource(s)' end |
#no_content_type_message ⇒ Object
193 194 195 |
# File 'lib/inferno/dsl/assertions.rb', line 193 def 'Response did not contain a `Content-Type` header.' end |
#normalize_resource_type(resource_type) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/inferno/dsl/assertions.rb', line 138 def normalize_resource_type(resource_type) if resource_type.is_a? Class resource_type.name.demodulize else resource_type.to_s.camelize end end |
#normalize_types_to_check(resource_types) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/inferno/dsl/assertions.rb', line 147 def normalize_types_to_check(resource_types) case resource_types when Hash resource_types.transform_keys { |type| normalize_resource_type(type) } when String { normalize_resource_type(resource_types) => nil } when Array resource_types.each_with_object({}) { |type, types| types[normalize_resource_type(type)] = nil } end end |