[Zope3-checkins] CVS: Zope3/src/zope/app/browser/services/registration/tests - __init__.py:1.1.2.1 test_changeregistrations.py:1.1.2.1 test_editregistration.py:1.1.2.1 test_namecomponentregistryview.py:1.1.2.1 test_registrationstatuswidget.py:1.1.2.1

Grégoire Weber zope@i-con.ch
Sun, 22 Jun 2003 10:22:47 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/services/registration/tests
In directory cvs.zope.org:/tmp/cvs-serv24874/src/zope/app/browser/services/registration/tests

Added Files:
      Tag: cw-mail-branch
	__init__.py test_changeregistrations.py 
	test_editregistration.py test_namecomponentregistryview.py 
	test_registrationstatuswidget.py 
Log Message:
Synced up with HEAD

=== Added File Zope3/src/zope/app/browser/services/registration/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/browser/services/registration/tests/test_changeregistrations.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.
#
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: test_changeregistrations.py,v 1.1.2.1 2003/06/22 14:22:46 gregweb Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.publisher.browser import TestRequest
from zope.app.services.tests.registrationstack \
     import TestingRegistrationStack
from zope.app.browser.services.registration import ChangeRegistrations

class Test(TestCase):

    def test_applyUpdates_and_setPrefix(self):
        registry = TestingRegistrationStack('a', 'b', 'c')
        request = TestRequest()
        view = ChangeRegistrations(registry, request)
        view.setPrefix("Roles")

        # Make sure we don't apply updates unless asked to
        request.form = {'Roles.active': 'disable'}
        view.applyUpdates()
        self.assertEqual(registry._data, ('a', 'b', 'c'))

        # Now test disabling
        request.form = {'submit_update': '', 'Roles.active': 'disable'}
        view.applyUpdates()
        self.assertEqual(registry._data, (None, 'a', 'b', 'c'))

        # Now test enabling c
        request.form = {'submit_update': '', 'Roles.active': 'c'}
        view.applyUpdates()
        self.assertEqual(registry._data, ('c', 'a', 'b'))



def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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


=== Added File Zope3/src/zope/app/browser/services/registration/tests/test_editregistration.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.
#
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: test_editregistration.py,v 1.1.2.1 2003/06/22 14:22:46 gregweb Exp $
"""
__metaclass__ = type

from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.browser.services.registration import EditRegistration
from zope.app.event.tests.placelesssetup import getEvents
from zope.app.interfaces.container import IContainer
from zope.app.interfaces.event import IObjectModifiedEvent
from zope.app.interfaces.event import IObjectRemovedEvent
from zope.app.interfaces.services.registration import ActiveStatus
from zope.app.interfaces.traversing import IContainmentRoot
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.component.adapter import provideAdapter
from zope.component.view import provideView
from zope.interface import Interface, implements
from zope.publisher.browser import BrowserView
from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserPresentation
from zope.app.context import ContextWrapper

class Container(dict):
    implements(IContainer, IContainmentRoot)

class I(Interface):
    pass

class C:
    implements(I)
    status = ActiveStatus


class Test(PlacefulSetup, TestCase):

    def test_remove_objects(self):
        c1 = C()
        c2 = C()
        c7 = C()
        d = Container({'1': c1, '2': c2, '7': c7})
        d = ContextWrapper(d, None)
        view = EditRegistration(d, TestRequest())
        view.remove_objects(['2', '7'])
        self.assertEqual(d, {'1': c1})

        self.failUnless(
            getEvents(IObjectRemovedEvent,
                      filter = lambda event: event.object == c2),
            )
        self.failUnless(
            getEvents(IObjectRemovedEvent,
                      filter = lambda event: event.object == c7)
            )
        self.failUnless(
            getEvents(IObjectModifiedEvent,
                      filter = lambda event: event.object == d)
            )

    def test_configInfo(self):

        class V(BrowserView):
            def setPrefix(self, p):
                self._prefix = p

        provideView(I, 'ItemEdit', IBrowserPresentation, V)

        c1 = C()
        c2 = C()
        c7 = C()
        d = Container({'1': c1, '2': c2, '7': c7})

        view = EditRegistration(d, TestRequest())

        info = view.configInfo()
        self.assertEqual(len(info), 3)
        self.assertEqual(info[0]['name'], '1')
        self.assertEqual(info[1]['name'], '2')
        self.assertEqual(info[2]['name'], '7')

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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


=== Added File Zope3/src/zope/app/browser/services/registration/tests/test_namecomponentregistryview.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.
#
##############################################################################
"""Unit test for the generic NameComponentRegistry view mixin

$Id: test_namecomponentregistryview.py,v 1.1.2.1 2003/06/22 14:22:46 gregweb Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.interface import Interface, implements
from zope.publisher.browser import TestRequest
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.publisher.interfaces.browser import IBrowserPresentation
from zope.component.view import provideView
from zope.component.adapter import provideAdapter
from zope.publisher.browser import BrowserView
from zope.app.browser.services.registration import NameComponentRegistryView
from zope.app.interfaces.traversing import ITraversable, ITraverser
from zope.app.traversing.adapters import Traverser


class SM:

    def __init__(self, **data):
        self._data = data

    def listRegistrationNames(self):
        return self._data.keys()

    def queryRegistrations(self, name):
        return self._data[name]

class I(Interface): pass

class Registry:
    implements(I)

    def __init__(self, active):
        self._active = active

    def active(self):
        return self._active

class ITestRegistration(Interface): pass

class Registration:

    implements(ITestRegistration, ITraversable)

    def __init__(self, path):
        self.componentPath = path

    def traverse(self, name, parameters, original_name, furtherPath):
        return self

class V(BrowserView):

    _update = 0

    def setPrefix(self, p):
        self._prefix = p

    def update(self):
        self._update += 1

class AU(BrowserView):

    def __str__(self):
        return "/" + self.context.componentPath

class Test(PlacelessSetup, TestCase):

    def test_update(self):
        provideAdapter(None, ITraverser, Traverser)
        provideView(I, 'ChangeRegistrations', IBrowserPresentation, V)
        provideView(ITestRegistration, 'absolute_url', IBrowserPresentation,
                    AU)

        r1 = Registry(None)
        r2 = Registry(Registration('1'))
        r3 = Registry(Registration('1'))

        sm = SM(test1=r1, test2=r2, test3=r3)

        services = NameComponentRegistryView(sm, TestRequest()).update()

        self.assertEqual(len(services), 3)

        self.assertEqual(services[0]['name'], 'test1')
        self.assertEqual(services[0]['active'], False)
        self.assertEqual(services[0]['inactive'], True)
        self.assertEqual(services[0]['view'].context, r1)
        self.assertEqual(services[0]['view']._prefix, "test1")
        self.assertEqual(services[0]['view']._update, 1)
        self.assertEqual(services[0]['url'], None)

        self.assertEqual(services[1]['name'], 'test2')
        self.assertEqual(services[1]['active'], True)
        self.assertEqual(services[1]['inactive'], False)
        self.assertEqual(services[1]['view'].context, r2)
        self.assertEqual(services[1]['view']._prefix, "test2")
        self.assertEqual(services[1]['view']._update, 1)
        self.assertEqual(services[1]['url'], '/1')

        self.assertEqual(services[2]['name'], 'test3')
        self.assertEqual(services[2]['active'], True)
        self.assertEqual(services[2]['inactive'], False)
        self.assertEqual(services[2]['view'].context, r3)
        self.assertEqual(services[2]['view']._prefix, "test3")
        self.assertEqual(services[2]['view']._update, 1)
        self.assertEqual(services[2]['url'], '/1')



def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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


=== Added File Zope3/src/zope/app/browser/services/registration/tests/test_registrationstatuswidget.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.
#
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: test_registrationstatuswidget.py,v 1.1.2.1 2003/06/22 14:22:46 gregweb Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.publisher.browser import TestRequest
from zope.app.interfaces.services.registration import RegistrationStatus
from zope.app.browser.services.registration import RegistrationStatusWidget

class Test(TestCase):

    def test_call(self):
        field = RegistrationStatus(__name__="status")
        request = TestRequest()
        widget = RegistrationStatusWidget(field, request)
        widget.setPrefix("f")

        text = ' '.join(widget().split())
        self.assertEqual(
            text,
            '<label><input type="radio" name="f.status" value="Unregistered"'
            ' checked>'
            '&nbsp;Unregistered</label> '
            '<label><input type="radio" name="f.status" value="Registered">'
            '&nbsp;Registered</label> '
            '<label><input type="radio" name="f.status" value="Active">'
            '&nbsp;Active</label>'
            )

        request.form['f.status'] = u'Registered'
        text = ' '.join(widget().split())
        self.assertEqual(
            text,
            '<label><input type="radio" name="f.status" value="Unregistered">'
            '&nbsp;Unregistered</label> '
            '<label><input type="radio" name="f.status" value="Registered"'
            ' checked>'
            '&nbsp;Registered</label> '
            '<label><input type="radio" name="f.status" value="Active">'
            '&nbsp;Active</label>'
            )

        widget.setData("Active")
        text = ' '.join(widget().split())
        self.assertEqual(
            text,
            '<label><input type="radio" name="f.status" value="Unregistered">'
            '&nbsp;Unregistered</label> '
            '<label><input type="radio" name="f.status" value="Registered">'
            '&nbsp;Registered</label> '
            '<label><input type="radio" name="f.status" value="Active"'
            ' checked>'
            '&nbsp;Active</label>'
            )

        widget.setData(u"Unregistered")
        text = ' '.join(widget().split())
        self.assertEqual(
            text,
            '<label><input type="radio" name="f.status" value="Unregistered"'
            ' checked>'
            '&nbsp;Unregistered</label> '
            '<label><input type="radio" name="f.status" value="Registered">'
            '&nbsp;Registered</label> '
            '<label><input type="radio" name="f.status" value="Active">'
            '&nbsp;Active</label>'
            )


def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

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