[Zope3-checkins] CVS: Products3/demo/smileyservice/tests - __init__.py:1.1 smiley.zcml:1.1 test_directives.py:1.1 test_globalservice.py:1.1 test_localservice.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Aug 22 18:27:37 EDT 2003


Update of /cvs-repository/Products3/demo/smileyservice/tests
In directory cvs.zope.org:/tmp/cvs-serv5789/smileyservice/tests

Added Files:
	__init__.py smiley.zcml test_directives.py 
	test_globalservice.py test_localservice.py 
Log Message:
Initial checkin of the Smiley Service. :-)

This service registers smiley text representations and their corresponding
url to an image representation. This implements a global and a local 
version. In order to make the implementation more interesting, I also support
themes, which are basically collections of similar icons. (Thanks goes to
dreamcatcher, who gave me the idea.)

In the local version of the service I implemented Themes using the new 
named utility/service pattern.

Since the service is pretty much useless without being used, I also extended
the messageboard example by another step using this service. This also 
keeps a common theme is the Devel Cookbook, which is good (thanks to 
philliKON for the tip). The new step will follow soon. This also gives me
a good base for a vocabulary and vocabulary widget, which I assume will be-
come step 14.

Finally, this product was written for the Zope Devel Cookbook. It will be 
covered in the "New Registries via Services" and "Writing Local Services".


=== Added File Products3/demo/smileyservice/tests/__init__.py ===


=== Added File Products3/demo/smileyservice/tests/smiley.zcml ===
<configure 
    xmlns:zope="http://namespaces.zope.org/zope"
    xmlns="http://namespaces.zope.org/smiley">

  <zope:include package="zopeproducts.demo.smileyservice" file="meta.zcml" />

  <smileys theme="yazoo">
    <smiley text=":("   file="../smileys/yazoo/sad.png"/>
    <smiley text=":)"   file="../smileys/yazoo/smile.png"/>
  </smileys>

  <smiley
      theme="plain"
      text=":("
      file="../smileys/yazoo/sad.png"/>

  <defaultTheme theme="plain" />

</configure>


=== Added File Products3/demo/smileyservice/tests/test_directives.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 workflow ZCML namespace directives.

$Id: test_directives.py,v 1.1 2003/08/22 21:27:36 srichter Exp $
"""
import unittest

from zope.app.tests.placelesssetup import PlacelessSetup
from zope.configuration import xmlconfig
from zopeproducts.demo.smileyservice.globalservice import smileyService 

from zopeproducts.demo.smileyservice import tests 


class DirectivesTest(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        self.context = xmlconfig.file("smiley.zcml", tests)

    def test_SmileyDirectives(self):
        self.assertEqual(smileyService._GlobalSmileyService__themes, {
            u'yazoo': {u':)': u'/++resource++yazoo__smile.png',
                       u':(': u'/++resource++yazoo__sad.png'},
            u'plain': {u':(': u'/++resource++plain__sad.png'}
                      })

    def test_defaultTheme(self):
        self.assertEqual(smileyService.defaultTheme, 'plain')

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(DirectivesTest),
        ))

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


=== Added File Products3/demo/smileyservice/tests/test_globalservice.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 workflow ZCML namespace directives.

$Id: test_globalservice.py,v 1.1 2003/08/22 21:27:36 srichter Exp $
"""
import os
import unittest

from zope.component.exceptions import ComponentLookupError
from zope.component.tests.placelesssetup import PlacelessSetup
from zope.component.view import provideView
from zope.interface import implements, Interface
from zope.interface.verify import verifyClass
from zope.publisher.browser import TestRequest 
from zope.publisher.interfaces.browser import IBrowserPresentation
from zopeproducts.demo.smileyservice.interfaces import \
     IGlobalSmileyService
from zopeproducts.demo.smileyservice.globalservice import GlobalSmileyService

class AbsoluteURL:

    def __init__(self, context, request):
        pass

    def __str__(self):
        return ''


class ServiceTest(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        PlacelessSetup.setUp(self)
        provideView(Interface, 'absolute_url', IBrowserPresentation,
                    AbsoluteURL)
        self.service = GlobalSmileyService()
        self.service.provideSmiley(
            ':-)', '++resource++plain__smile.png', 'plain')
        self.service.provideSmiley(
            ':-)', '++resource++yazoo__smile.png', 'yazoo')
        self.service.defaultTheme = 'plain'

    def test_verifyInterface(self):
        self.assert_(verifyClass(IGlobalSmileyService, GlobalSmileyService))

    def test_getSmiley(self):
        self.assertEqual(self.service.getSmiley(':-)', TestRequest(), 'yazoo'),
                         '/++resource++yazoo__smile.png')

        self.assertEqual(self.service.getSmiley(':-)', TestRequest(), 'plain'),
                         '/++resource++plain__smile.png')

        self.assertEqual(self.service.getSmiley(':-)', TestRequest()),
                         '/++resource++plain__smile.png')

        self.assertRaises(ComponentLookupError,
                          self.service.getSmiley,
                          ':(', TestRequest(), 'plain')


    def test_querySmiley(self):
        req = TestRequest()
        self.assertEqual(self.service.querySmiley(':-)', req, 'yazoo'),
                         '/++resource++yazoo__smile.png')

        self.assertEqual(self.service.querySmiley(':-)', req, 'plain'),
                         '/++resource++plain__smile.png')

        self.assertEqual(self.service.querySmiley(':-)', req),
                         '/++resource++plain__smile.png')

        self.assertEqual(self.service.querySmiley(':(', req, 'plain'),
                         None)


    def test_getThemes(self):
        themes = self.service.getThemes()
        themes.sort()
        self.assertEqual(themes, ['plain', 'yazoo'])

    def test_getSmileysMapping(self):
        req = TestRequest()
        service = self.service
        self.assertEqual(service.getSmileysMapping(req, 'yazoo'),
                         {':-)': '/++resource++yazoo__smile.png'})

        self.assertEqual(service.getSmileysMapping(req, 'plain'),
                         {':-)': '/++resource++plain__smile.png'})

        self.assertEqual(service.getSmileysMapping(req),
                         {':-)': '/++resource++plain__smile.png'})

        self.assertEqual(self.service.getSmileysMapping(req, 'foobar'),
                         {})
        


def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(ServiceTest),
        ))

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


=== Added File Products3/demo/smileyservice/tests/test_localservice.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Smiley Service Tests

$Id: test_localservice.py,v 1.1 2003/08/22 21:27:36 srichter Exp $
"""
import os
import unittest

from zope.app import zapi
from zope.app.content.image import Image 
from zope.app.interfaces.annotation import IAttributeAnnotatable
from zope.app.interfaces.services.registration import ActiveStatus
from zope.app.interfaces.services.utility import ILocalUtility
from zope.app.services.utility import LocalUtilityService, UtilityRegistration
from zope.app.services.servicenames import Utilities
from zope.app.tests import setup
from zope.component import getServiceManager
from zope.component.exceptions import ComponentLookupError
from zope.interface import classImplements
from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserPublisher

from zopeproducts.demo import smileyservice 
from zopeproducts.demo.smileyservice.globalservice import smileyService
from zopeproducts.demo.smileyservice.localservice import \
     LocalSmileyService, Theme 
from zopeproducts.demo.smileyservice.interfaces import \
     ISmileyService, ILocalSmileyService, ITheme 

def addTheme(servicemanager, name):
    """Add a theme to the service manager's default package."""
    theme = Theme()

    default = zapi.traverse(servicemanager, 'default')
    default.setObject(name, theme)
    path = "%s/default/%s" % (zapi.getPath(servicemanager), name)
    reg = UtilityRegistration(name, ITheme, path)
    key = default.getRegistrationManager().setObject("", reg)
    zapi.traverse(default.getRegistrationManager(), key).status = ActiveStatus
    return zapi.traverse(default, name)    


def addSmiley(theme, text, filename):
    base_dir = os.path.dirname(smileyservice.__file__)
    filename = os.path.join(base_dir, filename)
    smiley = Image(open(filename, 'r'))
    theme.setObject(text, smiley)
    return zapi.traverse(theme, text)


class LocalSmileyServiceTest(unittest.TestCase):

    def setUp(self):
        setup.placefulSetUp()
        self.rootFolder = setup.buildSampleFolderTree()

        # Define Menu Service
        sm = getServiceManager(None)
        sm.defineService('Smileys', ISmileyService)
        sm.provideService('Smileys', smileyService)
        classImplements(Theme, ILocalUtility)
        classImplements(Theme, IAttributeAnnotatable)
        smileyService.defaultTheme = 'plain'
        
        # Create Components in root folder
        mgr = setup.createServiceManager(self.rootFolder)
        setup.addService(mgr, Utilities, LocalUtilityService())
        self.root_ss = setup.addService(mgr, 'Smileys', LocalSmileyService())
        self.root_ss.defaultTheme = None

        theme = addTheme(mgr, 'plain')
        addSmiley(theme, ':)',  'smileys/plain/smile.png')
        addSmiley(theme, ':(',  'smileys/plain/sad.png')

        theme = addTheme(mgr, 'yazoo')
        addSmiley(theme, ':)',  'smileys/yazoo/smile.png')
        addSmiley(theme, ':(',  'smileys/yazoo/sad.png')
        
        # Create Components in folder1
        folder1 = zapi.traverse(self.rootFolder, 'folder1')
        mgr = setup.createServiceManager(folder1)
        setup.addService(mgr, Utilities, LocalUtilityService())
        self.folder_ss = setup.addService(mgr, 'Smileys', LocalSmileyService())
        self.folder_ss.defaultTheme = 'yazoo'

        theme = addTheme(mgr, 'plain')
        addSmiley(theme, ':)',  'smileys/plain/biggrin.png')
        addSmiley(theme, '8)',  'smileys/plain/cool.png')
        theme = addTheme(mgr, 'foobar')

    def tearDown(self):
        setup.placefulTearDown()
        smileyService.__init__()

    def test_VerifyInterfaceImplementation(self):
        self.assert_(ILocalSmileyService.isImplementedBy(LocalSmileyService()))
        self.assert_(ITheme.isImplementedBy(Theme()))

    def test_defaultTheme(self):
        self.assertEqual(smileyService.defaultTheme, 'plain')
        self.assertEqual(self.root_ss.defaultTheme, 'plain')
        self.assertEqual(self.folder_ss.defaultTheme, 'yazoo')

    def test_get_and_querySmiley(self):
        self.assertEqual(
            self.root_ss.getSmiley(':)', TestRequest(), 'plain'),
            'http://127.0.0.1/++etc++site/default/plain/:)')
        self.assertEqual(
            self.root_ss.getSmiley(':(', TestRequest(), 'plain'),
            'http://127.0.0.1/++etc++site/default/plain/:(')
        self.assertEqual(
            self.root_ss.getSmiley(':)', TestRequest()),
            'http://127.0.0.1/++etc++site/default/plain/:)')
        self.assertEqual(
            self.root_ss.getSmiley(':(', TestRequest()),
            'http://127.0.0.1/++etc++site/default/plain/:(')
        self.assertEqual(
            self.folder_ss.getSmiley(':)', TestRequest(), 'plain'),
            'http://127.0.0.1/folder1/++etc++site/default/plain/:)')
        self.assertEqual(
            self.folder_ss.getSmiley(':(', TestRequest(), 'plain'),
            'http://127.0.0.1/++etc++site/default/plain/:(')
        self.assertEqual(
            self.folder_ss.getSmiley(':(', TestRequest()),
            'http://127.0.0.1/++etc++site/default/yazoo/:(')
        self.assertRaises(
            ComponentLookupError,
            self.root_ss.getSmiley, ':|', TestRequest())
        self.assertRaises(
            ComponentLookupError,
            self.folder_ss.getSmiley, ':|', TestRequest())
        self.assertEqual(
            None,
            self.root_ss.querySmiley(':|', TestRequest()))
        self.assertEqual(
            None,
            self.folder_ss.querySmiley(':|', TestRequest()))


    def test_getThemes(self):
        themes = list(self.root_ss.getThemes())
        themes.sort()
        self.assertEqual(themes, ['plain', 'yazoo'])
        themes = list(self.folder_ss.getThemes())
        themes.sort()
        self.assertEqual(themes, ['foobar', 'plain', 'yazoo'])


    def test_getLocalThemes(self):
        themes = list(self.root_ss.getLocalThemes())
        themes.sort()
        self.assertEqual(themes, ['plain', 'yazoo'])
        themes = list(self.folder_ss.getLocalThemes())
        themes.sort()
        self.assertEqual(themes, ['foobar', 'plain'])


    def test_getSmileyMapping(self):
        req = TestRequest()
        self.assertEqual(
            self.root_ss.getSmileysMapping(req),
            {':)': 'http://127.0.0.1/++etc++site/default/plain/:)',
             ':(': 'http://127.0.0.1/++etc++site/default/plain/:('})
        self.assertEqual(
            self.root_ss.getSmileysMapping(req, 'plain'),
            {':)': 'http://127.0.0.1/++etc++site/default/plain/:)',
             ':(': 'http://127.0.0.1/++etc++site/default/plain/:('})
        self.assertEqual(
            self.root_ss.getSmileysMapping(req, 'yazoo'),
            {':)': 'http://127.0.0.1/++etc++site/default/yazoo/:)',
             ':(': 'http://127.0.0.1/++etc++site/default/yazoo/:('})
        self.assertEqual(
            self.folder_ss.getSmileysMapping(req, 'plain'),
            {':)': 'http://127.0.0.1/folder1/++etc++site/default/plain/:)',
             ':(': 'http://127.0.0.1/++etc++site/default/plain/:(',
             '8)': 'http://127.0.0.1/folder1/++etc++site/default/plain/8)'})
        self.assertEqual(
            self.folder_ss.getSmileysMapping(req, 'yazoo'),
            {':)': 'http://127.0.0.1/++etc++site/default/yazoo/:)',
             ':(': 'http://127.0.0.1/++etc++site/default/yazoo/:('})
        self.assertEqual(
            self.folder_ss.getSmileysMapping(req, 'foobar'), {})
        self.assertEqual(
            self.folder_ss.getSmileysMapping(req, 'blah'), {})


    def test_get_and_queryTheme(self):
        self.assertEqual(
            self.root_ss.getTheme('plain'),
            zapi.getParent(self.root_ss)['plain'])
        self.assertEqual(
            self.root_ss.getTheme('yazoo'),
            zapi.getParent(self.root_ss)['yazoo'])
        self.assertRaises(
            ComponentLookupError,
            self.root_ss.getTheme, 'foobar')
        self.assertEqual(
            self.root_ss.queryTheme('foobar'),
            None)
        self.assertEqual(
            self.folder_ss.getTheme('plain'),
            zapi.getParent(self.folder_ss)['plain'])
        self.assertEqual(
            self.folder_ss.getTheme('yazoo'),
            zapi.getParent(self.root_ss)['yazoo'])
        self.assertEqual(
            self.folder_ss.getTheme('foobar'),
            zapi.getParent(self.folder_ss)['foobar'])


    def test_get_and_queryTheme_local_only(self):
        self.assertEqual(
            self.root_ss.getTheme('plain', True),
            zapi.getParent(self.root_ss)['plain'])
        self.assertEqual(
            self.root_ss.getTheme('yazoo', True),
            zapi.getParent(self.root_ss)['yazoo'])
        self.assertRaises(
            ComponentLookupError,
            self.root_ss.getTheme, 'foobar', True)
        self.assertEqual(
            self.root_ss.queryTheme('foobar', True),
            None)
        self.assertEqual(
            self.folder_ss.getTheme('plain', True),
            zapi.getParent(self.folder_ss)['plain'])
        self.assertRaises(
            ComponentLookupError,
            self.folder_ss.getTheme, 'yazoo', True)
        self.assertEqual(
            self.folder_ss.getTheme('foobar', True),
            zapi.getParent(self.folder_ss)['foobar'])
        

def test_suite():
    return unittest.makeSuite(LocalSmileyServiceTest)

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




More information about the Zope3-Checkins mailing list