Skip to content

Commit

Permalink
Merge pull request #30 from sundeep-co-in/STAGE
Browse files Browse the repository at this point in the history
ZNTA-779: configuration: locales mappings
  • Loading branch information
definite committed Nov 27, 2015
2 parents 0971c46 + fad041c commit 7d1b2ad
Show file tree
Hide file tree
Showing 19 changed files with 1,048 additions and 993 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
* Sun Nov 15 2015 Sundeep Anand <[email protected]>
* Thu Nov 26 2015 Sundeep Anand <[email protected]>
- Bug 1156236 - use locale aliases defined in the server
- added ProjectContext, Improved help, fixed code issues
- added <src-dir> and <trans-dir> in zanata.xml
- refactor code - added config to centralize rest resources
- Organize exception messages, added test.
- HTTP to HTTPS redirect - auto, if found in httplib2 response
- In listing of projects, including project-type also, if exists.
- Rename zanatalib/client.py to zanatalib/resource.py
- flake8 changes and addition of: make flake8
- added requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Required package for compilation::

Required package for unit tests::

yum install python-minimock
yum install python-minimock python-mock

To run pylint against the source code::

Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
flake8==2.4.1
funcsigs==0.4
httplib2==0.9.2
ipdb==0.8.1
mccabe==0.3.1
MiniMock==1.2.8
mock==1.3.0
pbr==1.8.1
pep8==1.5.7
polib==1.0.7
pyflakes==0.8.1
six==1.10.0
2 changes: 2 additions & 0 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
from test_publicanutil import PublicanUtilityTest
# from test_zanatacmd import ZanataCmdTest
from test_service import ServiceTest
from test_context import ProjectContextTest

suite = unittest.TestSuite()
# suite.addTest(unittest.makeSuite(ZanataTest))
suite.addTest(unittest.makeSuite(ConfigTest))
suite.addTest(unittest.makeSuite(PublicanUtilityTest))
# suite.addTest(unittest.makeSuite(ZanataCmdTest))
suite.addTest(unittest.makeSuite(ServiceTest))
suite.addTest(unittest.makeSuite(ProjectContextTest))
results = unittest.TextTestRunner(verbosity=2).run(suite)
155 changes: 155 additions & 0 deletions test/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#
# Zanata Python Client
#
# Copyright (c) 2015 Sundeep Anand <[email protected]>
# Copyright (c) 2015 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

all__ = (
"ProjectContextTest",
)

import unittest
import sys
import os
import mock
sys.path.insert(0, os.path.abspath(__file__ + "/../.."))
from zanataclient.context import ProjectContext

# test data
command_options = {'comment_cols': [{'name': '--commentcols', 'value': 'en-US,es,pos,description',
'internal': 'comment_cols', 'long': ['--commentcols'],
'type': 'command', 'metavar': 'COMMENTCOLS'}],
'user_config': [{'name': '--user-config',
'value': './testfiles/zanata.ini', 'internal': 'user_config',
'long': ['--user-config'], 'type': 'command', 'metavar': 'USER-CONFIG'}],
'project_config': [{'name': '--project-config', 'value': './testfiles/zanata.xml',
'internal': 'project_config', 'long': ['--project-config'],
'type': 'command', 'metavar': 'PROJECT-CONFIG'}],
'project_type': [{'name': '--project-type', 'value': 'podir', 'internal': 'project_type',
'long': ['--project-type'], 'type': 'command', 'metavar': 'PROJECTTYPE'}]}

version_service_return_content = {'versionNo': '3.7.3', 'buildTimeStamp': 'unknown', 'scmDescribe': 'unknown'}

iteration_locales_return_content = [{'displayName': 'English (United States)', 'localeId': 'en-US'},
{'alias': 'pa-IN', 'displayName': 'Punjabi', 'localeId': 'pa'},
{'alias': 'hi-IN', 'displayName': 'Hindi', 'localeId': 'hi'},
{'displayName': 'Tamil (India)', 'localeId': 'ta-IN'},
{'displayName': 'Bengali (India)', 'localeId': 'bn-IN'}]

project_locales_return_content = [{"displayName": "English (United States)", "localeId": "en-US"},
{"displayName": "Hindi", "localeId": "hi"}, {"displayName": "Croatian", "localeId": "hr"},
{"displayName": "Japanese", "localeId": "ja"}, {"displayName": "Kannada", "localeId": "kn"},
{"displayName": "Chinese (Traditional, Taiwan)", "localeId": "zh-Hant-TW"}]

project_config_without_locale_map = {'transdir': '/home/user/project/target', 'project_type': 'gettext',
'http_headers': {'Accept': 'application/json', 'X-Auth-User': 'username', 'X-Auth-Token': 'key'},
'url': 'http://localhost:8080/zanata', 'key': 'key', 'srcdir': '/home/user/project/source',
'project_version': '1.0', 'client_version': '1.3.12-74-g0b1d-mod', 'project_id': 'test-project',
'user_name': 'username'}


class ProjectContextTest(unittest.TestCase):
def setUp(self):
self.context = ProjectContext(command_options)

def test_command_options(self):
command_options_keys = ['project_type', 'comment_cols', 'user_config', 'project_config']
self.assertEqual(self.context.command_options.keys(), command_options_keys)
self.assertEqual(
self.context.command_dict,
{'project_config': './testfiles/zanata.xml', 'comment_cols': 'en-US,es,pos,description',
'user_config': './testfiles/zanata.ini', 'project_type': 'podir'}
)

def test_build_local_config(self):
self.context.build_local_config()
self.assertEqual(self.context.local_config['url'], 'http://localhost:8080/zanata')
self.assertEqual(self.context.local_config['project_id'], "test-project")
self.assertEqual(self.context.local_config['project_version'], "1.0")
self.assertEqual(self.context.local_config['project_type'], "gettext")
self.assertEqual(self.context.local_config['locale_map'], {"zh-CN": "zh-Hans"})
self.assertEqual(self.context.local_config['srcdir'], "/home/user/project/source")
self.assertEqual(self.context.local_config['transdir'], "/home/user/project/target")
self.assertEqual(self.context.local_config['user_name'], 'username')
self.assertEqual(self.context.local_config['key'], 'key')
self.assertEqual(
self.context.local_config['http_headers'],
{'Accept': 'application/json', 'X-Auth-User': 'username', 'X-Auth-Token': 'key'}
)
self.assertIn('client_version', self.context.local_config,
'local_config should contain client_version')

@mock.patch('zanataclient.zanatalib.projectservice.LocaleService.get_locales')
@mock.patch('zanataclient.zanatalib.versionservice.VersionService.get_server_version')
def test_build_remote_config(self, mock_get_server_version, mock_get_locales):
mock_get_server_version.return_value = version_service_return_content
mock_get_locales.return_value = iteration_locales_return_content
self.context.build_local_config()
self.assertEqual(self.context.local_config['locale_map'], {"zh-CN": "zh-Hans"})
# removing locale_map from local_config
self.context.local_config.pop('locale_map', None)
self.context.build_remote_config()
self.assertEqual(
self.context.remote_config['locale_map'],
{'bn-IN': 'bn-IN', 'pa-IN': 'pa', 'en-US': 'en-US', 'hi-IN': 'hi', 'ta-IN': 'ta-IN'},
'if not found context will go for remote locale_map'
)
self.assertEqual(self.context.remote_config['server_version'], '3.7.3')

@mock.patch('zanataclient.zanatalib.projectservice.LocaleService.get_locales')
@mock.patch('zanataclient.zanatalib.versionservice.VersionService.get_server_version')
def test_get_context_data_local_locale_map(self, mock_get_server_version, mock_get_locales):
mock_get_server_version.return_value = version_service_return_content
mock_get_locales.return_value = project_locales_return_content
context_data = self.context.get_context_data()
self.assertEqual(context_data['project_type'], 'podir',
'Command option overrides the project config project_type')
self.assertEqual(context_data['locale_map'], {"zh-CN": "zh-Hans"},
'context_data should contain locale_map from project_config')
options_set = []
for optionset in (self.context.remote_config.keys(), self.context.local_config.keys(),
self.context.command_dict.keys()):
options_set.extend(optionset)
options_set = list(set(options_set))
# Adding 1, as 'key' is being filtered out from context_data
self.assertEqual(len(options_set), (len(context_data.keys()) + 1),
'context_data should contain all unique keys and their values')

@mock.patch('zanataclient.parseconfig.ZanataConfig.read_project_config')
@mock.patch('zanataclient.zanatalib.projectservice.LocaleService.get_locales')
@mock.patch('zanataclient.zanatalib.versionservice.VersionService.get_server_version')
def test_get_context_data_remote_locale_map(
self, mock_get_server_version, mock_get_locales, mock_read_project_config
):
mock_get_server_version.return_value = version_service_return_content
mock_get_locales.return_value = project_locales_return_content
mock_read_project_config.return_value = project_config_without_locale_map
context_data = self.context.get_context_data()
self.assertEqual(
context_data['locale_map'],
{'hi': 'hi', 'hr': 'hr', 'en-US': 'en-US', 'zh-Hant-TW': 'zh-Hant-TW', 'ja': 'ja', 'kn': 'kn'},
'context_data should contain locale_map fetched from server'
)

def test_process_locales(self):
locale_map = self.context.process_locales(iteration_locales_return_content)
self.assertEqual(locale_map, {'bn-IN': 'bn-IN', 'pa-IN': 'pa', 'en-US': 'en-US',
'hi-IN': 'hi', 'ta-IN': 'ta-IN'})

if __name__ == '__main__':
unittest.main()
6 changes: 3 additions & 3 deletions test/test_parseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def test_user_config(self):

def test_project_config(self):
project_config = self.config.read_project_config("./testfiles/zanata.xml")
self.assertEqual(project_config['project_url'], "http://localhost:8080/zanata/")
self.assertEqual(project_config['url'], "http://localhost:8080/zanata/")
self.assertEqual(project_config['project_id'], "test-project")
self.assertEqual(project_config['project_version'], "1.0")
self.assertEqual(project_config['locale_map'], {"zh-CN": "zh-Hans"})
self.assertEqual(project_config['src_dir'], "/home/user/project/source")
self.assertEqual(project_config['trans_dir'], "/home/user/project/target")
self.assertEqual(project_config['srcdir'], "/home/user/project/source")
self.assertEqual(project_config['transdir'], "/home/user/project/target")

if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions test/testfiles/zanata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<url>http://localhost:8080/zanata/</url>
<project>test-project</project>
<project-version>1.0</project-version>
<project-type>gettext</project-type>
<locales>
<locale map-from="zh-CN">zh-Hans</locale>
</locales>
Expand Down
Loading

0 comments on commit 7d1b2ad

Please sign in to comment.