diff --git a/src/caosdb/high_level_api.py b/src/caosdb/high_level_api.py index 8ee0d16001e2447dd7dec38533b5f238b71ccc9b..884234bd530938b8673abdaa1fcb613ad787cfd6 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 bc2c6a9a2bb10dcd8545737d57b6ebbff476d5cc..34cb62f35af5e23c1ea6b1d73753b77054edf01d 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")