From 24a9f86ef53d821ea8534781ae204fafe0675ff9 Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <alexander@mail-schlemmer.de> Date: Mon, 7 Mar 2022 09:27:08 +0100 Subject: [PATCH] ENH: enhanced utility function and added test --- src/caosdb/high_level_api.py | 38 ++++++++++++++++++++++++++------ unittests/test_high_level_api.py | 19 +++++++++++++--- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/caosdb/high_level_api.py b/src/caosdb/high_level_api.py index 3daf9f2f..8ee0d160 100644 --- a/src/caosdb/high_level_api.py +++ b/src/caosdb/high_level_api.py @@ -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: diff --git a/unittests/test_high_level_api.py b/unittests/test_high_level_api.py index 3e7a7369..bc2c6a9a 100644 --- a/unittests/test_high_level_api.py +++ b/unittests/test_high_level_api.py @@ -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" -- GitLab