Select Git revision
test_affiliation.py
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_error_stuff.py 10.23 KiB
# encoding: utf-8
#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# 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
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ** end header
#
"""Test different entity errors.
Created on 19.02.2015.
@author: tf
"""
import caosdb as h
from caosdb.exceptions import (EntityDoesNotExistError, EntityError,
EntityHasNoDatatypeError,
TransactionError, UniqueNamesError,
UnqualifiedParentsError,
UnqualifiedPropertiesError)
import pytest
def setup_module():
try:
h.execute_query("FIND Test*").delete()
except:
pass
def setup():
"""No additional setup required."""
setup_module()
def teardown():
"""Delete everything."""
setup_module()
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="TestNon-ExistentProperty").retrieve(unique=True,
raise_exception_on_error=True)
assert len(te.value.get_errors()) == 1
ee = te.value.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 not ee.get_entity().is_valid()
assert ee.get_entity().has_errors()
def test_insertion_no_exception_raised():
"""Test whether insertion fails but no error is raised."""
p = h.Property(name="TestNoTypeProperty").insert(
raise_exception_on_error=False)
assert not p.is_valid()
assert (p.id is None or p.id < 0)
def test_insertion_exception_raised():
"""Test insertion of a property with missing datatype."""
p = h.Property(name="TestNoTypeProperty")
with pytest.raises(TransactionError) as te:
p.insert(raise_exception_on_error=True)
assert te.value.has_error(EntityHasNoDatatypeError)
def test_insertion_with_invalid_parents():
with pytest.raises(TransactionError) as te:
p = h.Property(
name="TestNoTypeProperty",
datatype="Text").add_parent(
id=-1)
p.insert(raise_exception_on_error=True)
upe = te.value.get_errors()[0]
print(upe)
assert isinstance(upe, UnqualifiedParentsError)
assert not upe.get_entity() is None
assert upe.get_entity().name == p.name
assert upe.get_entity().id == p.id
assert upe.get_entity().has_errors()
assert not p.is_valid()
assert not upe.get_entity().is_valid()
assert not upe.get_entities() is None
def test_insertion_with_invalid_properties():
with pytest.raises(TransactionError) as te:
p = h.Property(
name="TestNoTypeProperty",
datatype="Text").add_property(
id=-1)
p.insert(raise_exception_on_error=True)
upe = te.value.get_errors()[0]
assert isinstance(upe, UnqualifiedPropertiesError)
assert not upe.get_entity() is None
assert upe.get_entity().name == p.name
assert upe.get_entity().has_errors()
assert p.has_errors()
assert not p.is_valid()
assert not upe.get_entity().is_valid()
def test_entity_does_not_exist():
"""When retrieving a container with existing and non-existing
entities, only those that don't exist should cause
EntityDoesNotExistErrors.
"""
p1 = h.Property(name="TestNon-ExistentProperty1").retrieve(
raise_exception_on_error=False)
p2 = h.Property(name="TestNon-ExistentProperty2").retrieve(
raise_exception_on_error=False)
p3 = h.Property(name="TestNon-ExistentProperty3").retrieve(
raise_exception_on_error=False)
# None of them should exist
assert not p1.is_valid()
assert (p1.id is None or p1.id < 0)
assert not p2.is_valid()
assert (p2.id is None or p2.id < 0)
assert not p3.is_valid()
assert (p3.id is None or p3.id < 0)
pe = h.Property(name="TestExistentProperty", datatype="text").insert()
c = h.Container().extend(
[
h.Property(
name="TestNon-ExistentProperty1"),
h.Property(
name="TestNon-ExistentProperty2"),
h.Property(
name="TestNon-ExistentProperty3"),
h.Property(
name="TestExistentProperty")])
with pytest.raises(TransactionError) as te:
c.retrieve()
te = te.value
assert te.has_error(EntityDoesNotExistError)
# Only non-existing entities caused the container error
assert not pe.name in [x.name for x in te.get_all_entities()]
for p in (p1, p2, p3):
assert p.name in [x.name for x in te.get_all_entities()]
def test_insert_existent_entity():
"""Insertion of an already existing entity should cause a
UniqueNamesError.
"""
p1 = h.Property(name="TestNon-ExistentProperty1").retrieve(
raise_exception_on_error=False)
p2 = h.Property(name="TestNon-ExistentProperty2").retrieve(
raise_exception_on_error=False)
p3 = h.Property(name="TestNon-ExistentProperty3").retrieve(
raise_exception_on_error=False)
# None of them should exist
assert not p1.is_valid()
assert (p1.id is None or p1.id < 0)
assert not p2.is_valid()
assert (p2.id is None or p2.id < 0)
assert not p3.is_valid()
assert (p3.id is None or p3.id < 0)
pe = h.Property(name="TestExistentProperty", datatype="text").insert()
assert pe.is_valid()
c = h.Container().extend(
[
h.Property(
name="TestNon-ExistentProperty1",
datatype="text"),
h.Property(
name="TestNon-ExistentProperty2",
datatype="text"),
h.Property(
name="TestNon-ExistentProperty3",
datatype="text"),
h.Property(
name="TestExistentProperty",
datatype="text")])
with pytest.raises(TransactionError) as te:
c.insert(unique=True)
te = te.value
assert te.has_error(UniqueNamesError)
une = te.get_errors()[0]
assert not une.get_entity() is None
assert pe.name == une.get_entity().name
for p in (p1, p2, p3):
assert not p in te.get_all_entities()
def test_double_insertion():
c1 = h.Container()
c1.append(
h.Property(
name="TestSimpleTextProperty",
description="simple text property (from test_error_stuff.py)",
datatype='text'))
c1.append(
h.Property(
name="TestSimpleDoubleProperty",
description="simple double property (from test_error_stuff.py)",
datatype='double'))
c1.append(
h.Property(
name="TestSimpleIntegerProperty",
description="simple integer property (from test_error_stuff.py)",
datatype='integer'))
c1.append(
h.Property(
name="TestSimpleDatetimeProperty",
description="simple datetime property (from test_error_stuff.py)",
datatype='datetime'))
c1.append(
h.RecordType(
name="TestSimpleRecordType",
description="simple recordType (from test_error_stuff.py)").add_property(
name='TestSimpleTextProperty').add_property(
name='TestSimpleDoubleProperty').add_property(
name='TestSimpleIntegerProperty').add_property(
name='TestSimpleDatetimeProperty'))
c1.insert()
c2 = h.Container()
c2.append(
h.Property(
name="TestSimpleTextProperty",
description="simple text property (from test_error_stuff.py)",
datatype='text'))
c2.append(
h.Property(
name="TestSimpleDoubleProperty",
description="simple double property (from test_error_stuff.py)",
datatype='double'))
c2.append(
h.Property(
name="TestSimpleIntegerProperty",
description="simple integer property (from test_error_stuff.py)",
datatype='integer'))
c2.append(
h.Property(
name="TestSimpleDatetimeProperty",
description="simple datetime property (from test_error_stuff.py)",
datatype='datetime'))
c2.append(
h.RecordType(
name="TestSimpleRecordType",
description="simple recordType (from test_error_stuff.py)").add_property(
name='TestSimpleTextProperty').add_property(
name='TestSimpleDoubleProperty').add_property(
name='TestSimpleIntegerProperty').add_property(
name='TestSimpleDatetimeProperty'))
with pytest.raises(TransactionError) as te:
c2.insert()
te = te.value
assert te.has_error(UniqueNamesError)
# c2 caused the ContainerError
assert te.get_container() == c2
# exactly 5 faulty entities in c2
assert len(te.get_entities()) == 5