[z3-checkins] r18318 - z3/sqlos/trunk/ftests
jinty at codespeak.net
jinty at codespeak.net
Mon Oct 10 03:29:33 CEST 2005
Author: jinty
Date: Mon Oct 10 03:29:29 2005
New Revision: 18318
Added:
z3/sqlos/trunk/ftests/containers.txt
z3/sqlos/trunk/ftests/test_doctest.py
Log:
Add some functional doctests for the container.
Added: z3/sqlos/trunk/ftests/containers.txt
==============================================================================
--- (empty file)
+++ z3/sqlos/trunk/ftests/containers.txt Mon Oct 10 03:29:29 2005
@@ -0,0 +1,111 @@
+First let's get a container for sqlos objects:
+
+ >>> from sqlos.testing import sampleperson
+ >>> from sqlos.interfaces.container import ISQLObjectContainer
+ >>> from zope.interface.verify import verifyObject
+
+ >>> container = sampleperson.SamplePersonContainer()
+ >>> verifyObject(ISQLObjectContainer, container)
+ True
+
+We are not in the business of letting errors pass silently, so looking inside
+we get a database error:
+
+ >>> [i for i in container.items()]
+ Traceback (most recent call last):
+ ...
+ DatabaseError: no such table: sample_person
+
+In the ftesting.zcml a SamplePerson factory should be registered:
+
+ >>> factory_names = container.allowedFactories()
+ >>> factory_names
+ [u'SamplePerson']
+
+So let's make oneand create it's database if not already there:
+
+ >>> from sqlos import getFactory
+ >>> import transaction
+
+ >>> SamplePerson = getFactory(factory_names[0])
+ >>> SamplePerson.createTable(ifNotExists=True)
+ >>> transaction.get().commit()
+
+We should now be able to look inside an empty container:
+
+ >>> [i for i in container.keys()]
+ []
+ >>> [i for i in container.items()]
+ []
+ >>> [i for i in container]
+ []
+ >>> [i for i in container.values()]
+ []
+ >>> len(container)
+ 0
+
+Lets add some objects:
+
+ >>> people = [{'username': 'harry',
+ ... 'fullname': 'Harry the Hack',
+ ... 'password': 'harrypass'},
+ ... {'username': 'sally',
+ ... 'fullname': 'Sally the Wack',
+ ... 'password': 'sallypass'}]
+ >>> harry = SamplePerson(**people[0])
+ >>> len(container)
+ 1
+ >>> sally = SamplePerson(**people[1])
+ >>> len(container)
+ 2
+
+Lets see whats inside:
+
+ >>> [i[0] for i in container.items()]
+ [u'SamplePerson.1', u'SamplePerson.2']
+ >>> [i[1] for i in container.items()] == [harry, sally]
+ True
+ >>> [i for i in container.values()] == [harry, sally]
+ True
+ >>> [i for i in container.keys()]
+ [u'SamplePerson.1', u'SamplePerson.2']
+ >>> [i for i in container]
+ [u'SamplePerson.1', u'SamplePerson.2']
+
+Notice that the keys are generated from the name of the factory and the id of
+the instance:
+
+ >>> harry_name = '%s.%s' % (factory_names[0], harry.id)
+ >>> container[harry_name] == harry
+ True
+
+You can get() as well:
+
+ >>> container.get(harry_name) == harry
+ True
+ >>> container.get('sss', 'default')
+ 'default'
+
+Setitem passes but is really a no-op:
+
+ >>> container['sss'] = 'yyy'
+ >>> len(container)
+ 2
+
+Finally lets delete harry:
+
+ >>> del container[harry_name]
+ >>> len(container)
+ 1
+ >>> container[harry_name]
+ Traceback (most recent call last):
+ ...
+ KeyError: u'SamplePerson.1'
+
+XXX - Add a second factory here and test with that.
+
+Tear down the fixtures:
+
+ >>> transaction.commit()
+ >>> SamplePerson.dropTable()
+ >>> transaction.commit()
Added: z3/sqlos/trunk/ftests/test_doctest.py
==============================================================================
--- (empty file)
+++ z3/sqlos/trunk/ftests/test_doctest.py Mon Oct 10 03:29:29 2005
@@ -0,0 +1,6 @@
+from zope.testing import doctest
+from zope.app.tests.functional import FunctionalDocFileSuite
+
+def test_suite():
+ return FunctionalDocFileSuite('containers.txt',
+ optionflags=doctest.ELLIPSIS)
More information about the z3-checkins
mailing list