From 5469ff58ce6ca8b2be05a92770bd3f7547c8a8df Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <alexander@mail-schlemmer.de> Date: Mon, 7 Mar 2022 09:36:24 +0100 Subject: [PATCH] TST: added tests for runtime errors --- src/caosdb/high_level_api.py | 25 ++++++++++++++++++++++--- unittests/test_high_level_api.py | 12 ++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/caosdb/high_level_api.py b/src/caosdb/high_level_api.py index 8ee0d160..884234bd 100644 --- a/src/caosdb/high_level_api.py +++ b/src/caosdb/high_level_api.py @@ -78,6 +78,20 @@ def standard_type_for_high_level_type(high_level_record: "CaosDBPythonEntity", raise RuntimeError("Incompatible type.") +def high_level_type_for_role(role: str): + if role == "Record": + return CaosDBPythonRecord + if role == "File": + return CaosDBPythonFile + if role == "Property": + return CaosDBPythonProperty + if role == "RecordType": + return CaosDBPythonRecordType + if role == "Entity": + return CaosDBPythonEntity + raise RuntimeError("Unknown role.") + + def high_level_type_for_standard_type(standard_record: db.Entity): if type(standard_record) == db.Record: return CaosDBPythonRecord @@ -586,8 +600,13 @@ class CaosDBPythonEntity(object): return [p for p in self.__dict__ if p not in self._forbidden] - def deserialize(self, serialization: dict): - raise NotImplementedError() + @staticmethod + def deserialize(serialization: dict): + """ + Deserialize a yaml representation of an entity in high level API form. + """ + pass + def serialize(self, without_metadata: bool = False): """ @@ -605,7 +624,7 @@ class CaosDBPythonEntity(object): fulldict = dict() # Add CaosDB role: - + fulldict["role"] = standard_type_for_high_level_type(self, True) for parent in self._parents: if isinstance(parent, CaosDBPythonEntity): diff --git a/unittests/test_high_level_api.py b/unittests/test_high_level_api.py index bc2c6a9a..34cb62f3 100644 --- a/unittests/test_high_level_api.py +++ b/unittests/test_high_level_api.py @@ -32,6 +32,7 @@ from caosdb.high_level_api import (CaosDBPythonUnresolvedParent, CaosDBPythonRecord, CaosDBPythonFile, high_level_type_for_standard_type, standard_type_for_high_level_type, + high_level_type_for_role, CaosDBPythonEntity) from caosdb.apiutils import compare_entities @@ -522,3 +523,14 @@ def test_type_conversion(): assert standard_type_for_high_level_type(CaosDBPythonEntity()) == db.Entity assert standard_type_for_high_level_type(CaosDBPythonFile(), True) == "File" assert standard_type_for_high_level_type(CaosDBPythonRecord(), True) == "Record" + assert high_level_type_for_role("Record") == CaosDBPythonRecord + assert high_level_type_for_role("Entity") == CaosDBPythonEntity + assert high_level_type_for_role("File") == CaosDBPythonFile + with pytest.raises(RuntimeError, match="Unknown role."): + high_level_type_for_role("jkaldjfkaldsjf") + + with pytest.raises(RuntimeError, match="Incompatible type."): + standard_type_for_high_level_type(42, True) + + with pytest.raises(RuntimeError, match="Incompatible type."): + high_level_type_for_standard_type("ajsdkfjasfkj") -- GitLab