From da04056f8cd864ea1e9060da963c77d12d40478b Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Thu, 4 Jun 2020 22:30:07 +0200
Subject: [PATCH] TST: _parse_value when datatype is an entity

---
 src/caosdb/common/models.py | 35 ++++++++++++++++++-----------------
 unittests/test_datatype.py  | 11 +++++++++++
 2 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py
index 06617d1e..61281739 100644
--- a/src/caosdb/common/models.py
+++ b/src/caosdb/common/models.py
@@ -992,25 +992,26 @@ def _parse_value(datatype, value):
             return value
 
     # deal with collections
-    matcher = re.compile(r"^(?P<col>[^<]+)<(?P<dt>[^>]+)>$")
-    m = matcher.match(datatype)
-    if m:
-        col = m.group("col")
-        dt = m.group("dt")
-
-        if col == "LIST":
-            ret = list()
-        else:
-            return value
+    if isinstance(datatype, str):
+        matcher = re.compile(r"^(?P<col>[^<]+)<(?P<dt>[^>]+)>$")
+        m = matcher.match(datatype)
+        if m:
+            col = m.group("col")
+            dt = m.group("dt")
+
+            if col == "LIST":
+                ret = list()
+            else:
+                return value
 
-        if hasattr(value, "__iter__") and not isinstance(value, str):
-            for v in value:
-                ret.append(_parse_value(dt, v))
-        else:
-            # put a single value into a list since the datatype says so.
-            ret.append(_parse_value(dt, value))
+            if hasattr(value, "__iter__") and not isinstance(value, str):
+                for v in value:
+                    ret.append(_parse_value(dt, v))
+            else:
+                # put a single value into a list since the datatype says so.
+                ret.append(_parse_value(dt, value))
 
-        return ret
+            return ret
 
     # This is for a special case, where the xml parser could not differentiate
     # between single values and lists with one element. As
diff --git a/unittests/test_datatype.py b/unittests/test_datatype.py
index ef650ce2..e5c457d1 100644
--- a/unittests/test_datatype.py
+++ b/unittests/test_datatype.py
@@ -77,3 +77,14 @@ def test_parsing_of_references():
 
     entity = db.Record(name="bla")
     assert id(_parse_value(dtype, entity)) == id(entity)
+
+
+    dtype = db.RecordType(name="Person")
+    assert _parse_value(dtype, "Anna Lytik") == "Anna Lytik"
+    assert _parse_value(None, "Anna Lytik") == "Anna Lytik"
+    assert _parse_value(dtype, "2345@sdfg") == "2345@sdfg"
+    assert _parse_value(dtype, "2345") == 2345
+    assert _parse_value(dtype, 2345) == 2345
+
+    entity = db.Record(name="bla")
+    assert id(_parse_value(dtype, entity)) == id(entity)
-- 
GitLab