Skip to content

Commit

Permalink
fix: make POEditor methods independent of behave context
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Mar 11, 2022
1 parent 7da0524 commit 69f9711
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 132 deletions.
38 changes: 38 additions & 0 deletions toolium/test/utils/test_dataset_map_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ def test_a_context_param():
assert expected_st == result_st


def test_a_context_param_deprecated():
"""
Verification of a mapped parameter as CONTEXT and check that deprecated message is logged
"""
context = mock.MagicMock()
context.attribute = "attribute value"
dataset.logger = mock.MagicMock()

result_att = map_param("[CONTEXT:attribute]", context)
expected_att = "attribute value"
assert expected_att == result_att
dataset.logger.warning.assert_called_with("Deprecated context parameter has been sent to map_param method. Please, "
"configure dataset global variables instead of passing context to "
"map_param.")


def test_a_poe_param_single_result():
"""
Verification of a POE mapped parameter with a single result for a reference
Expand All @@ -124,6 +140,28 @@ def test_a_poe_param_single_result():
assert result == expected


def test_a_poe_param_single_result_deprecated():
"""
Verification of a POE mapped parameter with a single result for a reference
"""
context = mock.MagicMock()
context.poeditor_export = [
{
"term": "Poniendo mute",
"definition": "Ahora la tele está silenciada",
"reference": "home:home.tv.mute",
}
]
dataset.logger = mock.MagicMock()

result = map_param('[POE:home.tv.mute]', context)
expected = "Ahora la tele está silenciada"
assert result == expected
dataset.logger.warning.assert_called_with("Deprecated context parameter has been sent to map_param method. Please, "
"configure dataset global variables instead of passing context to "
"map_param.")


def test_a_poe_param_no_result_assertion():
"""
Verification of a POE mapped parameter without result
Expand Down
64 changes: 51 additions & 13 deletions toolium/test/utils/test_poeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,69 @@
"""

import mock
import os
import pytest

from toolium.config_parser import ExtendedConfigParser
from toolium.utils import dataset, poeditor
from toolium.utils.poeditor import get_valid_lang, load_poeditor_texts


def test_poe_lang_param():
def test_get_valid_lang():
"""
Verification of a POEditor language param
"""
context = mock.MagicMock()
context.poeditor_language_list = ['en-gb', 'de', 'pt-br', 'es', 'es-ar', 'es-cl', 'es-co', 'es-ec']
assert get_valid_lang(context, 'pt-br') == 'pt-br'
assert get_valid_lang(context, 'es') == 'es'
assert get_valid_lang(context, 'es-es') == 'es'
assert get_valid_lang(context, 'es-co') == 'es-co'
language_codes = ['en-gb', 'de', 'pt-br', 'es', 'es-ar', 'es-cl', 'es-co', 'es-ec']
assert get_valid_lang(language_codes, 'pt-br') == 'pt-br'
assert get_valid_lang(language_codes, 'es') == 'es'
assert get_valid_lang(language_codes, 'es-es') == 'es'
assert get_valid_lang(language_codes, 'es-co') == 'es-co'


def test_get_valid_lang_wrong_lang():
"""
Verification of a POEditor language param
"""
language_codes = ['en-gb', 'de', 'pt-br']
with pytest.raises(Exception) as excinfo:
get_valid_lang(language_codes, 'en-en')
assert "Language en-en is not included in valid codes: en-gb, de, pt-br" == str(excinfo.value)


class ContextLogger(object):
def __init__(self):
self.logger = mock.MagicMock()
def test_poe_lang_param_from_project_config():
"""
Verification of a POEditor language param getting language from project config
"""
config_file_path = os.path.join("toolium", "test", "resources", "toolium.cfg")
dataset.toolium_config = ExtendedConfigParser.get_config_from_file(config_file_path)
language_codes = ['en-gb', 'de', 'pt-br', 'es', 'es-ar', 'es-cl', 'es-co', 'es-ec', 'en']
assert get_valid_lang(language_codes) == 'en'


def test_poe_texts_load_without_api_token():
def test_load_poeditor_texts_without_api_token():
"""
Verification of POEditor texts load abortion when api_token is not configured
"""
context = ContextLogger()
dataset.project_config = {
"poeditor": {
"project_name": "My-Bot"
}
}
poeditor.logger = mock.MagicMock()

load_poeditor_texts()
poeditor.logger.info.assert_called_with("POEditor is not configured")


def test_load_poeditor_texts_without_api_token_deprecated():
"""
Verification of POEditor texts load abortion when api_token is not configured and deprecated message is logged
"""
context = mock.MagicMock()
poeditor.logger = mock.MagicMock()

load_poeditor_texts(context)
context.logger.info.assert_called_with("POEditor is not configured")
poeditor.logger.info.assert_called_with("POEditor is not configured")
poeditor.logger.warning.assert_called_with("Deprecated context parameter has been sent to load_poeditor_texts "
"method. Please, configure POEditor global variables instead of passing "
"context to load_poeditor_texts.")
22 changes: 11 additions & 11 deletions toolium/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def _infer_param_type(param):
E.g. "1234" -> 1234, "0.50" -> 0.5, "True" -> True, "None" -> None,
"['a', None]" -> ['a', None], "{'a': None}" -> {'a': None},
'["a", null]' -> ["a", None], '{"a": null}' -> {"a": None}
v
:param param: data to be transformed
:return data with the inferred type
"""
Expand Down Expand Up @@ -298,8 +298,8 @@ def map_param(param, context=None):
:return: string with the applied replacements
"""
if context:
context.logger.warning('Deprecated context parameter used in map_param method. Please, configure '
'dataset global variables instead of passing context to map_param.')
logger.warning('Deprecated context parameter has been sent to map_param method. Please, configure dataset'
' global variables instead of passing context to map_param.')
global language, language_terms, project_config, toolium_config, poeditor_terms, behave_context
if hasattr(context, 'language'):
language = context.language
Expand Down Expand Up @@ -560,19 +560,19 @@ def get_message_property(param, language_terms, language_key):
return language_terms_aux[language_key]


def get_translation_by_poeditor_reference(reference, poeditor_config):
def get_translation_by_poeditor_reference(reference, poeditor_terms):
"""
Return the translation(s) for the given POEditor reference from the given terms in poeditor_terms.
:param reference: POEditor reference
:param poeditor_config: poeditor terms
:param poeditor_terms: poeditor terms
:return: list of strings with the translations from POEditor or string with the translation if only one was found
"""
poeditor_conf = project_config['poeditor'] if 'poeditor' in project_config else {}
key = poeditor_conf['key_field'] if 'key_field' in poeditor_conf else 'reference'
search_type = poeditor_conf['search_type'] if 'search_type' in poeditor_conf else 'contains'
poeditor_config = project_config['poeditor'] if 'poeditor' in project_config else {}
key = poeditor_config['key_field'] if 'key_field' in poeditor_config else 'reference'
search_type = poeditor_config['search_type'] if 'search_type' in poeditor_config else 'contains'
# Get POEditor prefixes and add no prefix option
poeditor_prefixes = poeditor_conf['prefixes'] if 'prefixes' in poeditor_conf else []
poeditor_prefixes = poeditor_config['prefixes'] if 'prefixes' in poeditor_config else []
poeditor_prefixes.append('')
translation = []
for prefix in poeditor_prefixes:
Expand All @@ -582,10 +582,10 @@ def get_translation_by_poeditor_reference(reference, poeditor_config):
else:
complete_reference = '%s%s' % (prefix, reference)
if search_type == 'exact':
translation = [term['definition'] for term in poeditor_config
translation = [term['definition'] for term in poeditor_terms
if complete_reference == term[key] and term['definition'] is not None]
else:
translation = [term['definition'] for term in poeditor_config
translation = [term['definition'] for term in poeditor_terms
if complete_reference in term[key] and term['definition'] is not None]
if len(translation) > 0:
break
Expand Down
Loading

0 comments on commit 69f9711

Please sign in to comment.