Skip to content

Commit

Permalink
Add testcase and clean the code base on pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesni committed May 23, 2011
1 parent 15a1129 commit 34399ab
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 59 deletions.
1 change: 1 addition & 0 deletions test/__init__.py
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions test/flies.ini
@@ -0,0 +1,15 @@
[flies]
url = http://hudson.englab.bne.redhat.com/flies/
username = admin
key = b6d7044e9ee3b2447c28fb7c50d86d98

[defaults]
debug = false
#errors = true

[servers]
local.url = http://localhost:8080/flies
local.username = username
local.key = key


25 changes: 25 additions & 0 deletions test/flies.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config xmlns="http://flies.openl10n.net/config/v1/">


<url>http://localhost:8080/flies/</url>
<!--
<url>http://hudson.englab.bne.redhat.com/flies/</url>
<url>http://translate.jboss.org/flies/</url>
<url>http://localhost:8080/flies/</url> -->
<project>test-project</project>
<project-version>5.4</project-version>
<locales>
<!--
<locale map-from="de-DE">de</locale>
<locale map-from="es-ES">es</locale>
<locale map-from="fr-FR">fr</locale>
<locale map-from="ja-JP">ja</locale>
<locale map-from="ko-KR">ko</locale>
<locale>pt-BR</locale>
-->
<locale map-from="zh-CN">zh-Hans-CN</locale>
</locales>
</config>

27 changes: 27 additions & 0 deletions test/po/test.po
@@ -0,0 +1,27 @@
# translation of Acls.po to Traditional Chinese
# translation of Acls.po to
# Copyright (C) 2003, 2007 Free Software Foundation, Inc.
#
# Sarah Wang <sarahs@redhat.com>, 2003.
# Xi HUANG <xhuang@redhat.com>, 2007.
# Chester Cheng <ccheng@redhat.com>, 2007.
msgid ""
msgstr ""
"Project-Id-Version: Acls\n"
"Report-Msgid-Bugs-To: http://bugs.kde.org\n"
"POT-Creation-Date: 2001-02-09 01:25+0100\n"
"PO-Revision-Date: 2007-07-11 14:49+1000\n"
"Last-Translator: Chester Cheng <ccheng@redhat.com>\n"
"Language-Team: Traditional Chinese <zh_TW@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: KBabel 1.11.4\n"

# EXACT MATCH
#. Tag: title
#: Acls.xml:8
#, no-c-format
msgid "<title>Access Control Lists</title>"
msgstr "<title>访问存取控制列表</title>"

22 changes: 22 additions & 0 deletions test/pot/test.pot
@@ -0,0 +1,22 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR Free Software Foundation, Inc.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2001-02-09 01:25+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: application/x-xml2pot; charset=UTF-8\n"
"Content-Transfer-Encoding: ENCODING\n"

#. Tag: title
#: Acls.xml:8
#, no-c-format
msgid "<title>Access Control Lists</title>"
msgstr ""

54 changes: 54 additions & 0 deletions test/test_parseconfig.py
@@ -0,0 +1,54 @@
#
# Flies Python Client
#
# Copyright (c) 2010 Jian Ni <jni@redhat.com>
# Copyright (c) 2010 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., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA

all__ = (
"ConfigTest",
)

import unittest
import sys, os
sys.path.insert(0, os.path.abspath(__file__+"/../.."))

from zanataclient.parseconfig import ZanataConfig


class ConfigTest(unittest.TestCase):
def setUp(self):
self.config = ZanataConfig()

def test_user_config(self):
self.config.set_userconfig("./flies.ini")
server = self.config.get_server("http://localhost:8080/flies")
user_name = self.config.get_config_value("username", 'servers', server)
apikey = self.config.get_config_value("key", 'servers',server)
self.assertEqual(server, "local")
self.assertEqual(user_name, "username")
self.assertEqual(apikey, "key")

def test_project_config(self):
project_config = self.config.read_project_config("./flies.xml")
self.assertEqual(project_config['project_url'], "http://localhost:8080/flies/")
self.assertEqual(project_config['project_id'], "test-project")
self.assertEqual(project_config['project_version'], "5.4")
self.assertEqual(project_config['locale_map'], {"zh-CN": "zh-Hans-CN"})

if __name__ == '__main__':
unittest.main()
54 changes: 54 additions & 0 deletions test/test_publicanutil.py
@@ -0,0 +1,54 @@
#
# Zanata Python Client
#
# Copyright (c) 2011 Jian Ni <jni@redhat.com>
# Copyright (c) 2011 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., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA

all__ = (
"PublicanUtilityTest",
)

import unittest
import sys, os
sys.path.insert(0, os.path.abspath(__file__+"/../.."))

from zanataclient.publicanutil import PublicanUtility


class PublicanUtilityTest(unittest.TestCase):
def setUp(self):
self.publican = PublicanUtility()

def test_strippath(self):
filename = self.publican.strip_path("./pot/test.pot", "./pot")
self.assertEqual(filename, "test")

def test_potfiletojson(self):
body, filename = self.publican.potfile_to_json("./pot/test.pot", "./pot")
expect = """{"lang": "en-US", "extensions": [{"comment": "SOME DESCRIPTIVE TITLE.\\nCopyright (C) YEAR Free Software Foundation, Inc.\\nFIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\\n", "object-type": "po-header", "entries": [{"value": "PACKAGE VERSION", "key": "Project-Id-Version"}, {"value": "2001-02-09 01:25+0100", "key": "POT-Creation-Date"}, {"value": "YEAR-MO-DA HO:MI+ZONE", "key": "PO-Revision-Date"}, {"value": "FULL NAME <EMAIL@ADDRESS>", "key": "Last-Translator"}, {"value": "LANGUAGE <LL@li.org>", "key": "Language-Team"}, {"value": "1.0", "key": "MIME-Version"}, {"value": "application/x-xml2pot; charset=UTF-8", "key": "Content-Type"}, {"value": "ENCODING", "key": "Content-Transfer-Encoding"}]}], "contentType": "application/x-gettext", "name": "test", "textFlows": [{"lang": "en-US", "content": "<title>Access Control Lists</title>", "extensions": [{"object-type": "comment", "value": "Tag: title", "space": "preserve"}, {"object-type": "pot-entry-header", "references": ["Acls.xml:8"], "flags": ["no-c-format"], "context": "", "extractedComment": ""}], "id": "782f49c4e93c32403ba0b51821b38b90", "revision": 1}]}"""
self.assertEqual(expect, body)
self.assertEqual(filename, "test")

def test_pofiletojson(self):
body = self.publican.pofile_to_json("./po/test.po")
expect = """{"textFlowTargets": [{"content": "<title>\\u8bbf\\u95ee\\u5b58\\u53d6\\u63a7\\u5236\\u5217\\u8868</title>", "extensions": [{"object-type": "comment", "value": "Tag: title", "space": "preserve"}], "state": "Approved", "resId": "782f49c4e93c32403ba0b51821b38b90"}], "extensions": [{"comment": "translation of Acls.po to Traditional Chinese\\ntranslation of Acls.po to\\nCopyright (C) 2003, 2007 Free Software Foundation, Inc.\\n\\nSarah Wang <sarahs@redhat.com>, 2003.\\nXi HUANG <xhuang@redhat.com>, 2007.\\nChester Cheng <ccheng@redhat.com>, 2007.", "object-type": "po-target-header", "entries": [{"value": "Acls", "key": "Project-Id-Version"}, {"value": "http://bugs.kde.org", "key": "Report-Msgid-Bugs-To"}, {"value": "2001-02-09 01:25+0100", "key": "POT-Creation-Date"}, {"value": "2007-07-11 14:49+1000", "key": "PO-Revision-Date"}, {"value": "Chester Cheng <ccheng@redhat.com>", "key": "Last-Translator"}, {"value": "Traditional Chinese <zh_TW@li.org>", "key": "Language-Team"}, {"value": "1.0", "key": "MIME-Version"}, {"value": "text/plain; charset=UTF-8", "key": "Content-Type"}, {"value": "8bit", "key": "Content-Transfer-Encoding"}, {"value": "KBabel 1.11.4", "key": "X-Generator"}]}], "links": []}"""
self.assertEqual(expect, body)

if __name__ == '__main__':
unittest.main()

29 changes: 29 additions & 0 deletions test/test_zanata.py
@@ -0,0 +1,29 @@
'''
Created on May 20, 2011
@author: jamesni
'''
import unittest
import sys, os
sys.path.insert(0, os.path.abspath(__file__+"/../.."))

from zanataclient import zanata


class ZanataTest(unittest.TestCase):
def setup(self):
pass

def test_convert_serverversion(self):
server_version = "1.3.3"
version_number = zanata.convert_serverversion(server_version)
self.assertEqual(version_number, 1.3)

def test_seachfile(self):
path = "./po"
result = zanata.search_file(path, "test.po")
self.assertEqual(result, "./po/test.po")

if __name__ == '__main__':
unittest.main()

71 changes: 71 additions & 0 deletions test/test_zanatacmd.py
@@ -0,0 +1,71 @@
#
# Flies Python Client
#
# Copyright (c) 2010 Jian Ni <jni@redhat.com>
# Copyright (c) 2010 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., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA

all__ = (
"CommandlineTest",
)

import unittest
import sys, os
from minimock import Mock
sys.path.insert(0, os.path.abspath(__file__+"/../.."))

from zanataclient.zanatacmd import ZanataCommand
from zanataclient.zanatalib import ZanataResource
from zanataclient.zanatalib.project import Project


class CommandlineTest(unittest.TestCase):
def setUp(self):
self.zanatacmd = ZanataCommand()

def test_list_projects(self):
projects = []
project_data = {'id':"test-project", 'name':"Test Project", 'type':"IterationProject", 'links':[]}
projects.append(Project(project_data))

url = "http://localhost"
zanata = ZanataResource(url)
zanata.projects = Mock(url)
zanata.projects.list.mock_returns = projects
result = self.zanatacmd.list_projects(zanata)
self.assertEqual(result[0].id, 'test-project')

def test_project_info(self):
pass

def test_version_info(self):
pass

def test_create_project(self):
pass

def test_create_version(self):
pass

def test_pull_command(self):
pass

def test_push_command(self):
pass

if __name__ == '__main__':
unittest.main()
4 changes: 1 addition & 3 deletions zanataclient/command.py
Expand Up @@ -26,9 +26,7 @@

import getopt
import sys
import logging
import os
import inspect


class OptionConfigurationError(Exception):
pass
Expand Down
27 changes: 16 additions & 11 deletions zanataclient/parseconfig.py
Expand Up @@ -30,12 +30,17 @@

project_config = {'project_url':'', 'project_id':'', 'project_version':'', 'locale_map':{}}


class ZanataConfig:
def set_userconfig(self, path):
def __init__(self):
self.configparser = ""
self._config = ""

def set_userconfig(self, path):
self.configparser = ConfigParser.ConfigParser()
self._config = self.configparser.read(['zanata.ini', path])

def get_server(self, url):
def get_server(self, url):
if self._config:
try:
item_list = self.configparser.items('servers')
Expand All @@ -49,24 +54,24 @@ def get_server(self, url):
if url == address:
server = item[0][:-4]
return server
except ConfigParser.NoOptionError, NoSectionError:
except ConfigParser.NoOptionError, ConfigParser.NoSectionError:
raise
else:
return None

def get_config_value(self, name, section, server):
def get_config_value(self, name, section, server):
if self._config:
try:
value = self.configparser.get(section, server+'.'+name)
return value
except ConfigParser.NoOptionError, NoSectionError:
except ConfigParser.NoOptionError, ConfigParser.NoSectionError:
raise
else:
return None

def read_project_config(self, filename):
def read_project_config(self, filename):
log = Logger()
project_config={'project_url':'', 'project_id':'', 'project_version':'', 'locale_map':{}}

if os.path.getsize(filename) == 0:
log.info('The project xml: flies.xml is empty, need command line options')
return project_config
Expand Down Expand Up @@ -111,11 +116,11 @@ def read_project_config(self, filename):
for node in locale.childNodes:
if node.nodeType == node.TEXT_NODE:
if locale.getAttribute("map-from"):
map = {locale.getAttribute("map-from"):node.data}
project_config['locale_map'].update(map)
locale_map = {locale.getAttribute("map-from"):node.data}
project_config['locale_map'].update(locale_map)
else:
map = {node.data:node.data}
project_config['locale_map'].update(map)
locale_map = {node.data:node.data}
project_config['locale_map'].update(locale_map)

return project_config

Expand Down

0 comments on commit 34399ab

Please sign in to comment.