Skip to content
Snippets Groups Projects
Commit 442acd02 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

TST: added tests for serialization

parent 097a2a5c
No related branches found
No related tags found
2 merge requests!57RELEASE 0.7.3,!52F refactor high level api
Pipeline #19931 failed
......@@ -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))
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment