B MeiliSearch, SearchIndexingWorker: Use Config.Getting, make tests async.
This commit is contained in:
parent
c1402af293
commit
d3f8950588
3 changed files with 72 additions and 48 deletions
|
|
@ -5,51 +5,50 @@
|
|||
defmodule Pleroma.Search.MeilisearchTest do
|
||||
require Pleroma.Constants
|
||||
|
||||
use Pleroma.DataCase
|
||||
use Pleroma.DataCase, async: true
|
||||
use Oban.Testing, repo: Pleroma.Repo
|
||||
|
||||
import Pleroma.Factory
|
||||
import Tesla.Mock
|
||||
import Mock
|
||||
import Mox
|
||||
|
||||
alias Pleroma.Search.Meilisearch
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Workers.SearchIndexingWorker
|
||||
|
||||
setup_all do
|
||||
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
||||
:ok
|
||||
end
|
||||
alias Pleroma.UnstubbedConfigMock, as: Config
|
||||
|
||||
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
|
||||
user = insert(:user)
|
||||
|
||||
mock_global(fn
|
||||
%{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
|
||||
Tesla.Mock.mock(fn
|
||||
%{
|
||||
method: :put,
|
||||
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
||||
body: body
|
||||
} ->
|
||||
assert match?(
|
||||
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
||||
Jason.decode!(body)
|
||||
)
|
||||
|
||||
# To make sure that the worker is called
|
||||
send(self(), "posted_to_meilisearch")
|
||||
json(%{updateId: 1})
|
||||
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} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
|
|
@ -64,8 +63,7 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
)
|
||||
|
||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||
|
||||
assert_called(Meilisearch.add_to_index(activity))
|
||||
assert_received("posted_to_meilisearch")
|
||||
end
|
||||
|
||||
test "doesn't index posts that are not public" do
|
||||
|
|
@ -80,21 +78,26 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
|
||||
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||
|
||||
Config
|
||||
|> expect(:get, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
Meilisearch
|
||||
end)
|
||||
|
||||
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||
|
||||
assert_not_called(Meilisearch.meili_put(:_))
|
||||
end)
|
||||
|
||||
history = call_history(Meilisearch)
|
||||
assert Enum.count(history) == 2
|
||||
end
|
||||
|
||||
test "deletes posts from index when deleted locally" do
|
||||
user = insert(:user)
|
||||
|
||||
mock_global(fn
|
||||
%{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
|
||||
Tesla.Mock.mock(fn
|
||||
%{
|
||||
method: :put,
|
||||
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
||||
body: body
|
||||
} ->
|
||||
assert match?(
|
||||
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
||||
Jason.decode!(body)
|
||||
|
|
@ -103,10 +106,23 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
json(%{updateId: 1})
|
||||
|
||||
%{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
|
||||
send(self(), "called_delete")
|
||||
assert String.length(id) > 1
|
||||
json(%{updateId: 2})
|
||||
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} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
|
|
@ -123,7 +139,7 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
|
||||
assert :ok = perform_job(SearchIndexingWorker, delete_args)
|
||||
|
||||
assert_called(Meilisearch.remove_from_index(:_))
|
||||
assert_received("called_delete")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue