def test_same_resource_id_different_referred_object(self): """ Tests the handling of the case that different ResourceIdentifier instances are created that have the same resource id but different objects. This should not happen and thus a warning should be emitted. Skipped for Python 2.5 because it does not have the catch_warnings context manager. """ object_a = UTCDateTime() object_b = UTCDateTime() self.assertEqual(object_a is object_b, False) resource_id = 'obspy.org/tests/test_resource' res_a = ResourceIdentifier(resource_id=resource_id, referred_object=object_a) # Now create a new resource with the same id but a different object. # This will raise a warning. with warnings.catch_warnings(record=True): warnings.simplefilter('error', UserWarning) self.assertRaises(UserWarning, ResourceIdentifier, resource_id=resource_id, referred_object=object_b) # Now ignore the warning and actually create the new # ResourceIdentifier. warnings.simplefilter('ignore', UserWarning) res_b = ResourceIdentifier(resource_id=resource_id, referred_object=object_b) # Object b was the last to added, thus all resource identifiers will # now point to it. self.assertEqual(object_b is res_a.getReferredObject(), True) self.assertEqual(object_b is res_b.getReferredObject(), True)
def test_same_resource_id_different_referred_object(self): """ Tests the handling of the case that different ResourceIdentifier instances are created that have the same resource id but different objects. This should not happen and thus a warning should be emitted. Skipped for Python 2.5 because it does not have the catch_warnings context manager. """ object_a = UTCDateTime() object_b = UTCDateTime() self.assertEqual(object_a is object_b, False) resource_id = 'obspy.org/tests/test_resource' res_a = ResourceIdentifier(resource_id=resource_id, referred_object=object_a) # Now create a new resource with the same id but a different object. # This will raise a warning. with warnings.catch_warnings(record=True): warnings.simplefilter('error', UserWarning) self.assertRaises(UserWarning, ResourceIdentifier, resource_id=resource_id, referred_object=object_b) # Now ignore the warning and actually create the new # ResourceIdentifier. warnings.simplefilter('ignore', UserWarning) res_b = ResourceIdentifier(resource_id=resource_id, referred_object=object_b) # Object b was the last to added, thus all resource identifiers will # now point to it. self.assertEqual(object_b is res_a.getReferredObject(), True) self.assertEqual(object_b is res_b.getReferredObject(), True)
def test_resources_in_global_dict_get_garbage_collected(self): """ Tests that the ResourceIdentifiers in the class level resource dict get deleted if they have no other reference and the object they refer to goes out of scope. """ obj_a = UTCDateTime() obj_b = UTCDateTime() res1 = ResourceIdentifier(referred_object=obj_a) res2 = ResourceIdentifier(referred_object=obj_b) # Now two keys should be in the global dict. rdict = ResourceIdentifier._ResourceIdentifier__resource_id_weak_dict self.assertEqual(len(rdict.keys()), 2) # Deleting the objects should also remove the from the dictionary. del obj_a, obj_b self.assertEqual(len(rdict.keys()), 0) # references are still around but no longer have associates objects. self.assertEqual(res1.getReferredObject(), None) self.assertEqual(res2.getReferredObject(), None)
def test_resources_in_global_dict_get_garbage_collected(self): """ Tests that the ResourceIdentifiers in the class level resource dict get deleted if they have no other reference and the object they refer to goes out of scope. """ obj_a = UTCDateTime() obj_b = UTCDateTime() res1 = ResourceIdentifier(referred_object=obj_a) res2 = ResourceIdentifier(referred_object=obj_b) # Now two keys should be in the global dict. rdict = ResourceIdentifier._ResourceIdentifier__resource_id_weak_dict self.assertEqual(len(list(rdict.keys())), 2) # Deleting the objects should also remove the from the dictionary. del obj_a, obj_b self.assertEqual(len(list(rdict.keys())), 0) # references are still around but no longer have associates objects. self.assertEqual(res1.getReferredObject(), None) self.assertEqual(res2.getReferredObject(), None)
def test_adding_a_referred_object_after_creation(self): """ Check that the referred objects can also be made available after the ResourceIdentifier instances have been created. """ obj = UTCDateTime() obj_id = id(obj) res_id = "obspy.org/time/test" ref_a = ResourceIdentifier(res_id) ref_b = ResourceIdentifier(res_id) ref_c = ResourceIdentifier(res_id) # All three will have no resource attached. self.assertEqual(ref_a.getReferredObject(), None) self.assertEqual(ref_b.getReferredObject(), None) self.assertEqual(ref_c.getReferredObject(), None) # Setting the object for one will make it available to all other # instances. ref_b.setReferredObject(obj) self.assertEqual(id(ref_a.getReferredObject()), obj_id) self.assertEqual(id(ref_b.getReferredObject()), obj_id) self.assertEqual(id(ref_c.getReferredObject()), obj_id)
def test_adding_a_referred_object_after_creation(self): """ Check that the referred objects can also be made available after the ResourceIdentifier instances have been created. """ obj = UTCDateTime() obj_id = id(obj) res_id = "obspy.org/time/test" ref_a = ResourceIdentifier(res_id) ref_b = ResourceIdentifier(res_id) ref_c = ResourceIdentifier(res_id) # All three will have no resource attached. self.assertEqual(ref_a.getReferredObject(), None) self.assertEqual(ref_b.getReferredObject(), None) self.assertEqual(ref_c.getReferredObject(), None) # Setting the object for one will make it available to all other # instances. ref_b.setReferredObject(obj) self.assertEqual(id(ref_a.getReferredObject()), obj_id) self.assertEqual(id(ref_b.getReferredObject()), obj_id) self.assertEqual(id(ref_c.getReferredObject()), obj_id)