diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..341e9a1 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,30 @@ +name: Unit Tests + +on: + push: + branches: + - master + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-24.04 + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.4.9' + bundler-cache: true + + - name: Run unit tests + run: >- + bundle exec ruby -Ilib -Itest -e + 'ARGV.each { |file| require File.expand_path(file) }' + $(find test -name '*_test.rb' -print) diff --git a/.simplecov b/.simplecov index a555a37..3bdcf24 100644 --- a/.simplecov +++ b/.simplecov @@ -1,9 +1,6 @@ -require 'simplecov' SimpleCov.command_name 'Unit Tests' -SimpleCov.start do - add_filter "test/" - add_group "Library", "lib" -end +SimpleCov.skip "test/" +SimpleCov.group "Library", "lib" class SimpleCov::Formatter::QualityFormatter def format(result) diff --git a/Gemfile b/Gemfile index 7949e73..86426b6 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,10 @@ source 'https://rubygems.org' gemspec +gem 'fhir_dstu2_models', git: 'https://github.com/incendilabs/fhir_dstu2_models.git', branch: 'master' +gem 'fhir_models', git: 'https://github.com/incendilabs/fhir_models.git', branch: 'master' +gem 'fhir_stu3_models', git: 'https://github.com/incendilabs/fhir_stu3_models.git', branch: 'master' + group :test do gem 'rubocop', '~> 0.52.1', require: false gem 'awesome_print', require: 'ap' diff --git a/fhir_client.gemspec b/fhir_client.gemspec index 4e3b54f..500a2d5 100644 --- a/fhir_client.gemspec +++ b/fhir_client.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'fhir_stu3_models', '>= 3.0.1' spec.add_dependency 'fhir_dstu2_models', '>= 1.0.10' spec.add_dependency 'nokogiri', '>= 1.10.4' - spec.add_dependency 'oauth2', '~> 1.1' + spec.add_dependency 'oauth2', '>= 2.0', '< 3.0' spec.add_dependency 'rack', '>= 1.5' spec.add_dependency 'rest-client', '~> 2.0' spec.add_dependency 'tilt', '>= 1.1' diff --git a/lib/fhir_client/client.rb b/lib/fhir_client/client.rb index ce5f2ee..92f2d29 100644 --- a/lib/fhir_client/client.rb +++ b/lib/fhir_client/client.rb @@ -347,7 +347,8 @@ def parse_reply(klass, format, response) res = format.include?('xml') ? parser.from_xml(response.body) : parser.from_json(response.body) res.client = self unless res.nil? rescue => e - FHIR.logger.error "Failed to parse #{format} as resource #{klass}: #{e.message}" + message = "Failed to parse #{format} as resource #{klass}: #{e.message}" + FHIR.logger.debug(message) res = nil end res @@ -492,7 +493,7 @@ def get(path, headers = {}) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "GET - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "GET - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end response = e.response if e.response @@ -539,7 +540,7 @@ def get(path, headers = {}) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "GET - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "GET - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end response = e.response @@ -574,7 +575,7 @@ def post(path, resource, headers) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "POST - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "POST - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end response = e.response if e.response @@ -621,7 +622,7 @@ def put(path, resource, headers) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "PUT - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "PUT - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end response = e.response if e.response @@ -668,7 +669,7 @@ def patch(path, patchset, headers) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "PATCH - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "PATCH - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end response = e.response if e.response @@ -704,7 +705,7 @@ def patch(path, patchset, headers) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "PATCH - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "PATCH - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end req = { @@ -736,7 +737,7 @@ def delete(path, headers) if !e.respond_to?(:response) || e.response.nil? # Re-raise the client error if there's no response. Otherwise, logging # and other things break below! - FHIR.logger.error "DELETE - Request: #{url} failed! No response from server: #{e}" + FHIR.logger.debug "DELETE - Request: #{url} failed! No response from server: #{e}" raise # Re-raise the same error we caught. end response = e.response if e.response diff --git a/test/test_helper.rb b/test/test_helper.rb index 8772f7b..4adecac 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,6 +2,7 @@ require 'simplecov' SimpleCov.start +require 'stringio' require 'pry' require 'test/unit' require 'webmock/test_unit' @@ -14,4 +15,3 @@ ACCEPT_REGEX_XML_DSTU2 = /^(\s*application\/xml\+fhir\s*)(;\s*charset\s*=\s*utf-8\s*)?$/ ACCEPT_REGEX_JSON_DSTU2 = /^(\s*application\/json\+fhir\s*)(;\s*charset\s*=\s*utf-8\s*)?$/ - diff --git a/test/unit/multiversion_test.rb b/test/unit/multiversion_test.rb index 1fed24c..501a2e6 100644 --- a/test/unit/multiversion_test.rb +++ b/test/unit/multiversion_test.rb @@ -17,6 +17,26 @@ def test_autodetect_stu3 assert client.default_format.include? 'json' end + def test_autodetect_logs_intermediate_parse_failure_at_debug + root = File.expand_path '..', File.dirname(File.absolute_path(__FILE__)) + capabilitystatement = File.read(File.join(root, 'fixtures', 'capabilitystatement.json')) + stub_request(:get, /autodetect/).to_return(body: capabilitystatement) + original_logger = FHIR.logger + log_output = StringIO.new + FHIR.logger = Logger.new(log_output) + + client = FHIR::Client.new('autodetect', fhir_version: :auto) + client.default_xml + client.use_r4 + client.detect_version + + message = 'Failed to parse application/fhir+xml as resource FHIR::STU3::CapabilityStatement' + assert_match(/DEBUG -- : #{Regexp.escape(message)}/, log_output.string) + assert_no_match(/ERROR -- : #{Regexp.escape(message)}/, log_output.string) + ensure + FHIR.logger = original_logger + end + def test_autodetect_dstu2 root = File.expand_path '..', File.dirname(File.absolute_path(__FILE__)) conformance = File.read(File.join(root, 'fixtures', 'conformance.json'))