[z3-checkins] r18354 - in z3/sqlos/trunk: container ftests testing

jinty at codespeak.net jinty at codespeak.net
Tue Oct 11 06:44:04 CEST 2005


Author: jinty
Date: Tue Oct 11 06:44:00 2005
New Revision: 18354

Modified:
   z3/sqlos/trunk/container/__init__.py
   z3/sqlos/trunk/ftests/containers.txt
   z3/sqlos/trunk/testing/sampleperson.py
Log:
Flesh out the container ftest and slightly optimize __len__.


Modified: z3/sqlos/trunk/container/__init__.py
==============================================================================
--- z3/sqlos/trunk/container/__init__.py	(original)
+++ z3/sqlos/trunk/container/__init__.py	Tue Oct 11 06:44:00 2005
@@ -145,7 +145,9 @@
     def __len__(self):
         """Return the number of objects in the folder."""
         i = 0
-        for k in self.keys(): i += 1
+        for factoryName in self.allowedFactories():
+            factory = getFactory(factoryName)
+            i += factory.select().count() # optimal, does not get all objects
         return i
 
     def __delitem__(self, name):

Modified: z3/sqlos/trunk/ftests/containers.txt
==============================================================================
--- z3/sqlos/trunk/ftests/containers.txt	(original)
+++ z3/sqlos/trunk/ftests/containers.txt	Tue Oct 11 06:44:00 2005
@@ -102,10 +102,30 @@
         ...
     KeyError: u'SamplePerson.1'
 
-XXX - Add a second factory here and test with that.
+Lets make another container which can have dogs as well as people:
 
-Tear down the fixtures:
+    >>> sampleperson.Dog.createTable(ifNotExists=True)
+    >>> transaction.commit()
+    >>> multicontainer = sampleperson.MultiContainer()
+
+Surprisingly our brand new container will contain a number of people:
+
+    >>> len(multicontainer)
+    1
+    >>> sally.id == [i for i in multicontainer.values()][0].id
+    True
+
+Now we add sally's dog:
+
+    >>> fido = sampleperson.Dog(fullname='Fido', owner='sally')
+    >>> len(multicontainer)
+    2
+    >>> fido in [i for i in multicontainer.values()]
+    True
+
+CleanUp:
 
     >>> transaction.commit()
-    >>> SamplePerson.dropTable()
+    >>> sampleperson.SamplePerson.dropTable()
+    >>> sampleperson.Dog.dropTable()
     >>> transaction.commit()

Modified: z3/sqlos/trunk/testing/sampleperson.py
==============================================================================
--- z3/sqlos/trunk/testing/sampleperson.py	(original)
+++ z3/sqlos/trunk/testing/sampleperson.py	Tue Oct 11 06:44:00 2005
@@ -40,3 +40,28 @@
 
 class SamplePersonContainer(SQLObjectContainer):
     implements(IPersonContainer)
+
+
+class IDog(ISQLSchema):
+
+    fullname = TextLine(title=u'Full Name',
+                        description=u'The full name of the dog')
+    owner = TextLine(title=u'Owner username',
+                     description=u'The username of the dog\'s owner')
+
+
+class Dog(SQLOS):
+    implements(IDog)
+
+    fullname = StringCol(length=50, notNull=1)
+    owner = StringCol(length=20, notNull=1)
+
+
+class IMultiContainer(ISQLObjectContainer):
+    def __setitem__(name, item):
+        pass
+    __setitem__.precondition = ItemTypePrecondition(IDog, IPerson)
+
+
+class MultiContainer(SQLObjectContainer):
+    implements(IMultiContainer)


More information about the z3-checkins mailing list