Skip to content

Commit

Permalink
FI-1370: Add POST search (#57)
Browse files Browse the repository at this point in the history
* bump fhir_client version

* add post search support
  • Loading branch information
Jammjammjamm committed Nov 30, 2021
1 parent d5b55a1 commit 2055736
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 90 deletions.
68 changes: 42 additions & 26 deletions Gemfile.lock
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (6.1.0)
activesupport (6.1.4.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -71,36 +71,52 @@ GEM
dry-logic (~> 1.0, >= 1.0.2)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
faraday (1.2.0)
faraday (1.8.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0.1)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
ffi (1.15.4)
fhir_client (4.0.4)
fhir_client (5.0.2)
activesupport (>= 3)
addressable (>= 2.3)
fhir_dstu2_models (>= 1.0.10)
fhir_models (>= 4.0.2)
fhir_stu3_models (>= 3.0.1)
fhir_dstu2_models (>= 1.1.1)
fhir_models (>= 4.2.1)
fhir_stu3_models (>= 3.1.1)
nokogiri (>= 1.10.4)
oauth2 (~> 1.1)
rack (>= 1.5)
rest-client (~> 2.0)
tilt (>= 1.1)
fhir_dstu2_models (1.0.10)
fhir_dstu2_models (1.1.1)
bcp47 (>= 0.3)
date_time_precision (>= 0.8)
mime-types (>= 3.0)
nokogiri (>= 1.10.4)
fhir_models (4.2.0)
nokogiri (>= 1.11.4)
fhir_models (4.2.1)
bcp47 (>= 0.3)
date_time_precision (>= 0.8)
mime-types (>= 3.0)
nokogiri (>= 1.11.4)
fhir_stu3_models (3.0.1)
fhir_stu3_models (3.1.1)
bcp47 (>= 0.3)
date_time_precision (>= 0.8)
mime-types (>= 3.0)
nokogiri (>= 1.10.4)
nokogiri (>= 1.11.4)
forwardable-extended (2.6.0)
hanami-controller (1.3.3)
hanami-utils (~> 1.3)
Expand All @@ -114,12 +130,12 @@ GEM
transproc (~> 1.0)
hashdiff (1.0.1)
http-accept (1.7.0)
http-cookie (1.0.3)
http-cookie (1.0.4)
domain_name (~> 0.5)
http_router (0.11.2)
rack (>= 1.0.0)
url_mount (~> 0.2.1)
i18n (1.8.10)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
jekyll (3.6.3)
Expand All @@ -137,28 +153,28 @@ GEM
sass (~> 3.4)
jekyll-watch (1.5.1)
listen (~> 3.0)
jwt (2.2.2)
jwt (2.3.0)
kramdown (1.17.0)
liquid (4.0.3)
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.3.6)
method_source (1.0.0)
mime-types (3.3.1)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0704)
mini_portile2 (2.5.3)
minitest (5.14.2)
mime-types-data (3.2021.1115)
mini_portile2 (2.6.1)
minitest (5.14.4)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
netrc (0.11.0)
nio4r (2.5.7)
nokogiri (1.11.7)
mini_portile2 (~> 2.5.0)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
oauth2 (1.4.4)
oauth2 (1.4.7)
faraday (>= 0.8, < 2.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
Expand All @@ -179,7 +195,7 @@ GEM
public_suffix (4.0.6)
puma (5.3.2)
nio4r (~> 2.0)
racc (1.5.2)
racc (1.6.0)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
Expand Down Expand Up @@ -229,7 +245,7 @@ GEM
rubocop-sequel (0.1.0)
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.2)
ruby2_keywords (0.0.5)
safe_yaml (1.0.5)
sass (3.7.4)
sass-listen (~> 4.0.0)
Expand All @@ -255,7 +271,7 @@ GEM
concurrent-ruby (~> 1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
unf_ext (0.0.8)
unicode-display_width (2.0.0)
url_mount (0.2.1)
rack
Expand All @@ -268,7 +284,7 @@ GEM
backports (>= 3.18)
rainbow
yard
zeitwerk (2.4.2)
zeitwerk (2.5.1)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion inferno_core.gemspec
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'dry-configurable', '0.12.0' # 0.13.0 causes a lot of deprecation warnings
spec.add_runtime_dependency 'dry-system', '0.18.1'
spec.add_runtime_dependency 'faraday', '~> 1.2'
spec.add_runtime_dependency 'fhir_client', '~> 4.0'
spec.add_runtime_dependency 'fhir_client', '~> 5.0'
spec.add_runtime_dependency 'fhir_models', '~> 4.2.0'
spec.add_runtime_dependency 'hanami-controller', '~> 1.3'
spec.add_runtime_dependency 'hanami-router', '~> 1.3'
Expand Down
32 changes: 18 additions & 14 deletions lib/inferno/dsl/fhir_client.rb
Expand Up @@ -69,15 +69,15 @@ def fhir_clients
# @param client [Symbol]
# @param name [Symbol] Name for this request to allow it to be used by
# other tests
# @option options [Hash] Input headers here - headers are optional and
# must be entered as the last piece of input to this method
# @param headers [Hash] custom headers for this operation
# @return [Inferno::Entities::Request]
def fhir_operation(path, body: nil, client: :default, name: nil, **options)
def fhir_operation(path, body: nil, client: :default, name: nil, headers: {})
store_request('outgoing', name) do
headers = fhir_client(client).fhir_headers
headers.merge!('Content-Type' => 'application/fhir+json') if body.present?
headers.merge!(options[:headers]) if options[:headers].present?
fhir_client(client).send(:post, path, body, headers)
operation_headers = fhir_client(client).fhir_headers
operation_headers.merge!('Content-Type' => 'application/fhir+json') if body.present?
operation_headers.merge!(headers) if headers.present?

fhir_client(client).send(:post, path, body, operation_headers)
end
end

Expand All @@ -86,9 +86,8 @@ def fhir_operation(path, body: nil, client: :default, name: nil, **options)
# @param client [Symbol]
# @param name [Symbol] Name for this request to allow it to be used by
# other tests
# @param _options [Hash] TODO
# @return [Inferno::Entities::Request]
def fhir_get_capability_statement(client: :default, name: nil, **_options)
def fhir_get_capability_statement(client: :default, name: nil)
store_request('outgoing', name) do
fhir_client(client).conformance_statement
fhir_client(client).reply
Expand All @@ -102,9 +101,8 @@ def fhir_get_capability_statement(client: :default, name: nil, **_options)
# @param client [Symbol]
# @param name [Symbol] Name for this request to allow it to be used by
# other tests
# @param _options [Hash] TODO
# @return [Inferno::Entities::Request]
def fhir_read(resource_type, id, client: :default, name: nil, **_options)
def fhir_read(resource_type, id, client: :default, name: nil)
store_request('outgoing', name) do
fhir_client(client).read(fhir_class_from_resource_type(resource_type), id)
end
Expand All @@ -117,12 +115,18 @@ def fhir_read(resource_type, id, client: :default, name: nil, **_options)
# @param params [Hash] the search params
# @param name [Symbol] Name for this request to allow it to be used by
# other tests
# @param _options [Hash] TODO
# @param search_method [Symbol] Use `:post` to search via POST
# @return [Inferno::Entities::Request]
def fhir_search(resource_type, client: :default, params: {}, name: nil, **_options)
def fhir_search(resource_type, client: :default, params: {}, name: nil, search_method: :get)
search =
if search_method == :post
{ body: params }
else
{ parameters: params }
end
store_request('outgoing', name) do
fhir_client(client)
.search(fhir_class_from_resource_type(resource_type), search: { parameters: params })
.search(fhir_class_from_resource_type(resource_type), { search: search })
end
end

Expand Down
119 changes: 70 additions & 49 deletions spec/inferno/dsl/fhir_client_spec.rb
Expand Up @@ -107,41 +107,40 @@ def setup_default_client
end
Inferno::DSL::FHIRClientBuilder.new.build(group, block)
end
let(:stub_custom_header_request) do
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'CustomHeader' => 'CustomTest' })
.to_return(status: 200, body: resource.to_json)
end
let(:stub_default_header_request) do
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'DefaultHeader' => 'ClientHeader' })
.to_return(status: 200, body: resource.to_json)
end

let(:stub_custom_and_default_header_request) do
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'DefaultHeader' => 'ClientHeader', 'CustomHeader' => 'CustomTest' })
.to_return(status: 200, body: resource.to_json)
end

it 'as custom only, performs a get' do
operation_request =
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'CustomHeader' => 'CustomTest' })
.to_return(status: 200, body: resource.to_json)

group.fhir_operation(path, headers: { 'CustomHeader' => 'CustomTest' })

expect(stub_custom_header_request).to have_been_made.once
expect(operation_request).to have_been_made.once
end

it 'as default only, performs a get' do
operation_request =
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'DefaultHeader' => 'ClientHeader' })
.to_return(status: 200, body: resource.to_json)

group.fhir_clients[:client_with_header] = client_with_header
group.fhir_operation(path, client: :client_with_header)

expect(stub_default_header_request).to have_been_made.once
expect(operation_request).to have_been_made.once
end

it 'as both default and custom, performs a get' do
operation_request =
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'DefaultHeader' => 'ClientHeader', 'CustomHeader' => 'CustomTest' })
.to_return(status: 200, body: resource.to_json)

group.fhir_clients[:client_with_header] = client_with_header
group.fhir_operation(path, client: :client_with_header, headers: { 'CustomHeader' => 'CustomTest' })

expect(stub_custom_and_default_header_request).to have_been_made.once
expect(operation_request).to have_been_made.once
end
end
end
Expand Down Expand Up @@ -242,48 +241,70 @@ def setup_default_client
end

describe '#fhir_search' do
let(:stub_search_request) do
stub_request(:get, "#{base_url}/#{resource.resourceType}?patient=123")
.to_return(status: 200, body: bundle.to_json)
end
context 'when performing a GET search' do
let(:stub_search_request) do
stub_request(:get, "#{base_url}/#{resource.resourceType}?patient=123")
.to_return(status: 200, body: bundle.to_json)
end

before do
setup_default_client
stub_search_request
end
before do
setup_default_client
stub_search_request
end

it 'performs a FHIR search' do
group.fhir_search(resource.resourceType, params: { patient: 123 })
it 'performs a FHIR search' do
group.fhir_search(resource.resourceType, params: { patient: 123 })

expect(stub_search_request).to have_been_made.once
end
expect(stub_search_request).to have_been_made.once
end

it 'returns an Inferno::Entities::Request' do
result = group.fhir_search(resource.resourceType, params: { patient: 123 })
it 'returns an Inferno::Entities::Request' do
result = group.fhir_search(resource.resourceType, params: { patient: 123 })

expect(result).to be_a(Inferno::Entities::Request)
end
expect(result).to be_a(Inferno::Entities::Request)
end

it 'adds the request to the list of requests' do
result = group.fhir_search(resource.resourceType, params: { patient: 123 })
it 'adds the request to the list of requests' do
result = group.fhir_search(resource.resourceType, params: { patient: 123 })

expect(group.requests).to include(result)
expect(group.request).to eq(result)
expect(group.requests).to include(result)
expect(group.request).to eq(result)
end

context 'with the client parameter' do
it 'uses that client' do
other_url = 'http://www.example.com/fhir/r4'
group.fhir_clients[:other_client] = FHIR::Client.new(other_url)

other_request_stub =
stub_request(:get, "#{other_url}/#{resource.resourceType}?patient=123")
.to_return(status: 200, body: resource.to_json)

group.fhir_search(resource.resourceType, client: :other_client, params: { patient: 123 })

expect(other_request_stub).to have_been_made
expect(stub_search_request).to_not have_been_made
end
end
end

context 'with the client parameter' do
it 'uses that client' do
other_url = 'http://www.example.com/fhir/r4'
group.fhir_clients[:other_client] = FHIR::Client.new(other_url)
context 'when performing a POST search' do
let(:search_params) { { patient: '123' } }
let(:stub_search_request) do
stub_request(:post, "#{base_url}/#{resource.resourceType}/_search")
.with(body: search_params)
.to_return(status: 200, body: bundle.to_json)
end

other_request_stub =
stub_request(:get, "#{other_url}/#{resource.resourceType}?patient=123")
.to_return(status: 200, body: resource.to_json)
before do
setup_default_client
stub_search_request
end

group.fhir_search(resource.resourceType, client: :other_client, params: { patient: 123 })
it 'performs a FHIR search' do
group.fhir_search(resource.resourceType, params: search_params, search_method: :post)

expect(other_request_stub).to have_been_made
expect(stub_search_request).to_not have_been_made
expect(stub_search_request).to have_been_made.once
end
end
end
Expand Down

0 comments on commit 2055736

Please sign in to comment.