diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py index 6b43b382ab460d37af855f8359d5a6005312e84f..5768d2d8c0c9acdc9ec355b3f21c4fb5275a3711 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():