diff --git a/unittests/test_error_handling.py b/unittests/test_error_handling.py index 6ba66a62b66b1a63af6c3b8957ddd151314c2a47..e467517f3799c6c0782e8e1b7f0b19fb72b97699 100644 --- a/unittests/test_error_handling.py +++ b/unittests/test_error_handling.py @@ -29,7 +29,8 @@ children. import caosdb as db from caosdb.common.models import raise_errors # TODO: Import the relevant error classes once they have been finalized. -from caosdb.exceptions import (AuthorizationException, +from caosdb.exceptions import (AmbiguityException, + AuthorizationException, EntityDoesNotExistError, EntityError, EntityHasNoDatatypeError, TransactionError, UniqueNamesError) @@ -171,7 +172,7 @@ def test_parent_and_properties_errors(): prop_code = 114 parent_code = 116 entity_code = 0 - no_entity_code = 0 + no_entity_code = 101 parent = _add_error_message_to_entity( db.RecordType(name="TestParent"), no_entity_code) prop1 = _add_error_message_to_entity(db.Property( @@ -200,7 +201,7 @@ def test_container_with_faulty_elements(): name_code = 152 auth_code = 403 entity_code = 0 - no_entity_code = 0 + no_entity_code = 101 # Broken parents and properties parent = _add_error_message_to_entity( db.RecordType(name="TestParent"), no_entity_code) @@ -228,3 +229,53 @@ def test_container_with_faulty_elements(): raise_errors(cont) # TODO: Check whether all errors and all broken entities are # listed correctly. The healthy entities must not appear. + + +def test_convenience_functions(): + """Test whether get_error and and get_entity work and break as + intended, and whether has_error works properly. + + """ + # Only one child + no_entity_code = 101 + ent = _add_error_message_to_entity( + db.Entity(name="TestEnt"), no_entity_code) + with raises(TransactionError) as e: + raise_errors(ent) + te = e.value + # Works since there is exactly one child + assert te.get_entity().name == ent.name + assert te.get_error() == ent.get_errors()[0] + # Has to have this + assert te.has_error(EntityDoesNotExistError) + # EntityDoesNotExistError is an EntityError + assert te.has_error(EntityError) + # Shouldn't be there + assert not te.has_error(UniqueNamesError) + + # Two children + prop_code = 114 + parent_code = 116 + entity_code = 0 + parent = _add_error_message_to_entity( + db.RecordType(name="TestParent"), no_entity_code) + prop1 = _add_error_message_to_entity(db.Property( + name="TestProp1"), entity_code) + prop2 = _add_error_message_to_entity(db.Property( + name="TestProp2"), no_entity_code) + rec = _add_error_message_to_entity(db.Record(name="TestRecord"), + prop_code) + rec = _add_error_message_to_entity(rec, parent_code) + rec.add_parent(parent) + rec.add_property(prop1).add_property(prop2) + with raises(TransactionError) as e: + raise_errors(rec) + te = e.value + # Two children, should raise an Error + with raises(AmbiguityException): + te.get_error() + with raises(AmbiguityException): + te.get_entity() + for error_t in [EntityDoesNotExistError, UnqualifiedParentsError, + UnqualifiedPropertiesError]: + assert te.has_error(error_t)