[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Container/tests - __init__.py:1.1.2.1 testIContainer.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/tests
In directory cvs.zope.org:/tmp/cvs-serv12102/Container/tests
Added Files:
Tag: Zope-3x-branch
__init__.py testIContainer.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/tests/__init__.py ===
=== Added File Zope3/lib/python/Zope/App/OFS/Container/tests/testIContainer.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: testIContainer.py,v 1.1.2.1 2002/03/04 16:46:26 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.App.OFS.IContainer import IContainer
from Interface import verify
class BaseTestIContainer:
"""Base test cases for containers.
Subclasses need to define a method, '_Test__new', that
takes no arguments and that returns a new empty test container.
"""
def __setUp(self):
self.__container = container = self._Test__new()
for k, v in [(3, 0), (2, 1), (4, 2), (6, 3), (0, 4),
(5, 5), (1, 6), (8, 7), (7, 8), (9, 9)]:
container.setObject(k, v)
return container
############################################################
# Interface-driven tests:
def testIContainerVerify(self):
verify(IContainer, self._Test__new())
def test_objectIds(self):
'''See interface IReadContainer'''
container = self._Test__new()
data = container.objectIds()
self.assertEqual(list(data), [])
container = self.__setUp()
data = container.objectIds()
data = list(data); data.sort() # convert to sorted list
self.assertEqual(data, range(10))
def test_getObject(self):
'''See interface IReadContainer'''
container = self._Test__new()
self.assertRaises(KeyError, container.getObject, 1)
self.assertEqual(container.getObject(1, 99), 99)
container = self.__setUp()
self.assertRaises(KeyError, container.getObject, 100)
self.assertEqual(container.getObject(100, 99), 99)
self.assertEqual(container.getObject(1, 99), 6)
self.assertEqual(container.getObject(7), 8)
self.assertEqual(container.getObject(0), 4)
self.assertEqual(container.getObject(9), 9)
def test_objectValues(self):
'''See interface IReadContainer'''
container = self._Test__new()
data = container.objectValues()
self.assertEqual(list(data), [])
container = self.__setUp()
data = container.objectValues()
data = list(data); data.sort() # convert to sorted list
self.assertEqual(data, range(10))
def test_objectCount(self):
'''See interface IReadContainer'''
container = self._Test__new()
self.assertEqual(container.objectCount(), 0)
container = self.__setUp()
self.assertEqual(container.objectCount(), 10)
def test_objectItems(self):
'''See interface IReadContainer'''
container = self._Test__new()
data = container.objectItems()
self.assertEqual(list(data), [])
container = self.__setUp()
data = container.objectItems()
data = list(data); data.sort() # convert to sorted list
self.assertEqual(data, [
(0, 4), (1, 6), (2, 1), (3, 0), (4, 2),
(5, 5), (6, 3), (7, 8), (8, 7), (9, 9)
])
def test_hasObject(self):
'''See interface IReadContainer'''
container = self._Test__new()
self.assertEqual(not not container.hasObject(1), 0)
container = self.__setUp()
self.assertEqual(not not container.hasObject(100), 0)
self.assertEqual(not not container.hasObject(1), 1)
self.assertEqual(not not container.hasObject(0), 1)
self.assertEqual(not not container.hasObject(9), 1)
def test_delObject(self):
'''See interface IWriteContainer'''
container = self._Test__new()
self.assertRaises(KeyError, container.delObject, 1)
container = self.__setUp()
self.assertRaises(KeyError, container.delObject, 100)
container.delObject(1)
container.delObject(9)
self.assertRaises(KeyError, container.getObject, 1)
self.assertRaises(KeyError, container.getObject, 9)
self.assertEqual(container.getObject(1, 99), 99)
self.assertEqual(container.getObject(7), 8)
self.assertEqual(container.getObject(0), 4)
self.assertEqual(container.getObject(9, 88), 88)
############################################################
# Tests from Folder
def testEmpty( self ):
folder = self._Test__new()
self.failIf( folder.objectIds() )
self.failIf( folder.objectValues() )
self.failIf( folder.objectItems() )
self.failIf( folder.objectCount() )
self.failIf( folder.hasObject( 'foo' ) )
self.assertEquals( folder.getObject( 'foo', None ), None )
self.assertRaises( KeyError, folder.getObject, 'foo' )
self.assertRaises( KeyError, folder.delObject, 'foo' )
def testOneItem( self ):
folder = self._Test__new()
foo = []
folder.setObject( 'foo', foo )
self.assertEquals( len( folder.objectIds() ), 1 )
self.assertEquals( folder.objectIds()[0], 'foo' )
self.assertEquals( len( folder.objectValues() ), 1 )
self.assertEquals( folder.objectValues()[0], foo )
self.assertEquals( len( folder.objectItems() ), 1 )
self.assertEquals( folder.objectItems()[0], ( 'foo', foo ) )
self.assertEquals( folder.objectCount(), 1 )
self.failUnless( folder.hasObject( 'foo' ) )
self.failIf( folder.hasObject( 'bar' ) )
self.assertEquals( folder.getObject( 'foo', None ), foo )
self.assertEquals( folder.getObject( 'foo' ), foo )
self.assertRaises( KeyError, folder.getObject, 'qux' )
foo2 = []
folder.setObject( 'foo', foo )
self.assertEquals( len( folder.objectIds() ), 1 )
self.assertEquals( folder.objectIds()[0], 'foo' )
self.assertEquals( len( folder.objectValues() ), 1 )
self.assertEquals( folder.objectValues()[0], foo2 )
self.assertEquals( len( folder.objectItems() ), 1 )
self.assertEquals( folder.objectItems()[0], ( 'foo', foo2 ) )
self.assertEquals( folder.objectCount(), 1 )
folder.delObject( 'foo' )
self.failIf( folder.objectIds() )
self.failIf( folder.objectValues() )
self.failIf( folder.objectItems() )
self.failIf( folder.objectCount() )
self.failIf( folder.hasObject( 'foo' ) )
self.assertRaises( KeyError, folder.getObject, 'foo' )
self.assertEquals( folder.getObject( 'foo', None ), None )
self.assertRaises( KeyError, folder.delObject, 'foo' )
def testManyItems( self ):
folder = self._Test__new()
objects = [ [0], [1], [2], [3] ]
folder.setObject( 'foo', objects[0] )
folder.setObject( 'bar', objects[1] )
folder.setObject( 'baz', objects[2] )
folder.setObject( 'bam', objects[3] )
self.assertEquals( len( folder.objectIds() ), len( objects ) )
self.failUnless( 'foo' in folder.objectIds() )
self.failUnless( 'bar' in folder.objectIds() )
self.failUnless( 'baz' in folder.objectIds() )
self.failUnless( 'bam' in folder.objectIds() )
self.assertEquals( len( folder.objectValues() ), len( objects ) )
self.failUnless( objects[0] in folder.objectValues() )
self.failUnless( objects[1] in folder.objectValues() )
self.failUnless( objects[2] in folder.objectValues() )
self.failUnless( objects[3] in folder.objectValues() )
self.assertEquals( len( folder.objectItems() ), len( objects ) )
self.failUnless( ( 'foo', objects[0] ) in folder.objectItems() )
self.failUnless( ( 'bar', objects[1] ) in folder.objectItems() )
self.failUnless( ( 'baz', objects[2] ) in folder.objectItems() )
self.failUnless( ( 'bam', objects[3] ) in folder.objectItems() )
self.assertEquals( folder.objectCount(), len( objects ) )
self.failUnless( folder.hasObject( 'foo' ) )
self.failUnless( folder.hasObject( 'bar' ) )
self.failUnless( folder.hasObject( 'baz' ) )
self.failUnless( folder.hasObject( 'bam' ) )
self.failIf( folder.hasObject( 'qux' ) )
self.assertEquals( folder.getObject( 'foo', None ), objects[0] )
self.assertEquals( folder.getObject( 'foo' ), objects[0] )
self.assertEquals( folder.getObject( 'bar', None ), objects[1] )
self.assertEquals( folder.getObject( 'bar' ), objects[1] )
self.assertEquals( folder.getObject( 'baz', None ), objects[2] )
self.assertEquals( folder.getObject( 'baz' ), objects[2] )
self.assertEquals( folder.getObject( 'bam', None ), objects[3] )
self.assertEquals( folder.getObject( 'bam' ), objects[3] )
self.assertEquals( folder.getObject( 'qux', None ), None )
self.assertRaises( KeyError, folder.getObject, 'qux' )
folder.delObject( 'foo' )
self.assertEquals( folder.objectCount(), len( objects ) - 1 )
self.failIf( folder.hasObject( 'foo' ) )
self.failIf( 'foo' in folder.objectIds() )
self.failIf( objects[0] in folder.objectValues() )
self.failIf( ( 'foo', objects[0] ) in folder.objectItems() )
self.assertEquals( folder.getObject( 'foo', None ), None )
self.assertRaises( KeyError, folder.getObject, 'foo' )
self.assertRaises( KeyError, folder.delObject, 'foo' )
folder.delObject( 'bar' )
folder.delObject( 'baz' )
folder.delObject( 'bam' )
self.failIf( folder.objectIds() )
self.failIf( folder.objectValues() )
self.failIf( folder.objectItems() )
self.failIf( folder.objectCount() )
self.failIf( folder.hasObject( 'foo' ) )
self.failIf( folder.hasObject( 'bar' ) )
self.failIf( folder.hasObject( 'baz' ) )
self.failIf( folder.hasObject( 'bam' ) )
class Test(BaseTestIContainer, TestCase):
def _Test__new(self):
from Zope.App.OFS.Container.SampleContainer import Container
return Container()
def test_suite():
return TestSuite((
makeSuite(Test),
))
if __name__=='__main__':
main(defaultTest='test_suite')