diff --git a/tests/test_error_stuff.py b/tests/test_error_stuff.py index 5e2e11837d5ede6a330252751da3eff61bcc1ea5..17fe003747a18d03c96040290d35bd5f8b2bc8ca 100644 --- a/tests/test_error_stuff.py +++ b/tests/test_error_stuff.py @@ -5,6 +5,8 @@ # # Copyright (C) 2018 Research Group Biomedical Physics, # Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# Copyright (C) 2020 Indiscale GmbH <info@indiscale.com> +# Copyright (C) 2020 Florian Spreckelsen <f.spreckelsen@indiscale.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -21,62 +23,67 @@ # # ** end header # -"""Created on 19.02.2015. +"""Test different entity errors. + +Created on 19.02.2015. @author: tf + """ -from caosdb.exceptions import EntityDoesNotExistError, UniqueNamesError,\ - TransactionError, EntityError, UnqualifiedPropertiesError,\ - EntityHasNoDatatypeError, UnqualifiedParentsError +import caosdb as h +from caosdb.exceptions import (EntityDoesNotExistError, + UniqueNamesError, TransactionError, EntityError, + UnqualifiedPropertiesError, EntityHasNoDatatypeError, + UnqualifiedParentsError) +import pytest -def test_retrieval_no_exception_raised(): - import caosdb as h - from nose.tools import assert_true, assert_false # @UnresolvedImport +def setup_module(): + try: + h.execute_query("FIND Test*").delete() + except: + pass - p = h.Property(name="Non-ExsistentProperty").retrieve(unique=True, - raise_exception_on_error=False) - assert_false(p.is_valid()) - assert_true(p.id is None or p.id < 0) +def setup(): + """No additional setup required.""" + setup_module() -def test_retrieval_exception_raised(): - import caosdb as h - from nose.tools import assert_true, assert_is_not_none, assert_equal # @UnresolvedImport - # special error: EntityDoesNotExistError - try: - h.Property(name="Non-ExistentProperty").retrieve(unique=True, - raise_exception_on_error=True) - assert_true(False) - except EntityDoesNotExistError as e: - print("print(" + str(id(e)) + ")") - print(e) - assert_is_not_none(e.get_entity()) +def teardown(): + """Delete everything.""" + setup_module() - # more general error: EntityError - try: - h.Property(name="Non-ExistentProperty").retrieve(unique=True, - raise_exception_on_error=True) - assert_true(False) - except EntityError as e: - print(e) - assert_is_not_none(e.get_entity()) - assert_equal('Non-ExistentProperty', e.get_entity().name) - # most general error: TransactionError - try: +def test_retrieval_no_exception_raised(): + """Test whether retrieval fails but error is suppressed.""" + p = h.Property(name="TestNon-ExsistentProperty").retrieve( + unique=True, raise_exception_on_error=False) + assert not p.is_valid() + assert (p.id is None or p.id < 0) + + +def test_retrieval_exception_raised(): + """Test if a TransactionError with the correct child is raised, and if + the child has the correct super classes. + + """ + propname = "TestNon-ExistentProperty" + with pytest.raises(TransactionError) as te: h.Property(name="Non-ExistentProperty").retrieve(unique=True, raise_exception_on_error=True) - assert_true(False) - except EntityDoesNotExistError as e: - print(e) - assert_is_not_none(e.get_entities()) - print(e.get_entities()) - assert_is_not_none(e.get_entity()) - print(e.get_entity()) - assert_equal(1, len(e.get_entities())) - assert_equal('Non-ExistentProperty', e.get_entities()[0].name) + te = te.value + assert len(te.get_errors()) == 1 + ee = te.get_errors()[0] + # Check for type incl. inheritance + assert isinstance(ee, EntityDoesNotExistError) + assert isinstance(ee, EntityError) + assert isinstance(ee, TransactionError) + # Correct entity causing the error: + assert not (ee.get_entity() is None) + assert ee.get_entity().name == propname + assert len(ee.get_entities()) == 1 + assert ee.get_entities()[0].name == propname def test_insertion_no_exception_raised():