From 4442fc40e61882090f44b3954daabac54a7b81c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 3 Nov 2022 22:43:20 +0100 Subject: [PATCH] Add test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/language/language_detector.ex | 4 +-- .../language/language_detector_test.ex | 31 +++++++++++++++++++ test/pleroma/language/translation_test.ex | 5 ++- test/support/language_detector_mock.ex | 18 +++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/pleroma/language/language_detector_test.ex create mode 100644 test/support/language_detector_mock.ex diff --git a/lib/pleroma/language/language_detector.ex b/lib/pleroma/language/language_detector.ex index 3901a8b90b..b19eb45711 100644 --- a/lib/pleroma/language/language_detector.ex +++ b/lib/pleroma/language/language_detector.ex @@ -11,7 +11,7 @@ def missing_dependencies do if provider do provider.missing_dependencies() else - nil + [] end end @@ -28,7 +28,7 @@ def detect(text) do end end - defp get_provider() do + defp get_provider do Pleroma.Config.get([__MODULE__, :provider]) end end diff --git a/test/pleroma/language/language_detector_test.ex b/test/pleroma/language/language_detector_test.ex new file mode 100644 index 0000000000..4d9af33bf7 --- /dev/null +++ b/test/pleroma/language/language_detector_test.ex @@ -0,0 +1,31 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Language.LanguageDetectorTest do + use Pleroma.Web.ConnCase + + alias Pleroma.Language.LanguageDetector + + setup do: clear_config([Pleroma.Language.LanguageDetector, :provider], LanguageDetectorMock) + + test "it detects text language" do + detected_language = LanguageDetector.detect("Je viens d'atterrir en Tchéquie.") + + assert detected_language == "fr" + end + + test "it returns nil if text is not long enough" do + detected_language = LanguageDetector.detect("it returns nil") + + assert detected_language == nil + end + + test "it returns nil if no provider specified" do + clear_config([Pleroma.Language.LanguageDetector, :provider], nil) + + detected_language = LanguageDetector.detect("this should also return nil") + + assert detected_language == nil + end +end diff --git a/test/pleroma/language/translation_test.ex b/test/pleroma/language/translation_test.ex index ecab3d20f7..c518e74918 100644 --- a/test/pleroma/language/translation_test.ex +++ b/test/pleroma/language/translation_test.ex @@ -1,8 +1,11 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Language.TranslationTest do use Pleroma.Web.ConnCase alias Pleroma.Language.Translation - # use Oban.Testing, repo: Pleroma.Repo setup do: clear_config([Pleroma.Language.Translation, :provider], TranslationMock) diff --git a/test/support/language_detector_mock.ex b/test/support/language_detector_mock.ex new file mode 100644 index 0000000000..2a85dcd633 --- /dev/null +++ b/test/support/language_detector_mock.ex @@ -0,0 +1,18 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule LanguageDetectorMock do + alias Pleroma.Language.LanguageDetector.Provider + + @behaviour Provider + + @impl Provider + def missing_dependencies, do: [] + + @impl Provider + def configured?, do: true + + @impl Provider + def detect(text), do: "fr" +end