Separates PR for Better Testing and Readability

This commit is contained in:
2025-08-19 18:39:37 -03:00
parent 9797fb35d9
commit cc6f2d42d5
6 changed files with 100 additions and 173 deletions

View File

@@ -1,13 +1,12 @@
from jambo.parser import ConstTypeParser
from typing_extensions import Annotated, Literal, get_args, get_origin
from typing_extensions import Annotated, get_args, get_origin
from unittest import TestCase
class TestConstTypeParser(TestCase):
def test_const_type_parser_hashable_value(self):
"""Test const parser with hashable values (uses Literal)"""
def test_const_type_parser(self):
parser = ConstTypeParser()
expected_const_value = "United States of America"
@@ -17,60 +16,8 @@ class TestConstTypeParser(TestCase):
"country", properties
)
# Check that we get a Literal type for hashable values
self.assertEqual(get_origin(parsed_type), Literal)
self.assertEqual(get_args(parsed_type), (expected_const_value,))
self.assertEqual(parsed_properties["default"], expected_const_value)
def test_const_type_parser_non_hashable_value(self):
"""Test const parser with non-hashable values (uses Annotated with validator)"""
parser = ConstTypeParser()
expected_const_value = [1, 2, 3] # Lists are not hashable
properties = {"const": expected_const_value}
parsed_type, parsed_properties = parser.from_properties_impl(
"list_const", properties
)
# Check that we get an Annotated type for non-hashable values
self.assertEqual(get_origin(parsed_type), Annotated)
self.assertIn(list, get_args(parsed_type))
self.assertEqual(parsed_properties["default"], expected_const_value)
def test_const_type_parser_integer_value(self):
"""Test const parser with integer values (uses Literal)"""
parser = ConstTypeParser()
expected_const_value = 42
properties = {"const": expected_const_value}
parsed_type, parsed_properties = parser.from_properties_impl(
"int_const", properties
)
# Check that we get a Literal type for hashable values
self.assertEqual(get_origin(parsed_type), Literal)
self.assertEqual(get_args(parsed_type), (expected_const_value,))
self.assertEqual(parsed_properties["default"], expected_const_value)
def test_const_type_parser_boolean_value(self):
"""Test const parser with boolean values (uses Literal)"""
parser = ConstTypeParser()
expected_const_value = True
properties = {"const": expected_const_value}
parsed_type, parsed_properties = parser.from_properties_impl(
"bool_const", properties
)
# Check that we get a Literal type for hashable values
self.assertEqual(get_origin(parsed_type), Literal)
self.assertEqual(get_args(parsed_type), (expected_const_value,))
self.assertIn(str, get_args(parsed_type))
self.assertEqual(parsed_properties["default"], expected_const_value)
@@ -99,4 +46,4 @@ class TestConstTypeParser(TestCase):
self.assertIn(
"Const type invalid_country must have 'const' value of allowed types",
str(context.exception),
)
)

View File

@@ -97,7 +97,7 @@ class TestOneOfTypeParser(TestCase):
"email": {"type": "string", "format": "email"}
},
"required": ["email"],
"additionalProperties": False
"additionalProperties": False,
},
{
"type": "object",
@@ -105,8 +105,8 @@ class TestOneOfTypeParser(TestCase):
"phone": {"type": "string", "pattern": "^[0-9-]+$"}
},
"required": ["phone"],
"additionalProperties": False
}
"additionalProperties": False,
},
]
},
},
@@ -135,25 +135,23 @@ class TestOneOfTypeParser(TestCase):
"type": "object",
"properties": {
"type": {"const": "cat"},
"meows": {"type": "boolean"}
"meows": {"type": "boolean"},
},
"required": ["type", "meows"]
"required": ["type", "meows"],
},
{
"type": "object",
"properties": {
"type": {"const": "dog"},
"barks": {"type": "boolean"}
"barks": {"type": "boolean"},
},
"required": ["type", "barks"]
}
"required": ["type", "barks"],
},
],
"discriminator": {
"propertyName": "type"
}
"discriminator": {"propertyName": "type"},
}
},
"required": ["pet"]
"required": ["pet"],
}
Model = SchemaConverter.build(schema)
@@ -183,29 +181,33 @@ class TestOneOfTypeParser(TestCase):
"type": "object",
"properties": {
"vehicle_type": {"const": "car"},
"doors": {"type": "integer", "minimum": 2, "maximum": 4}
"doors": {
"type": "integer",
"minimum": 2,
"maximum": 4,
},
},
"required": ["vehicle_type", "doors"]
"required": ["vehicle_type", "doors"],
},
{
"type": "object",
"properties": {
"vehicle_type": {"const": "motorcycle"},
"engine_size": {"type": "number", "minimum": 125}
"engine_size": {"type": "number", "minimum": 125},
},
"required": ["vehicle_type", "engine_size"]
}
"required": ["vehicle_type", "engine_size"],
},
],
"discriminator": {
"propertyName": "vehicle_type",
"mapping": {
"car": "#/properties/vehicle/oneOf/0",
"motorcycle": "#/properties/vehicle/oneOf/1"
}
}
"motorcycle": "#/properties/vehicle/oneOf/1",
},
},
}
},
"required": ["vehicle"]
"required": ["vehicle"],
}
Model = SchemaConverter.build(schema)
@@ -229,25 +231,23 @@ class TestOneOfTypeParser(TestCase):
"type": "object",
"properties": {
"type": {"const": "circle"},
"radius": {"type": "number", "minimum": 0}
"radius": {"type": "number", "minimum": 0},
},
"required": ["type", "radius"]
"required": ["type", "radius"],
},
{
"type": "object",
"properties": {
"type": {"const": "square"},
"side": {"type": "number", "minimum": 0}
"side": {"type": "number", "minimum": 0},
},
"required": ["type", "side"]
}
"required": ["type", "side"],
},
],
"discriminator": {
"propertyName": "type"
}
"discriminator": {"propertyName": "type"},
}
},
"required": ["shape"]
"required": ["shape"],
}
Model = SchemaConverter.build(schema)
@@ -283,9 +283,7 @@ class TestOneOfTypeParser(TestCase):
"title": "Test",
"type": "object",
"properties": {
"value": {
"oneOf": None
},
"value": {"oneOf": None},
},
}
@@ -302,7 +300,7 @@ class TestOneOfTypeParser(TestCase):
{"type": "string"},
{"type": "integer"},
],
"default": "test"
"default": "test",
},
},
}
@@ -321,7 +319,7 @@ class TestOneOfTypeParser(TestCase):
{"type": "string", "minLength": 5},
{"type": "integer", "minimum": 10},
],
"default": "hi"
"default": "hi",
},
},
}
@@ -340,20 +338,20 @@ class TestOneOfTypeParser(TestCase):
"type": "object",
"properties": {
"type": {"const": "a"},
"value": {"type": "string"}
}
"value": {"type": "string"},
},
},
{
"type": "object",
"properties": {
"type": {"const": "b"},
"value": {"type": "integer"}
}
}
"value": {"type": "integer"},
},
},
],
"discriminator": {} # discriminator without propertyName
"discriminator": {}, # discriminator without propertyName
}
}
},
}
Model = SchemaConverter.build(schema)
@@ -383,23 +381,18 @@ class TestOneOfTypeParser(TestCase):
"properties": {
"value": {
"oneOf": [
{
"type": "object",
"properties": {
"data": {"type": "string"}
}
},
{"type": "object", "properties": {"data": {"type": "string"}}},
{
"type": "object",
"properties": {
"data": {"type": "string"},
"optional": {"type": "string"}
}
}
"optional": {"type": "string"},
},
},
],
"discriminator": {} # discriminator without propertyName
"discriminator": {}, # discriminator without propertyName
}
}
},
}
Model = SchemaConverter.build(schema)
@@ -419,11 +412,11 @@ class TestOneOfTypeParser(TestCase):
"value": {
"oneOf": [
{"type": "string", "maxLength": 6},
{"type": "string", "minLength": 4}
{"type": "string", "minLength": 4},
]
}
},
"required": ["value"]
"required": ["value"],
}
Model = SchemaConverter.build(schema)
@@ -453,26 +446,24 @@ class TestOneOfTypeParser(TestCase):
"type": "object",
"properties": {
"type": {"const": "circle"},
"radius": {"type": "number", "minimum": 0}
"radius": {"type": "number", "minimum": 0},
},
"required": ["type", "radius"]
"required": ["type", "radius"],
},
{
"type": "object",
"properties": {
"type": {"const": "rectangle"},
"width": {"type": "number", "minimum": 0},
"height": {"type": "number", "minimum": 0}
"height": {"type": "number", "minimum": 0},
},
"required": ["type", "width", "height"]
}
"required": ["type", "width", "height"],
},
],
"discriminator": {
"propertyName": "type"
}
"discriminator": {"propertyName": "type"},
}
},
"required": ["shape"]
"required": ["shape"],
}
Model = SchemaConverter.build(schema)