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

ENH: enhanced utility function and added test

parent a56e8186
No related branches found
No related tags found
2 merge requests!57RELEASE 0.7.3,!52F refactor high level api
Pipeline #20046 canceled
...@@ -48,21 +48,37 @@ from datetime import datetime ...@@ -48,21 +48,37 @@ from datetime import datetime
from dateutil import parser from dateutil import parser
def standard_type_for_high_level_type(high_level_record): def standard_type_for_high_level_type(high_level_record: "CaosDBPythonEntity",
return_string: bool = False):
"""
For a given CaosDBPythonEntity either return the corresponding
class in the standard CaosDB API or - if return_string is True - return
the role as a string.
"""
if type(high_level_record) == CaosDBPythonRecord: if type(high_level_record) == CaosDBPythonRecord:
if not return_string:
return db.Record return db.Record
return "Record"
elif type(high_level_record) == CaosDBPythonFile: elif type(high_level_record) == CaosDBPythonFile:
if not return_string:
return db.File return db.File
return "File"
elif type(high_level_record) == CaosDBPythonProperty: elif type(high_level_record) == CaosDBPythonProperty:
if not return_string:
return db.Property return db.Property
return "Property"
elif type(high_level_record) == CaosDBPythonRecordType: elif type(high_level_record) == CaosDBPythonRecordType:
if not return_string:
return db.RecordType return db.RecordType
return "RecordType"
elif type(high_level_record) == CaosDBPythonEntity: elif type(high_level_record) == CaosDBPythonEntity:
if not return_string:
return db.Entity return db.Entity
return "Entity"
raise RuntimeError("Incompatible type.") raise RuntimeError("Incompatible type.")
def high_level_type_for_standard_type(standard_record): def high_level_type_for_standard_type(standard_record: db.Entity):
if type(standard_record) == db.Record: if type(standard_record) == db.Record:
return CaosDBPythonRecord return CaosDBPythonRecord
elif type(standard_record) == db.File: elif type(standard_record) == db.File:
...@@ -588,6 +604,9 @@ class CaosDBPythonEntity(object): ...@@ -588,6 +604,9 @@ class CaosDBPythonEntity(object):
# The full information to be returned: # The full information to be returned:
fulldict = dict() fulldict = dict()
# Add CaosDB role:
for parent in self._parents: for parent in self._parents:
if isinstance(parent, CaosDBPythonEntity): if isinstance(parent, CaosDBPythonEntity):
parents.append(parent.serialize()) parents.append(parent.serialize())
...@@ -729,6 +748,11 @@ def _single_convert_to_entity(entity: db.Entity, ...@@ -729,6 +748,11 @@ def _single_convert_to_entity(entity: db.Entity,
for base_attribute in BASE_ATTRIBUTES: for base_attribute in BASE_ATTRIBUTES:
if base_attribute in ("file", "path") and not isinstance(robj, CaosDBPythonFile): if base_attribute in ("file", "path") and not isinstance(robj, CaosDBPythonFile):
continue continue
# Skip version:
if base_attribute == "version":
continue
val = robj.__getattribute__(base_attribute) val = robj.__getattribute__(base_attribute)
if val is not None: if val is not None:
......
...@@ -26,10 +26,13 @@ ...@@ -26,10 +26,13 @@
import caosdb as db import caosdb as db
from caosdb.high_level_api import (convert_to_entity, convert_to_python_object, from caosdb.high_level_api import (convert_to_entity, convert_to_python_object,
new_high_level_entity_for_record_type) new_high_level_entity)
from caosdb.high_level_api import (CaosDBPythonUnresolvedParent, from caosdb.high_level_api import (CaosDBPythonUnresolvedParent,
CaosDBPythonUnresolvedReference, CaosDBPythonUnresolvedReference,
CaosDBPythonRecord, CaosDBPythonFile) CaosDBPythonRecord, CaosDBPythonFile,
high_level_type_for_standard_type,
standard_type_for_high_level_type,
CaosDBPythonEntity)
from caosdb.apiutils import compare_entities from caosdb.apiutils import compare_entities
from caosdb.common.datatype import (is_list_datatype, from caosdb.common.datatype import (is_list_datatype,
...@@ -413,7 +416,7 @@ def test_record_generator(): ...@@ -413,7 +416,7 @@ def test_record_generator():
simrt = db.RecordType(name="SimOutput") simrt = db.RecordType(name="SimOutput")
rt.add_property(name="outputfile", datatype="SimOutput") rt.add_property(name="outputfile", datatype="SimOutput")
obj = new_high_level_entity_for_record_type( obj = new_high_level_entity(
rt, "SUGGESTED", "", True) rt, "SUGGESTED", "", True)
print(obj) print(obj)
assert False assert False
...@@ -509,3 +512,13 @@ def test_list_types(): ...@@ -509,3 +512,13 @@ def test_list_types():
text = [line[4:] for line in text.split("\n")] text = [line[4:] for line in text.split("\n")]
for line in text2: for line in text2:
assert line in text assert line in text
# Test utility functions:
def test_type_conversion():
assert high_level_type_for_standard_type(db.Record()) == CaosDBPythonRecord
assert high_level_type_for_standard_type(db.Entity()) == CaosDBPythonEntity
assert standard_type_for_high_level_type(CaosDBPythonRecord()) == db.Record
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"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment