[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/TranslationService/tests - __init__.py:1.1 testGettextExportImport.py:1.1 testMessageCatalog.py:1.1 testTranslationService.py:1.1

Stephan Richter srichter@cbu.edu
Thu, 11 Jul 2002 03:12:46 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/TranslationService/tests
In directory cvs.zope.org:/tmp/cvs-serv24977/lib/python/Zope/App/OFS/Services/TranslationService/tests

Added Files:
	__init__.py testGettextExportImport.py testMessageCatalog.py 
	testTranslationService.py 
Log Message:
I moved the OFS-specific parts of the Translation Service to 
Zope.App.OFS.Services, which is the way all the other local/placeful 
service impolementations do it.


=== Added File Zope3/lib/python/Zope/App/OFS/Services/TranslationService/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################


=== Added File Zope3/lib/python/Zope/App/OFS/Services/TranslationService/tests/testGettextExportImport.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""This module tests the Gettext Export and Import funciotnality of the
Translation Service.

$Id: testGettextExportImport.py,v 1.1 2002/07/11 07:12:44 srichter Exp $
"""
import unittest, time

from cStringIO import StringIO

from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
from Zope.App.ComponentArchitecture.metaConfigure import \
     provideService, managerHandler
from Zope.App.ComponentArchitecture.metaConfigure import handler

from Zope.App.OFS.Services.TranslationService.MessageCatalog import \
     MessageCatalog 
from Zope.I18n.Negotiator import negotiator
from Zope.I18n.INegotiator import INegotiator
from Zope.I18n.IUserPreferredLanguages import IUserPreferredLanguages

from Zope.App.OFS.Services.TranslationService.TranslationService import \
     TranslationService
from Zope.App.OFS.Services.TranslationService.GettextImportFilter import \
     GettextImportFilter
from Zope.App.OFS.Services.TranslationService.GettextExportFilter import \
     GettextExportFilter



class Environment:

    __implements__ = IUserPreferredLanguages

    def __init__(self, langs=()):
        self.langs = langs

    def getPreferredLanguages(self):
        return self.langs


class TestGettextExportImport(PlacelessSetup, unittest.TestCase):


    _data = '''msgid ""
msgstr ""
"Project-Id-Version: Zope 3\\n"
"PO-Revision-Date: %s\\n"
"Last-Translator: Zope 3 Gettext Export Filter\\n"
"Zope-Language: de\\n"
"Zope-Domain: default\\n" 
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"

msgid "Choose"
msgstr "Ausw\xc3\xa4hlen!"

msgid "greeting"
msgstr "hallo"
'''

    def setUp(self):
        PlacelessSetup.setUp(self)
        # Setup the negotiator service registry entry
        managerHandler('defineService', 'LanguageNegotiation', INegotiator) 
        provideService('LanguageNegotiation', negotiator, 'Zope.Public')
        self._service = TranslationService('default') 
        handler('Factories', 'provideFactory', 'Message Catalog',
                MessageCatalog)


    def testImportExport(self):
        service = self._service

        imp = GettextImportFilter(service)
        imp.importMessages(['default'], ['de'],
                           StringIO(self._data %'2002/02/02 02:02'))

        exp = GettextExportFilter(service)
        result = exp.exportMessages(['default'], ['de'])
        
        dt = time.time()
        dt = time.localtime(dt)
        dt = time.strftime('%Y/%m/%d %H:%M', dt)

        self.assertEqual(result.strip(), (self._data %dt).strip())


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(TestGettextExportImport)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())



=== Added File Zope3/lib/python/Zope/App/OFS/Services/TranslationService/tests/testMessageCatalog.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""Test the generic persistent Message Catalog.

$Id: testMessageCatalog.py,v 1.1 2002/07/11 07:12:44 srichter Exp $
"""
import unittest

from Zope.App.OFS.Services.TranslationService.MessageCatalog import \
     MessageCatalog
from Zope.I18n.tests.testIReadMessageCatalog import TestIReadMessageCatalog
from Zope.I18n.tests.testIWriteMessageCatalog import TestIWriteMessageCatalog


class MessageCatalogTest(TestIReadMessageCatalog, TestIWriteMessageCatalog):


    def startUp(self):
        TestIReadMessageCatalog.startUp(self)
        TestIWriteMessageCatalog.startUp(self)


    def _getMessageCatalog(self):
        catalog = MessageCatalog('en', 'default')
        catalog.setMessage('short_greeting', 'Hello!', 0)
        catalog.setMessage('greeting', 'Hello $name, how are you?', 0)
        return catalog
    
    def _getUniqueIndentifier(self):
        return ('en', 'default')


def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(MessageCatalogTest)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/lib/python/Zope/App/OFS/Services/TranslationService/tests/testTranslationService.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""This module tests the regular persistent Translation Service.

$Id: testTranslationService.py,v 1.1 2002/07/11 07:12:44 srichter Exp $
"""
import unittest, sys

from Zope.App.ComponentArchitecture.metaConfigure import handler

from Zope.App.OFS.Services.TranslationService.TranslationService import \
     TranslationService
from Zope.App.OFS.Services.TranslationService.MessageCatalog import \
     MessageCatalog 
from Zope.I18n.tests.testIReadTranslationService import \
     TestIReadTranslationService
from Zope.I18n.tests.testIWriteTranslationService import \
     TestIWriteTranslationService
from Zope.I18n.tests.testISyncTranslationService import \
     TestISyncTranslationService


class TestTranslationService(TestIReadTranslationService,
                             TestIWriteTranslationService,
                             TestISyncTranslationService):


    def setUp(self):
        TestISyncTranslationService.setUp(self)
        TestIReadTranslationService.setUp(self)
        TestIWriteTranslationService.setUp(self)
        handler('Factories', 'provideFactory', 'Message Catalog',
                MessageCatalog)


    def _getTranslationService(self):
        service = TranslationService('default') 

        en_catalog = MessageCatalog('en', 'default')
        de_catalog = MessageCatalog('de', 'default')
        # Populate the catalogs with translations of a message id
        en_catalog.setMessage('short_greeting', 'Hello!', 10)
        de_catalog.setMessage('short_greeting', 'Hallo!', 10)
        # And another message id with interpolation placeholders
        en_catalog.setMessage('greeting', 'Hello $name, how are you?', 0)
        de_catalog.setMessage('greeting', 'Hallo $name, wie geht es Dir?', 0)

        service.setObject('en-default-1', en_catalog)
        service.setObject('de-default-1', de_catalog)

        return service

    def testParameterNames(self):
        service = self._service
        translate = service.translate
        eq = self.assertEqual
        raises = self.assertRaises
        # Test that the second argument is called `msgid'
        eq(translate('default', msgid='short_greeting', target_language='en'),
           'Hello!')
        # This is what the argument used to be called
        raises(TypeError, translate, 'default', source='short_greeting',
               target_language='en')


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(TestTranslationService)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())