Skip to content

Commit

Permalink
Add unit tests for crud change.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawalonoski committed Apr 13, 2017
1 parent 98e9cda commit e251697
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fhir_client/sections/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions test/unit/client_interface_sections/create_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e251697

Please sign in to comment.