From e25169741820cef11174be8196e8e8ffb39dfde4 Mon Sep 17 00:00:00 2001 From: Jason Walonoski Date: Thu, 13 Apr 2017 10:34:20 -0400 Subject: [PATCH] Add unit tests for crud change. --- lib/fhir_client/sections/crud.rb | 2 +- .../client_interface_sections/create_test.rb | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/unit/client_interface_sections/create_test.rb diff --git a/lib/fhir_client/sections/crud.rb b/lib/fhir_client/sections/crud.rb index e24b20d3..252e8242 100644 --- a/lib/fhir_client/sections/crud.rb +++ b/lib/fhir_client/sections/crud.rb @@ -147,7 +147,7 @@ def base_create(resource, options, format) options[:format] = format reply = post resource_url(options), resource, fhir_headers(options) if [200, 201].include? reply.code - type = reply.response[:headers].select{|x| x.downcase=='content-type'}['content-type'] + type = reply.response[:headers].detect{|x, _y| x.downcase=='content-type'}.try(:last) if !type.nil? reply.resource = if type.include?('xml') && !reply.body.empty? FHIR::Xml.from_xml(reply.body) diff --git a/test/unit/client_interface_sections/create_test.rb b/test/unit/client_interface_sections/create_test.rb new file mode 100644 index 00000000..c4bd99aa --- /dev/null +++ b/test/unit/client_interface_sections/create_test.rb @@ -0,0 +1,50 @@ +require_relative '../../test_helper' + +class ClientInterfaceCreateTest < Test::Unit::TestCase + def client + @client ||= FHIR::Client.new('create-test') + end + + def test_create_response_properly_parsed_xml + patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false}) + outcome = FHIR::OperationOutcome.new({'issue'=>[{'code'=>'informational', 'severity'=>'information', 'diagnostics'=>'Successfully created "Patient/foo" in 0 ms'}]}) + + stub_request(:post, /create-test/).to_return(body: outcome.to_xml, headers: {'Content-Type'=>'application/fhir+xml', 'Content-Location'=>'Patient/foo/_history/0'}) + reply = client.create(patient) + assert reply.resource.is_a?(FHIR::OperationOutcome) + assert reply.resource_class == FHIR::Patient + assert reply.id == 'foo' + end + + def test_create_response_properly_parsed_json + patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false}) + outcome = FHIR::OperationOutcome.new({'issue'=>[{'code'=>'informational', 'severity'=>'information', 'diagnostics'=>'Successfully created "Patient/foo" in 0 ms'}]}) + + stub_request(:post, /create-test/).to_return(body: outcome.to_json, headers: {'Content-Type'=>'application/fhir+json', 'Content-Location'=>'Patient/foo/_history/0'}) + reply = client.create(patient) + assert reply.resource.is_a?(FHIR::OperationOutcome) + assert reply.resource_class == FHIR::Patient + assert reply.id == 'foo' + end + + def test_create_response_blank + patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false}) + + stub_request(:post, /create-test/).to_return(headers: {'Content-Location'=>'Patient/foo/_history/0'}) + reply = client.create(patient) + assert reply.resource.is_a?(FHIR::Patient) + assert reply.resource_class == FHIR::Patient + assert reply.id == 'foo' + end + + def test_read + patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false}) + + stub_request(:get, /create-test/).to_return(body: patient.to_json, headers: {'Content-Type'=>'application/fhir+json', 'Content-Location'=>'Patient/foo/_history/0'}) + reply = client.read(FHIR::Patient,'foo') + assert reply.resource.is_a?(FHIR::Patient) + assert reply.resource_class == FHIR::Patient + assert reply.id == 'foo' + end + +end