[Zope-Checkins] CVS: Zope3/lib/python/Zope/ObjectHub/tests - __init__.py:1.1.2.1 testObjectHub.py:1.1.2.1

Steve Alexander steve@cat-box.net
Fri, 22 Feb 2002 15:05:45 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/ObjectHub/tests
In directory cvs.zope.org:/tmp/cvs-serv1471/tests

Added Files:
      Tag: Zope-3x-branch
	__init__.py testObjectHub.py 
Log Message:
Added in object hub naive implementation, interface and unit test.


=== Added File Zope3/lib/python/Zope/ObjectHub/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""

Revision information:
$Id: __init__.py,v 1.1.2.1 2002/02/22 20:05:45 stevea Exp $
"""



=== Added File Zope3/lib/python/Zope/ObjectHub/tests/testObjectHub.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""

Revision information:
$Id: testObjectHub.py,v 1.1.2.1 2002/02/22 20:05:45 stevea Exp $
"""

import unittest, sys

from Zope.Event.ObjectEvent import ObjectAddedEvent, ObjectModifiedEvent
from Zope.Event.ObjectEvent import ObjectRemovedEvent, ObjectMovedEvent
from Zope.ObjectHub.ObjectHub import ObjectHub, ObjectHubError
from Zope.Exceptions import NotFoundError

class BasicHubTest(unittest.TestCase):

    location = '/foo/bar'
    new_location = '/baz/spoo'
    
    def setUp(self):
        self.added_event = ObjectAddedEvent(self.location)
        self.added_new_location_event = ObjectAddedEvent(self.new_location)
        self.removed_event = ObjectRemovedEvent(self.location)
        self.modified_event = ObjectModifiedEvent(self.location)
        self.moved_event = ObjectMovedEvent(self.location,
                                            self.new_location)
        ObjectHub() # self.object_hub = ObjectHub()


class TestObjectAddedEvent(BasicHubTest):
            
    def testLookingUpLocation(self):
        """Test that the location is in the lookup
        
        Compare getRuidForLocation and getLocationForRuid
        
        """
        hub = ObjectHub() # self.object_hub
        event = self.added_event
        location = self.location
        
        hub.notify(event)
        
        ruid = hub.lookupRuid(location)
        # check that ruid is an int
        int(ruid)
        
        location_from_hub = hub.lookupLocation(ruid)

        self.assertEqual(location_from_hub, location)

        
    def testLookupUpAbsentLocation(self):
        """Test that we don't find an ruid for location
           that we haven't added.
        """
        hub = ObjectHub() # self.object_hub
        event = self.added_event
        location = self.location
        
        # Do not add the location to the hub
        # hub.notify(event)
        
        self.assertRaises(NotFoundError, hub.lookupRuid, location)

        
    def testLookupUpAbsentRuid(self):
        """Test that we don't find a location for an ruid
           that isn't there.
        """
        hub = ObjectHub() # self.object_hub
        event = self.added_event
        
        # Do not add the location to the hub
        # hub.notify(event)
        
        absent_ruid = 12
        
        self.assertRaises(NotFoundError, hub.lookupLocation, absent_ruid)


class TestObjectRemovedEvent(BasicHubTest):

    def testRemovedLocation(self):
        """Test that a location that is added then removed is
           actually gone.        
        """
        hub = ObjectHub() # self.object_hub
        added_event = self.added_event
        removed_event = self.removed_event
        location = self.location
        
        hub.notify(added_event)
        
        ruid = hub.lookupRuid(location)
        
        # check that ruid is an int
        int(ruid)
        
        hub.notify(removed_event)
        
        self.assertRaises(NotFoundError, hub.lookupRuid, location)
        self.assertRaises(NotFoundError, hub.lookupLocation, ruid)
        
    def testRemovedAbsentLocation(self):
        """Test that removing an absent location is silently ignored.
        """
        hub = ObjectHub() # self.object_hub
        added_event = self.added_event
        removed_event = self.removed_event
        location = self.location
        
        # Do not add location
        # hub.notify(added_event)
                
        hub.notify(removed_event)
                

class TestObjectModifiedEvent(BasicHubTest):

    def testModifiedLocation(self):
        """Test that lookup state does not change after an object
        modify event.
        """
        hub = ObjectHub() # self.object_hub
        added_event = self.added_event
        modified_event = self.modified_event
        location = self.location
        
        hub.notify(added_event)
        
        ruid = hub.lookupRuid(location)
        # check that ruid is an int
        int(ruid)
        
        location_from_hub = hub.lookupLocation(ruid)
        self.assertEqual(location_from_hub, location)
        
        hub.notify(modified_event)
        
        ruid2 = hub.lookupRuid(location)
        location_from_hub2 = hub.lookupLocation(ruid2)
        
        self.assertEqual(location_from_hub, location_from_hub2)
        self.assertEqual(ruid, ruid2)
        
    def testModifiedAbsentLocation(self):
        """Test that lookup state does not change after an object
        modify event. In this case, modify of an absent location is
        a noop.
        """
        hub = ObjectHub() # self.object_hub
        added_event = self.added_event
        modified_event = self.modified_event
        location = self.location
        
        # Do not add location
        # hub.notify(added_event)
        
        hub.notify(modified_event)
        self.assertRaises(NotFoundError, hub.lookupRuid, location)

class TestObjectMovedEvent(BasicHubTest):

    def testMovedLocation(self):
        """Test that the location does indeed change after a move.
        """
        hub = ObjectHub() # self.object_hub
        added_event = self.added_event
        moved_event = self.moved_event
        location = self.location
        new_location = self.new_location
        
        hub.notify(added_event)
        ruid = hub.lookupRuid(location)
        
        hub.notify(moved_event)
        
        location_from_hub = hub.lookupLocation(ruid)
                
        self.assertEqual(location_from_hub, new_location)
        self.assertRaises(NotFoundError, hub.lookupRuid, location)
                
        ruid2 = hub.lookupRuid(new_location)
        self.assertEqual(ruid2, ruid)

    def testMovedAbsentLocation(self):
        """Test that moving an absent location is a noop.
        """
        hub = ObjectHub() # self.object_hub
        added_event = self.added_event
        moved_event = self.moved_event
        location = self.location
        new_location = self.new_location
        
        # Do not add location
        # hub.notify(added_event)
        
        hub.notify(moved_event)
        self.assertRaises(NotFoundError, hub.lookupRuid, location)
        self.assertRaises(NotFoundError, hub.lookupRuid, new_location)

    def testMovedToExistingLocation(self):
        """Test that moving to an existing location raises ObjectHubError.
        """
        hub = ObjectHub() # self.object_hub
        added_event2 = self.added_new_location_event
        moved_event = self.moved_event
        
        hub.notify(added_event2)
        self.assertRaises(ObjectHubError, hub.notify, moved_event)
        

        
def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(TestObjectAddedEvent),
        unittest.makeSuite(TestObjectRemovedEvent),
        unittest.makeSuite(TestObjectModifiedEvent),
        unittest.makeSuite(TestObjectMovedEvent),
        ))

if __name__=='__main__':
    unittest.main(defaultTest='test_suite')