def test_debug_class_state(self): """ Ensure the debug_class_state method juggles the state within context manager. """ def get_id_object_list(): """ return the current _id_object_list """ return ResourceIdentifier._id_order def make_resouce_id(): some_obj = event.Event() return ResourceIdentifier(referred_object=some_obj) rdict1 = get_id_object_list() rid1 = make_resouce_id() self.assertIn(rid1._resource_key, rdict1) with ResourceIdentifier._debug_class_state(): # rdict state should have been replaced self.assertNotIn(rid1._resource_key, get_id_object_list()) rid2 = make_resouce_id() self.assertIn(rid2._resource_key, get_id_object_list()) with ResourceIdentifier._debug_class_state(): self.assertNotIn(rid1._resource_key, get_id_object_list()) self.assertNotIn(rid2._resource_key, get_id_object_list()) rid3 = make_resouce_id() self.assertIn(rid3._resource_key, get_id_object_list()) self.assertNotIn(rid3._resource_key, get_id_object_list()) self.assertIn(rid2._resource_key, get_id_object_list()) self.assertNotIn(rid2._resource_key, get_id_object_list()) self.assertIn(rid1._resource_key, get_id_object_list())
def test_catalog_resource_ids(self): """ Basic tests on the catalog resource ids. """ cat1 = read_events() # The resource_id attached to the first event is self-pointing self.assertIs(cat1[0], cat1[0].resource_id.get_referred_object()) # make a copy and re-read catalog cat2 = cat1.copy() cat3 = read_events() # the resource_id on the new catalogs point to attached objects self.assertIs(cat1[0], cat1[0].resource_id.get_referred_object()) self.assertIs(cat2[0], cat2[0].resource_id.get_referred_object()) self.assertIs(cat3[0], cat3[0].resource_id.get_referred_object()) # now delete cat1 and make sure cat2 and cat3 still work del cat1 self.assertIs(cat2[0], cat2[0].resource_id.get_referred_object()) self.assertIs(cat3[0], cat3[0].resource_id.get_referred_object()) # create a resource_id with the same id as the last defined object # with the same resource id (that is still in scope) is returned new_id = cat2[0].resource_id.id rid = ResourceIdentifier(new_id) self.assertIs(rid.get_referred_object(), cat3[0]) del cat3 gc.collect() # Call gc to ensure WeakValueDict works # raises UserWarning, suppress to keep std out cleaner with WarningsCapture(): self.assertIs(rid.get_referred_object(), cat2[0]) del cat2 self.assertIs(rid.get_referred_object(), None)
def test_resource_id_valid_quakemluri(self): """ Test that a resource identifier per default (i.e. no arguments to __init__()) gets set up with a QUAKEML conform ID. """ rid = ResourceIdentifier() self.assertEqual(rid.id, rid.get_quakeml_uri())
def test_de_referencing_when_object_goes_out_of_scope(self): """ Tests that objects that have no more referrer are no longer stored in the reference dictionary. """ r_dict = self.id_order t1 = UTCDateTime(2010, 1, 1) # test object rid = 'a' # test resource id # Create object and assert the reference has been created. r1 = ResourceIdentifier(rid, referred_object=t1) self.assertEqual(r1.get_referred_object(), t1) self.assertTrue(r1._resource_key in r_dict) # Deleting the object should remove the reference. r_dict_len = len(r_dict) del r1 self.assertEqual(len(r_dict), r_dict_len - 1) # Now create two equal references. r1 = ResourceIdentifier(rid, referred_object=t1) r2 = ResourceIdentifier(rid, referred_object=t1) self.assertEqual(r1.get_referred_object(), t1) # Deleting one should not remove the reference. del r1 self.assertEqual(r2.get_referred_object(), t1) self.assertIn(r2._resource_key, r_dict) # Deleting the second one should (r_dict should now be empty) del r2 self.assertEqual(len(r_dict), 0) r3 = ResourceIdentifier(rid) self.assertNotIn(r3._resource_key, r_dict)
def test_resource_id_valid_quakemluri(self): """ Test that a resource identifier per default (i.e. no arguments to __init__()) gets set up with a QUAKEML conform ID. """ rid = ResourceIdentifier() self.assertEqual(rid.id, rid.get_quakeml_uri_str())
def test_de_referencing_when_object_goes_out_of_scope(self, debug_state): """ Tests that objects that have no more referrer are no longer stored in the reference dictionary. """ r_dict = debug_state['id_order'] t1 = UTCDateTime(2010, 1, 1) # test object rid = 'a' # test resource id # Create object and assert the reference has been created. r1 = ResourceIdentifier(rid, referred_object=t1) assert r1.get_referred_object() == t1 assert r1._resource_key in r_dict # Deleting the object should remove the reference. r_dict_len = len(r_dict) del r1 assert len(r_dict) == r_dict_len - 1 # Now create two equal references. r1 = ResourceIdentifier(rid, referred_object=t1) r2 = ResourceIdentifier(rid, referred_object=t1) assert r1.get_referred_object() == t1 # Deleting one should not remove the reference. del r1 assert r2.get_referred_object() == t1 assert r2._resource_key in r_dict # Deleting the second one should (r_dict should now be empty) del r2 assert len(r_dict) == 0 r3 = ResourceIdentifier(rid) assert r3._resource_key not in r_dict
def test_initialize_with_resource_identifier(self): """ Test initializing an ResourceIdentifier with an ResourceIdentifier. """ rid = ResourceIdentifier() rid2 = ResourceIdentifier(str(rid)) rid3 = ResourceIdentifier(rid) assert rid == rid2 assert rid == rid3
def test_initialize_with_resource_identifier(self): """ Test initializing an ResourceIdentifier with an ResourceIdentifier. """ rid = ResourceIdentifier() rid2 = ResourceIdentifier(str(rid)) rid3 = ResourceIdentifier(rid) self.assertEqual(rid, rid2) self.assertEqual(rid, rid3)
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. The referred objects should still return the same objects used in the ResourceIdentifier construction or set_referred_object call. However, if an object is set to a resource_id that is not equal to the last object set it should issue a warning. """ warnings.simplefilter('default') object_a = UTCDateTime(1000) object_b = UTCDateTime(1000) object_c = UTCDateTime(1001) self.assertFalse(object_a is object_b) id = 'obspy.org/tests/test_resource' res_a = ResourceIdentifier(id=id, referred_object=object_a) # Now create a new resource with the same id but a different object. # This should not raise a warning as the object a and b are equal. with WarningsCapture() as w: res_b = ResourceIdentifier(id=id, referred_object=object_b) self.assertEqual(len(w), 0) # if the set object is not equal to the last object set to the same # resource_id, however, a warning should be issued. with WarningsCapture() as w: res_c = ResourceIdentifier(id=id, referred_object=object_c) self.assertEqual(len(w), 1) expected_text = 'which is not equal to the last object bound' self.assertIn(expected_text, str(w[0])) # even though the resource_id are the same, the referred objects # should point to the original (different) objects self.assertIs(object_a, res_a.get_referred_object()) self.assertIs(object_b, res_b.get_referred_object()) self.assertIs(object_c, res_c.get_referred_object())
def test_set_referred_object_warning(self): """ Setting a referred object equal to an object that is equal to the last referred object should not emit a warning. """ def assert_differnt_referred_object_warning_issued(w): """ assert the different referred object warning is emitted """ self.assertEqual(len(w), 1) self.assertIn('is not equal', str(w[0].message)) obj1 = UTCDateTime(10) obj2 = UTCDateTime(11) obj3 = UTCDateTime(10) rid1 = ResourceIdentifier('abc123', referred_object=obj1) # this should raise a warning as the new object != old object with WarningsCapture() as w: rid2 = ResourceIdentifier('abc123', referred_object=obj2) assert_differnt_referred_object_warning_issued(w) with WarningsCapture() as w: rid2.set_referred_object(obj1) assert_differnt_referred_object_warning_issued(w) # set the referred object back to previous state, this should warn with WarningsCapture() as w: ResourceIdentifier('abc123', referred_object=obj2) assert_differnt_referred_object_warning_issued(w) # this should not emit a warning since obj1 == obj3 with WarningsCapture() as w: rid1.set_referred_object(obj3) self.assertEqual(len(w), 0)
def test_resetting_id_warns_on_set_id(self): """ Because the ResourceIdentifier class hashes on the id attribute, it should warn if it is being changed. This tests the case were an id is manually specified. """ rid = ResourceIdentifier('a very unique string indeed') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('default') rid.id = 'Another string that will mess up the hash. Bad.' assert len(w) == 1 assert 'overwritting the id attribute' in str(w[0].message)
def test_resetting_id_warns_on_set_id(self): """ Because the ResourceIdentifier class hashes on the id attribute, it should warn if it is being changed. This tests the case were an id is manually specified. """ rid = ResourceIdentifier('a very unique string indeed') with WarningsCapture() as w: warnings.simplefilter('default') rid.id = 'Another string that will mess up the hash. Bad.' self.assertEqual(len(w), 1) self.assertIn('overwritting the id attribute', str(w[0].message))
def test_resetting_id_warns_on_default_id(self): """ Because the ResourceIdentifier class hashes on the id attribute, it should warn if it is being changed. This tests the case were an id is not specified and the default uuid is used. """ rid = ResourceIdentifier() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('default') rid.id = 'Another string that will mess up the hash. Bad.' self.assertEqual(len(w), 1) self.assertIn('overwritting the id attribute', str(w[0]))
def test_get_quakeml_id(self): """ Tests for returning valid quakeml ids using the get_quakeml_id method. """ obj = UTCDateTime('2017-09-17') rid1 = ResourceIdentifier('invalid_id', referred_object=obj) rid2 = rid1.get_quakeml_id(authority_id='remote') # The resource ids should not be equal but should refer to the same # object. self.assertNotEqual(rid2, rid1) self.assertIs(rid1.get_referred_object(), rid2.get_referred_object()) # A valid resource id should return a resource id that is equal. rid3 = rid2.get_quakeml_id() self.assertEqual(rid2, rid3)
def test_quakeml_regex(self): """ Tests that regex used to check for QuakeML validatity actually works. """ # This one contains all valid characters. It should pass the # validation. res_id = ( "smi:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "1234567890-.*()_~'/abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQR" "STUVWXYZ0123456789-.*()_~'+?=,;&") res = ResourceIdentifier(res_id) self.assertEqual(res_id, res.get_quakeml_uri()) # The id has to valid from start to end. Due to the spaces this cannot # automatically be converted to a correct one. res_id = "something_before smi:local/something something_after" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri) # A colon is an invalid character. res_id = "smi:local/hello:yea" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri) # Space as well res_id = "smi:local/hello yea" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri) # Dots are fine res_id = "smi:local/hello....yea" res = ResourceIdentifier(res_id) self.assertEqual(res_id, res.get_quakeml_uri()) # Hats not res_id = "smi:local/hello^^yea" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri)
def test_resource_ids_refer_to_newest_object(self): """ Tests that resource ids which are assigned multiple times but point to identical objects always point to the newest object. """ t1 = UTCDateTime(2010, 1, 1) t2 = UTCDateTime(2010, 1, 1) ResourceIdentifier("a", referred_object=t1) rid = ResourceIdentifier("a", referred_object=t2) del t1 self.assertIs(rid.get_referred_object(), t2)
def test_getting_gc_no_shared_resource_id(self): """ Test that calling get_referred_object() on a resource id whose object has been garbage collected, and whose resource_id is unique, returns None """ obj1 = UTCDateTime() rid1 = ResourceIdentifier(referred_object=obj1) # delete obj1, make sure rid1 return None del obj1 # raises UserWarning with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) self.assertIs(rid1.get_referred_object(), None)
def __init__(self, *args, **kwargs): # Make sure the args work as expected. Therefore any specified # arg will overwrite a potential kwarg, e.g. arg at position 0 will # overwrite kwargs class_attributes[0]. for _i, item in enumerate(args): # Use the class_attributes list here because it is not yet # polluted be the error quantities. kwargs[class_attributes[_i][0]] = item # Set all property values to None or the kwarg value. for key, _ in self._properties: value = kwargs.get(key, None) # special handling for resource id if key == "resource_id": if kwargs.get("force_resource_id", False): if value is None: value = ResourceIdentifier() setattr(self, key, value) # Containers currently are simple lists. for name in self._containers: setattr(self, name, list(kwargs.get(name, []))) # All errors are QuantityError. If they are not set yet, set them # now. for key, _ in self._properties: if key.endswith("_errors") and getattr(self, key) is None: setattr(self, key, QuantityError())
def test_issue_2278(self): """ Tests for issue # 2278 which has to do with resource ids returning the wrong objects when the bound object has gone out of scope and a new object adopts the old object's python id. """ # Create a simple class for resource_ids to refere to. class Simple(object): def __init__(self, value): self.value = value parent = Simple('parent1') # keep track of objects, resource_ids, and used python ids obj_list1, rid_list1, used_ids1 = [], [], set() # create a slew of objects and resource_ids for _ in range(100): obj_list1.append(Simple(1)) used_ids1.add(id(obj_list1[-1])) for obj in obj_list1: kwargs = dict(referred_object=obj, parent=parent) rid_list1.append(ResourceIdentifier(**kwargs)) # delete objects and create second set del obj_list1 # create another slew of objects and resource_ids. Some will reuse # deleted object ids obj_list2, rid_list2, used_ids2 = [], [], set() for _ in range(100): obj_list2.append(Simple(2)) used_ids2.add(id(obj_list2[-1])) for obj in obj_list2: kwargs = dict(referred_object=obj, parent=parent) rid_list2.append(ResourceIdentifier(**kwargs)) # since we cannot control which IDs python uses, skip the test if # no overlapping ids were created. if not used_ids1 & used_ids2: self.skipTest('setup requires reuse of python ids') # Iterate over first list of ids. Referred objects should be None for rid in rid_list1: # should raise a warning with WarningsCapture(): self.assertIs(rid.get_referred_object(), None)
def test_error_message_for_failing_quakeml_id_conversion(self): """ Converting an id to a QuakeML compatible id might fail. Test the error message. """ invalid_id = "http://example.org" rid = ResourceIdentifier(invalid_id) msg = ""\ "The id 'http://example.org' is not a valid QuakeML resource " \ "identifier. ObsPy tried modifying it to " \ "'smi:local/http://example.org' but it is still not valid. " \ "Please make sure all resource ids are either valid or can be " \ "made valid by prefixing them with 'smi:<authority_id>/'. " \ "Valid ids are specified in the QuakeML manual section 3.1 " \ "and in particular exclude colons for the final part." with pytest.raises(ValueError, match=msg): rid.get_quakeml_uri_str()
def test_error_message_for_failing_quakeml_id_conversion(self): """ Converting an id to a QuakeML compatible id might fail. Test the error message. """ invalid_id = "http://example.org" rid = ResourceIdentifier(invalid_id) with self.assertRaises(ValueError) as e: rid.get_quakeml_uri_str() self.assertEqual( e.exception.args[0], "The id 'http://example.org' is not a valid QuakeML resource " "identifier. ObsPy tried modifying it to " "'smi:local/http://example.org' but it is still not valid. Please " "make sure all resource ids are either valid or can be made valid " "by prefixing them with 'smi:<authority_id>/'. Valid ids are " "specified in the QuakeML manual section 3.1 and in particular " "exclude colons for the final part.")
def test_error_message_for_failing_quakeml_id_conversion(self): """ Converting an id to a QuakeML compatible id might fail. Test the error message. """ invalid_id = "http://example.org" rid = ResourceIdentifier(invalid_id) with self.assertRaises(ValueError) as e: rid.get_quakeml_uri() self.assertEqual( e.exception.args[0], "The id 'http://example.org' is not a valid QuakeML resource " "identifier. ObsPy tried modifying it to " "'smi:local/http://example.org' but it is still not valid. Please " "make sure all resource ids are either valid or can be made valid " "by prefixing them with 'smi:<authority_id>/'. Valid ids are " "specified in the QuakeML manual section 3.1 and in particular " "exclude colons for the final part.")
def test_objects_garbage_collection(self): """ Test that the ResourceIdentifier class does not mess with the garbage collection of the attached objects. """ object_a = UTCDateTime() ref_count = sys.getrefcount(object_a) _res_id = ResourceIdentifier(referred_object=object_a) self.assertEqual(sys.getrefcount(object_a), ref_count) self.assertTrue(bool(_res_id))
def test_resource_id_state_cleanup(self): """ Tests that the state in the ResourceIdentifier class gets gets cleaned up when resource_ids are garbage collected. """ 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. self.assertEqual(len(self.id_order), 2) self.assertEqual(len(self.id_object_map), 2) del obj_a, obj_b # raises UserWarnings self.assertEqual(len(self.id_order), 2) with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) self.assertIs(res1.get_referred_object(), None) self.assertIs(res2.get_referred_object(), None)
def setUp(self): """ Setup code to run before each test. Temporary replaces the state on the ResourceIdentifier class level to reset the ResourceID mechanisms before each run. """ # setup temporary id dict for tests in this case and bind to self context = ResourceIdentifier._debug_class_state() state = setup_context_testcase(self, context) self.parent_id_tree = state['parent_id_tree'] self.id_order = state['id_order'] self.id_object_map = state['id_object_map']
def test_getting_gc_with_shared_resource_id(self): """ Test that calling get_referred_object on a resource id whose object has been garbage collected, but that has another object that shares the same resource_id, returns the other object with the same resource id and issues a warning """ uri = 'testuri' obj1 = UTCDateTime(1000) obj2 = UTCDateTime(1000) rid1 = ResourceIdentifier(uri, referred_object=obj1) rid2 = ResourceIdentifier(uri, referred_object=obj2) assert not (rid1.get_referred_object() is rid2.get_referred_object()) del obj1 with WarningsCapture() as w: rid1.get_referred_object() assert len(w) == 1 assert 'The object with identity' in str(w[0]) # now both rids should return the same object assert rid1.get_referred_object() is rid2.get_referred_object() # the object id should now be bound to obj2 assert rid1._object_id == rid2._object_id
def test_mutative_methods_deprecation(self): """ Because Resource ids are hashable they should be immutable. Make sure any methods that mutate resource_ids are deprecated. Currently there are two: 1. `convert_id_to_quakeml_uri` 2. `regnerate_uuid` """ rid = ResourceIdentifier('not_a_valid_quakeml_uri') with WarningsCapture() as w: rid.convert_id_to_quakeml_uri() self.assertGreaterEqual(len(w), 1) self.assertTrue([isinstance(x, ObsPyDeprecationWarning) for x in w]) rid = ResourceIdentifier() with WarningsCapture() as w: rid.regenerate_uuid() self.assertGreaterEqual(len(w), 1) self.assertTrue([isinstance(x, ObsPyDeprecationWarning) for x in w])
def test_getting_gc_with_shared_resource_id(self): """ Test that calling get_referred_object on a resource id whose object has been garbage collected, but that has another object that shares the same resource_id, returns the other object with the same resource id and issues a warning """ uri = 'testuri' obj1 = UTCDateTime(1000) obj2 = UTCDateTime(1000) rid1 = ResourceIdentifier(uri, referred_object=obj1) rid2 = ResourceIdentifier(uri, referred_object=obj2) self.assertFalse(rid1.get_referred_object() is rid2.get_referred_object()) del obj1 with WarningsCapture() as w: rid1.get_referred_object() self.assertEqual(len(w), 1) self.assertIn('The object with identity', str(w[0])) # now both rids should return the same object self.assertIs(rid1.get_referred_object(), rid2.get_referred_object()) # the object id should now be bound to obj2 self.assertEqual(rid1._object_id, rid2._object_id)
def test_resource_id_state_cleanup(self, debug_state): """ Tests that the state in the ResourceIdentifier class gets gets cleaned up when resource_ids are garbage collected. """ 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. assert len(debug_state['id_order']) == 2 assert len(debug_state['id_object_map']) == 2 del obj_a, obj_b # raises UserWarnings assert len(debug_state['id_order']) == 2 with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) assert res1.get_referred_object() is None assert res2.get_referred_object() is None
def test_get_object_hook(self): """ Test that custom logic for getting referred objects can be plugged into resource ids. """ class MyClass(): pass new_obj1 = MyClass() new_obj2 = MyClass() def _get_object_hook(arg): if str(arg) == '123': return new_obj1 elif str(arg) == '789': return new_obj2 else: return None with ResourceIdentifier._debug_class_state(): ResourceIdentifier.register_get_object_hook(_get_object_hook) rid1 = ResourceIdentifier('123') rid2 = ResourceIdentifier('456') rid3 = ResourceIdentifier('789') self.assertIs(rid1.get_referred_object(), new_obj1) self.assertIs(rid2.get_referred_object(), None) # Now clear the hook, _get_object_hook should no longer be called ResourceIdentifier.remove_get_object_hook(_get_object_hook) self.assertIs(rid3.get_referred_object(), None) # But rid1 should have been bound to new_obj1 (so it no longer # needs to call get_object_hook to find it self.assertIs(rid1.get_referred_object(), new_obj1)
def test_quakeml_regex(self): """ Tests that regex used to check for QuakeML validatity actually works. """ # This one contains all valid characters. It should pass the # validation. res_id = ( "smi:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "1234567890-.*()_~'/abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQR" "STUVWXYZ0123456789-.*()_~'+?=,;&") res = ResourceIdentifier(res_id) self.assertEqual(res_id, res.get_quakeml_uri_str()) # The id has to valid from start to end. Due to the spaces this cannot # automatically be converted to a correct one. res_id = "something_before smi:local/something something_after" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri_str) # A colon is an invalid character. res_id = "smi:local/hello:yea" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri_str) # Space as well res_id = "smi:local/hello yea" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri_str) # Dots are fine res_id = "smi:local/hello....yea" res = ResourceIdentifier(res_id) self.assertEqual(res_id, res.get_quakeml_uri_str()) # Hats not res_id = "smi:local/hello^^yea" res = ResourceIdentifier(res_id) self.assertRaises(ValueError, res.get_quakeml_uri_str)
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() 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.get_referred_object(), None) self.assertEqual(ref_b.get_referred_object(), None) self.assertEqual(ref_c.get_referred_object(), None) # Setting the object for one will make it available to all other # instances, provided they weren't bound to specific objects. ref_b.set_referred_object(obj) self.assertIs(ref_a.get_referred_object(), obj) self.assertIs(ref_b.get_referred_object(), obj) self.assertIs(ref_c.get_referred_object(), obj)
def make_resouce_id(): some_obj = event.Event() return ResourceIdentifier(referred_object=some_obj)
def debug_state(self): """Return a new Resource id context for testsing.""" with ResourceIdentifier._debug_class_state() as state: yield state