Skip to content
Snippets Groups Projects
Commit b0445eae authored by florian's avatar florian
Browse files

TST: mock execute_query in unittests

parent 15811aba
No related branches found
No related tags found
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!81F schema export references
...@@ -24,10 +24,35 @@ import linkahead as db ...@@ -24,10 +24,35 @@ import linkahead as db
from jsonschema import FormatChecker, validate, ValidationError from jsonschema import FormatChecker, validate, ValidationError
from pytest import raises from pytest import raises
from unittest.mock import Mock, patch
from caosadvancedtools.json_schema_exporter import recordtype_to_json_schema as rtjs 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(): def test_empty_rt():
rt = db.RecordType(name="Test", description="descr") rt = db.RecordType(name="Test", description="descr")
...@@ -243,6 +268,7 @@ def test_rt_with_list_props(): ...@@ -243,6 +268,7 @@ def test_rt_with_list_props():
validate(example, schema, format_checker=FormatChecker()) validate(example, schema, format_checker=FormatChecker())
@patch("linkahead.execute_query", new=Mock(side_effect=_mock_execute_query))
def test_rt_with_references(): def test_rt_with_references():
"""References and lists of references will come later, so test if the errors """References and lists of references will come later, so test if the errors
are thrown correctly. are thrown correctly.
...@@ -252,30 +278,22 @@ def test_rt_with_references(): ...@@ -252,30 +278,22 @@ def test_rt_with_references():
rt = db.RecordType() rt = db.RecordType()
rt.add_property(name="RefProp", datatype=db.REFERENCE) rt.add_property(name="RefProp", datatype=db.REFERENCE)
with raises(NotImplementedError): schema = rtjs(rt)
rtjs(rt)
rt = db.RecordType() rt = db.RecordType()
rt.add_property(name="RefProp", datatype="OtherType") rt.add_property(name="RefProp", datatype="OtherType")
with raises(NotImplementedError): schema = rtjs(rt)
rtjs(rt)
rt = db.RecordType() rt = db.RecordType()
rt.add_property(name="RefProp", datatype=db.LIST(db.REFERENCE)) rt.add_property(name="RefProp", datatype=db.LIST(db.REFERENCE))
with raises(NotImplementedError): schema = rtjs(rt)
rtjs(rt)
rt = db.RecordType() rt = db.RecordType()
rt.add_property(name="RefProp", datatype=db.LIST("OtherType")) rt.add_property(name="RefProp", datatype=db.LIST("OtherType"))
with raises(NotImplementedError): schema = rtjs(rt)
rtjs(rt)
def test_broken(): def test_broken():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment