Skip to content

Commit

Permalink
refactor(Algolia.Api): use config more consistently for dependency in…
Browse files Browse the repository at this point in the history
…jection

Co-authored-by: Melody Horn <[email protected]>
  • Loading branch information
KaylaBrady and boringcactus committed Jan 17, 2024
1 parent 002f5bc commit ddefc1a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
12 changes: 7 additions & 5 deletions lib/mobile_app_backend/search/algolia/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ defmodule MobileAppBackend.Search.Algolia.Api do
alias MobileAppBackend.Search.Algolia
require Logger

@spec multi_index_search([Algolia.QueryPayload.t()], Keyword.t()) ::
@spec multi_index_search([Algolia.QueryPayload.t()]) ::
{:ok, [any()]} | {:error, any}
@doc """
Perform the given index queries and return a flattened list of parsed results
"""
def multi_index_search(queries, opts \\ []) do
def multi_index_search(queries) do
perform_request_fn =
Keyword.get(opts, :perform_request_fn, fn url, body, headers ->
HTTPoison.post(url, body, headers)
end)
Application.get_env(
:mobile_app_backend,
:algolia_perform_request_fn,
&HTTPoison.post/3
)

body = multi_query_encoded_payload(queries)

Expand Down
70 changes: 38 additions & 32 deletions test/mobile_app_backend/search/algolia/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ defmodule MobileAppBackend.Search.Algolia.ApiTest do

describe "multi_index_search/2" do
test "when request is successful, returns flattened list of parsed results" do
reassign_env(:mobile_app_backend, :algolia_perform_request_fn, &mock_perform_request_fn/3)

assert {:ok, results} =
Api.multi_index_search(
[QueryPayload.for_index(:stop, "1"), QueryPayload.for_index(:route, "1")],
perform_request_fn: fn url, body, headers ->
mock_perform_request_fn(url, body, headers)
end
)
Api.multi_index_search([
QueryPayload.for_index(:stop, "1"),
QueryPayload.for_index(:route, "1")
])

assert [
%StopResult{
Expand All @@ -36,6 +36,8 @@ defmodule MobileAppBackend.Search.Algolia.ApiTest do
end

test "makes requests to the algolia endpoint with expected headers and parameters properly encoded" do
pid = self()

default_env = Application.get_env(:mobile_app_backend, MobileAppBackend.Search.Algolia)

reassign_env(
Expand All @@ -47,15 +49,15 @@ defmodule MobileAppBackend.Search.Algolia.ApiTest do
)
)

pid = self()
reassign_env(:mobile_app_backend, :algolia_perform_request_fn, fn url, body, headers ->
send(pid, %{url: url, body: body, headers: headers})
{:ok, %{body: "\{\"results\":[]\}"}}
end)

Api.multi_index_search(
[QueryPayload.for_index(:stop, "1"), QueryPayload.for_index(:route, "1")],
perform_request_fn: fn url, body, headers ->
send(pid, %{url: url, body: body, headers: headers})
{:ok, %{body: "\{\"results\":[]\}"}}
end
)
Api.multi_index_search([
QueryPayload.for_index(:stop, "1"),
QueryPayload.for_index(:route, "1")
])

assert_received(%{url: url, body: body, headers: headers})

Expand All @@ -80,36 +82,40 @@ defmodule MobileAppBackend.Search.Algolia.ApiTest do
Keyword.merge(default_env, search_key: nil)
)

reassign_env(:mobile_app_backend, :algolia_perform_request_fn, &mock_perform_request_fn/3)

assert {:error, :search_failed} =
Api.multi_index_search(
[QueryPayload.for_index(:stop, "1"), QueryPayload.for_index(:route, "1")],
perform_request_fn: fn url, body, headers ->
mock_perform_request_fn(url, body, headers)
end
)
Api.multi_index_search([
QueryPayload.for_index(:stop, "1"),
QueryPayload.for_index(:route, "1")
])
end

@tag capture_log: true
test "when invalid json, returns error" do
reassign_env(:mobile_app_backend, :algolia_perform_request_fn, fn _url, _body, _headers ->
{:ok, %{body: "[123 this is not json]"}}
end)

assert {:error, :search_failed} =
Api.multi_index_search(
[QueryPayload.for_index(:stop, "1"), QueryPayload.for_index(:route, "1")],
perform_request_fn: fn _url, _body, _headers ->
{:ok, %{body: "[123 this is not json]"}}
end
)
Api.multi_index_search([
QueryPayload.for_index(:stop, "1"),
QueryPayload.for_index(:route, "1")
])
end

@tag capture_log: true

test "when request is unsuccessful, returns error" do
reassign_env(:mobile_app_backend, :algolia_perform_request_fn, fn _url, _body, _headers ->
{:error, "oops"}
end)

assert {:error, :search_failed} =
Api.multi_index_search(
[QueryPayload.for_index(:stop, "1"), QueryPayload.for_index(:route, "1")],
perform_request_fn: fn _url, _body, _headers ->
{:error, "oops"}
end
)
Api.multi_index_search([
QueryPayload.for_index(:stop, "1"),
QueryPayload.for_index(:route, "1")
])
end
end

Expand Down

0 comments on commit ddefc1a

Please sign in to comment.