haystack/test/components/routers/test_text_language_router.py
Massimiliano Pippi 3a03fce71c
ci: Add code formatting checks (#7882)
* ruff settings

enable ruff format and re-format outdated files

feat: `EvaluationRunResult` add parameter to specify columns to keep in the comparative `Dataframe`  (#7879)

* adding param to explictily state which cols to keep

* adding param to explictily state which cols to keep

* adding param to explictily state which cols to keep

* updating tests

* adding release notes

* Update haystack/evaluation/eval_run_result.py

Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com>

* Update releasenotes/notes/add-keep-columns-to-EvalRunResult-comparative-be3e15ce45de3e0b.yaml

Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com>

* updating docstring

---------

Co-authored-by: Madeesh Kannan <shadeMe@users.noreply.github.com>

add format-check

fail on format and linting failures

fix string formatting

reformat long lines

fix tests

fix typing

linter

pull from main

* reformat

* lint -> check

* lint -> check
2024-06-18 15:52:46 +00:00

57 lines
2.3 KiB
Python

# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import logging
import pytest
from _pytest.logging import LogCaptureFixture
from haystack import Document
from haystack.components.routers import TextLanguageRouter
class TestTextLanguageRouter:
def test_non_string_input(self):
with pytest.raises(TypeError, match="TextLanguageRouter expects a string as input."):
classifier = TextLanguageRouter()
classifier.run(text=Document(content="This is an english sentence."))
def test_list_of_string(self):
with pytest.raises(TypeError, match="TextLanguageRouter expects a string as input."):
classifier = TextLanguageRouter()
classifier.run(text=["This is an english sentence."])
def test_empty_string(self):
classifier = TextLanguageRouter()
result = classifier.run(text="")
assert result == {"unmatched": ""}
def test_detect_language(self):
classifier = TextLanguageRouter()
detected_language = classifier._detect_language("This is an english sentence.")
assert detected_language == "en"
def test_route_to_en(self):
classifier = TextLanguageRouter()
english_sentence = "This is an english sentence."
result = classifier.run(text=english_sentence)
assert result == {"en": english_sentence}
def test_route_to_unmatched(self):
classifier = TextLanguageRouter()
german_sentence = "Ein deutscher Satz ohne Verb."
result = classifier.run(text=german_sentence)
assert result == {"unmatched": german_sentence}
def test_warning_if_no_language_detected(self, caplog: LogCaptureFixture):
with caplog.at_level(logging.WARNING):
classifier = TextLanguageRouter()
classifier.run(text=".")
assert "Langdetect cannot detect the language of text. Error: No features in text." in caplog.text
def test_warning_if_no_language_detected_if_debug(self, caplog: LogCaptureFixture):
with caplog.at_level(logging.DEBUG):
classifier = TextLanguageRouter()
classifier.run(text=".")
assert "Langdetect cannot detect the language of text. Error: No features in text." in caplog.text
assert "Langdetect cannot detect the language of text: ." in caplog.text