diff --git a/src/caosdb/high_level_api.py b/src/caosdb/high_level_api.py index c5289d97d8405cea31887cf403ecb28b7f8e0242..6c254f1d9a828d91900287128bccfc9a0317f0e4 100644 --- a/src/caosdb/high_level_api.py +++ b/src/caosdb/high_level_api.py @@ -146,10 +146,16 @@ class CaosDBPythonEntity(object): """ Getter for the file. """ + if type(self) != CaosDBPythonFile: + raise RuntimeError("Please don't use the file attribute for entities" + " that are no files.") return self._file - @name.setter + @file.setter def file(self, val: str): + if val is not None and type(self) != CaosDBPythonFile: + raise RuntimeError("Please don't use the file attribute for entities" + " that are no files.") self._file = val @property @@ -157,10 +163,16 @@ class CaosDBPythonEntity(object): """ Getter for the path. """ + if type(self) != CaosDBPythonFile: + raise RuntimeError("Please don't use the path attribute for entities" + " that are no files.") return self._path - @name.setter + @path.setter def path(self, val: str): + if val is not None and type(self) != CaosDBPythonFile: + raise RuntimeError("Please don't use the path attribute for entities" + " that are no files.") self._path = val @property @@ -526,6 +538,9 @@ class CaosDBPythonEntity(object): properties = dict() parents = list() + # The full information to be returned: + fulldict = dict() + for parent in self._parents: if isinstance(parent, CaosDBPythonEntity): parents.append(parent.serialize()) @@ -535,6 +550,15 @@ class CaosDBPythonEntity(object): else: raise RuntimeError("Incompatible class used as parent.") + for baseprop in ("name", "id", "description", "version"): + val = self.__getattribute__(baseprop) + if val is not None: + fulldict[baseprop] = val + + if type(self) == CaosDBPythonFile: + fulldict["file"] = self.file + fulldict["path"] = self.path + for p in self.get_properties(): m = self.get_property_metadata(p) metadata[p] = dict() @@ -550,14 +574,13 @@ class CaosDBPythonEntity(object): properties[p] = val.serialize(without_metadata) else: properties[p] = val - if without_metadata: - return { - "properties": properties, - "parents": parents} - return { - "metadata": metadata, - "properties": properties, - "parents": parents} + + fulldict["properties"] = properties + fulldict["parents"] = parents + + if not without_metadata: + fulldict["metadata"] = metadata + return fulldict def __str__(self): return yaml.dump(self.serialize(False)) diff --git a/unittests/test_high_level_api.py b/unittests/test_high_level_api.py index c94fd8aa66946fdc3bedaec67a1bd0608f23cc74..e5d4cf6b4c770d7d62494d841dea0f444fed3a39 100644 --- a/unittests/test_high_level_api.py +++ b/unittests/test_high_level_api.py @@ -280,6 +280,20 @@ def test_wrong_entity_for_file(): with pytest.raises(RuntimeError): obj = convert_to_python_object(r) + +def test_serialization(): + r = db.Record(id=5, name="test", description="ok") + r.add_property(name="v", value=15, datatype=db.INTEGER, unit="kpx", + importance="RECOMMENDED") + + obj = convert_to_python_object(r) + text = str(obj) + teststrs = ["description: ok", "id: 5", "datatype: INTEGER", + "importance: RECOMMENDED", "unit: kpx", "name: test", "v: 15"] + for teststr in teststrs: + assert teststr in text + + def test_files(): # empty file: r = db.File() @@ -301,3 +315,7 @@ def test_files(): assert obj.path == "test.dat" assert obj.file == "/local/path/test.dat" + + print(obj) + assert "path: test.dat" in str(obj) + assert "file: /local/path/test.dat" in str(obj)