From b0445eae69fea026279e70e5947eea734b0882af Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 24 Oct 2023 11:45:49 +0200
Subject: [PATCH] TST: mock execute_query in unittests

---
 unittests/test_json_schema_exporter.py | 42 ++++++++++++++++++--------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py
index 6b43b382..5768d2d8 100644
--- a/unittests/test_json_schema_exporter.py
+++ b/unittests/test_json_schema_exporter.py
@@ -24,10 +24,35 @@ import linkahead as db
 
 from jsonschema import FormatChecker, validate, ValidationError
 from pytest import raises
+from unittest.mock import Mock, patch
 
 from caosadvancedtools.json_schema_exporter import recordtype_to_json_schema as rtjs
 
 
+def _mock_execute_query(query_string, unique=False, **kwargs):
+    """Mock the response to queries for references."""
+    all_records = db.Container()
+    all_files = db.Container()
+    other_type_rt = db.RecordType(name="OtherType")
+    other_type_records = db.Container().extend([
+        db.Record(id=100, name="otherA").add_parent(other_type_rt),
+        db.Record(id=101, name="otherB").add_parent(other_type_rt),
+        db.Record(id=102).add_parent(other_type_rt)
+    ])
+    all_records.extend(other_type_records)
+
+    if query_string == "FIND RECORD 'OtherType'":
+        return other_type_records
+    elif query_string == "FIND RECORDTYPE 'OtherType'" and unique is True:
+        return other_type_rt
+    elif query_string == "FIND RECORD":
+        return all_records
+    elif query_string == "FIND FILE":
+        return all_files
+    else:
+        return db.Container()
+
+
 def test_empty_rt():
 
     rt = db.RecordType(name="Test", description="descr")
@@ -243,6 +268,7 @@ def test_rt_with_list_props():
         validate(example, schema, format_checker=FormatChecker())
 
 
+@patch("linkahead.execute_query", new=Mock(side_effect=_mock_execute_query))
 def test_rt_with_references():
     """References and lists of references will come later, so test if the errors
     are thrown correctly.
@@ -252,30 +278,22 @@ def test_rt_with_references():
     rt = db.RecordType()
     rt.add_property(name="RefProp", datatype=db.REFERENCE)
 
-    with raises(NotImplementedError):
-
-        rtjs(rt)
+    schema = rtjs(rt)
 
     rt = db.RecordType()
     rt.add_property(name="RefProp", datatype="OtherType")
 
-    with raises(NotImplementedError):
-
-        rtjs(rt)
+    schema = rtjs(rt)
 
     rt = db.RecordType()
     rt.add_property(name="RefProp", datatype=db.LIST(db.REFERENCE))
 
-    with raises(NotImplementedError):
-
-        rtjs(rt)
+    schema = rtjs(rt)
 
     rt = db.RecordType()
     rt.add_property(name="RefProp", datatype=db.LIST("OtherType"))
 
-    with raises(NotImplementedError):
-
-        rtjs(rt)
+    schema = rtjs(rt)
 
 
 def test_broken():
-- 
GitLab