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
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:
return db.Record
if not return_string:
return db.Record
return "Record"
elif type(high_level_record) == CaosDBPythonFile:
return db.File
if not return_string:
return db.File
return "File"
elif type(high_level_record) == CaosDBPythonProperty:
return db.Property
if not return_string:
return db.Property
return "Property"
elif type(high_level_record) == CaosDBPythonRecordType:
return db.RecordType
if not return_string:
return db.RecordType
return "RecordType"
elif type(high_level_record) == CaosDBPythonEntity:
return db.Entity
if not return_string:
return db.Entity
return "Entity"
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:
return CaosDBPythonRecord
elif type(standard_record) == db.File:
......@@ -588,6 +604,9 @@ class CaosDBPythonEntity(object):
# The full information to be returned:
fulldict = dict()
# Add CaosDB role:
for parent in self._parents:
if isinstance(parent, CaosDBPythonEntity):
parents.append(parent.serialize())
......@@ -729,6 +748,11 @@ def _single_convert_to_entity(entity: db.Entity,
for base_attribute in BASE_ATTRIBUTES:
if base_attribute in ("file", "path") and not isinstance(robj, CaosDBPythonFile):
continue
# Skip version:
if base_attribute == "version":
continue
val = robj.__getattribute__(base_attribute)
if val is not None:
......
......@@ -26,10 +26,13 @@
import caosdb as db
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,
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.common.datatype import (is_list_datatype,
......@@ -413,7 +416,7 @@ def test_record_generator():
simrt = db.RecordType(name="SimOutput")
rt.add_property(name="outputfile", datatype="SimOutput")
obj = new_high_level_entity_for_record_type(
obj = new_high_level_entity(
rt, "SUGGESTED", "", True)
print(obj)
assert False
......@@ -509,3 +512,13 @@ def test_list_types():
text = [line[4:] for line in text.split("\n")]
for line in text2:
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