diff --git a/src/caosdb/high_level_api.py b/src/caosdb/high_level_api.py index 6c254f1d9a828d91900287128bccfc9a0317f0e4..8bcbbcdc59e8c83f2f9d10ddea94b71addb2f832 100644 --- a/src/caosdb/high_level_api.py +++ b/src/caosdb/high_level_api.py @@ -52,8 +52,6 @@ class CaosDBPropertyMetaData: # name is already the name of the attribute unit: Optional[str] = None datatype: Optional[str] = None - file: Optional[str] = None - path: Optional[str] = None description: Optional[str] = None id: Optional[int] = None importance: Optional[str] = None @@ -638,8 +636,9 @@ def _single_convert_to_python_object(robj: CaosDBPythonEntity, Returns the input object robj. """ for base_attribute in BASE_ATTRIBUTES: - robj.__setattr__(base_attribute, - entity.__getattribute__(base_attribute)) + val = entity.__getattribute__(base_attribute) + if val is not None: + robj.__setattr__(base_attribute, val) for prop in entity.properties: robj._set_property_from_entity(prop, entity.get_importance(prop), references) @@ -663,13 +662,13 @@ def _single_convert_to_entity(entity: db.Entity, The CaosDBPythonEntity that is supposed to be converted to the entity. """ - # for base_attribute in BASE_ATTRIBUTES: - # modified_base_attribute = base_attribute - # if base_attribute in ("file", "path"): - # modified_base_attribute = "_" + modified_base_attribute - - # entity.__setattr__(modified_base_attribute, - # robj.__getattribute__(base_attribute)) + for base_attribute in BASE_ATTRIBUTES: + if base_attribute in ("file", "path") and not isinstance(robj, CaosDBPythonFile): + continue + val = robj.__getattribute__(base_attribute) + + if val is not None: + entity.__setattr__(base_attribute, val) for parent in robj.get_parents(): if isinstance(parent, CaosDBPythonUnresolvedParent): diff --git a/unittests/test_high_level_api.py b/unittests/test_high_level_api.py index e5d4cf6b4c770d7d62494d841dea0f444fed3a39..fdf4aa08aaf698b1eb91b8bc679e337fdbb8c58e 100644 --- a/unittests/test_high_level_api.py +++ b/unittests/test_high_level_api.py @@ -260,6 +260,31 @@ def test_conversion_to_entity(): == r.get_property("ref").value.get_property("a").value) # TODO: add more tests here +def test_base_properties(): + r = db.Record(id=5, name="test", description="ok") + r.add_property(name="v", value=15, datatype=db.INTEGER, unit="kpx", + importance="RECOMMENDED", description="description") + obj = convert_to_python_object(r) + assert obj.name == "test" + assert obj.id == 5 + assert obj.description == "ok" + metadata = obj.get_property_metadata("v") + assert metadata.id is None + assert metadata.datatype == db.INTEGER + assert metadata.unit == "kpx" + assert metadata.importance == "RECOMMENDED" + assert metadata.description == "description" + + rconv = convert_to_entity(obj) + assert rconv.name == "test" + assert rconv.id == 5 + assert rconv.description == "ok" + prop = rconv.get_property("v") + assert prop.value == 15 + assert prop.datatype == db.INTEGER + assert prop.unit == "kpx" + assert prop.description == "description" + assert rconv.get_importance("v") == "RECOMMENDED" def test_empty(): r = db.Record() @@ -293,6 +318,14 @@ def test_serialization(): for teststr in teststrs: assert teststr in text + r = db.Record(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) + assert "name" not in text + assert "id" not in text + def test_files(): # empty file: @@ -316,6 +349,5 @@ 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)