Index: component/tests/test_registration.py =================================================================== --- component/tests/test_registration.py (revision 71983) +++ component/tests/test_registration.py (working copy) @@ -419,7 +419,81 @@ """ +barcode = """ +from zope.interface import Interface +class IBar(Interface): pass +class IBaz(Interface): pass +""" +class Bar(persistent.Persistent): pass +class Baz(persistent.Persistent): pass + +def test_persistent_interfaces(): + """ +Registrations for persistent interfaces are accessible from separate +connections. + +Setup the DB and our first connection:: + + >>> import ZODB.tests.util + >>> db = ZODB.tests.util.DB() + >>> conn1 = db.open() + >>> root1 = conn1.root() + +Setup the persistent module registry and the local component +registry:: + + >>> from zodbcode.module import ManagedRegistry + >>> registry = root1['registry'] = ManagedRegistry() + >>> from zope.component.persistentregistry import PersistentComponents + >>> manager = root1['manager'] = PersistentComponents() + +Create a persistent module:: + + >>> registry.newModule('barmodule', barcode) + >>> barmodule = registry.findModule('barmodule') + +Create a persistent instance:: + + >>> bar = root1['bar'] = Bar() + >>> from zope.interface import directlyProvides + >>> directlyProvides(bar, barmodule.IBar) + >>> from transaction import commit + >>> commit() + +Register an adapter:: + + >>> manager.queryAdapter(bar, barmodule.IBaz) + >>> manager.registerAdapter(Baz, [barmodule.IBar], barmodule.IBaz) + >>> manager.getAdapter(bar, barmodule.IBaz) # doctest: +ELLIPSIS + + +Before commit, the adapter is not available from another connection:: + + >>> conn2 = db.open() + >>> root2 = conn2.root() + >>> registry2 = root2['registry'] + >>> barmodule2 = registry2.findModule('barmodule') + >>> bar2 = root2['bar'] + >>> manager2 = root2['manager'] + >>> manager2.queryAdapter(bar2, barmodule2.IBaz) + +After commit, it is:: + + >>> commit() + >>> conn2.sync() + >>> manager2.getAdapter(bar2, barmodule2.IBaz) + ... # doctest: +ELLIPSIS + + +Cleanup:: + + >>> conn1.close() + >>> conn2.close() + >>> db.close() +""" + + def test_suite(): suite = unittest.TestSuite(( doctest.DocFileSuite('deprecated35_statusproperty.txt', Index: interface/tests/test_interface.py =================================================================== --- interface/tests/test_interface.py (revision 71983) +++ interface/tests/test_interface.py (working copy) @@ -17,14 +17,18 @@ """ __docformat__ = 'restructuredtext' +from gc import collect + import unittest +from persistent import Persistent + import transaction from ZODB.tests.util import DB from zodbcode.module import ManagedRegistry -from zope.interface import Interface, implements +from zope.interface import Interface, implements, directlyProvides from zope.app.interface import PersistentInterface # TODO: for some reason changing this code to use implements() does not @@ -42,11 +46,24 @@ aFoo = Foo() """ +bar_code = """\ +from zope.interface import Interface +class IBar(Interface): pass +class IBaz(Interface): pass +""" + +class Bar(Persistent): pass +class Baz(Persistent): pass + +class IQux(Interface): pass + class PersistentInterfaceTest(unittest.TestCase): def setUp(self): + self.db = DB() - self.root = self.db.open().root() + self.conn = self.db.open() + self.root = self.conn.root() self.registry = ManagedRegistry() self.root["registry"] = self.registry transaction.commit() @@ -76,6 +93,56 @@ # the conversion should not affect Interface self.assert_(imodule.Interface is Interface) + def test_provides(self): + """Provides are persistent.""" + + self.registry.newModule("barmodule", bar_code) + barmodule = self.registry.findModule("barmodule") + bar = Bar() + directlyProvides(bar, barmodule.IBar) + self.root['bar'] = bar + self.assertTrue(barmodule.IBar.providedBy(bar)) + transaction.commit() + self.db.close() + root = self.db.open().root() + barmodule = root['registry'].findModule("barmodule") + bar = root['bar'] + self.assertTrue(barmodule.IBar.providedBy(bar)) + + def test_weakref(self): + """Weak references to persistent objects don't remain after + ZODB pack and garbage collection.""" + + bar = self.root['bar'] = Bar() + baz = self.root['baz'] = Baz() + + self.registry.newModule("barmodule", bar_code) + barmodule = self.registry.findModule("barmodule") + + self.assertEqual(IQux.dependents.keys(), []) + self.assertEqual(barmodule.IBar.dependents.keys(), []) + + directlyProvides(baz, IQux) + directlyProvides(bar, barmodule.IBar) + + self.assertEqual(len(IQux.dependents), 1) + self.assertEqual(len(barmodule.IBar.dependents), 1) + + transaction.commit() + del bar + del self.root['bar'] + del baz + del self.root['baz'] + self.db.pack() + transaction.commit() + collect() + + root = self.db.open().root() + barmodule = root['registry'].findModule("barmodule") + + self.assertEqual(IQux.dependents.keys(), []) + self.assertEqual(barmodule.IBar.dependents.keys(), []) + def test_suite(): return unittest.makeSuite(PersistentInterfaceTest)