Add tests

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-10-30 21:06:31 +01:00
parent 25a9ff0f3d
commit c1f5138f73
5 changed files with 97 additions and 4 deletions

View file

@ -3,7 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Translation do
@cache_ttl 86_400_000
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
def configured? do
@ -45,7 +44,7 @@ defp get_cache_key(text, source_language, target_language) do
end
defp store_result({:ok, result}, cache_key) do
@cachex.put(:translations_cache, cache_key, result, ttl: @cache_ttl)
@cachex.put(:translations_cache, cache_key, result)
end
defp store_result(_, _), do: nil

View file

@ -142,7 +142,10 @@ def features do
if Config.get([:instance, :profile_directory]) do
"profile_directory"
end,
"pleroma:get:main/ostatus"
"pleroma:get:main/ostatus",
if Pleroma.Translation.configured?() do
"translation"
end
]
|> Enum.filter(& &1)
end
@ -206,7 +209,7 @@ def configuration2 do
configuration()
|> Map.merge(%{
urls: %{streaming: Pleroma.Web.Endpoint.websocket_url()},
translation: %{enabled: Pleroma.Translation.configured?}
translation: %{enabled: Pleroma.Translation.configured?()}
})
end

View file

@ -0,0 +1,29 @@
defmodule Pleroma.TranslationTest do
use Pleroma.Web.ConnCase
alias Pleroma.Translation
# use Oban.Testing, repo: Pleroma.Repo
setup do: clear_config([Pleroma.Translation, :service], TranslationMock)
test "it translates text" do
assert {:ok,
%{
content: "txet emos",
detected_source_language: _,
provider: _
}} = Translation.translate("some text", "en", "uk")
end
test "it stores translation result in cache" do
Translation.translate("some text", "en", "uk")
assert {:ok, result} =
Cachex.get(
:translations_cache,
"en/uk/#{:crypto.hash(:sha256, "some text") |> Base.encode64()}"
)
assert result.content == "txet emos"
end
end

View file

@ -2212,4 +2212,44 @@ test "it returns 404 if the user cannot see the post", %{conn: conn} do
|> json_response_and_validate_schema(:not_found)
end
end
describe "translating statuses" do
setup do: clear_config([Pleroma.Translation, :service], TranslationMock)
test "it translates a status to user language" do
user = insert(:user, language: "fr")
%{conn: conn, user: user} = oauth_access(["read:statuses"], user: user)
another_user = insert(:user)
{:ok, activity} =
CommonAPI.post(another_user, %{
status: "Cześć!",
visibility: "public",
language: "pl"
})
response =
conn
|> post("/api/v1/statuses/#{activity.id}/translate")
|> json_response_and_validate_schema(200)
assert response == %{"content" => "!ćśezC", "detected_source_language" => "pl", "provider" => "TranslationMock"}
end
test "it returns an error if no target language provided" do
%{conn: conn, user: user} = oauth_access(["read:statuses"])
another_user = insert(:user)
{:ok, activity} =
CommonAPI.post(another_user, %{
status: "Cześć!",
language: "pl"
})
response =
conn
|> post("/api/v1/statuses/#{activity.id}/translate")
|> json_response_and_validate_schema(400)
end
end
end

View file

@ -0,0 +1,22 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule TranslationMock do
alias Pleroma.Translation.Service
@behaviour Service
@impl Service
def configured?, do: true
@impl Service
def translate(content, source_language, _target_language) do
{:ok,
%{
content: content |> String.reverse(),
detected_source_language: source_language,
provider: "TranslationMock"
}}
end
end