From 8b99aed698140ee0f78927fad361a0e7c55191d7 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Mon, 20 Apr 2020 14:20:07 +0200 Subject: [PATCH] TST: deletion of entities with strage subproperties --- tests/test_empty_text_value.py | 6 +- tests/test_list.py | 68 +++++++----------- tests/test_update.py | 126 ++++++++++++++++----------------- 3 files changed, 87 insertions(+), 113 deletions(-) diff --git a/tests/test_empty_text_value.py b/tests/test_empty_text_value.py index 671dd00..b5d6165 100644 --- a/tests/test_empty_text_value.py +++ b/tests/test_empty_text_value.py @@ -39,7 +39,6 @@ def teardown(): print(e) -@pytest.mark.xfail def test_empty_string(): r1 = db.Record() r1.add_parent("TestRT") @@ -95,7 +94,6 @@ def test_null_value(): assert len(db.execute_query("FIND Record TestRT")) == 2 -@pytest.mark.xfail def test_list_with_empty_string(): r1 = db.Record() r1.add_parent("TestRT") @@ -134,7 +132,7 @@ def test_null_list(): "FIND TestRT WHERE TestProp IS NULL", unique=True).id == r1.id -@pytest.mark.skip(reason="""this is the confirmation for +@pytest.mark.xfail(reason="""this is the confirmation for https://gitlab.com/caosdb/caosdb-server/issues/new. Empty lists cannot be distinguished from None.""") def test_empty_list(): @@ -146,7 +144,7 @@ def test_empty_list(): unique=True).get_property("TestProp").value == [] -@pytest.mark.skip(reason="""this is the confirmation for +@pytest.mark.xfail(reason="""this is the confirmation for https://gitlab.com/caosdb/caosdb-server/issues/new. `None` cannot be distinguished from [None] lists.""") def test_list_with_null_value(): diff --git a/tests/test_list.py b/tests/test_list.py index ee2ff15..5a1485d 100644 --- a/tests/test_list.py +++ b/tests/test_list.py @@ -25,29 +25,23 @@ @author: tf """ +import os import caosdb as db -# @UnresolvedImport -from nose.tools import nottest, assert_true, assert_raises, assert_equal, with_setup +from pytest import mark +from nose.tools import nottest, assert_true, assert_raises, assert_equal from caosdb.exceptions import TransactionError def setup(): - try: - db.execute_query("FIND Test*").delete() - except Exception as e: - print(e) + d = db.execute_query("FIND ENTITY WITH ID > 99") + if len(d) > 0: + d.delete() def teardown(): - try: - import os + if os.path.isfile("test.dat"): os.remove("test.dat") - except Exception as e: - print(e) - try: - db.execute_query("FIND Test*").delete() - except Exception as e: - print(e) + setup() def test_list_of_files(): @@ -84,7 +78,6 @@ def test_list_of_files(): "Reference not qualified. The value of this Reference Property is to be a child of its data type.") -@with_setup(setup, teardown) def test_list_datatype_know(): p = db.Property( name="TestListProperty", @@ -104,7 +97,6 @@ def test_list_datatype_know(): unique=True).datatype) -@with_setup(setup, teardown) def test_rt_property_without_value(): p = db.Property( name="TestListProperty", @@ -129,7 +121,6 @@ def test_rt_property_without_value(): unique=True).get_property("TestListProperty").datatype) -@with_setup(setup, teardown) def test_concrete_property_with_single_value(): p = db.Property( name="TestListProperty", @@ -167,7 +158,6 @@ def test_concrete_property_with_single_value(): unique=True).get_property("TestListProperty").value) -@with_setup(setup, teardown) def test_query_concrete_property_with_single_value(): p = db.Property( name="TestListProperty", @@ -188,7 +178,6 @@ def test_query_concrete_property_with_single_value(): unique=True).id) -@with_setup(setup, teardown) def test_query_concrete_property_with_more_values(): p = db.Property( name="TestListProperty", @@ -209,7 +198,6 @@ def test_query_concrete_property_with_more_values(): unique=True).id) -@with_setup(setup, teardown) def test_error_on_wrong_value(): p = db.Property( name="TestListProperty", @@ -228,7 +216,6 @@ def test_error_on_wrong_value(): "Cannot parse value to integer.") -@with_setup(setup, teardown) def test_data_type_with_non_existing_ref1(): with assert_raises(TransactionError) as cm: db.Property(name="TestListProperty", @@ -236,7 +223,6 @@ def test_data_type_with_non_existing_ref1(): assert_equal(cm.exception.msg, "Unknown datatype.") -@with_setup(setup, teardown) def test_data_type_with_non_existing_ref2(): with assert_raises(TransactionError) as cm: db.Property( @@ -245,14 +231,12 @@ def test_data_type_with_non_existing_ref2(): assert_equal(cm.exception.msg, "Unknown datatype.") -@with_setup(setup, teardown) def test_data_type_with_non_existing_ref3(): with assert_raises(TransactionError) as cm: db.Property(name="TestListProperty", datatype=db.LIST(-2341)).insert() assert_equal(cm.exception.msg, "Unknown datatype.") -@with_setup(setup, teardown) def test_data_type_with_existing_ref1(): c = db.Container().append(db.RecordType(name="TestRT")).append( db.Property(name="TestListProperty", datatype=db.LIST("TestRT"))) @@ -260,7 +244,6 @@ def test_data_type_with_existing_ref1(): assert_true(c.is_valid()) -@with_setup(setup, teardown) def test_data_type_with_existing_ref2(): rt = db.RecordType(name="TestRT") c = db.Container().append(rt).append( @@ -269,21 +252,18 @@ def test_data_type_with_existing_ref2(): assert_true(c.is_valid()) -@with_setup(setup, teardown) def test_data_type_with_existing_ref3(): rt = db.RecordType(name="TestRT").insert() p = db.Property(name="TestListProperty", datatype=db.LIST(rt)).insert() assert_true(p.is_valid()) -@with_setup(setup, teardown) def test_data_type_with_existing_ref4(): rt = db.RecordType(name="TestRT").insert() p = db.Property(name="TestListProperty", datatype=db.LIST(rt.id)).insert() assert_true(p.is_valid()) -@with_setup(setup, teardown) def test_data_type_with_existing_ref5(): rt = db.RecordType(name="TestRT").insert() p = db.Property( @@ -293,7 +273,6 @@ def test_data_type_with_existing_ref5(): assert_true(p.is_valid()) -@with_setup(setup, teardown) def test_rt_ref_property_without_value(): rt = db.RecordType(name="TestRT").insert() p = db.Property( @@ -319,7 +298,6 @@ def test_rt_ref_property_without_value(): rt.name)) -@with_setup(setup, teardown) def test_datatype_inheritance1(): rt = db.RecordType(name="TestRT").insert() db.Property(name="TestListProperty", datatype=db.LIST(rt.name)).insert() @@ -336,7 +314,6 @@ def test_datatype_inheritance1(): rt.name)) -@with_setup(setup, teardown) def test_datatype_inheritance2(): rt = db.RecordType(name="TestRT").insert() rt2 = db.RecordType(name="TestRT2").add_parent(name="TestRT").insert() @@ -357,7 +334,6 @@ def test_datatype_inheritance2(): rt2.name)) -@with_setup(setup, teardown) def test_datatype_inheritance3(): rt = db.RecordType(name="TestRT").insert() rt2 = db.RecordType(name="TestRT2").insert() @@ -378,7 +354,6 @@ def test_datatype_inheritance3(): rt2.name)) -@with_setup(setup, teardown) def test_single_ref_value(): rt = db.RecordType(name="TestRT").insert() rec = db.Record(name="TestRec").add_parent(name="TestRT").insert() @@ -391,7 +366,6 @@ def test_single_ref_value(): assert_true(rt2.is_valid()) -@with_setup(setup, teardown) def test_single_ref_value_scope_error(): db.RecordType(name="TestRT").insert() rec = db.Record(name="TestRec").add_parent(name="TestRT").insert() @@ -409,7 +383,6 @@ def test_single_ref_value_scope_error(): "Reference not qualified. The value of this Reference Property is to be a child of its data type.") -@with_setup(setup, teardown) def test_error_single_non_existing_ref_value(): rt = db.RecordType(name="TestRT").insert() db.Property(name="TestProp", datatype=db.LIST(rt)).insert() @@ -440,7 +413,6 @@ def test_error_single_non_existing_ref_value(): "Referenced entity does not exist.") -@with_setup(setup, teardown) def test_error_multi_non_existing_ref_value(): rt = db.RecordType(name="TestRT").insert() db.Record(name="TestRec1").add_parent(name="TestRT").insert() @@ -481,7 +453,6 @@ def test_error_multi_non_existing_ref_value(): "Referenced entity does not exist.") -@with_setup(setup, teardown) def test_multi_ref_value_scope_error(): db.RecordType(name="TestRT").insert() rec = db.Record(name="TestRec").add_parent(name="TestRT").insert() @@ -504,7 +475,6 @@ def test_multi_ref_value_scope_error(): "Reference not qualified. The value of this Reference Property is to be a child of its data type.") -@with_setup(setup, teardown) def test_multi_ref_value(): rt = db.RecordType(name="TestRT").insert() rec = db.Record(name="TestRec").add_parent(name="TestRT").insert() @@ -534,7 +504,6 @@ def test_multi_ref_value(): unique=True).id, rt2.id) -@with_setup(setup, teardown) def test_multi_ref_with_doublets(): rt = db.RecordType(name="TestRT").insert() rec1 = db.Record(name="TestRec").add_parent(name="TestRT").insert() @@ -570,7 +539,6 @@ def test_multi_ref_with_doublets(): unique=True).id, rt2.id) -@with_setup(setup, teardown) def test_multi_ref_with_null(): rt = db.RecordType(name="TestRT").insert() rec1 = db.Record(name="TestRec1").add_parent(name="TestRT").insert() @@ -608,7 +576,6 @@ def test_multi_ref_with_null(): rt2.id) -@with_setup(setup, teardown) def test_rec_with_value(): rt = db.RecordType(name="TestRT").insert() rec1 = db.Record(name="TestRec1").add_parent(name="TestRT").insert() @@ -638,7 +605,6 @@ def test_rec_with_value(): rec4.id) -@with_setup(setup, teardown) def test_rec_with_null_value(): rt = db.RecordType(name="TestRT").insert() rec1 = db.Record(name="TestRec1").add_parent(name="TestRT").insert() @@ -662,7 +628,6 @@ def test_rec_with_null_value(): rec4.id) -@with_setup(setup, teardown) def test_list_of_references(): rt1 = db.RecordType(name="Test_RT1").insert() rt2 = db.RecordType( @@ -693,3 +658,20 @@ def test_list_of_references(): rt2rec = db.Record(name="Test_RT2_Rec").add_parent("Test_RT2").add_property( name="Test_RT1", datatype=db.LIST("Test_RT1"), value=[rt1rec1]).insert() assert_true(rt2rec.is_valid()) + + +def test_list_in_sub_property(): + rt1 = db.RecordType(name="TestRT1").insert() + rt2 = db.RecordType(name="TestRT2").insert() + p = db.Property(name="TestProperty", datatype="TestRT1").insert() + p2 = db.Property(name="TestBogusProperty", datatype=db.TEXT).insert() + rec1 = db.Record(name="TestRT1Rec1").add_parent("TestRT1").insert() + rec2 = db.Record(name="TestRT2Rec2").add_parent("TestRT2").add_property("TestProperty", "TestRT1Rec1") + rec2.get_property("TestProperty").add_property("TestBogusProperty", + datatype=db.LIST(db.TEXT)) + rec2.insert() + + rec2.delete() + + + diff --git a/tests/test_update.py b/tests/test_update.py index 9fff05a..a97d821 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -25,21 +25,23 @@ @author: tf """ +from pytest import mark +from lxml import etree -from nose.tools import assert_true, assert_equal, nottest # @UnresolvedImport import caosdb as db - from caosdb.connection.connection import get_connection -from lxml import etree from caosdb.common.utils import xml2str from caosdb.common.models import raise_errors -def teardown_module(): - try: - db.execute_query("FIND Entity WITH ID > 100").delete() - except db.TransactionError as terr: - print(terr) +def setup(): + d = db.execute_query("FIND Entity WITH ID > 99") + if len(d) > 0: + d.delete() + + +def teardown(): + setup() def test_property_no_id(): @@ -47,8 +49,7 @@ def test_property_no_id(): rt1 = db.RecordType(name="RT1").insert() rt1.add_property(name="P1").update(raise_exception_on_error=False) - assert_equal(rt1.get_property("P1").get_errors()[ - 0].description, "Entity has no ID.") + assert rt1.get_property("P1").get_errors()[0].description == "Entity has no ID." def test_parent_no_id(): @@ -56,62 +57,55 @@ def test_parent_no_id(): parent = db.RecordType(name="RTP").insert() child.add_parent(name="RTP").update(raise_exception_on_error=False) - assert_equal(child.get_parent("RTP").get_errors() - [0].description, "Entity has no ID.") + assert child.get_parent("RTP").get_errors()[0].description == "Entity has no ID." def test_update_1(): - - try: - p1 = db.Property(name="FirstName", datatype='TEXT').insert() - assert_true(p1.is_valid()) - p2 = db.Property(name="LastName", datatype='TEXT').insert() - assert_true(p2.is_valid()) - p3 = db.Property(name="StartDate", datatype='DATETIME').insert() - assert_true(p3.is_valid()) - - rt1 = db.RecordType( - name="Person", - description="A natural person.").add_property( - p1, - importance='OBLIGATORY').add_property( - p2, - importance='OBLIGATORY').insert() - assert_true(rt1.is_valid()) - assert_equal(2, len(rt1.get_properties())) - - rt1_xml = rt1.to_xml() - p3_xml = etree.Element("Property") - p3_xml.set("id", str(p3.id)) - p3_xml.set("importance", "OBLIGATORY") - rt1_xml.append(p3_xml) - xml_str = '<Update>' + xml2str(rt1_xml) + '</Update>' - - con = get_connection() - - response = con.update(entity_uri_segment=["Entity"], body=xml_str) - - c = db.Container._response_to_entities(response) - raise_errors(c) - - rt1.retrieve() - assert_true(rt1.is_valid()) - assert_equal(3, len(rt1.get_properties())) - - finally: - try: - rt1.delete() - except BaseException: - pass - try: - p3.delete() - except BaseException: - pass - try: - p2.delete() - except BaseException: - pass - try: - p1.delete() - except BaseException: - pass + p1 = db.Property(name="FirstName", datatype='TEXT').insert() + assert p1.is_valid() + p2 = db.Property(name="LastName", datatype='TEXT').insert() + assert p2.is_valid() + p3 = db.Property(name="StartDate", datatype='DATETIME').insert() + assert p3.is_valid() + + rt1 = db.RecordType( + name="Person", + description="A natural person.").add_property( + p1, + importance='OBLIGATORY').add_property( + p2, + importance='OBLIGATORY').insert() + assert rt1.is_valid() + assert 2 == len(rt1.get_properties()) + + rt1_xml = rt1.to_xml() + p3_xml = etree.Element("Property") + p3_xml.set("id", str(p3.id)) + p3_xml.set("importance", "OBLIGATORY") + rt1_xml.append(p3_xml) + xml_str = '<Update>' + xml2str(rt1_xml) + '</Update>' + + con = get_connection() + + response = con.update(entity_uri_segment=["Entity"], body=xml_str) + + c = db.Container._response_to_entities(response) + raise_errors(c) + + rt1.retrieve() + assert rt1.is_valid() + assert 3 == len(rt1.get_properties()) + + +@mark.xfail(reason="TODO: investigate whats the problem here") +def test_server_error_during_update(): + rt1 = db.RecordType(name="TestRT1").insert() + rt2 = db.RecordType(name="TestRT2").insert() + p = db.Property(name="TestProperty", datatype="TestRT1").insert() + p2 = db.Property(name="TestBogusProperty", datatype=db.TEXT).insert() + rec1 = db.Record(name="TestRT1Rec1").add_parent("TestRT1").insert() + rec2 = db.Record(name="TestRT2Rec2").add_parent("TestRT2").add_property("TestProperty", "TestRT1Rec1").insert() + + # do stupid things + rec2.get_property("TestProperty").add_property("TestBogusProperty") + rec2.update() -- GitLab