[Zope3-checkins] CVS: Zope3/src/zope/app/services/translation/tests - test_messagecatalog.py:1.3 test_translationservice.py:1.6

Barry Warsaw barry@wooz.org
Tue, 25 Mar 2003 18:25:45 -0500


Update of /cvs-repository/Zope3/src/zope/app/services/translation/tests
In directory cvs.zope.org:/tmp/cvs-serv19100/src/zope/app/services/translation/tests

Modified Files:
	test_messagecatalog.py test_translationservice.py 
Log Message:
Refactored the translation service interfaces and updated all the
tests.


=== Zope3/src/zope/app/services/translation/tests/test_messagecatalog.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/services/translation/tests/test_messagecatalog.py:1.2	Wed Dec 25 09:13:23 2002
+++ Zope3/src/zope/app/services/translation/tests/test_messagecatalog.py	Tue Mar 25 18:25:13 2003
@@ -17,19 +17,74 @@
 """
 import unittest
 
-from zope.app.services.translation.messagecatalog import \
-     MessageCatalog
-from zope.i18n.tests.test_ireadmessagecatalog import TestIReadMessageCatalog
-from zope.i18n.tests.test_iwritemessagecatalog import TestIWriteMessageCatalog
+from zope.interface.verify import verifyObject
+from zope.app.services.translation.messagecatalog import MessageCatalog
+from zope.app.interfaces.services.translation import ILocalMessageCatalog
+from zope.i18n.tests.test_imessagecatalog import TestIMessageCatalog
 
 
-class MessageCatalogTest(TestIReadMessageCatalog, TestIWriteMessageCatalog):
+# This is a mixin class -- don't add it to the suite
+class TestILocalMessageCatalog:
 
+    # This should be overwritten by every class that inherits this test
+    def _getMessageCatalog(self):
+        pass
 
-    def startUp(self):
-        TestIReadMessageCatalog.startUp(self)
-        TestIWriteMessageCatalog.startUp(self)
+    def _getUniqueIndentifier(self):
+        pass
 
+    def setUp(self):
+        self._catalog = self._getMessageCatalog()
+        assert verifyObject(ILocalMessageCatalog, self._catalog)
+
+    def testGetFullMessage(self):
+        catalog = self._catalog
+        self.assertEqual(catalog.getFullMessage('short_greeting'),
+                         {'domain': 'default',
+                          'language': 'en',
+                          'msgid': 'short_greeting',
+                          'msgstr': 'Hello!',
+                          'mod_time': 0})
+
+    def testSetMessage(self):
+        catalog = self._catalog
+        catalog.setMessage('test', 'Test', 1)
+        self.assertEqual(catalog.getFullMessage('test'),
+                         {'domain': 'default',
+                          'language': 'en',
+                          'msgid': 'test',
+                          'msgstr': 'Test',
+                          'mod_time': 1})
+        catalog.deleteMessage('test')
+
+    def testDeleteMessage(self):
+        catalog = self._catalog
+        self.assertEqual(catalog.queryMessage('test'), None)
+        catalog.setMessage('test', 'Test', 1)
+        self.assertEqual(catalog.queryMessage('test'), 'Test')
+        catalog.deleteMessage('test')
+        self.assertEqual(catalog.queryMessage('test'), None)
+
+    def testGetMessageIds(self):
+        catalog = self._catalog
+        ids = catalog.getMessageIds()
+        ids.sort()
+        self.assertEqual(ids, ['greeting', 'short_greeting'])
+
+    def testGetMessages(self):
+        catalog = self._catalog
+        ids = catalog.getMessageIds()
+        ids.sort()
+        self.assertEqual(ids, ['greeting', 'short_greeting'])
+
+
+class LocalMessageCatalogTest(unittest.TestCase,
+                              TestIMessageCatalog,
+                              TestILocalMessageCatalog):
+
+    def setUp(self):
+        TestIMessageCatalog.setUp(self)
+        TestILocalMessageCatalog.setUp(self)
 
     def _getMessageCatalog(self):
         catalog = MessageCatalog('en', 'default')
@@ -42,8 +97,6 @@
 
 
 def test_suite():
-    loader=unittest.TestLoader()
-    return loader.loadTestsFromTestCase(MessageCatalogTest)
-
-if __name__=='__main__':
-    unittest.TextTestRunner().run(test_suite())
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(LocalMessageCatalogTest))
+    return suite


=== Zope3/src/zope/app/services/translation/tests/test_translationservice.py 1.5 => 1.6 ===
--- Zope3/src/zope/app/services/translation/tests/test_translationservice.py:1.5	Tue Feb 11 21:17:35 2003
+++ Zope3/src/zope/app/services/translation/tests/test_translationservice.py	Tue Mar 25 18:25:13 2003
@@ -15,33 +15,229 @@
 
 $Id$
 """
-import unittest, sys
+import sys
+import unittest
 
+from zope.interface.verify import verifyObject
 from zope.app.services.servicenames import Factories
-
 from zope.app.component.metaconfigure import handler
 
+from zope.i18n.interfaces import IUserPreferredLanguages, ITranslationService
 from zope.app.services.translation.translationservice import \
      TranslationService
+from zope.app.interfaces.services.translation import ISyncTranslationService
 from zope.app.services.translation.messagecatalog import \
      MessageCatalog
-from zope.i18n.tests.test_ireadtranslationservice import \
-     TestIReadTranslationService
-from zope.i18n.tests.test_iwritetranslationservice import \
-     TestIWriteTranslationService
-from zope.i18n.tests.test_isynctranslationservice import \
-     TestISyncTranslationService
+from zope.i18n.tests.test_itranslationservice import \
+     TestITranslationService
+from zope.app.component.metaconfigure import provideService, managerHandler
+
+
+class Environment:
+
+    __implements__ = IUserPreferredLanguages
+
+    def __init__(self, langs=()):
+        self.langs = langs
+
+    def getPreferredLanguages(self):
+        return self.langs
+
+
+class TestILocalTranslationService(unittest.TestCase):
+
+    def _getTranslationService(self):
+        """This should be overwritten by every clas that inherits this test.
+
+           We expect the TranslationService to contain exactly 2 languages:
+           de and en
+        """
+
+    def setUp(self):
+        self._service = self._getTranslationService()
+        assert verifyObject(ITranslationService, self._service)
+        managerHandler('defineService', 'Translation', ITranslationService)
+        provideService('Translation', self._service, 'zope.Public')
+
+
+    def _getDomains(self, service):
+        domains = service.getAllDomains()
+        domains.sort()
+        return domains
+
+
+    def testGetAddDeleteDomain(self):
+        service = self._service
+        service.addLanguage('de')
+        d = self._getDomains(service)
+        self.assertEqual(service.getAllDomains(), d+[])
+        service.addDomain('test')
+        self.assertEqual(service.getAllDomains(), d+['test'])
+        service.addDomain('test2')
+        self.assertEqual(service.getAllDomains(), d+['test', 'test2'])
+        self.assertEqual(service.getAvailableDomains('de'),
+                         d+['test', 'test2'])
+        service.deleteDomain('test')
+        self.assertEqual(service.getAllDomains(), d+['test2'])
+        service.deleteDomain('test2')
+        self.assertEqual(service.getAllDomains(), d+[])
+
+
+    def _getLanguages(self, service):
+        languages = service.getAllLanguages()
+        languages.sort()
+        return languages
+
+
+    def testGetAddDeleteLanguage(self):
+        service = self._service
+        service.addDomain('test')
+        langs = self._getLanguages(service)
+        service.addLanguage('es')
+        self.assertEqual(self._getLanguages(service), langs+['es'])
+        service.addLanguage('fr')
+        self.assertEqual(self._getLanguages(service), langs+['es', 'fr'])
+        self.assertEqual(service.getAvailableLanguages('test'),
+                         langs+['es', 'fr'])
+        service.deleteLanguage('es')
+        self.assertEqual(self._getLanguages(service), langs+['fr'])
+        service.deleteLanguage('fr')
+        self.assertEqual(self._getLanguages(service), langs)
 
 
-class TestTranslationService(TestIReadTranslationService,
-                             TestIWriteTranslationService,
+    def testAddUpdateDeleteMessage(self):
+        service = self._service
+        self.assertEqual(service.translate('test', 'greeting',
+                                           target_language='de'), 'greeting')
+        service.addMessage('test', 'greeting', 'Hallo!', 'de')
+        self.assertEqual(service.translate('test', 'greeting',
+                                           target_language='de'), 'Hallo!')
+        service.updateMessage('test', 'greeting', 'Hallo Ihr da!', 'de')
+        self.assertEqual(service.translate('test', 'greeting',
+                                           target_language='de'),
+                         'Hallo Ihr da!')
+        service.deleteMessage('test', 'greeting', 'de')
+        self.assertEqual(service.translate('test', 'greeting',
+                                           target_language='de'), None)
+
+
+    def _getMessageIds(self, service, domain, filter="%"):
+        ids = service.getMessageIdsOfDomain(domain, filter)
+        ids.sort()
+        return ids
+
+
+    def testFilteredGetAllMessageIdsOfDomain(self):
+        service = self._service
+        service.addMessage('test',  'greeting', 'Greeting!', 'en')
+        service.addMessage('test',  'greeting2', 'Greeting 2!', 'en')
+        service.addMessage('test2', 'greeting3', 'Greeting 3!', 'en')
+        service.addMessage('test2', 'greeting4', 'Greeting 4!', 'en')
+
+        self.assertEqual(self._getMessageIds(service, 'test'),
+                         ['greeting', 'greeting2'])
+        self.assertEqual(self._getMessageIds(service, 'test2'),
+                         ['greeting3', 'greeting4'])
+        self.assertEqual(self._getMessageIds(service, 'test', 'greeting'),
+                         ['greeting', 'greeting2'])
+        self.assertEqual(self._getMessageIds(service, 'test', '%2'),
+                         ['greeting2'])
+        self.assertEqual(self._getMessageIds(service, 'test', 'gre%2'),
+                         ['greeting2'])
+        self.assertEqual(self._getMessageIds(service, 'test2', 'gre%'),
+                         ['greeting3', 'greeting4'])
+
+
+
+# A test mixing -- don't add this to the suite
+class TestISyncTranslationService:
+
+    foreign_messages = [
+        # Message that is not locally available
+        {'domain': 'default', 'language': 'en', 'msgid': 'test',
+         'msgstr': 'Test', 'mod_time': 0},
+        # This message is newer than the local one.
+        {'domain': 'default', 'language': 'de', 'msgid': 'short_greeting',
+         'msgstr': 'Hallo.', 'mod_time': 20},
+        # This message is older than the local one.
+        {'domain': 'default', 'language': 'en', 'msgid': 'short_greeting',
+         'msgstr': 'Hello', 'mod_time': 0},
+        # This message is up-to-date.
+        {'domain': 'default', 'language': 'en', 'msgid': 'greeting',
+         'msgstr': 'Hello $name, how are you?', 'mod_time': 0}]
+
+
+    local_messages = [
+        # This message is older than the foreign one.
+        {'domain': 'default', 'language': 'de', 'msgid': 'short_greeting',
+         'msgstr': 'Hallo!', 'mod_time': 10},
+        # This message is newer than the foreign one.
+        {'domain': 'default', 'language': 'en', 'msgid': 'short_greeting',
+         'msgstr': 'Hello!', 'mod_time': 10},
+        # This message is up-to-date.
+        {'domain': 'default', 'language': 'en', 'msgid': 'greeting',
+         'msgstr': 'Hello $name, how are you?', 'mod_time': 0},
+        # This message is only available locally.
+        {'domain': 'default', 'language': 'de', 'msgid': 'greeting',
+         'msgstr': 'Hallo $name, wie geht es Dir?', 'mod_time': 0},
+        ]
+
+
+    # This should be overwritten by every clas that inherits this test
+    def _getTranslationService(self):
+        pass
+
+
+    def setUp(self):
+        self._service = self._getTranslationService()
+        assert verifyObject(ISyncTranslationService, self._service)
+
+
+    def testGetMessagesMapping(self):
+        mapping = self._service.getMessagesMapping(['default'], ['de', 'en'],
+                                                  self.foreign_messages)
+        self.assertEqual(mapping[('test', 'default', 'en')],
+                         (self.foreign_messages[0], None))
+        self.assertEqual(mapping[('short_greeting', 'default', 'de')],
+                         (self.foreign_messages[1], self.local_messages[0]))
+        self.assertEqual(mapping[('short_greeting', 'default', 'en')],
+                         (self.foreign_messages[2], self.local_messages[1]))
+        self.assertEqual(mapping[('greeting', 'default', 'en')],
+                         (self.foreign_messages[3], self.local_messages[2]))
+        self.assertEqual(mapping[('greeting', 'default', 'de')],
+                         (None, self.local_messages[3]))
+
+
+    def testSynchronize(self):
+        service = self._service
+        mapping = service.getMessagesMapping(['default'], ['de', 'en'],
+                                             self.foreign_messages)
+        service.synchronize(mapping)
+
+        self.assertEqual(service.getMessage('test', 'default', 'en'),
+                         self.foreign_messages[0])
+        self.assertEqual(service.getMessage('short_greeting', 'default', 'de'),
+                         self.foreign_messages[1])
+        self.assertEqual(service.getMessage('short_greeting', 'default', 'en'),
+                         self.local_messages[1])
+        self.assertEqual(service.getMessage('greeting', 'default', 'en'),
+                         self.local_messages[2])
+        self.assertEqual(service.getMessage('greeting', 'default', 'en'),
+                         self.foreign_messages[3])
+        self.assertEqual(service.getMessage('greeting', 'default', 'de'),
+                         None)
+
+
+class TestTranslationService(unittest.TestCase,
+                             TestITranslationService,
+                             TestILocalTranslationService,
                              TestISyncTranslationService):
 
 
     def setUp(self):
         TestISyncTranslationService.setUp(self)
-        TestIReadTranslationService.setUp(self)
-        TestIWriteTranslationService.setUp(self)
+        TestITranslationService.setUp(self)
+        TestILocalTranslationService.setUp(self)
         handler(Factories, 'provideFactory', 'Message Catalog',
                 MessageCatalog)
 
@@ -76,10 +272,8 @@
                target_language='en')
 
 
-def test_suite():
-    loader = unittest.TestLoader()
-    return loader.loadTestsFromTestCase(TestTranslationService)
 
-
-if __name__ == '__main__':
-    unittest.TextTestRunner().run(test_suite())
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(TestTranslationService))
+    return suite