[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/app/observable/ Add a preliminary ftest for the persistency interaction.

Philipp von Weitershausen philikon at philikon.de
Fri Sep 24 14:11:25 EDT 2004


Log message for revision 27679:
  Add a preliminary ftest for the persistency interaction.
  


Changed:
  A   Zope3/branches/ZopeX3-3.0/src/zope/app/observable/ftests.py
  A   Zope3/branches/ZopeX3-3.0/src/zope/app/observable/observable.txt


-=-
Added: Zope3/branches/ZopeX3-3.0/src/zope/app/observable/ftests.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/observable/ftests.py	2004-09-24 18:10:47 UTC (rev 27678)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/observable/ftests.py	2004-09-24 18:11:25 UTC (rev 27679)
@@ -0,0 +1,17 @@
+import unittest
+from persistent import Persistent
+from zope.interface import implements
+from zope.app.annotation.interfaces import IAttributeAnnotatable
+from zope.app.tests.functional import FunctionalDocFileSuite
+
+class Chicken(Persistent):
+     implements(IAttributeAnnotatable)
+
+def test_suite():
+    globs = {'Chicken': Chicken}
+    return unittest.TestSuite((
+	FunctionalDocFileSuite('observable.txt', globs=globs),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Added: Zope3/branches/ZopeX3-3.0/src/zope/app/observable/observable.txt
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/observable/observable.txt	2004-09-24 18:10:47 UTC (rev 27678)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/observable/observable.txt	2004-09-24 18:11:25 UTC (rev 27679)
@@ -0,0 +1,44 @@
+===========
+Observables
+===========
+
+This document not only demonstrates how to use observables, it also
+shows how they interact with the ZODB persistence machinery.
+
+``Chicken`` is a persistent and annotatable class.  We add an instance
+to the database by placing it in the root folder:
+
+  >>> from zope.app.container.contained import setitem
+  >>> root_folder = getRootFolder()
+  >>> setitem(root_folder, root_folder.__setitem__, u'chicken', Chicken())
+  >>> get_transaction().commit()
+  >>> chicken = root_folder['chicken']
+
+Now consider a possible subscriber for an object event:
+
+  >>> def chickenModified(event):
+  ...     print "buh-PAH! I was modified."
+
+Now we register it using the observable adapter:
+
+  >>> from zope.app.observable.interfaces import IObservable
+  >>> from zope.app.event.interfaces import IObjectModifiedEvent
+  >>> observer = IObservable(chicken)
+  >>> observer.handle([IObjectModifiedEvent], chickenModified)
+
+  TODO:
+
+  We really should be committing the transation here because that's
+  when the actual interaction of observers with the persistency
+  machinery comes in.  Right now it fails for no apparent reason.
+  --philiKON
+
+  >>> #get_transaction().commit()
+
+When we now issue an ``IObjectModifiedEvent`` event, our subscriber
+will be called:
+
+  >>> from zope.event import notify
+  >>> from zope.app.event.objectevent import ObjectModifiedEvent
+  >>> notify(ObjectModifiedEvent(chicken))
+  buh-PAH! I was modified.



More information about the Zope3-Checkins mailing list