|
16 | 16 |
|
17 | 17 | """Tests for the schema module.""" |
18 | 18 |
|
| 19 | +import pytest |
19 | 20 | from pydantic import BaseModel, Field |
20 | 21 |
|
21 | 22 | from genkit.core.schema import to_json_schema |
@@ -70,3 +71,133 @@ def test_to_json_schema_already_schema(): |
70 | 71 | } |
71 | 72 |
|
72 | 73 | assert to_json_schema(json_schema) == json_schema |
| 74 | + |
| 75 | + |
| 76 | +# ============================================================================= |
| 77 | +# JSON Schema Specification-based Tests |
| 78 | +# See: https://json-schema.org/understanding-json-schema/reference/type |
| 79 | +# ============================================================================= |
| 80 | + |
| 81 | + |
| 82 | +class TestNullType: |
| 83 | + """Tests for null type as per JSON Schema spec. |
| 84 | +
|
| 85 | + See: https://json-schema.org/understanding-json-schema/reference/null |
| 86 | + """ |
| 87 | + |
| 88 | + def test_none_produces_null_type(self): |
| 89 | + """Python None should produce JSON Schema null type.""" |
| 90 | + assert to_json_schema(None) == {'type': 'null'} |
| 91 | + |
| 92 | + |
| 93 | +class TestStringType: |
| 94 | + """Tests for string type as per JSON Schema spec. |
| 95 | +
|
| 96 | + See: https://json-schema.org/understanding-json-schema/reference/string |
| 97 | + """ |
| 98 | + |
| 99 | + def test_str_type(self): |
| 100 | + """Python str type should produce JSON Schema string type.""" |
| 101 | + assert to_json_schema(str) == {'type': 'string'} |
| 102 | + |
| 103 | + |
| 104 | +class TestNumericTypes: |
| 105 | + """Tests for numeric types as per JSON Schema spec. |
| 106 | +
|
| 107 | + See: https://json-schema.org/understanding-json-schema/reference/numeric |
| 108 | + Note: JSON Schema has 'integer' and 'number' (floating point). |
| 109 | + """ |
| 110 | + |
| 111 | + @pytest.mark.parametrize( |
| 112 | + 'py_type, json_type_name', |
| 113 | + [ |
| 114 | + (int, 'integer'), |
| 115 | + (float, 'number'), |
| 116 | + ], |
| 117 | + ) |
| 118 | + def test_numeric_types(self, py_type, json_type_name): |
| 119 | + """Python numeric types should produce correct JSON Schema numeric types.""" |
| 120 | + assert to_json_schema(py_type) == {'type': json_type_name} |
| 121 | + |
| 122 | + |
| 123 | +class TestBooleanType: |
| 124 | + """Tests for boolean type as per JSON Schema spec. |
| 125 | +
|
| 126 | + See: https://json-schema.org/understanding-json-schema/reference/boolean |
| 127 | + """ |
| 128 | + |
| 129 | + def test_bool_type(self): |
| 130 | + """Python bool type should produce JSON Schema boolean type.""" |
| 131 | + assert to_json_schema(bool) == {'type': 'boolean'} |
| 132 | + |
| 133 | + |
| 134 | +class TestArrayType: |
| 135 | + """Tests for array type as per JSON Schema spec. |
| 136 | +
|
| 137 | + See: https://json-schema.org/understanding-json-schema/reference/array |
| 138 | + """ |
| 139 | + |
| 140 | + @pytest.mark.parametrize( |
| 141 | + 'list_type, item_schema', |
| 142 | + [ |
| 143 | + (list[str], {'type': 'string'}), |
| 144 | + (list[int], {'type': 'integer'}), |
| 145 | + ], |
| 146 | + ) |
| 147 | + def test_list_types(self, list_type, item_schema): |
| 148 | + """Python list types should produce array schema with correct item types.""" |
| 149 | + result = to_json_schema(list_type) |
| 150 | + assert result['type'] == 'array' |
| 151 | + assert result['items'] == item_schema |
| 152 | + |
| 153 | + |
| 154 | +class TestObjectType: |
| 155 | + """Tests for object type as per JSON Schema spec. |
| 156 | +
|
| 157 | + See: https://json-schema.org/understanding-json-schema/reference/object |
| 158 | + """ |
| 159 | + |
| 160 | + def test_dict_type(self): |
| 161 | + """Python dict should produce object schema.""" |
| 162 | + result = to_json_schema(dict) |
| 163 | + assert result['type'] == 'object' |
| 164 | + |
| 165 | + def test_pydantic_model(self): |
| 166 | + """Pydantic BaseModel should produce object schema with properties.""" |
| 167 | + |
| 168 | + class Person(BaseModel): |
| 169 | + name: str |
| 170 | + age: int |
| 171 | + |
| 172 | + result = to_json_schema(Person) |
| 173 | + assert result['type'] == 'object' |
| 174 | + assert 'properties' in result |
| 175 | + assert result['properties']['name']['type'] == 'string' |
| 176 | + assert result['properties']['age']['type'] == 'integer' |
| 177 | + assert result['required'] == ['name', 'age'] |
| 178 | + |
| 179 | + |
| 180 | +class TestPassthroughBehavior: |
| 181 | + """Tests for passthrough behavior when input is already a JSON Schema dict.""" |
| 182 | + |
| 183 | + @pytest.mark.parametrize( |
| 184 | + 'schema', |
| 185 | + [ |
| 186 | + {'type': 'string', 'minLength': 1}, |
| 187 | + { |
| 188 | + 'type': 'object', |
| 189 | + 'properties': { |
| 190 | + 'name': {'type': 'string'}, |
| 191 | + 'items': { |
| 192 | + 'type': 'array', |
| 193 | + 'items': {'type': 'integer'}, |
| 194 | + }, |
| 195 | + }, |
| 196 | + 'required': ['name'], |
| 197 | + }, |
| 198 | + ], |
| 199 | + ids=['simple_schema', 'complex_schema'], |
| 200 | + ) |
| 201 | + def test_passthrough_behavior(self, schema): |
| 202 | + """A dict representing a JSON Schema should be returned as-is.""" |
| 203 | + assert to_json_schema(schema) == schema |
0 commit comments