From c3589f83e9220d093ec6ce8233aee035dc48d06f Mon Sep 17 00:00:00 2001 From: Caique Hitoshi Mitsuoka Date: Mon, 18 Feb 2019 13:31:00 -0300 Subject: [PATCH 01/12] Add Synonyms --- lib/algolia.ex | 105 ++++++++++++++++++++++++++++++++++++++++++ lib/algolia/paths.ex | 9 ++++ test/algolia_test.exs | 32 +++++++++++++ 3 files changed, 146 insertions(+) diff --git a/lib/algolia.ex b/lib/algolia.ex index 647da6a..d589ce7 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -491,6 +491,111 @@ defmodule Algolia do |> inject_index_into_response(index) end + @doc """ + Get all the synonyms of a index. + + Only the index is required + + ## Examples + + iex> Algolia.export_synonyms("index") + {:ok, %{ + "indexName" => "index", + "nbHits" => 33, + "hits" => [%{ + "objectID" => "1539816124475_0", + "synonyms" => ["big", "large"], + "type" => "synonym" + "_highlightResult" => %{...} + }, ...] + }} + """ + def export_synonyms(index) do + get_all_paginated_hits(index, &search_synonyms/3) + end + + defp get_all_paginated_hits(index, search, hits_per_page \\ 100) do + 0 + |> Stream.iterate(&(&1 + 1)) + |> Stream.flat_map(fn page -> get_page_hits(index, page, hits_per_page, search) end) + |> Stream.take_while(&(&1 != :stop)) + end + + defp get_page_hits(index, page, hits_per_page, search) do + case search.(index, "", page: page, hits_per_page: hits_per_page) do + {:ok, %{"hits" => hits, "nbPages" => pages}} when page + 1 < pages -> page_hits(hits) + {:ok, %{"hits" => hits}} -> page_hits(hits) ++ [:stop] + error -> [error, :stop] + end + end + + defp page_hits(hits) do + Enum.map(hits, &{:ok, Map.drop(&1, ["_highlightResult"])}) + end + + @doc """ + Search for synonyms of a index matching a query + + Allowed parameters: + + * `query` Required + * `type` Defaults to `""`(all). Allowed the types: `"synonym,oneWaySynonym,altCorrection1,altCorrection2,placeholder"` + * `page` + * `hitsPerPage` + + ## Examples + + iex> Algolia.search_synonyms("index", "query", hits_per_page: 20) + {:ok, %{ + "indexName" => "index", + "nbHits" => 33, + "hits" => [%{ + "objectID" => "1539816124475_0", + "synonyms" => ["big", "large"], + "type" => "synonym" + "_highlightResult" => %{...} + }, ...] + }} + """ + def search_synonyms(index, query, opts \\ []) do + body = + opts + |> Enum.into(%{}) + |> Map.put("query", query) + |> Map.put("page", opts[:page] || 0) + |> Map.put("hitsPerPage", opts[:hits_per_page] || 20) + |> Map.drop([:page, :hits_per_page]) + |> Jason.encode!() + + :write + |> send_request(%{method: :post, path: Paths.search_synonyms(index), body: body}) + |> inject_index_into_response(index) + end + + @doc """ + Create or update an list of synonyms. + + Allowed params: + * `forwardToReplicas` + * `replaceExistingSynonyms` + * `synonyms` Required: With [synonyms objects](https://www.algolia.com/doc/api-reference/api-methods/save-synonym/#method-param-synonym-object). + * - `objectID` Required: If the Id do not exist it will be created. + * - `type` Required: Allowed the types: `"synonym,oneWaySynonym,altCorrection1,altCorrection2,placeholder"`. + * - `synonyms` Required if type=synonym or type=oneWaySynonym: List of strings. + * - `input` Required if type=oneWaySynonym. + * - `word` Required if type=altCorrection1 or type=altCorrection2. + * - `corrections` Required if type=altCorrection1 or type=altCorrection2 + * - `placeholder` Required if type=placeholder + * - `replacements` Required if type=placeholder + """ + def batch_synonyms(index, batch, opts \\ []) do + body = Jason.encode!(batch) + + :write + |> send_request(%{method: :post, path: Paths.batch_synonyms(index, opts), body: body}) + |> inject_index_into_response(index) + end + @doc """ Moves an index to new one """ diff --git a/lib/algolia/paths.ex b/lib/algolia/paths.ex index 8383ba2..1460fae 100644 --- a/lib/algolia/paths.ex +++ b/lib/algolia/paths.ex @@ -43,6 +43,15 @@ defmodule Algolia.Paths do def settings(index), do: index(index) <> "/settings" + def synonyms(index), do: index(index) <> "/synonyms" + + def search_synonyms(index), do: synonyms(index) <> "/search" + + def batch_synonyms(index, opts \\ []) do + params = Keyword.take(opts, [:forward_to_replicas, :replace_existing_synonyms]) + synonyms(index) <> "/batch" <> to_query(params) + end + def logs(opts) do params = Keyword.take(opts, [:indexName, :offset, :length, :type]) "/#{@version}/logs" <> to_query(params) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index 103191f..7022ae9 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -370,4 +370,36 @@ defmodule AlgoliaTest do %{"index" => "test", "query_headers" => headers} = log assert headers =~ ~r/X-Forwarded-For: 1\.2\.3\.4/ end + + test "create or update synonyms" do + synonyms = [ + %{ + "objectID" => "1550092819012", + "synonyms" => ["big", "large", "huge"], + "type" => "synonym" + }, + %{ + "synonyms" => ["tiny"], + "type" => "oneWaySynonym", + "objectID" => "785493758483", + "input" => "little" + } + ] + + {:ok, _} = + @settings_test_index + |> batch_synonyms(synonyms, replace_existing_synonyms: true) + |> wait() + + hits = @settings_test_index |> export_synonyms() |> Enum.map(& &1) + + for {:ok, hit} <- hits do + synonym = Enum.find(synonyms, &(&1["objectID"] == hit["objectID"])) + + assert synonym["synonyms"] == hit["synonyms"] + assert synonym["type"] == hit["type"] + assert synonym["objectID"] == hit["objectID"] + assert synonym["input"] == hit["input"] + end + end end From 99a3f7a3251c15f644416fa18a850f2016f4df98 Mon Sep 17 00:00:00 2001 From: Caique Hitoshi Mitsuoka Date: Mon, 18 Feb 2019 13:31:30 -0300 Subject: [PATCH 02/12] Add Query Rules --- lib/algolia.ex | 47 +++++++++++++++++++++++++++++++ lib/algolia/paths.ex | 9 ++++++ test/algolia_test.exs | 64 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) diff --git a/lib/algolia.ex b/lib/algolia.ex index d589ce7..d74a96b 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -596,6 +596,53 @@ defmodule Algolia do |> inject_index_into_response(index) end + @doc """ + Search for query rules of a index matching a query + + Allowed parameters: + + * `query` Required + * `page` + * `hitsPerPage` + """ + def search_rules(index, query, opts \\ []) do + body = + opts + |> Enum.into(%{}) + |> Map.put("query", query) + |> Map.put("page", opts[:page] || 0) + |> Map.put("hitsPerPage", opts[:hits_per_page] || 20) + |> Map.drop([:page, :hits_per_page]) + |> Jason.encode!() + + :write + |> send_request(%{method: :post, path: Paths.search_rules(index), body: body}) + |> inject_index_into_response(index) + end + + @doc """ + Get all the query rules of a index. + + Only the index is required + """ + def export_rules(index) do + get_all_paginated_hits(index, &search_rules/3) + end + + @doc """ + Create or update an list of synonyms. + + Allowed params: + + """ + def batch_rules(index, batch, opts \\ []) do + body = Jason.encode!(batch) + + :write + |> send_request(%{method: :post, path: Paths.batch_rules(index, opts), body: body}) + |> inject_index_into_response(index) + end + @doc """ Moves an index to new one """ diff --git a/lib/algolia/paths.ex b/lib/algolia/paths.ex index 1460fae..34864ac 100644 --- a/lib/algolia/paths.ex +++ b/lib/algolia/paths.ex @@ -52,6 +52,15 @@ defmodule Algolia.Paths do synonyms(index) <> "/batch" <> to_query(params) end + def rules(index), do: index(index) <> "/rules" + + def search_rules(index), do: rules(index) <> "/search" + + def batch_rules(index, opts \\ []) do + params = Keyword.take(opts, [:forward_to_replicas, :replace_existing_synonyms]) + rules(index) <> "/batch" <> to_query(params) + end + def logs(opts) do params = Keyword.take(opts, [:indexName, :offset, :length, :type]) "/#{@version}/logs" <> to_query(params) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index 7022ae9..8319588 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -402,4 +402,68 @@ defmodule AlgoliaTest do assert synonym["input"] == hit["input"] end end + + test "create or update rules" do + rules = [ + %{ + "condition" => %{ + "anchoring" => "contains", + "pattern" => "name" + }, + "consequence" => %{ + "params" => %{ + "query" => %{ + "edits" => [ + %{ + "delete" => "name", + "type" => "remove" + } + ] + } + } + }, + "description" => "", + "objectID" => "1550012768674" + }, + %{ + "condition" => %{ + "anchoring" => "contains", + "pattern" => "yellow" + }, + "consequence" => %{ + "params" => %{ + "query" => %{ + "edits" => [ + %{ + "type" => "replace", + "delete" => "yellow", + "insert" => "blue" + } + ] + } + } + }, + "description" => "", + "enabled" => true, + "objectID" => "1548387674495" + } + ] + + {:ok, _} = + @settings_test_index + |> batch_rules(rules, replace_existing_synonyms: true) + |> wait() + + hits = @settings_test_index |> export_rules() |> Enum.map(& &1) + + for {:ok, hit} <- hits do + query_rule = Enum.find(rules, &(&1["objectID"] == hit["objectID"])) + + assert query_rule["condition"] == hit["condition"] + assert query_rule["consequence"] == hit["consequence"] + assert query_rule["description"] == hit["description"] + assert query_rule["enabled"] == hit["enabled"] + assert query_rule["objectID"] == hit["objectID"] + end + end end From bab02ff01bf4a4684c59c068bd4f08ed513195f5 Mon Sep 17 00:00:00 2001 From: Caique Hitoshi Mitsuoka Date: Sun, 24 Feb 2019 19:38:20 -0300 Subject: [PATCH 03/12] Update documentation --- lib/algolia.ex | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/algolia.ex b/lib/algolia.ex index d74a96b..86382e7 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -575,18 +575,19 @@ defmodule Algolia do @doc """ Create or update an list of synonyms. - Allowed params: - * `forwardToReplicas` - * `replaceExistingSynonyms` * `synonyms` Required: With [synonyms objects](https://www.algolia.com/doc/api-reference/api-methods/save-synonym/#method-param-synonym-object). * - `objectID` Required: If the Id do not exist it will be created. * - `type` Required: Allowed the types: `"synonym,oneWaySynonym,altCorrection1,altCorrection2,placeholder"`. * - `synonyms` Required if type=synonym or type=oneWaySynonym: List of strings. * - `input` Required if type=oneWaySynonym. * - `word` Required if type=altCorrection1 or type=altCorrection2. - * - `corrections` Required if type=altCorrection1 or type=altCorrection2 - * - `placeholder` Required if type=placeholder - * - `replacements` Required if type=placeholder + * - `corrections` Required if type=altCorrection1 or type=altCorrection2. + * - `placeholder` Required if type=placeholder. + * - `replacements` Required if type=placeholder. + + Allowed params: + * `forwardToReplicas` + * `replaceExistingSynonyms` """ def batch_synonyms(index, batch, opts \\ []) do body = Jason.encode!(batch) @@ -630,10 +631,29 @@ defmodule Algolia do end @doc """ - Create or update an list of synonyms. + Create or update an list of Query Rules. - Allowed params: + * `batch` Required: list with [Query Rules](https://www.algolia.com/doc/api-reference/api-methods/save-rule/#method-param-rule) + * - `objectID` Required: If the Id do not exist it will be created. + * - `description` To ease searching for rules and presenting them to human readers. + * - `enabled` Whether the rule is enabled. Disabled rules remain in the index, but are not applied at query time. + * - `validity` By default, rules are permanently valid. When validity periods are specified, the rule applies only during those periods. + * - `condition` Required: [condition](https://www.algolia.com/doc/api-reference/api-methods/save-rule/?language=javascript#method-param-condition-2) + * -- `pattern` Required: Query patterns are expressed as a string with a specific syntax. + * -- `anchoring` Required: Enum `["is", "startsWith", "endsWith", "contains"]`. + * -- `context`: Rule context. When specified, the rule is contextual and applies only when the same context is specified at query time. + * - `consequence` Required at least 1 [consequence](https://www.algolia.com/doc/api-reference/api-methods/save-rule/?language=javascript#method-param-consequence-2) + * -- `params`: Additional search parameters. Any valid search parameter is allowed. + * -- `promote`: List with objects to promote as hits. + * --- `objectID` + * --- `position` + * -- `hide`: List with objects to hide from hits. + * --- `objectID` + * -- `userData`: Custom JSON object that will be appended to the userData array in the response. + Allowed params: + * `forwardToReplicas` When true, the change is forwarded to all replicas of this index. + * `clearExistingRules` When true, existing rules are cleared before adding this batch. """ def batch_rules(index, batch, opts \\ []) do body = Jason.encode!(batch) From 5d3489343ff44fd3593430de8a411a9db1f0b286 Mon Sep 17 00:00:00 2001 From: Caique Hitoshi Mitsuoka Date: Sun, 24 Feb 2019 19:41:25 -0300 Subject: [PATCH 04/12] Fix query rule param --- lib/algolia.ex | 8 ++++---- lib/algolia/paths.ex | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/algolia.ex b/lib/algolia.ex index 86382e7..03826b8 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -586,8 +586,8 @@ defmodule Algolia do * - `replacements` Required if type=placeholder. Allowed params: - * `forwardToReplicas` - * `replaceExistingSynonyms` + * `forward_to_replicas` + * `replace_existing_synonyms` """ def batch_synonyms(index, batch, opts \\ []) do body = Jason.encode!(batch) @@ -652,8 +652,8 @@ defmodule Algolia do * -- `userData`: Custom JSON object that will be appended to the userData array in the response. Allowed params: - * `forwardToReplicas` When true, the change is forwarded to all replicas of this index. - * `clearExistingRules` When true, existing rules are cleared before adding this batch. + * `forward_to_replicas` When true, the change is forwarded to all replicas of this index. + * `clear_existing_rules` When true, existing rules are cleared before adding this batch. """ def batch_rules(index, batch, opts \\ []) do body = Jason.encode!(batch) diff --git a/lib/algolia/paths.ex b/lib/algolia/paths.ex index 34864ac..abfabf1 100644 --- a/lib/algolia/paths.ex +++ b/lib/algolia/paths.ex @@ -57,7 +57,7 @@ defmodule Algolia.Paths do def search_rules(index), do: rules(index) <> "/search" def batch_rules(index, opts \\ []) do - params = Keyword.take(opts, [:forward_to_replicas, :replace_existing_synonyms]) + params = Keyword.take(opts, [:forward_to_replicas, :clearExistingRules]) rules(index) <> "/batch" <> to_query(params) end From 12149317303febe71fe936d88a9f08db30057709 Mon Sep 17 00:00:00 2001 From: Caique Hitoshi Mitsuoka Date: Tue, 26 Feb 2019 13:53:22 -0300 Subject: [PATCH 05/12] Remove params sanitization from Path We are removing params sanitization since Algolia already validates it. With that we can keep the lib as thin as possible and as close as possible with the documentation. --- lib/algolia.ex | 8 ++++---- lib/algolia/paths.ex | 11 ++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/algolia.ex b/lib/algolia.ex index 03826b8..86382e7 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -586,8 +586,8 @@ defmodule Algolia do * - `replacements` Required if type=placeholder. Allowed params: - * `forward_to_replicas` - * `replace_existing_synonyms` + * `forwardToReplicas` + * `replaceExistingSynonyms` """ def batch_synonyms(index, batch, opts \\ []) do body = Jason.encode!(batch) @@ -652,8 +652,8 @@ defmodule Algolia do * -- `userData`: Custom JSON object that will be appended to the userData array in the response. Allowed params: - * `forward_to_replicas` When true, the change is forwarded to all replicas of this index. - * `clear_existing_rules` When true, existing rules are cleared before adding this batch. + * `forwardToReplicas` When true, the change is forwarded to all replicas of this index. + * `clearExistingRules` When true, existing rules are cleared before adding this batch. """ def batch_rules(index, batch, opts \\ []) do body = Jason.encode!(batch) diff --git a/lib/algolia/paths.ex b/lib/algolia/paths.ex index abfabf1..82a1c39 100644 --- a/lib/algolia/paths.ex +++ b/lib/algolia/paths.ex @@ -48,8 +48,7 @@ defmodule Algolia.Paths do def search_synonyms(index), do: synonyms(index) <> "/search" def batch_synonyms(index, opts \\ []) do - params = Keyword.take(opts, [:forward_to_replicas, :replace_existing_synonyms]) - synonyms(index) <> "/batch" <> to_query(params) + synonyms(index) <> "/batch" <> to_query(opts) end def rules(index), do: index(index) <> "/rules" @@ -57,13 +56,11 @@ defmodule Algolia.Paths do def search_rules(index), do: rules(index) <> "/search" def batch_rules(index, opts \\ []) do - params = Keyword.take(opts, [:forward_to_replicas, :clearExistingRules]) - rules(index) <> "/batch" <> to_query(params) + rules(index) <> "/batch" <> to_query(opts) end - def logs(opts) do - params = Keyword.take(opts, [:indexName, :offset, :length, :type]) - "/#{@version}/logs" <> to_query(params) + def logs(opts \\ []) do + "/#{@version}/logs" <> to_query(opts) end defp to_query([]), do: "" From 71c2ad963a619520ddfbf44d9ecb607459e3ed70 Mon Sep 17 00:00:00 2001 From: Caique Hitoshi Mitsuoka Date: Tue, 26 Feb 2019 14:08:11 -0300 Subject: [PATCH 06/12] Add query params to Algolia.set_settings --- lib/algolia.ex | 4 ++-- lib/algolia/paths.ex | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/algolia.ex b/lib/algolia.ex index 86382e7..e5928ff 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -473,9 +473,9 @@ defmodule Algolia do @doc """ Set the settings of a index """ - def set_settings(index, settings) do + def set_settings(index, settings, opts \\ []) do body = Jason.encode!(settings) - path = Paths.settings(index) + path = Paths.settings(index, opts) :write |> send_request(%{method: :put, path: path, body: body}) diff --git a/lib/algolia/paths.ex b/lib/algolia/paths.ex index 82a1c39..2b49f93 100644 --- a/lib/algolia/paths.ex +++ b/lib/algolia/paths.ex @@ -41,7 +41,7 @@ defmodule Algolia.Paths do def delete_by(index), do: index(index) <> "/deleteByQuery" - def settings(index), do: index(index) <> "/settings" + def settings(index, opts \\ []), do: index(index) <> "/settings" <> to_query(opts) def synonyms(index), do: index(index) <> "/synonyms" From dc4a9f312b4a3b9460601d092806e3106af13ed3 Mon Sep 17 00:00:00 2001 From: Elaine Watanabe Date: Fri, 6 Mar 2020 19:38:12 -0300 Subject: [PATCH 07/12] Fix synonyms pagination --- lib/algolia.ex | 18 +++++++++++++----- test/algolia_test.exs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/algolia.ex b/lib/algolia.ex index e5928ff..738ed92 100644 --- a/lib/algolia.ex +++ b/lib/algolia.ex @@ -510,8 +510,8 @@ defmodule Algolia do }, ...] }} """ - def export_synonyms(index) do - get_all_paginated_hits(index, &search_synonyms/3) + def export_synonyms(index, hits_per_page \\ 100) do + get_all_paginated_hits(index, &search_synonyms/3, hits_per_page) end defp get_all_paginated_hits(index, search, hits_per_page \\ 100) do @@ -523,9 +523,17 @@ defmodule Algolia do defp get_page_hits(index, page, hits_per_page, search) do case search.(index, "", page: page, hits_per_page: hits_per_page) do - {:ok, %{"hits" => hits, "nbPages" => pages}} when page + 1 < pages -> page_hits(hits) - {:ok, %{"hits" => hits}} -> page_hits(hits) ++ [:stop] - error -> [error, :stop] + {:ok, %{"hits" => hits, "nbPages" => pages}} when page + 1 < pages -> + page_hits(hits) + + {:ok, %{"hits" => hits, "nbHits" => nbHits}} when page + 1 < nbHits / hits_per_page -> + page_hits(hits) + + {:ok, %{"hits" => hits}} -> + page_hits(hits) ++ [:stop] + + error -> + [error, :stop] end end diff --git a/test/algolia_test.exs b/test/algolia_test.exs index 8319588..a478d88 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -403,6 +403,44 @@ defmodule AlgoliaTest do end end + test "export the complete synonyms list" do + synonyms = [ + %{ + "objectID" => "1550092819012", + "synonyms" => ["big", "large", "huge"], + "type" => "synonym" + }, + %{ + "synonyms" => ["tiny"], + "type" => "oneWaySynonym", + "objectID" => "785493758483", + "input" => "little" + }, + %{ + "synonyms" => ["small"], + "type" => "oneWaySynonym", + "objectID" => "785493768501", + "input" => "little" + } + ] + + {:ok, _} = + @settings_test_index + |> batch_synonyms(synonyms, replace_existing_synonyms: true) + |> wait() + + hits = @settings_test_index |> export_synonyms(2) |> Enum.map(& &1) + + for {:ok, hit} <- hits do + synonym = Enum.find(synonyms, &(&1["objectID"] == hit["objectID"])) + + assert synonym["synonyms"] == hit["synonyms"] + assert synonym["type"] == hit["type"] + assert synonym["objectID"] == hit["objectID"] + assert synonym["input"] == hit["input"] + end + end + test "create or update rules" do rules = [ %{ From ecb1f49e62c6b64ec1c9f10c53b9793122163b15 Mon Sep 17 00:00:00 2001 From: Elaine Watanabe Date: Fri, 6 Mar 2020 19:52:13 -0300 Subject: [PATCH 08/12] Improve tests --- test/algolia_test.exs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index a478d88..4099ea1 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -406,21 +406,21 @@ defmodule AlgoliaTest do test "export the complete synonyms list" do synonyms = [ %{ - "objectID" => "1550092819012", + "objectID" => "1550092817624", "synonyms" => ["big", "large", "huge"], "type" => "synonym" }, %{ "synonyms" => ["tiny"], "type" => "oneWaySynonym", - "objectID" => "785493758483", + "objectID" => "785493759172", "input" => "little" }, %{ - "synonyms" => ["small"], + "synonyms" => ["short"], "type" => "oneWaySynonym", "objectID" => "785493768501", - "input" => "little" + "input" => "small" } ] From 9829f2ad4164541ee40810636c2947aec9d7360e Mon Sep 17 00:00:00 2001 From: Elaine Watanabe Date: Fri, 6 Mar 2020 19:55:11 -0300 Subject: [PATCH 09/12] Fix broken test --- test/algolia_test.exs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index 4099ea1..67d5429 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -383,6 +383,12 @@ defmodule AlgoliaTest do "type" => "oneWaySynonym", "objectID" => "785493758483", "input" => "little" + }, + %{ + "synonyms" => ["short"], + "type" => "oneWaySynonym", + "objectID" => "785493768501", + "input" => "small" } ] From 8117fe5a53321477a1a8b2048f4e7b27968ee4c1 Mon Sep 17 00:00:00 2001 From: Elaine Watanabe Date: Fri, 6 Mar 2020 19:59:00 -0300 Subject: [PATCH 10/12] Remove unnecessary test --- test/algolia_test.exs | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index 67d5429..b78aff0 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -372,44 +372,6 @@ defmodule AlgoliaTest do end test "create or update synonyms" do - synonyms = [ - %{ - "objectID" => "1550092819012", - "synonyms" => ["big", "large", "huge"], - "type" => "synonym" - }, - %{ - "synonyms" => ["tiny"], - "type" => "oneWaySynonym", - "objectID" => "785493758483", - "input" => "little" - }, - %{ - "synonyms" => ["short"], - "type" => "oneWaySynonym", - "objectID" => "785493768501", - "input" => "small" - } - ] - - {:ok, _} = - @settings_test_index - |> batch_synonyms(synonyms, replace_existing_synonyms: true) - |> wait() - - hits = @settings_test_index |> export_synonyms() |> Enum.map(& &1) - - for {:ok, hit} <- hits do - synonym = Enum.find(synonyms, &(&1["objectID"] == hit["objectID"])) - - assert synonym["synonyms"] == hit["synonyms"] - assert synonym["type"] == hit["type"] - assert synonym["objectID"] == hit["objectID"] - assert synonym["input"] == hit["input"] - end - end - - test "export the complete synonyms list" do synonyms = [ %{ "objectID" => "1550092817624", From 7c49399b2ce45ff8a8bb578d547b59923f73bb59 Mon Sep 17 00:00:00 2001 From: Elaine Watanabe Date: Tue, 10 Mar 2020 17:34:28 -0300 Subject: [PATCH 11/12] Improve test --- test/algolia_test.exs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index b78aff0..94c0bd0 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -397,9 +397,11 @@ defmodule AlgoliaTest do |> batch_synonyms(synonyms, replace_existing_synonyms: true) |> wait() - hits = @settings_test_index |> export_synonyms(2) |> Enum.map(& &1) + {:ok, hits} = @settings_test_index |> export_synonyms(2) |> Enum.map(& &1) - for {:ok, hit} <- hits do + assert Enum.count(synonyms) == Enum.count(hits) + + for hit <- hits do synonym = Enum.find(synonyms, &(&1["objectID"] == hit["objectID"])) assert synonym["synonyms"] == hit["synonyms"] From 4df259f5bcbf82ed93144db0044f3c116a83f38c Mon Sep 17 00:00:00 2001 From: Elaine Watanabe Date: Tue, 10 Mar 2020 17:42:12 -0300 Subject: [PATCH 12/12] Fix broken spec --- test/algolia_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/algolia_test.exs b/test/algolia_test.exs index 94c0bd0..ecef561 100644 --- a/test/algolia_test.exs +++ b/test/algolia_test.exs @@ -397,11 +397,11 @@ defmodule AlgoliaTest do |> batch_synonyms(synonyms, replace_existing_synonyms: true) |> wait() - {:ok, hits} = @settings_test_index |> export_synonyms(2) |> Enum.map(& &1) + hits = @settings_test_index |> export_synonyms(2) |> Enum.map(& &1) assert Enum.count(synonyms) == Enum.count(hits) - for hit <- hits do + for {:ok, hit} <- hits do synonym = Enum.find(synonyms, &(&1["objectID"] == hit["objectID"])) assert synonym["synonyms"] == hit["synonyms"]