[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Container/Views/Browser/tests - AdderBaseTests.py:1.1.2.1 __init__.py:1.1.2.1 testContents.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 4 Mar 2002 11:46:26 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Container/Views/Browser/tests
In directory cvs.zope.org:/tmp/cvs-serv12102/Container/Views/Browser/tests

Added Files:
      Tag: Zope-3x-branch
	AdderBaseTests.py __init__.py testContents.py 
Log Message:
Added base-class framework for container content types, views, and
tests.

Among other things, this provides a framework for building interface
tests that can be subclasses by tests for implementation of the
interfaces.



=== Added File Zope3/lib/python/Zope/App/OFS/Container/Views/Browser/tests/AdderBaseTests.py ===
# Copyright (c) 2001 Zope Corporation and Contributors.  All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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.

from Zope.App.OFS.Container.Views.Browser.Adder import DuplicateIDError
from Zope.App.ZMI.provideClass import provideClass
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup

class Foo: pass
class Bar: pass
class Baz: pass

AddPermission = 'add'

class BaseRegistryTest(CleanUp):
    """Base adder registry interaction tests.

    Subclasses need to define a method, '_TestView__newContext', that
    takes no arguments and that returns a new test view context.

    Subclasses need to define a method, '_TestView__newView', that
    takes a context object and that returns a new test view.

    Subclasses need to define a method, '_TestAdderView__registry', that
    returns the appropriate registry.

    """
    
    def testNonesuch( self ):
        """
            Do we get the correct information back when no
            addables have been registered?
        """
        container = self._TestView__newContext()
        fa = self._TestView__newView(container)
        info_list = fa.listAddableInfo()
        self.failIf(info_list)

    def testHaveSome( self ):
        """
            Do we get the correct information back when no
            addables have been registered?
        """
        data = [ ( 'foo', 'Foo', 'Foo Thingies' )
               , ( 'bar', 'Bar', 'Barflies' )
               , ( 'baz', 'Baz', 'Bazzing Around' )
               ]

        addables = self._TestAdderView__registry()

        for datum in data:
            apply(addables.provideAddable, datum, {} )
        container = self._TestView__newContext()
        fa = self._TestView__newView( container )
        info_list = fa.listAddableInfo()
        self.assertEquals( len( info_list ), len( data ) )

        id_list = map( lambda x: x.id(), info_list )
        self.assert_( 'foo' in id_list )
        self.assert_( 'bar' in id_list )
        self.assert_( 'baz' in id_list )

        title_list = map( lambda x: x.title(), info_list )
        self.assert_( 'Foo' in title_list )
        self.assert_( 'Bar' in title_list )
        self.assert_( 'Baz' in title_list )

    def testNonesuchAction( self ):
        """
            Can we get add an object back when no classes have
            been registered?
        """
        container = self._TestView__newContext()
        fa = self._TestView__newView( container )
        self.assertRaises( KeyError, fa.action, type_name='foo', id='foo_123' )

    def testHaveSomeAction( self ):
        """
            Can we get add an object back when some classes have
            been registered?
        """
        provideClass( self._TestAdderView__registry()
                    ,  qualified_name='Zope.App.OFS.tests.testContainerAdd.Foo'
                    , _class=Foo
                    , permission=AddPermission
                    , title='Foo'
                    )
        provideClass(self._TestAdderView__registry()
                    , qualified_name='Zope.App.OFS.tests.testContainerAdd.Bar'
                    , _class=Bar
                    , permission=AddPermission
                    , title='Bar'
                    )
        provideClass(self._TestAdderView__registry()
                    , qualified_name='Zope.App.OFS.tests.testContainerAdd.Baz'
                    , _class=Baz
                    , permission=AddPermission
                    , title='Baz'
                    )

        container = self._TestView__newContext()
        fa = self._TestView__newView( container )
        info_list = fa.listAddableInfo()
        self.assertEquals( len( info_list ), 3 )

class BaseAddingTest(CleanUp):
    """Base adding tests

    Subclasses need to define a method, '_TestView__newContext', that
    takes no arguments and that returns a new test view context.

    Subclasses need to define a method, '_TestView__newView', that
    takes a context object and that returns a new test view.

    Subclasses need to define a method, '_TestAdderView__registry', that
    returns the appropriate registry.

    """

    def setUp(self):
        CleanUp.setUp(self)
        container = self._TestView__newContext()        
        provideClass(self._TestAdderView__registry()
                    , qualified_name='Zope.App.OFS.Container'
                    , _class=container.__class__
                    , permission=AddPermission
                    , title='Container'
                    )

    def testAdding(self):
        """
            Does addition of a new object with the same ID as an existing
            object fail?
        """
        container = self._TestView__newContext()
        fa = self._TestView__newView( container )
        fa.action( type_name='Zope.App.OFS.Container', id='foo' )

        self.assertEquals( len( container.objectIds() ), 1 )
        self.assertEquals( container.objectIds()[0], 'foo' )
        self.assertEquals( len( container.objectValues() ), 1 )
        self.assertEquals( container.objectValues()[0].__class__,
                           container.__class__ )

    def testDuplicates( self ):
        """
            Does addition of a new object with the same ID as an existing
            object fail?
        """
        container = self._TestView__newContext()
        fa = self._TestView__newView(container)
        fa.action( type_name='Zope.App.OFS.Container', id='foo' )

        self.assertRaises( DuplicateIDError
                         , fa.action
                         ,  type_name='Zope.App.OFS.Container'
                         , id='foo'
                         )


=== Added File Zope3/lib/python/Zope/App/OFS/Container/Views/Browser/tests/__init__.py ===



=== Added File Zope3/lib/python/Zope/App/OFS/Container/Views/Browser/tests/testContents.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
# 
##############################################################################
"""

Revision information:
$Id: testContents.py,v 1.1.2.1 2002/03/04 16:46:25 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup

class BaseTestContentsBrowserView:
    """Base class for testing browser contents.

    Subclasses need to define a method, '_TestView__newContext', that
    takes no arguments and that returns a new empty test view context.

    Subclasses need to define a method, '_TestView__newView', that
    takes a context object and that returns a new test view.
    """

    def testInfo(self):
        """ Do we get the correct information back from ContainerContents? """
        container = self._TestView__newContext()
        subcontainer = self._TestView__newContext()
        container.setObject( 'subcontainer', subcontainer )
        document = Document()
        container.setObject( 'document', document )

        fc = self._TestView__newView( container )
        info_list = fc.listContentInfo()

        self.assertEquals( len( info_list ), 2 )

        ids = map( lambda x: x['id'], info_list )
        self.assert_( 'subcontainer' in ids )

        objects = map( lambda x: x['object'], info_list )
        self.assert_( subcontainer in objects )

        titles = map( lambda x: x['title'], info_list )
        self.assert_( 'subcontainer' in titles )

        urls = map( lambda x: x['url'], info_list )
        self.assert_( 'subcontainer' in urls )

        self.failIf( filter( None, map( lambda x: x['icon'], info_list ) ) )

    def testRemove( self ):
        container = self._TestView__newContext()
        subcontainer = self._TestView__newContext()
        container.setObject( 'subcontainer', subcontainer )
        document = Document()
        container.setObject( 'document', document )
        container.setObject( 'document2', Document() )

        fc = self._TestView__newView( container )
        fc.remove( name='document2' )
        info_list = fc.listContentInfo()

        self.assertEquals( len( info_list ), 2 )

        ids = map( lambda x: x['id'], info_list )
        self.assert_( 'subcontainer' in ids )

        objects = map( lambda x: x['object'], info_list )
        self.assert_( subcontainer in objects )

        titles = map( lambda x: x['title'], info_list )
        self.assert_( 'subcontainer' in titles )

        urls = map( lambda x: x['url'], info_list )
        self.assert_( 'subcontainer' in urls )

        self.assertRaises( KeyError, fc.remove, 'document3' )

        fc.remove( 'document3', 1 )


class Document:
    pass


class Test(BaseTestContentsBrowserView, TestCase):

    def _TestView__newContext(self):
        from Zope.App.OFS.Container.SampleContainer import Container
        return Container()

    def _TestView__newView(self, container):
        from Zope.App.OFS.Container.Views.Browser.Contents import Contents
        return Contents(container)

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

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