B MeiliSearch, SearchIndexingWorker: Use Config.Getting, make tests async.
This commit is contained in:
parent
c1402af293
commit
d3f8950588
|
@ -3,6 +3,7 @@ defmodule Pleroma.Search.Meilisearch do
|
||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
|
alias Pleroma.Config.Getting, as: Config
|
||||||
|
|
||||||
import Pleroma.Search.DatabaseSearch
|
import Pleroma.Search.DatabaseSearch
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
@ -10,14 +11,14 @@ defmodule Pleroma.Search.Meilisearch do
|
||||||
@behaviour Pleroma.Search.SearchBackend
|
@behaviour Pleroma.Search.SearchBackend
|
||||||
|
|
||||||
defp meili_headers do
|
defp meili_headers do
|
||||||
private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
private_key = Config.get([Pleroma.Search.Meilisearch, :private_key])
|
||||||
|
|
||||||
[{"Content-Type", "application/json"}] ++
|
[{"Content-Type", "application/json"}] ++
|
||||||
if is_nil(private_key), do: [], else: [{"Authorization", "Bearer #{private_key}"}]
|
if is_nil(private_key), do: [], else: [{"Authorization", "Bearer #{private_key}"}]
|
||||||
end
|
end
|
||||||
|
|
||||||
def meili_get(path) do
|
def meili_get(path) do
|
||||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||||
|
|
||||||
result =
|
result =
|
||||||
Pleroma.HTTP.get(
|
Pleroma.HTTP.get(
|
||||||
|
@ -31,7 +32,7 @@ def meili_get(path) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def meili_post(path, params) do
|
def meili_post(path, params) do
|
||||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||||
|
|
||||||
result =
|
result =
|
||||||
Pleroma.HTTP.post(
|
Pleroma.HTTP.post(
|
||||||
|
@ -46,7 +47,7 @@ def meili_post(path, params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def meili_put(path, params) do
|
def meili_put(path, params) do
|
||||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||||
|
|
||||||
result =
|
result =
|
||||||
Pleroma.HTTP.request(
|
Pleroma.HTTP.request(
|
||||||
|
@ -63,15 +64,20 @@ def meili_put(path, params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def meili_delete(path) do
|
def meili_delete(path) do
|
||||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||||
|
|
||||||
|
with {:ok, _} <-
|
||||||
Pleroma.HTTP.request(
|
Pleroma.HTTP.request(
|
||||||
:delete,
|
:delete,
|
||||||
Path.join(endpoint, path),
|
Path.join(endpoint, path),
|
||||||
"",
|
"",
|
||||||
meili_headers(),
|
meili_headers(),
|
||||||
[]
|
[]
|
||||||
)
|
) do
|
||||||
|
:ok
|
||||||
|
else
|
||||||
|
_ -> :error
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
|
@ -3,10 +3,12 @@ defmodule Pleroma.Workers.SearchIndexingWorker do
|
||||||
|
|
||||||
@impl Oban.Worker
|
@impl Oban.Worker
|
||||||
|
|
||||||
|
alias Pleroma.Config.Getting, as: Config
|
||||||
|
|
||||||
def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) do
|
def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) do
|
||||||
activity = Pleroma.Activity.get_by_id_with_object(activity_id)
|
activity = Pleroma.Activity.get_by_id_with_object(activity_id)
|
||||||
|
|
||||||
search_module = Pleroma.Config.get([Pleroma.Search, :module])
|
search_module = Config.get([Pleroma.Search, :module])
|
||||||
|
|
||||||
search_module.add_to_index(activity)
|
search_module.add_to_index(activity)
|
||||||
end
|
end
|
||||||
|
@ -14,7 +16,7 @@ def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) do
|
||||||
def perform(%Job{args: %{"op" => "remove_from_index", "object" => object_id}}) do
|
def perform(%Job{args: %{"op" => "remove_from_index", "object" => object_id}}) do
|
||||||
object = Pleroma.Object.get_by_id(object_id)
|
object = Pleroma.Object.get_by_id(object_id)
|
||||||
|
|
||||||
search_module = Pleroma.Config.get([Pleroma.Search, :module])
|
search_module = Config.get([Pleroma.Search, :module])
|
||||||
|
|
||||||
search_module.remove_from_index(object)
|
search_module.remove_from_index(object)
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,51 +5,50 @@
|
||||||
defmodule Pleroma.Search.MeilisearchTest do
|
defmodule Pleroma.Search.MeilisearchTest do
|
||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase, async: true
|
||||||
use Oban.Testing, repo: Pleroma.Repo
|
use Oban.Testing, repo: Pleroma.Repo
|
||||||
|
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
import Tesla.Mock
|
import Tesla.Mock
|
||||||
import Mock
|
import Mox
|
||||||
|
|
||||||
alias Pleroma.Search.Meilisearch
|
alias Pleroma.Search.Meilisearch
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
alias Pleroma.Workers.SearchIndexingWorker
|
alias Pleroma.Workers.SearchIndexingWorker
|
||||||
|
alias Pleroma.UnstubbedConfigMock, as: Config
|
||||||
setup_all do
|
|
||||||
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "meilisearch" do
|
describe "meilisearch" do
|
||||||
setup do: clear_config([Pleroma.Search, :module], Meilisearch)
|
|
||||||
|
|
||||||
setup_with_mocks(
|
|
||||||
[
|
|
||||||
{Meilisearch, [:passthrough],
|
|
||||||
[
|
|
||||||
add_to_index: fn a -> passthrough([a]) end,
|
|
||||||
remove_from_index: fn a -> passthrough([a]) end,
|
|
||||||
meili_put: fn u, a -> passthrough([u, a]) end
|
|
||||||
]}
|
|
||||||
],
|
|
||||||
context,
|
|
||||||
do: {:ok, context}
|
|
||||||
)
|
|
||||||
|
|
||||||
test "indexes a local post on creation" do
|
test "indexes a local post on creation" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
mock_global(fn
|
Tesla.Mock.mock(fn
|
||||||
%{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
|
%{
|
||||||
|
method: :put,
|
||||||
|
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
||||||
|
body: body
|
||||||
|
} ->
|
||||||
assert match?(
|
assert match?(
|
||||||
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
||||||
Jason.decode!(body)
|
Jason.decode!(body)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# To make sure that the worker is called
|
||||||
|
send(self(), "posted_to_meilisearch")
|
||||||
json(%{updateId: 1})
|
json(%{updateId: 1})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Config
|
||||||
|
|> expect(:get, 3, fn
|
||||||
|
[Pleroma.Search, :module], nil ->
|
||||||
|
Meilisearch
|
||||||
|
|
||||||
|
[Pleroma.Search.Meilisearch, :url], nil ->
|
||||||
|
"http://127.0.0.1:7700"
|
||||||
|
|
||||||
|
[Pleroma.Search.Meilisearch, :private_key], nil ->
|
||||||
|
"secret"
|
||||||
|
end)
|
||||||
|
|
||||||
{:ok, activity} =
|
{:ok, activity} =
|
||||||
CommonAPI.post(user, %{
|
CommonAPI.post(user, %{
|
||||||
status: "guys i just don't wanna leave the swamp",
|
status: "guys i just don't wanna leave the swamp",
|
||||||
|
@ -64,8 +63,7 @@ test "indexes a local post on creation" do
|
||||||
)
|
)
|
||||||
|
|
||||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||||
|
assert_received("posted_to_meilisearch")
|
||||||
assert_called(Meilisearch.add_to_index(activity))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "doesn't index posts that are not public" do
|
test "doesn't index posts that are not public" do
|
||||||
|
@ -80,21 +78,26 @@ test "doesn't index posts that are not public" do
|
||||||
|
|
||||||
args = %{"op" => "add_to_index", "activity" => activity.id}
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||||
|
|
||||||
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
Config
|
||||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
|> expect(:get, fn
|
||||||
|
[Pleroma.Search, :module], nil ->
|
||||||
assert_not_called(Meilisearch.meili_put(:_))
|
Meilisearch
|
||||||
end)
|
end)
|
||||||
|
|
||||||
history = call_history(Meilisearch)
|
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
||||||
assert Enum.count(history) == 2
|
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "deletes posts from index when deleted locally" do
|
test "deletes posts from index when deleted locally" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
mock_global(fn
|
Tesla.Mock.mock(fn
|
||||||
%{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
|
%{
|
||||||
|
method: :put,
|
||||||
|
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
||||||
|
body: body
|
||||||
|
} ->
|
||||||
assert match?(
|
assert match?(
|
||||||
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
||||||
Jason.decode!(body)
|
Jason.decode!(body)
|
||||||
|
@ -103,10 +106,23 @@ test "deletes posts from index when deleted locally" do
|
||||||
json(%{updateId: 1})
|
json(%{updateId: 1})
|
||||||
|
|
||||||
%{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
|
%{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
|
||||||
|
send(self(), "called_delete")
|
||||||
assert String.length(id) > 1
|
assert String.length(id) > 1
|
||||||
json(%{updateId: 2})
|
json(%{updateId: 2})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Config
|
||||||
|
|> expect(:get, 6, fn
|
||||||
|
[Pleroma.Search, :module], nil ->
|
||||||
|
Meilisearch
|
||||||
|
|
||||||
|
[Pleroma.Search.Meilisearch, :url], nil ->
|
||||||
|
"http://127.0.0.1:7700"
|
||||||
|
|
||||||
|
[Pleroma.Search.Meilisearch, :private_key], nil ->
|
||||||
|
"secret"
|
||||||
|
end)
|
||||||
|
|
||||||
{:ok, activity} =
|
{:ok, activity} =
|
||||||
CommonAPI.post(user, %{
|
CommonAPI.post(user, %{
|
||||||
status: "guys i just don't wanna leave the swamp",
|
status: "guys i just don't wanna leave the swamp",
|
||||||
|
@ -123,7 +139,7 @@ test "deletes posts from index when deleted locally" do
|
||||||
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
|
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
|
||||||
assert :ok = perform_job(SearchIndexingWorker, delete_args)
|
assert :ok = perform_job(SearchIndexingWorker, delete_args)
|
||||||
|
|
||||||
assert_called(Meilisearch.remove_from_index(:_))
|
assert_received("called_delete")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue