Beispiel #1
0
class PersistentInterfaceTest(unittest.TestCase):

    def setUp(self):
        self.db = DB()
        self.root = self.db.open().root()
        self.registry = ManagedRegistry()
        self.root["registry"] = self.registry
        transaction.commit()

    def tearDown(self):
        transaction.abort() # just in case

    def test_creation(self):
        class IFoo(PersistentInterface):
            pass

        class Foo(object):
            implements(IFoo)

        self.assert_(IFoo.providedBy(Foo()))
        self.assertEqual(IFoo._p_oid, None)

    def test_patch(self):
        self.registry.newModule("imodule", code)
        transaction.commit()
        imodule = self.registry.findModule("imodule")

        # test for a pickling bug
        self.assertEqual(imodule.Foo.__implemented__, imodule.IFoo)

        self.assert_(imodule.IFoo.providedBy(imodule.aFoo))
        # the conversion should not affect Interface
        self.assert_(imodule.Interface is Interface)
 def setUp(self):
     super(PersistentInterfaceTest, self).setUp()
     util.setUp(self)
     self.db = DB(FileStorage('PersistentInterfaceTest.fs'))
     self.conn = self.db.open()
     self.root = self.conn.root()
     self.registry = ManagedRegistry()
     self.root["registry"] = self.registry
     transaction.commit()
Beispiel #3
0
 def setUp(self):
     self.db = ZODB.tests.util.DB()
     self.root = self.db.open().root()
     self.registry = ManagedRegistry()
     self.importer = TestPersistentModuleImporter(self.registry)
     self.importer.install()
     self.root["registry"] = self.registry
     transaction.commit()
     _dir, _file = os.path.split(tests.__file__)
     self._pmtest = os.path.join(_dir, "_pmtest.py")
Beispiel #4
0
class TestBase(unittest.TestCase):

    def setUp(self):
        self.db = ZODB.tests.util.DB()
        self.root = self.db.open().root()
        self.registry = ManagedRegistry()
        self.importer = TestPersistentModuleImporter(self.registry)
        self.importer.install()
        self.root["registry"] = self.registry
        transaction.commit()
        _dir, _file = os.path.split(tests.__file__)
        self._pmtest = os.path.join(_dir, "_pmtest.py")

    def tearDown(self):
        # just in case
        transaction.abort()
        self.db.close()
        self.importer.uninstall()

    def sameModules(self, registry):
        m1 = self.registry.modules()
        m1.sort()
        m2 = registry.modules()
        m2.sort()
        self.assertEqual(m1, m2)

    def useNewConnection(self):
        # load modules using a separate connection to test that
        # modules can be recreated from the database
        cn = self.db.open()
        reg = cn.root()["registry"]
        self.sameModules(reg)
        for name in reg.modules():
            mod = reg.findModule(name)
            mod._p_activate()
            self.assertEqual(mod._p_state, UPTODATE)
            for obj in mod.__dict__.values():
                if hasattr(obj, "_p_activate"):
                    obj._p_activate()
        # XXX somehow objects are getting registered here, but not
        # modified.  need to figure out what is going wrong, but for
        # now just abort the transaction.
        ##assert not cn._registered
        transaction.abort()
        cn.close()
class PersistentInterfaceTest(util.TestCase):

    def setUp(self):
        super(PersistentInterfaceTest, self).setUp()
        util.setUp(self)
        self.db = DB(FileStorage('PersistentInterfaceTest.fs'))
        self.conn = self.db.open()
        self.root = self.conn.root()
        self.registry = ManagedRegistry()
        self.root["registry"] = self.registry
        transaction.commit()

    def tearDown(self):
        transaction.abort() # just in case
        self.conn.close()
        self.db.close()
        util.tearDown(self)

    def test_creation(self):
        class IFoo(PersistentInterface):
            pass

        class Foo(object):
            implements(IFoo)

        self.assert_(IFoo.providedBy(Foo()))
        self.assertEqual(IFoo._p_oid, None)

    def test_patch(self):
        self.registry.newModule("imodule", code)
        transaction.commit()
        imodule = self.registry.findModule("imodule")

        # test for a pickling bug
        self.assertEqual(imodule.Foo.__implemented__, imodule.IFoo)

        self.assert_(imodule.IFoo.providedBy(imodule.aFoo))
        # the conversion should not affect Interface
        self.assert_(imodule.Interface is Interface)

    def test___hash___no_jar(self):
        class IFoo(PersistentInterface):
            pass
        self.assertEqual(hash(IFoo), hash((None, None)))

    def test___hash___w_jar(self):
        self.registry.newModule("imodule", code)
        transaction.commit()
        imodule = self.registry.findModule("imodule")
        self.assertEqual(hash(imodule.IFoo),
                         hash((self.conn, imodule.IFoo._p_oid)))

    def test___eq___no_jar(self):
        class IFoo(PersistentInterface):
            pass
        class IBar(PersistentInterface):
            pass
        self.failUnless(IFoo == IFoo)
        self.failIf(IFoo == IBar)

    def test___eq___w_jar(self):
        class IFoo(PersistentInterface):
            pass
        self.registry.newModule("imodule", code)
        transaction.commit()
        imodule = self.registry.findModule("imodule")
        self.failUnless(imodule.IFoo == imodule.IFoo) # Don't use assertEqual
        self.failIf(imodule.IFoo == imodule.ISpam)
        self.failIf(imodule.IFoo == IFoo)

    def test___ne___no_jar(self):
        class IFoo(PersistentInterface):
            pass
        class IBar(PersistentInterface):
            pass
        self.failIf(IFoo != IFoo)
        self.failUnless(IFoo != IBar)

    def test___ne___w_jar(self):
        class IFoo(PersistentInterface):
            pass
        self.registry.newModule("imodule", code)
        transaction.commit()
        imodule = self.registry.findModule("imodule")
        self.failIf(imodule.IFoo != imodule.IFoo) # Don't use assertNotEqual
        self.failUnless(imodule.IFoo != imodule.ISpam)
        self.failUnless(imodule.IFoo != IFoo)

    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))

        bah = Bar()
        directlyProvides(bah, barmodule.IBah)
        self.root['bah'] = bah
        self.assertTrue(barmodule.IBah.providedBy(bah))

        blah = Bar()
        directlyProvides(blah, barmodule.IBlah)
        self.root['blah'] = blah
        self.assertTrue(barmodule.IBlah.providedBy(blah))

        # Update the code to make sure everything works on update
        self.registry.updateModule('barmodule',
                                   bar_code + '\nfoo = 1')

        transaction.commit()

        self.db.close()
        db = DB(FileStorage('PersistentInterfaceTest.fs'))
        root = db.open().root()

        barmodule = root['registry'].findModule("barmodule")

        bar = root['bar']
        self.assertTrue(barmodule.IBar.providedBy(bar))

        bah = root['bah']
        self.assertTrue(barmodule.IBah.providedBy(bah))

        blah = root['blah']
        self.assertTrue(barmodule.IBlah.providedBy(blah))

        db.close()

    def test_persistentWeakref(self):
        """Verify interacton of declaration weak refs with ZODB

        Weak references to persistent objects don't remain after ZODB
        pack and garbage collection."""

        bar = self.root['bar'] = Bar()
        self.registry.newModule("barmodule", bar_code)
        barmodule = self.registry.findModule("barmodule")
        self.assertEqual(barmodule.IBar.dependents.keys(), [])
        directlyProvides(bar, barmodule.IBar)
        self.assertEqual(len(barmodule.IBar.dependents), 1)

        transaction.commit()
        del bar
        del self.root['bar']
        self.db.pack()
        transaction.commit()
        collect()

        root = self.db.open().root()
        barmodule = root['registry'].findModule("barmodule")
        self.assertEqual(barmodule.IBar.dependents.keys(), [])

    def test_persistentProvides(self):
        """Verify that provideInterface works."""

        self.registry.newModule("barmodule", provide_iface_code)
        barmodule = self.registry.findModule("barmodule")
        self.assertTrue(IBarInterface.providedBy(barmodule.IBar))

        self.registry.updateModule('barmodule',
                                   provide_iface_code + '\nfoo = 1')
        transaction.commit()
        barmodule = self.registry.findModule("barmodule")
        self.assertTrue(IBarInterface.providedBy(barmodule.IBar))
Beispiel #6
0
 def setUp(self):
     self.db = DB()
     self.root = self.db.open().root()
     self.registry = ManagedRegistry()
     self.root["registry"] = self.registry
     transaction.commit()