Skip to content

Commit

Permalink
add Styler (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfiedlerdejanze authored Mar 22, 2024
1 parent 6976ee5 commit fceca5e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
plugins: [Styler],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
11 changes: 7 additions & 4 deletions lib/algolia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Algolia do
alias Algolia.Paths

defmodule MissingApplicationIDError do
@moduledoc false
defexception message: """
The `application_id` settings is required to use Algolia. Please include your
application_id in your application config file like so:
Expand All @@ -17,6 +18,7 @@ defmodule Algolia do
end

defmodule MissingAPIKeyError do
@moduledoc false
defexception message: """
The `api_key` settings is required to use Algolia. Please include your
api key in your application config file like so:
Expand All @@ -27,6 +29,7 @@ defmodule Algolia do
end

defmodule InvalidObjectIDError do
@moduledoc false
defexception message: "The ObjectID cannot be an empty string"
end

Expand Down Expand Up @@ -57,7 +60,7 @@ defmodule Algolia do
Enum.map(queries, fn query ->
index_name = query[:index_name] || query["index_name"]

if !index_name,
unless index_name,
do: raise(ArgumentError, message: "Missing index_name for one of the multiple queries")

params =
Expand Down Expand Up @@ -374,7 +377,7 @@ defmodule Algolia do
Enum.map(objects, fn object ->
object_id = object[attribute] || object[to_string(attribute)]

if !object_id do
unless object_id do
raise ArgumentError, message: "id attribute `#{attribute}` doesn't exist"
end

Expand Down Expand Up @@ -648,13 +651,13 @@ defmodule Algolia do
wait(response, time_before_retry: time_before_retry)
end

def wait(response = {:ok, %{"indexName" => index, "taskID" => task_id}}, opts) do
def wait({:ok, %{"indexName" => index, "taskID" => task_id}} = response, opts) do
{time_before_retry, opts} = Keyword.pop(opts, :time_before_retry, 1000)

with :ok <- wait_task(index, task_id, time_before_retry, opts), do: response
end

def wait(response = {:error, _}, _opts), do: response
def wait({:error, _} = response, _opts), do: response
def wait(response, _opts), do: response

defp default_config, do: Config.new()
Expand Down
3 changes: 2 additions & 1 deletion lib/algolia/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule Algolia.Config do
Configuration for algolia API client
"""

alias Algolia.{MissingAPIKeyError, MissingApplicationIDError}
alias Algolia.MissingAPIKeyError
alias Algolia.MissingApplicationIDError

@type t :: %__MODULE__{
application_id: application_id,
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule Algolia.Mixfile do
{:tesla, "~> 1.5"},
{:telemetry, "~> 0.4 or ~> 1.0"},
{:jason, "~> 1.0"},
{:styler, "~> 0.11", only: [:dev, :test], runtime: false},
# Docs
{:ex_doc, "~> 0.19", only: :dev},
{:inch_ex, ">= 0.0.0", only: :dev}
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"styler": {:hex, :styler, "0.11.9", "2595393b94e660cd6e8b582876337cc50ff047d184ccbed42fdad2bfd5d78af5", [:mix], [], "hexpm", "8b7806ba1fdc94d0a75127c56875f91db89b75117fcc67572661010c13e1f259"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"tesla": {:hex, :tesla, "1.7.0", "a62dda2f80d4f8a925eb7b8c5b78c461e0eb996672719fe1a63b26321a5f8b4e", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.13", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:msgpax, "~> 2.3", [hex: :msgpax, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "2e64f01ebfdb026209b47bc651a0e65203fcff4ae79c11efb73c4852b00dc313"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
Expand Down
60 changes: 34 additions & 26 deletions test/algolia_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule AlgoliaTest do

setup_all do
@indexes
|> Enum.map(&delete_index/1)
|> Enum.map(&clear_index/1)
|> Enum.each(&wait/1)
end

Expand Down Expand Up @@ -52,7 +52,7 @@ defmodule AlgoliaTest do

test "wait task" do
:rand.seed(:exs1024, :erlang.timestamp())
object_id = :rand.uniform(1_000_000) |> to_string
object_id = 1_000_000 |> :rand.uniform() |> to_string()

{:ok, %{"objectID" => ^object_id, "taskID" => task_id}} = save_object("test", %{}, object_id)

Expand All @@ -63,10 +63,11 @@ defmodule AlgoliaTest do

test "save one object, and then read it, using wait_task pipeing" do
:rand.seed(:exs1024, :erlang.timestamp())
id = :rand.uniform(1_000_000) |> to_string
id = 1_000_000 |> :rand.uniform() |> to_string()

{:ok, %{"objectID" => object_id}} =
save_object("test", %{}, id)
"test"
|> save_object(%{}, id)
|> wait()

assert object_id == id
Expand All @@ -92,7 +93,7 @@ defmodule AlgoliaTest do
count = :rand.uniform(10)
docs = Enum.map(1..count, &%{id: &1, test: "search_single_index"})

{:ok, _} = save_objects("test", docs, id_attribute: :id) |> wait()
{:ok, _} = "test" |> save_objects(docs, id_attribute: :id) |> wait()

{:ok, %{"hits" => hits1}} = search("test", "search_single_index")
assert length(hits1) === count
Expand All @@ -103,7 +104,7 @@ defmodule AlgoliaTest do
count = :rand.uniform(10)
docs = Enum.map(1..count, &%{id: &1, test: "search with list opts"})

{:ok, _} = save_objects("test", docs, id_attribute: :id) |> wait()
{:ok, _} = "test" |> save_objects(docs, id_attribute: :id) |> wait()

opts = [
responseFields: ["hits", "nbPages"]
Expand All @@ -119,7 +120,7 @@ defmodule AlgoliaTest do
test "search > 1 pages" do
docs = Enum.map(1..40, &%{id: &1, test: "search_more_than_one_pages"})

{:ok, _} = save_objects("test", docs, id_attribute: :id) |> wait()
{:ok, _} = "test" |> save_objects(docs, id_attribute: :id) |> wait()

{:ok, %{"hits" => hits, "page" => page}} =
search("test", "search_more_than_one_pages", page: 1)
Expand Down Expand Up @@ -184,20 +185,21 @@ defmodule AlgoliaTest do
:rand.seed(:exs1024, :erlang.timestamp())
count = :rand.uniform(3)
objects = Enum.map(1..count, &%{objectID: &1, test: "search_multiple_indexes"})
save_objects(index, objects) |> wait(3_000)
index |> save_objects(objects) |> wait(3_000)
{index, length(objects)}
end

test "search query with special characters" do
{:ok, %{"hits" => _}} = search("test", "foo & bar") |> wait()
{:ok, %{"hits" => _}} = "test" |> search("foo & bar") |> wait()
end

test "partially update object" do
{:ok, %{"objectID" => object_id}} =
save_object("test", %{id: "partially_update_object"}, id_attribute: :id)
"test"
|> save_object(%{id: "partially_update_object"}, id_attribute: :id)
|> wait()

assert {:ok, _} = partial_update_object("test", %{update: "updated"}, object_id) |> wait()
assert {:ok, _} = "test" |> partial_update_object(%{update: "updated"}, object_id) |> wait()

{:ok, object} = get_object("test", object_id)
assert object["update"] == "updated"
Expand All @@ -207,7 +209,8 @@ defmodule AlgoliaTest do
id = "partially_update_object_upsert_true"

assert {:ok, _} =
partial_update_object("test", %{}, id)
"test"
|> partial_update_object(%{}, id)
|> wait()

{:ok, object} = get_object("test", id)
Expand All @@ -218,7 +221,8 @@ defmodule AlgoliaTest do
id = "partial_update_upsert_false"

assert {:ok, _} =
partial_update_object("test", %{update: "updated"}, id, upsert?: false)
"test"
|> partial_update_object(%{update: "updated"}, id, upsert?: false)
|> wait()

assert {:error, 404, _} = get_object("test", id)
Expand All @@ -228,7 +232,8 @@ defmodule AlgoliaTest do
objects = [%{id: "partial_update_multiple_1"}, %{id: "partial_update_multiple_2"}]

assert {:ok, _} =
partial_update_objects("test", objects, id_attribute: :id)
"test"
|> partial_update_objects(objects, id_attribute: :id)
|> wait()

assert {:ok, _} = get_object("test", "partial_update_multiple_1")
Expand All @@ -242,7 +247,8 @@ defmodule AlgoliaTest do
]

assert {:ok, _} =
partial_update_objects("test", objects, id_attribute: :id, upsert?: false)
"test"
|> partial_update_objects(objects, id_attribute: :id, upsert?: false)
|> wait()

assert {:error, 404, _} = get_object("test", "partial_update_multiple_1_no_upsert")
Expand All @@ -251,10 +257,11 @@ defmodule AlgoliaTest do

test "delete object" do
{:ok, %{"objectID" => object_id}} =
save_object("test", %{id: "delete_object"}, id_attribute: :id)
"test"
|> save_object(%{id: "delete_object"}, id_attribute: :id)
|> wait()

delete_object("test", object_id) |> wait()
"test" |> delete_object(object_id) |> wait()

assert {:error, 404, _} = get_object("test", object_id)
end
Expand All @@ -267,10 +274,11 @@ defmodule AlgoliaTest do
objects = [%{id: "delete_multipel_objects_1"}, %{id: "delete_multipel_objects_2"}]

{:ok, %{"objectIDs" => object_ids}} =
save_objects("test", objects, id_attribute: :id)
"test"
|> save_objects(objects, id_attribute: :id)
|> wait()

delete_objects("test", object_ids) |> wait()
"test" |> delete_objects(object_ids) |> wait()

assert {:error, 404, _} = get_object("test", "delete_multipel_objects_1")
assert {:error, 404, _} = get_object("test", "delete_multipel_objects_2")
Expand Down Expand Up @@ -331,8 +339,8 @@ defmodule AlgoliaTest do

objects = [%{id: "move_1"}, %{id: "move_2"}]

{:ok, _} = save_objects(src, objects, id_attribute: :id) |> wait()
{:ok, _} = move_index(src, dst) |> wait()
{:ok, _} = src |> save_objects(objects, id_attribute: :id) |> wait()
{:ok, _} = src |> move_index(dst) |> wait()

assert {:ok, %{"objectID" => "move_1"}} = get_object(dst, "move_1")
assert {:ok, %{"objectID" => "move_2"}} = get_object(dst, "move_2")
Expand All @@ -344,22 +352,22 @@ defmodule AlgoliaTest do

objects = [%{id: "copy_1"}, %{id: "copy_2"}]

{:ok, _} = save_objects(src, objects, id_attribute: :id) |> wait()
{:ok, _} = copy_index(src, dst) |> wait()
{:ok, _} = src |> save_objects(objects, id_attribute: :id) |> wait()
{:ok, _} = src |> copy_index(dst) |> wait()

assert {:ok, %{"objectID" => "copy_1"}} = get_object(dst, "copy_1")
assert {:ok, %{"objectID" => "copy_2"}} = get_object(dst, "copy_2")
end

test "deletes an index" do
index = "delete_test_index"
add_object(index, %{objectID: "delete_test"}) |> wait()
index |> add_object(%{objectID: "delete_test"}) |> wait()

{:ok, %{"items" => items}} = list_indexes()
all_indexes = Enum.map(items, & &1["name"])
assert index in all_indexes

assert {:ok, _} = delete_index(index) |> wait()
assert {:ok, _} = index |> delete_index() |> wait()
{:ok, %{"items" => items}} = list_indexes()
all_indexes = Enum.map(items, & &1["name"])
refute index in all_indexes
Expand All @@ -371,7 +379,7 @@ defmodule AlgoliaTest do
|> add_object(%{text: "hello"})
|> wait()

{:ok, _} = search("search_index", "test query") |> wait()
{:ok, _} = "search_index" |> search("test query") |> wait()

assert {:ok, %{"logs" => [log]}} =
get_logs(indexName: "search_index", length: 1, type: :query)
Expand Down

0 comments on commit fceca5e

Please sign in to comment.