Module: Inferno::DSL::Assertions
- 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 = '') ⇒ Object
Make an assertion.
-
#assert_resource_type(resource_type, resource: self.resource) ⇒ Object
Check a FHIR resource’s type.
-
#assert_response_content_type(type, request: self.request) ⇒ Object
Check the Content-Type header of a response.
-
#assert_response_status(status, response: self.response) ⇒ Object
Check an response’s status.
-
#assert_valid_bundle_entries(bundle: resource, resource_types: {}) ⇒ Object
Validate each entry of a Bundle.
-
#assert_valid_http_uri(uri, message = '') ⇒ Object
Check for a valid http/https uri.
-
#assert_valid_json(maybe_json_string, message = '') ⇒ Object
Check for valid JSON.
-
#assert_valid_resource(resource: self.resource, profile_url: nil) ⇒ Object
Validate a FHIR resource.
Instance Method Details
#assert(test, message = '') ⇒ Object
Make an assertion
15 16 17 |
# File 'lib/inferno/dsl/assertions.rb', line 15 def assert(test, = '') raise Exceptions::AssertionException, unless test end |
#assert_resource_type(resource_type, resource: self.resource) ⇒ Object
Check a FHIR resource’s type
47 48 49 50 51 52 |
# File 'lib/inferno/dsl/assertions.rb', line 47 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) ⇒ Object
Check the Content-Type header of a response
170 171 172 173 174 175 |
# File 'lib/inferno/dsl/assertions.rb', line 170 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, response: self.response) ⇒ Object
Check an response’s status
29 30 31 |
# File 'lib/inferno/dsl/assertions.rb', line 29 def assert_response_status(status, response: self.response) assert Array.wrap(status).include?(response[:status]), (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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/inferno/dsl/assertions.rb', line 96 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 = '') ⇒ Object
Check for a valid http/https uri
161 162 163 164 |
# File 'lib/inferno/dsl/assertions.rb', line 161 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 = '') ⇒ Object
Check for valid JSON
151 152 153 154 155 |
# File 'lib/inferno/dsl/assertions.rb', line 151 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) ⇒ Object
Validate a FHIR resource
64 65 66 67 |
# File 'lib/inferno/dsl/assertions.rb', line 64 def assert_valid_resource(resource: self.resource, profile_url: nil) assert resource_is_valid?(resource:, profile_url:), (profile_url) end |