Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search API v2: Add adapter for autocomplete #1292

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Add autocomplete endpoint for Search API v2 [PR](https://github.com/alphagov/gds-api-adapters/pull/1292)

## 97.2.0

* Add page and order args to get_content_by_embedded_document [PR](https://github.com/alphagov/gds-api-adapters/pull/1291)
Expand Down
6 changes: 6 additions & 0 deletions lib/gds_api/search_api_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ def search(args, additional_headers = {})
request_url = "#{endpoint}/search.json?#{Rack::Utils.build_nested_query(args)}"
get_json(request_url, additional_headers)
end

def autocomplete(query)
args = { q: query }
request_url = "#{endpoint}/autocomplete.json?#{Rack::Utils.build_nested_query(args)}"
get_json(request_url)
end
end
end
109 changes: 52 additions & 57 deletions test/search_api_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,74 @@
require "gds_api/search_api_v2"

describe GdsApi::SearchApiV2 do
before(:each) do
stub_request(:get, /example.com\/search/).to_return(body: "[]")
end

it "#search should raise an exception if the service at the search URI returns a 500" do
stub_request(:get, /example.com\/search.json/).to_return(status: [500, "Internal Server Error"])
assert_raises(GdsApi::HTTPServerError) do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
describe "#search" do
before(:each) do
stub_request(:get, /example.com\/search/).to_return(body: "[]")
end
end

it "#search should raise an exception if the service at the search URI returns a 404" do
stub_request(:get, /example.com\/search/).to_return(status: [404, "Not Found"])
assert_raises(GdsApi::HTTPNotFound) do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
it "should raise an exception if the request is unsuccessful" do
stub_request(:get, /example.com\/search.json/).to_return(status: [500, "Internal Server Error"])
assert_raises(GdsApi::HTTPServerError) do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
end
end
end

it "#search should raise an exception if the service at the search URI returns a 400" do
stub_request(:get, /example.com\/search/).to_return(
status: [400, "Bad Request"],
body: '"error":"Filtering by \"coffee\" is not allowed"',
)
assert_raises(GdsApi::HTTPClientError) do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query", filter_coffee: "tea")
it "should return the search deserialized from json" do
search_results = [{ "title" => "document-title" }]
stub_request(:get, /example.com\/search/).to_return(body: search_results.to_json)
results = GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
assert_equal search_results, results.to_hash
end
end

it "#search should raise an exception if the service at the search URI returns a 422" do
stub_request(:get, /example.com\/search/).to_return(
status: [422, "Bad Request"],
body: '"error":"Filtering by \"coffee\" is not allowed"',
)
assert_raises(GdsApi::HTTPUnprocessableEntity) do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query", filter_coffee: "tea")
it "should request the search results in JSON format" do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query")

assert_requested :get, /.*/, headers: { "Accept" => "application/json" }
end
end

it "#search should raise an exception if the service at the search URI times out" do
stub_request(:get, /example.com\/search/).to_timeout
assert_raises(GdsApi::TimedOutException) do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
it "should issue a request for all the params supplied" do
GdsApi::SearchApiV2.new("http://example.com").search(
q: "query & stuff",
filter_topics: %w[1 2],
order: "-public_timestamp",
)

assert_requested :get, /q=query%20%26%20stuff/
assert_requested :get, /filter_topics\[\]=1&filter_topics\[\]=2/
assert_requested :get, /order=-public_timestamp/
end
end

it "#search should return the search deserialized from json" do
search_results = [{ "title" => "document-title" }]
stub_request(:get, /example.com\/search/).to_return(body: search_results.to_json)
results = GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
assert_equal search_results, results.to_hash
it "can pass additional headers" do
GdsApi::SearchApiV2.new("http://example.com").search({ q: "query" }, "authorization" => "token")

assert_requested :get, /.*/, headers: { "authorization" => "token" }
end
end

it "#search should request the search results in JSON format" do
GdsApi::SearchApiV2.new("http://example.com").search(q: "query")
describe "#autocomplete" do
before(:each) do
stub_request(:get, /example.com\/autocomplete/).to_return(body: "[]")
end

assert_requested :get, /.*/, headers: { "Accept" => "application/json" }
end
it "should raise an exception if the request is unsuccessful" do
stub_request(:get, /example.com\/autocomplete.json/).to_return(
status: [500, "Internal Server Error"],
)
assert_raises(GdsApi::HTTPServerError) do
GdsApi::SearchApiV2.new("http://example.com").autocomplete("prime minis")
end
end

it "#search should issue a request for all the params supplied" do
GdsApi::SearchApiV2.new("http://example.com").search(
q: "query & stuff",
filter_topics: %w[1 2],
order: "-public_timestamp",
)
it "should request the autocomplete results in JSON format" do
GdsApi::SearchApiV2.new("http://example.com").autocomplete("prime minis")

assert_requested :get, /q=query%20%26%20stuff/
assert_requested :get, /filter_topics\[\]=1&filter_topics\[\]=2/
assert_requested :get, /order=-public_timestamp/
end
assert_requested :get, /.*/, headers: { "Accept" => "application/json" }
end

it "#search can pass additional headers" do
GdsApi::SearchApiV2.new("http://example.com").search({ q: "query" }, "authorization" => "token")
it "should issue a request for the correct query" do
GdsApi::SearchApiV2.new("http://example.com").autocomplete("prime minis")

assert_requested :get, /.*/, headers: { "authorization" => "token" }
assert_requested :get, /q=prime%20minis/
end
end
end