Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: tests/test_xtm_translations_utils.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Patch Set: Addressed comments from Patch Set #4 Created Oct. 5, 2018, 4:23 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tests/test_xtm_translations_utils.py
diff --git a/tests/test_xtm_translations_utils.py b/tests/test_xtm_translations_utils.py
new file mode 100644
index 0000000000000000000000000000000000000000..6cd5def9bd89fd248fecb23fa6c374f4054d0f29
--- /dev/null
+++ b/tests/test_xtm_translations_utils.py
@@ -0,0 +1,221 @@
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-present eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import json
+import os
+
+import pytest
+
+from cms.translations.xtm import utils
+import cms.translations.xtm.constants as const
+from cms.sources import FileSource
+from cms.translations.xtm.xtm_api import XTMCloudAPI
+from tests.utils import exception_test
+
+_API_TOKEN = 'TheXTM-APIToken-VALID'
+_PROJECT_ID = 1234
+
+
+@pytest.fixture
+def toydir(tmpdir):
+ """Toy directory fixture with two locales - 'de' and 'en'."""
+ toydir = tmpdir.mkdir('toydir')
+
+ en_dir = toydir.mkdir('en')
+ en_dir.join('file.json').write(json.dumps({'a': 'b'}))
+
+ de_dir = toydir.mkdir('de')
+ de_dir.join('file.json').write(json.dumps({'a': 'b'}))
+ de_dir.join('file.txt').write('test')
+
+ return toydir
+
+
+def test_get_files_to_upload(temp_site):
+ """Generation of correct datatype for the XTMCloudAPI class."""
+ files = utils.get_files_to_upload(FileSource(temp_site))
+
+ assert 'translate.json' in files
+ assert 'translate-not-enough.json' in files
+
+
+def test_read_token_invalid():
+ """Test if appropriate exception is raised when no token is found."""
+ exp_msg = const.ErrorMessages.NO_TOKEN_PROVIDED.format(
+ const.Token.CREATION_CMD,
+ )
+ with pytest.raises(Exception) as err:
+ utils.read_token()
+ assert exp_msg in str(err.value)
+
+
+def test_read_token_valid():
+ """Test if the token is read correctly when the env is setup."""
+ env_var = const.Token.ENV_VAR
+ token = 'test_token'
+ os.environ[env_var] = token
+
+ try:
+ assert token == utils.read_token()
+ finally:
+ del os.environ[env_var]
+
+
+def test_sanitize_project_name_characters():
+ """Test if invalid name characters are replaced as expected."""
+ test_in = '{0}{1}{0}'.format(
+ 'test', ''.join(const.ProjectName.INVALID_CHARS),
+ )
+ exp_out = '{0}{1}{0}'.format(
+ 'test',
+ const.ProjectName.NAME_WILDCARD *
+ len(const.ProjectName.INVALID_CHARS),
+ )
+
+ assert exp_out == utils.sanitize_project_name(test_in)
+
+
+def test_sanitize_project_name_length():
+ """Test if names that are too long are truncated as expected."""
+ test_in = 'a' * (const.ProjectName.MAX_LENGTH + 10)
+ exp_out = 'a' * const.ProjectName.MAX_LENGTH
+
+ assert exp_out == utils.sanitize_project_name(test_in)
+
+
+def test_map_locales(temp_site):
+ """Test if a local website's languages are mapped to XTM's format."""
+ test_source = FileSource(str(temp_site))
+ exp_out = {'de_DE'}
+
+ assert exp_out == utils.map_locales(test_source)
+
+
+def test_remote_to_local_good(temp_site):
+ """Test if a remote filename is resolved to a valid OS path."""
+ test_in = '___foo___bar___faz___test.json'
+ exp_out = os.path.join(str(temp_site), 'foo', 'bar', 'faz', 'test.json')
+
+ assert exp_out == utils.remote_to_local(test_in, str(temp_site),
+ locales=['foo'])
+
+
+def test_remote_to_local_exception(temp_site):
+ test_in = '___foo___bar___faz___test.json'
+ exp_msg = const.ErrorMessages.CANT_RESOLVE_REMOTE_LANG.format('foo')
+
+ exception_test(utils.remote_to_local, Exception, exp_msg, test_in,
+ str(temp_site), locales=['bar'])
+
+
+def test_run_and_wait_no_err():
+ exp_out = 'test'
+
+ def func():
+ return exp_out
+
+ assert exp_out == utils.run_and_wait(func, Exception, '')
+
+
+def test_run_and_wait_with_params():
+ test_in = 'test'
+
+ def func(a):
+ return a
+
+ assert test_in == utils.run_and_wait(func, Exception, '', a=test_in)
+
+
+@pytest.mark.slow_test
+def test_run_and_wait_infinite():
+ exp_msg = 'ERROR'
+
+ def func():
+ raise Exception(exp_msg)
+
+ with pytest.raises(Exception) as err:
+ utils.run_and_wait(func, Exception, exp_msg, retries=1)
+
+ assert exp_msg == str(err.value)
+
+
+def test_resolve_locales(intercept_populated, temp_site):
+ """Test if locales are resolved correctly.
+
+ In this environment, no languages have to be added.
+ """
+ api = XTMCloudAPI(_API_TOKEN)
+ source = FileSource(str(temp_site))
+ source.write_to_config('XTM', 'project_id', str(_PROJECT_ID))
+
+ utils.resolve_locales(api, source)
+
+
+def test_resolve_locales_adds_langs(intercept, temp_site):
+ """Test if locales are resolved correctly.
+
+ In this environment, new target languages are added.
+ """
+ api = XTMCloudAPI(_API_TOKEN)
+ source = FileSource(str(temp_site))
+ source.write_to_config('XTM', 'project_id', str(_PROJECT_ID))
+ exp_targets = {'de_DE'}
+
+ utils.resolve_locales(api, source)
+
+ assert exp_targets == api.get_target_languages(_PROJECT_ID)
+
+
+def test_resolve_locales_raise_exception(intercept_populated, temp_site):
+ """Test if locales are resolved correctly.
+
+ In this environment, the API has more target languages configured than are
+ available online. We except an exception to be raised.
+ """
+ api = XTMCloudAPI(_API_TOKEN)
+ api.add_target_languages(_PROJECT_ID, ['ro_RO'])
+ source = FileSource(str(temp_site))
+ source.write_to_config('XTM', 'project_id', str(_PROJECT_ID))
+ exp_msg = ('The following languages are enabled in the API, but not '
+ "listed in locales: set(['ro_RO'])! Please remove them manually"
+ ' from project number 1234 and then re-run the script!')
+
+ exception_test(utils.resolve_locales, Exception, exp_msg, api, source)
+
+
+def test_list_locales(toydir):
+ """Test if the the locales are listed correctly."""
+ assert utils.get_locales(str(toydir), 'en') == ['de']
+
+
+def test_clear_files(toydir):
+ """Test if local translation files are deleted, as expected."""
+ required_locales = ['de']
+
+ utils.clear_files(str(toydir), required_locales)
+
+ assert os.path.exists(str(toydir.join('en/file.json')))
+ assert os.path.isfile(str(toydir.join('de/file.txt')))
+ assert not os.path.exists(str(toydir.join('de/file.json')))
+
+
+@pytest.mark.parametrize('path', ['de/test.json', 'de/dir1/dir2/test.json'])
+def test_write_data(toydir, path):
+ """Test if writing data to files works as expected."""
+ data = bytes(json.dumps({'a': 'b'}))
+
+ utils.write_to_file(data, str(toydir.join(path)))
+
+ assert toydir.join(path).read('rb') == data
« docs/usage/xml-sync.md ('K') | « tests/test_xtm_translate.py ('k') | tests/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld