haystack/test/test_translator.py
Lalit Pagaria 5bd94ac5f7
Adding Translator (standalone component & wrapper for pipelines) (#782)
* Adding translator with many generic input parameter support

* Making dict_key as generic

* Fixing mypy issue

* Adding pipeline and using opus models

* Add latest docstring and tutorial changes

* Adding test cases for end-to-end translation for generator, summerizer etc

* raise error join and merge nodes

* Fix test failure

* add docstrings. add usage documentation. rm skip_special_tokens param

* Add latest docstring and tutorial changes

* fix code snippets in md

* Adding few extra configuration parameters and fixing tests

* Fixingmypy issue and updating usage document

* fix for mypy issue in pipeline.py

* reverting renaming of pytest_collection_modifyitems method

* Addressing review comments

* setting skip_special_tokens to True

* removing model_max_length argument as None type is not supported to many models

* Removing padding parameter. Better to leave it as default otherwise it cause tensor size miss match error. If this option required by used then it can be added later.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Malte Pietsch <malte.pietsch@deepset.ai>
2021-02-12 15:58:26 +01:00

47 lines
1.5 KiB
Python

from haystack import Document
import pytest
EXPECTED_OUTPUT = "Ich lebe in Berlin"
INPUT = "I live in Berlin"
def test_translator_with_query(en_to_de_translator):
assert en_to_de_translator.translate(query=INPUT) == EXPECTED_OUTPUT
def test_translator_with_list(en_to_de_translator):
assert en_to_de_translator.translate(documents=[INPUT])[0] == EXPECTED_OUTPUT
def test_translator_with_document(en_to_de_translator):
assert en_to_de_translator.translate(documents=[Document(text=INPUT)])[0].text == EXPECTED_OUTPUT
def test_translator_with_dictionary(en_to_de_translator):
assert en_to_de_translator.translate(documents=[{"text": INPUT}])[0]["text"] == EXPECTED_OUTPUT
def test_translator_with_dictionary_with_dict_key(en_to_de_translator):
assert en_to_de_translator.translate(documents=[{"key": INPUT}], dict_key="key")[0]["key"] == EXPECTED_OUTPUT
def test_translator_with_empty_input(en_to_de_translator):
with pytest.raises(AttributeError):
en_to_de_translator.translate()
def test_translator_with_query_and_documents(en_to_de_translator):
with pytest.raises(AttributeError):
en_to_de_translator.translate(query=INPUT, documents=[INPUT])
def test_translator_with_dict_without_text_key(en_to_de_translator):
with pytest.raises(AttributeError):
en_to_de_translator.translate(documents=[{"text1": INPUT}])
def test_translator_with_dict_with_non_string_value(en_to_de_translator):
with pytest.raises(AttributeError):
en_to_de_translator.translate(documents=[{"text": 123}])