[Zope3-checkins] CVS: Zope3/src/zope/app/container/tests - test_icontainer.py:1.4
R. David Murray
bitz@bitdance.com
Fri, 27 Dec 2002 13:36:29 -0500
Update of /cvs-repository/Zope3/src/zope/app/container/tests
In directory cvs.zope.org:/tmp/cvs-serv7418
Modified Files:
test_icontainer.py
Log Message:
Refactor the unit tests to not depend on particular data values.
Let the subclass supply the sample data through makeTestData.
=== Zope3/src/zope/app/container/tests/test_icontainer.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/container/tests/test_icontainer.py:1.3 Fri Dec 27 10:59:43 2002
+++ Zope3/src/zope/app/container/tests/test_icontainer.py Fri Dec 27 13:36:28 2002
@@ -21,19 +21,28 @@
from zope.app.interfaces.container import IContainer
from zope.interface.verify import verifyObject
+
+def DefaultTestData():
+ return [('3', '0'), ('2', '1'), ('4', '2'), ('6', '3'), ('0', '4'),
+ ('5', '5'), ('1', '6'), ('8', '7'), ('7', '8'), ('9', '9')]
+
class BaseTestIContainer:
"""Base test cases for containers.
Subclasses must define a makeTestObject that takes no
- arguments and that returns a new empty test container.
+ arguments and that returns a new empty test container,
+ and a makeTestData that also takes no arguments and returns
+ a sequence of (key, value) pairs that may be stored in
+ the test container. The list must be at least ten items long.
+ 'NoSuchKey' may not be used as a key value in the returned list.
"""
def __setUp(self):
self.__container = container = self.makeTestObject()
- for k,v in [('3', '0'), ('2', '1'), ('4', '2'), ('6', '3'), ('0', '4'),
- ('5', '5'), ('1', '6'), ('8', '7'), ('7', '8'), ('9', '9')]:
+ self.__data = data = self.makeTestData()
+ for k, v in data:
container.setObject(k, v)
- return container
+ return container, data
############################################################
# Interface-driven tests:
@@ -44,91 +53,90 @@
def test_keys(self):
# See interface IReadContainer
container = self.makeTestObject()
- data = container.keys()
- self.assertEqual(list(data), [])
+ keys = container.keys()
+ self.assertEqual(list(keys), [])
- container = self.__setUp()
- data = container.keys()
- # convert to sorted list
- data = list(data)
- data.sort()
- self.assertEqual(data, map(str, range(10)))
+ container, data = self.__setUp()
+ keys = container.keys()
+ keys = list(keys); keys.sort() # convert to sorted list
+ ikeys = [ k for k, v in data ]; ikeys.sort() # sort input keys
+ self.assertEqual(keys, ikeys)
def test_get(self):
# See interface IReadContainer
+ default = object()
container = self.makeTestObject()
self.assertRaises(KeyError, container.__getitem__, '1')
- self.assertEqual(container.get('1', '99'), '99')
+ self.assertEqual(container.get('1', default), default)
- container = self.__setUp()
- self.assertRaises(KeyError, container.__getitem__, '100')
- self.assertEqual(container.get('100', '99'), '99')
- self.assertEqual(container.get('1', '99'), '6')
- self.assertEqual(container['7'], '8')
- self.assertEqual(container['0'], '4')
- self.assertEqual(container['9'], '9')
+ container, data = self.__setUp()
+ self.assertRaises(KeyError, container.__getitem__, 'NoSuchKey')
+ self.assertEqual(container.get('NoSuchKey', default), default)
+ for i in (1, 8, 7, 3, 4):
+ self.assertEqual(container.get(data[i][0], default), data[i][1])
+ self.assertEqual(container.get(data[i][0]), data[i][1])
def test_values(self):
# See interface IReadContainer
container = self.makeTestObject()
- data = container.values()
- self.assertEqual(list(data), [])
+ values = container.values()
+ self.assertEqual(list(values), [])
- container = self.__setUp()
- data = container.values()
- data = list(data); data.sort() # convert to sorted list
- self.assertEqual(data, map(str, range(10)))
+ container, data = self.__setUp()
+ values = container.values()
+ # XXX: this assumes that sort produces a deterministic order for
+ # the data returned by container. This is valid for the data
+ # in DefaultTestData, but it may not be valid for all IContainers.
+ # Is there a better way to write this test?
+ values = list(values); values.sort() # convert to sorted list
+ ivalues = [ v for k, v in data ]; ivalues.sort() # sort original.
+ self.assertEqual(values, ivalues)
def test_len(self):
# See interface IReadContainer
container = self.makeTestObject()
self.assertEqual(len(container), 0)
- container = self.__setUp()
- self.assertEqual(len(container), 10)
+ container, data = self.__setUp()
+ self.assertEqual(len(container), len(data))
def test_items(self):
# See interface IReadContainer
container = self.makeTestObject()
- data = container.items()
- self.assertEqual(list(data), [])
+ items = container.items()
+ self.assertEqual(list(items), [])
- container = self.__setUp()
- data = container.items()
- # convert to sorted list
- data = list(data)
- data.sort()
- self.assertEqual(data, [
- ('0', '4'), ('1', '6'), ('2', '1'), ('3', '0'), ('4', '2'),
- ('5', '5'), ('6', '3'), ('7', '8'), ('8', '7'), ('9', '9')
- ])
+ container, data = self.__setUp()
+ items = container.items()
+ items = list(items); items.sort() # convert to sorted list
+ data.sort() # sort input data
+ self.assertEqual(items, data)
def test___contains__(self):
# See interface IReadContainer
container = self.makeTestObject()
self.assertEqual(not not ('1' in container), 0)
- container = self.__setUp()
- self.assertEqual(not not ('100' in container), 0)
- self.assertEqual(not not ('1' in container), 1)
- self.assertEqual(not not ('0' in container), 1)
- self.assertEqual(not not ('9' in container), 1)
+ container, data = self.__setUp()
+ self.assertEqual(not not ('NoSuchKey' in container), 0)
+ for i in (1, 8, 7, 3, 4):
+ self.assertEqual(not not (data[i][0] in container), 1)
def test_delObject(self):
# See interface IWriteContainer
+ default = object()
container = self.makeTestObject()
self.assertRaises(KeyError, container.__delitem__, '1')
- container = self.__setUp()
- self.assertRaises(KeyError, container.__delitem__, '100')
- del container['1']
- del container['9']
- self.assertRaises(KeyError, container.__getitem__, '1')
- self.assertRaises(KeyError, container.__getitem__, '9')
- self.assertEqual(container.get('1', '99'), '99')
- self.assertEqual(container['7'], '8')
- self.assertEqual(container['0'], '4')
- self.assertEqual(container.get('9', '88'), '88')
+ container, data = self.__setUp()
+ self.assertRaises(KeyError, container.__delitem__, 'NoSuchKey')
+ for i in (1, 8, 7, 3, 4):
+ del container[data[i][0]]
+ for i in (1, 8, 7, 3, 4):
+ self.assertRaises(KeyError, container.__getitem__, data[i][0])
+ self.assertEqual(container.get(data[i][0], default), default)
+ for i in (0, 2, 9, 6, 5):
+ self.assertEqual(container[data[i][0]], data[i][1])
############################################################
# Tests from Folder
@@ -151,10 +159,14 @@
value = []
self.assertRaises(TypeError, folder.setObject, None, value)
self.assertRaises(TypeError, folder.setObject, ['foo'], value)
+ self.assertRaises(TypeError, folder.setObject, 1, value)
+ self.assertRaises(TypeError, folder.setObject, '\xf3abc', value)
def testOneItem(self):
folder = self.makeTestObject()
- foo = []
+ data = self.makeTestData()
+
+ foo = data[0][1]
folder.setObject('foo', foo)
self.assertEquals(len(folder.keys()), 1)
@@ -173,15 +185,15 @@
self.assertRaises(KeyError, folder.__getitem__, 'qux')
- foo2 = []
- folder.setObject('foo2', foo)
+ foo2 = data[1][1]
+ folder.setObject('foo2', foo2)
self.assertEquals(len(folder.keys()), 2)
- self.assertEquals(folder.keys()[1], 'foo2')
+ self.assertEquals(not not 'foo2' in folder.keys(), True)
self.assertEquals(len(folder.values()), 2)
- self.assertEquals(folder.values()[1], foo2)
+ self.assertEquals(not not foo2 in folder.values(), True)
self.assertEquals(len(folder.items()), 2)
- self.assertEquals(folder.items()[1], ('foo2', foo2))
+ self.assertEquals(not not ('foo2', foo2) in folder.items(), True)
self.assertEquals(len(folder), 2)
del folder['foo']
@@ -199,7 +211,8 @@
def testManyItems(self):
folder = self.makeTestObject()
- objects = [ [0], [1], [2], [3] ]
+ data = self.makeTestData()
+ objects = [ data[i][1] for i in range(4) ]
folder.setObject('foo', objects[0])
folder.setObject('bar', objects[1])
folder.setObject('baz', objects[2])
@@ -271,9 +284,14 @@
class Test(BaseTestIContainer, TestCase):
+
def makeTestObject(self):
from zope.app.container.sample import SampleContainer
return SampleContainer()
+
+ def makeTestData(self):
+ return DefaultTestData()
+
def test_suite():
return makeSuite(Test)