From 258a41711985123e5a9945f50eff6238b2e83e7b Mon Sep 17 00:00:00 2001
From: Florian Spreckelsen <f.spreckelsen@indiscale.com>
Date: Thu, 12 Sep 2024 11:17:09 +0200
Subject: [PATCH] TST: Check new error messages in unit tests

---
 src/caoscrawler/crawl.py     |  1 +
 unittests/test_crawler.py    | 10 +++++++-
 unittests/test_sync_graph.py |  3 +--
 unittests/test_sync_node.py  | 46 +++++++++++++++++++++++++++++-------
 4 files changed, 49 insertions(+), 11 deletions(-)

diff --git a/src/caoscrawler/crawl.py b/src/caoscrawler/crawl.py
index e2110a69..a449a779 100644
--- a/src/caoscrawler/crawl.py
+++ b/src/caoscrawler/crawl.py
@@ -64,6 +64,7 @@ from linkahead.utils.escape import escape_squoted_text
 from .config import get_config_setting
 from .converters import Converter, ConverterValidationError
 from .debug_tree import DebugTree
+from .exceptions import ImpossibleMergeError
 from .identifiable_adapters import (CaosDBIdentifiableAdapter,
                                     IdentifiableAdapter)
 from .logging import configure_server_side_logging
diff --git a/unittests/test_crawler.py b/unittests/test_crawler.py
index 0a6aee44..aaddec9e 100644
--- a/unittests/test_crawler.py
+++ b/unittests/test_crawler.py
@@ -487,9 +487,17 @@ a: ([b1, b2])
     # The Bs cannot be merged due to different references to Cs
     with raises(ImpossibleMergeError) as rte:
         crawler._split_into_inserts_and_updates(st)
+
+    # The order of the Cs is random so we only know that they are the
+    # last two elements but not in which order they have been tried to
+    # be merged.
+    assert "The problematic property is 'C' with values " in str(rte.value)
+    assert f"'[{st.nodes[-2]}]'" in str(rte.value)
+    assert f"'[{st.nodes[-1]}]'" in str(rte.value)
+
     # TODO
     # assert not isinstance(rte.value, NotImplementedError), \
-        # "Exception must not be NotImplementedError, but plain RuntimeError."
+    # "Exception must not be NotImplementedError, but plain RuntimeError."
     # assert "Could not find referencing entities" in rte.value.args[0]
     # assert "merge conflicts in the referencing" in rte.value.args[0]
 
diff --git a/unittests/test_sync_graph.py b/unittests/test_sync_graph.py
index 9015e74b..84451790 100644
--- a/unittests/test_sync_graph.py
+++ b/unittests/test_sync_graph.py
@@ -30,8 +30,7 @@ from test_crawler import (basic_retrieve_by_name_mock_up,
                           mock_get_entity_by,
                           )
 
-from caoscrawler.exceptions import (ImpossibleMergeError,
-                                    MissingIdentifyingProperty,
+from caoscrawler.exceptions import (MissingIdentifyingProperty,
                                     MissingRecordType,
                                     )
 from caoscrawler.identifiable import Identifiable
diff --git a/unittests/test_sync_node.py b/unittests/test_sync_node.py
index 668a5347..bd9e1a6c 100644
--- a/unittests/test_sync_node.py
+++ b/unittests/test_sync_node.py
@@ -238,8 +238,9 @@ def test_export_node():
         messages = {str(w.message) for w in caught}
         assert ("Multiproperties are not supported by the crawler.") in messages
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
+    assert "The problematic property is 'a' with values '['b']' and '['a']'" in str(ime.value)
 
     # SyncNodes with same ID are considered equal
     rec_a = (db.Record(id=101)
@@ -269,18 +270,26 @@ def test_export_node():
              .add_property(name="a", value=SyncNode(db.Record()))
              .add_property(name="a", value=SyncNode(db.Record())))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
 
+    msg = (f"The problematic property is 'a' with values '[{SyncNode(db.Record())}]' "
+           f"and '[{SyncNode(db.Record())}]'")
+    assert msg in str(ime.value)
+
     # different SyncNode Objects with differing ID are not equal
     rec_a = (db.Record(id=101)
              .add_parent("B")
              .add_property(name="a", value=SyncNode(db.Record(id=1)))
              .add_property(name="a", value=SyncNode(db.Record(id=2))))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
 
+    msg = (f"The problematic property is 'a' with values '[{SyncNode(db.Record(id=1))}]' "
+           f"and '[{SyncNode(db.Record(id=2))}]'")
+    assert msg in str(ime.value)
+
     # SyncNodes with same ID are considered equal (list)
     rec_a = (db.Record(id=101)
              .add_parent("B")
@@ -297,9 +306,14 @@ def test_export_node():
              .add_property(name="a", value=[SyncNode(db.Record(id=1)), SyncNode(db.Record(id=2))])
              .add_property(name="a", value=[SyncNode(db.Record(id=2)), SyncNode(db.Record(id=1))]))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
 
+    msg = ("The problematic property is 'a' with values "
+           f"'{[SyncNode(db.Record(id=1)), SyncNode(db.Record(id=2))]}' "
+           f"and '{[SyncNode(db.Record(id=2)), SyncNode(db.Record(id=1))]}'")
+    assert msg in str(ime.value)
+
     # same SyncNode object is obviously equal (list)
     sn = SyncNode(db.Record(id=1))
     rec_a = (db.Record(id=101)
@@ -316,26 +330,37 @@ def test_export_node():
              .add_property(name="a", value=[SyncNode(db.Record())])
              .add_property(name="a", value=[SyncNode(db.Record())]))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
 
+    msg = ("The problematic property is 'a' with values "
+           f"'{[SyncNode(db.Record())]}' and '{[SyncNode(db.Record())]}'")
+    assert msg in str(ime.value)
+
     # different SyncNode Objects with differing are not equal (list)
     rec_a = (db.Record(id=101)
              .add_parent("B")
              .add_property(name="a", value=[SyncNode(db.Record(id=1))])
              .add_property(name="a", value=[SyncNode(db.Record(id=2))]))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
 
+    msg = ("The problematic property is 'a' with values "
+           f"'{[SyncNode(db.Record(id=1))]}' and '{[SyncNode(db.Record(id=2))]}'")
+    assert msg in str(ime.value)
+
     # list vs no list
     rec_a = (db.Record(id=101)
              .add_parent("B")
              .add_property(name="a", value=SyncNode(db.Record(id=1)))
              .add_property(name="a", value=[SyncNode(db.Record(id=1))]))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
+    msg = ("The problematic property is 'a' with values "
+           f"'[{SyncNode(db.Record(id=1))}]' and '{[SyncNode(db.Record(id=1))]}'")
+    assert msg in str(ime.value)
 
     # different list sizes
     rec_a = (db.Record(id=101)
@@ -343,5 +368,10 @@ def test_export_node():
              .add_property(name="a", value=[SyncNode(db.Record(id=1))])
              .add_property(name="a", value=[SyncNode(db.Record(id=1)), SyncNode(db.Record(id=1))]))
 
-    with pytest.raises(ImpossibleMergeError):
+    with pytest.raises(ImpossibleMergeError) as ime:
         exp = SyncNode(rec_a).export_entity()
+
+    msg = ("The problematic property is 'a' with values "
+           f"'{[SyncNode(db.Record(id=1))]}' and "
+           f"'{[SyncNode(db.Record(id=1)), SyncNode(db.Record(id=1))]}'")
+    assert msg in str(ime.value)
-- 
GitLab