From 7d76f8b91832e5ae7c790c24b3b4c4478c97d5e6 Mon Sep 17 00:00:00 2001
From: Daniel <d.hornung@indiscale.com>
Date: Fri, 1 Dec 2023 19:12:29 +0100
Subject: [PATCH] FIX: Also cover reference properties as elements of the
 container.

This should also work a bit around
[#242](https://gitlab.com/linkahead/linkahead-server/-/issues/241)
---
 src/linkahead/common/models.py | 14 ++++++++++++++
 unittests/test_container.py    | 16 +++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py
index be6acf3c..38c13490 100644
--- a/src/linkahead/common/models.py
+++ b/src/linkahead/common/models.py
@@ -3314,6 +3314,20 @@ class Container(list):
 
                 if hasattr(prop, 'id'):
                     is_property.add(prop.id)
+            if isinstance(container_item, Property):
+                dtype = container_item.datatype
+                if isinstance(dtype, Entity):
+                    is_being_referenced.add(dtype.id)
+                elif isinstance(dtype, str):
+                    if is_list_datatype(dtype):
+                        dtype = get_list_datatype(dtype)
+                    try:
+                        is_being_referenced.add(container.get_entity_by_name(dtype).id)
+                    except KeyError:
+                        pass
+                else:
+                    # plain old scalar datatype
+                    pass
 
         dependent_parents = item_id.intersection(is_parent)
         dependent_properties = item_id.intersection(is_property)
diff --git a/unittests/test_container.py b/unittests/test_container.py
index 5833fb3f..4cd8fefc 100644
--- a/unittests/test_container.py
+++ b/unittests/test_container.py
@@ -146,7 +146,6 @@ def test_container_dependencies_for_deletion_with_lists():
     assert db.Container._find_dependencies_in_container(container) == {2001}
 
 
-# @pytest.mark.xfail
 def test_container_deletion_with_references():
     """Test if dependencies are checked correctly.
     """
@@ -154,13 +153,28 @@ def test_container_deletion_with_references():
     RT1 = db.RecordType(name="RT1")
     RT2 = db.RecordType(name="RT2").add_property(name="prop2", datatype=RT1)
     RT3 = db.RecordType(name="RT3").add_property(name="prop3", datatype="LIST<RT1>")
+    prop4a = db.Property(name="prop4a", datatype=RT1)
+    prop4b = db.Property(name="prop4b", datatype="RT1")
+    prop5 = db.Property(name="prop5", datatype="LIST<RT1>")
     cont12 = db.Container().extend([RT1, RT2])
     cont13 = db.Container().extend([RT1, RT3])
+    cont14a = db.Container().extend([RT1, prop4a])
+    cont14b = db.Container().extend([RT1, prop4b])
+    cont15 = db.Container().extend([RT1, prop5])
     cont12.to_xml()
     cont13.to_xml()
+    cont14a.to_xml()
+    cont14b.to_xml()
+    cont15.to_xml()
 
     deps12 = db.Container._find_dependencies_in_container(cont12)
     deps13 = db.Container._find_dependencies_in_container(cont13)
+    deps14a = db.Container._find_dependencies_in_container(cont14a)
+    deps14b = db.Container._find_dependencies_in_container(cont14b)
+    deps15 = db.Container._find_dependencies_in_container(cont15)
 
     assert len(deps12) == 1 and deps12.pop() == -1
     assert len(deps13) == 1 and deps13.pop() == -1
+    assert len(deps14a) == 1 and deps14a.pop() == -1
+    assert len(deps14b) == 1 and deps14b.pop() == -1
+    assert len(deps15) == 1 and deps15.pop() == -1
-- 
GitLab