|
| 1 | +"""Tests for schema helper functions in tools.py. |
| 2 | +
|
| 3 | +Tests for _resolve_ref and _is_complex_anyof functions that handle |
| 4 | +JSON schema resolution and complex type detection. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from mellea.backends.tools import _is_complex_anyof, _resolve_ref |
| 10 | + |
| 11 | + |
| 12 | +class TestResolveRef: |
| 13 | + """Tests for the _resolve_ref helper function.""" |
| 14 | + |
| 15 | + def test_resolve_defs_style_ref(self): |
| 16 | + """Test resolving #/$defs/ style references.""" |
| 17 | + defs = { |
| 18 | + "Email": { |
| 19 | + "type": "object", |
| 20 | + "properties": {"to": {"type": "string"}, "subject": {"type": "string"}}, |
| 21 | + } |
| 22 | + } |
| 23 | + result = _resolve_ref("#/$defs/Email", defs) |
| 24 | + assert result == defs["Email"] |
| 25 | + |
| 26 | + def test_resolve_definitions_style_ref(self): |
| 27 | + """Test resolving #/definitions/ style references.""" |
| 28 | + defs = { |
| 29 | + "Address": { |
| 30 | + "type": "object", |
| 31 | + "properties": { |
| 32 | + "street": {"type": "string"}, |
| 33 | + "city": {"type": "string"}, |
| 34 | + }, |
| 35 | + } |
| 36 | + } |
| 37 | + result = _resolve_ref("#/definitions/Address", defs) |
| 38 | + assert result == defs["Address"] |
| 39 | + |
| 40 | + def test_resolve_missing_ref_returns_empty_dict(self): |
| 41 | + """Test that resolving a non-existent ref returns empty dict.""" |
| 42 | + defs = {"Email": {"type": "object"}} |
| 43 | + result = _resolve_ref("#/$defs/NotFound", defs) |
| 44 | + assert result == {} |
| 45 | + |
| 46 | + def test_resolve_invalid_ref_format_returns_empty_dict(self): |
| 47 | + """Test that invalid ref format returns empty dict.""" |
| 48 | + defs = {"Email": {"type": "object"}} |
| 49 | + result = _resolve_ref("#/invalid/Email", defs) |
| 50 | + assert result == {} |
| 51 | + |
| 52 | + def test_resolve_with_empty_defs(self): |
| 53 | + """Test resolving against empty defs dict.""" |
| 54 | + result = _resolve_ref("#/$defs/Email", {}) |
| 55 | + assert result == {} |
| 56 | + |
| 57 | + def test_resolve_nested_ref_name(self): |
| 58 | + """Test resolving refs with nested-like names.""" |
| 59 | + defs = { |
| 60 | + "User_v2": {"type": "object", "properties": {"id": {"type": "integer"}}} |
| 61 | + } |
| 62 | + result = _resolve_ref("#/$defs/User_v2", defs) |
| 63 | + assert result == defs["User_v2"] |
| 64 | + |
| 65 | + |
| 66 | +class TestIsComplexAnyof: |
| 67 | + """Tests for the _is_complex_anyof helper function.""" |
| 68 | + |
| 69 | + def test_simple_optional_primitive_not_complex(self): |
| 70 | + """Test that Optional[str] (anyOf with null and string) is not complex.""" |
| 71 | + schema = {"anyOf": [{"type": "string"}, {"type": "null"}]} |
| 72 | + assert _is_complex_anyof(schema) is False |
| 73 | + |
| 74 | + def test_optional_int_not_complex(self): |
| 75 | + """Test that Optional[int] is not complex.""" |
| 76 | + schema = {"anyOf": [{"type": "integer"}, {"type": "null"}]} |
| 77 | + assert _is_complex_anyof(schema) is False |
| 78 | + |
| 79 | + def test_anyof_with_ref_is_complex(self): |
| 80 | + """Test that anyOf with a $ref is complex.""" |
| 81 | + schema = {"anyOf": [{"$ref": "#/$defs/Email"}, {"type": "null"}]} |
| 82 | + assert _is_complex_anyof(schema) is True |
| 83 | + |
| 84 | + def test_anyof_with_nested_object_is_complex(self): |
| 85 | + """Test that anyOf with nested object (properties) is complex.""" |
| 86 | + schema = { |
| 87 | + "anyOf": [ |
| 88 | + { |
| 89 | + "type": "object", |
| 90 | + "properties": { |
| 91 | + "name": {"type": "string"}, |
| 92 | + "age": {"type": "integer"}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + {"type": "null"}, |
| 96 | + ] |
| 97 | + } |
| 98 | + assert _is_complex_anyof(schema) is True |
| 99 | + |
| 100 | + def test_anyof_with_multiple_types_no_ref_not_complex(self): |
| 101 | + """Test that anyOf with multiple primitives (no ref/props) is not complex.""" |
| 102 | + schema = {"anyOf": [{"type": "string"}, {"type": "integer"}, {"type": "null"}]} |
| 103 | + assert _is_complex_anyof(schema) is False |
| 104 | + |
| 105 | + def test_anyof_with_ref_and_primitive_is_complex(self): |
| 106 | + """Test that anyOf with both ref and primitive is complex.""" |
| 107 | + schema = {"anyOf": [{"$ref": "#/$defs/Email"}, {"type": "string"}]} |
| 108 | + assert _is_complex_anyof(schema) is True |
| 109 | + |
| 110 | + def test_anyof_with_only_null_not_complex(self): |
| 111 | + """Test that anyOf with only null type is not complex.""" |
| 112 | + schema = {"anyOf": [{"type": "null"}]} |
| 113 | + assert _is_complex_anyof(schema) is False |
| 114 | + |
| 115 | + def test_empty_anyof_not_complex(self): |
| 116 | + """Test that empty anyOf is not complex.""" |
| 117 | + schema = {"anyOf": []} |
| 118 | + assert _is_complex_anyof(schema) is False |
| 119 | + |
| 120 | + def test_anyof_missing_from_schema_not_complex(self): |
| 121 | + """Test that schema without anyOf is not complex.""" |
| 122 | + schema = {"type": "object"} |
| 123 | + assert _is_complex_anyof(schema) is False |
| 124 | + |
| 125 | + def test_anyof_with_allof_is_complex(self): |
| 126 | + """Test that anyOf containing allOf is complex (has properties-like structure).""" |
| 127 | + schema = { |
| 128 | + "anyOf": [ |
| 129 | + { |
| 130 | + "allOf": [ |
| 131 | + {"$ref": "#/$defs/Base"}, |
| 132 | + {"properties": {"extra": {"type": "string"}}}, |
| 133 | + ] |
| 134 | + }, |
| 135 | + {"type": "null"}, |
| 136 | + ] |
| 137 | + } |
| 138 | + # Note: Our implementation checks for $ref or properties in the sub_schema, |
| 139 | + # not recursively in allOf, so this should be not complex |
| 140 | + # (unless allOf itself has properties) |
| 141 | + assert _is_complex_anyof(schema) is False |
| 142 | + |
| 143 | + def test_anyof_union_with_ref(self): |
| 144 | + """Test anyOf representing a union of multiple types including ref.""" |
| 145 | + schema = {"anyOf": [{"$ref": "#/$defs/User"}, {"$ref": "#/$defs/Admin"}]} |
| 146 | + assert _is_complex_anyof(schema) is True |
| 147 | + |
| 148 | + def test_complex_nested_structure(self): |
| 149 | + """Test complex nested object with all optional fields.""" |
| 150 | + schema = { |
| 151 | + "anyOf": [ |
| 152 | + { |
| 153 | + "type": "object", |
| 154 | + "properties": { |
| 155 | + "user": { |
| 156 | + "type": "object", |
| 157 | + "properties": {"name": {"type": "string"}}, |
| 158 | + } |
| 159 | + }, |
| 160 | + }, |
| 161 | + {"type": "null"}, |
| 162 | + ] |
| 163 | + } |
| 164 | + assert _is_complex_anyof(schema) is True |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + pytest.main([__file__]) |
0 commit comments