feat: fixes tests to validate the type of exception thrown

This commit is contained in:
2025-09-14 00:47:24 -03:00
parent 30290771b1
commit e31002af32
17 changed files with 179 additions and 124 deletions

View File

@@ -1,5 +1,8 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser.allof_type_parser import AllOfTypeParser
from pydantic import ValidationError
from unittest import TestCase
@@ -42,13 +45,13 @@ class TestAllOfTypeParser(TestCase):
"placeholder", properties
)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
type_parsing(name="John", age=101)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
type_parsing(name="", age=30)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
type_parsing(name="John Invalid", age=30)
obj = type_parsing(name="John", age=30)
@@ -87,10 +90,10 @@ class TestAllOfTypeParser(TestCase):
"placeholder", properties
)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
type_parsing(name="John")
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
type_parsing(age=30)
obj = type_parsing(name="John", age=30)
@@ -154,7 +157,7 @@ class TestAllOfTypeParser(TestCase):
]
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AllOfTypeParser().from_properties("placeholder", properties)
def test_all_of_invalid_type_not_present(self):
@@ -167,7 +170,7 @@ class TestAllOfTypeParser(TestCase):
]
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AllOfTypeParser().from_properties("placeholder", properties)
def test_all_of_invalid_type_in_fields(self):
@@ -180,7 +183,7 @@ class TestAllOfTypeParser(TestCase):
]
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AllOfTypeParser().from_properties("placeholder", properties)
def test_all_of_invalid_type_not_all_equal(self):
@@ -196,7 +199,7 @@ class TestAllOfTypeParser(TestCase):
]
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AllOfTypeParser().from_properties("placeholder", properties)
def test_all_of_description_field(self):
@@ -304,5 +307,5 @@ class TestAllOfTypeParser(TestCase):
],
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AllOfTypeParser().from_properties("placeholder", properties)

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser.anyof_type_parser import AnyOfTypeParser
from typing_extensions import Annotated, Union, get_args, get_origin
@@ -14,7 +15,7 @@ class TestAnyOfTypeParser(TestCase):
],
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AnyOfTypeParser().from_properties("placeholder", properties)
def test_any_of_with_invalid_properties(self):
@@ -22,7 +23,7 @@ class TestAnyOfTypeParser(TestCase):
"anyOf": None,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AnyOfTypeParser().from_properties("placeholder", properties)
def test_any_of_string_or_int(self):
@@ -95,5 +96,5 @@ class TestAnyOfTypeParser(TestCase):
"default": 3.14,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
AnyOfTypeParser().from_properties("placeholder", properties)

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import ArrayTypeParser
from typing_extensions import get_args
@@ -67,7 +68,7 @@ class TestArrayTypeParser(TestCase):
properties = {"items": {"type": "string"}, "default": ["a", 1, "c"]}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_array_parser_with_invalid_default_type(self):
@@ -75,15 +76,15 @@ class TestArrayTypeParser(TestCase):
properties = {"items": {"type": "string"}, "default": 000}
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties=properties)
def test_array_parser_with_invalid_default_min(self):
parser = ArrayTypeParser()
properties = {"items": {"type": "string"}, "default": ["a"], "minItems": 2}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_array_parser_with_invalid_default_max(self):
@@ -95,5 +96,5 @@ class TestArrayTypeParser(TestCase):
"maxItems": 3,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import BooleanTypeParser
from unittest import TestCase
@@ -39,5 +40,5 @@ class TestBoolTypeParser(TestCase):
"default": "invalid",
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties_impl("placeholder", properties)

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import ConstTypeParser
from typing_extensions import Annotated, Literal, get_args, get_origin
@@ -80,7 +81,7 @@ class TestConstTypeParser(TestCase):
expected_const_value = "United States of America"
properties = {"notConst": expected_const_value}
with self.assertRaises(ValueError) as context:
with self.assertRaises(InvalidSchemaException) as context:
parser.from_properties_impl("invalid_country", properties)
self.assertIn(
@@ -93,7 +94,7 @@ class TestConstTypeParser(TestCase):
properties = {"const": {}}
with self.assertRaises(ValueError) as context:
with self.assertRaises(InvalidSchemaException) as context:
parser.from_properties_impl("invalid_country", properties)
self.assertIn(

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import EnumTypeParser
from enum import Enum
@@ -10,7 +11,7 @@ class TestEnumTypeParser(TestCase):
schema = {}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parsed_type, parsed_properties = parser.from_properties_impl(
"TestEnum",
schema,
@@ -23,7 +24,7 @@ class TestEnumTypeParser(TestCase):
"enum": "not_a_list",
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parsed_type, parsed_properties = parser.from_properties_impl(
"TestEnum",
schema,
@@ -86,5 +87,5 @@ class TestEnumTypeParser(TestCase):
"enum": ["value1", 42, dict()],
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties_impl("TestEnum", schema)

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import FloatTypeParser
from unittest import TestCase
@@ -61,7 +62,7 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_float_parser_with_default_invalid_maximum(self):
@@ -75,7 +76,7 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_float_parser_with_default_invalid_minimum(self):
@@ -89,7 +90,7 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_float_parser_with_default_invalid_exclusive_maximum(self):
@@ -103,7 +104,7 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_float_parser_with_default_invalid_exclusive_minimum(self):
@@ -117,7 +118,7 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_float_parser_with_default_invalid_multiple(self):
@@ -131,5 +132,5 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 2.0,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import IntTypeParser
from unittest import TestCase
@@ -61,7 +62,7 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_int_parser_with_default_invalid_maximum(self):
@@ -75,7 +76,7 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_int_parser_with_default_invalid_minimum(self):
@@ -89,7 +90,7 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_int_parser_with_default_invalid_exclusive_maximum(self):
@@ -103,7 +104,7 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_int_parser_with_default_invalid_exclusive_minimum(self):
@@ -117,7 +118,7 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_int_parser_with_default_invalid_multipleOf(self):
@@ -131,5 +132,5 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)

View File

@@ -1,12 +1,15 @@
from jambo import SchemaConverter
from jambo.exceptions import InvalidSchemaException
from jambo.parser.oneof_type_parser import OneOfTypeParser
from pydantic import ValidationError
from unittest import TestCase
class TestOneOfTypeParser(TestCase):
def test_oneof_raises_on_invalid_property(self):
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
OneOfTypeParser().from_properties_impl(
"test_field",
{
@@ -17,7 +20,7 @@ class TestOneOfTypeParser(TestCase):
ref_cache={},
)
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(
{
"title": "Test",
@@ -71,13 +74,13 @@ class TestOneOfTypeParser(TestCase):
Model = SchemaConverter.build(schema)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(id=-5)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(id="invalid")
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(id=123.45)
def test_oneof_with_conflicting_schemas(self):
@@ -103,11 +106,11 @@ class TestOneOfTypeParser(TestCase):
obj2 = Model(data=9)
self.assertEqual(obj2.data, 9)
with self.assertRaises(ValueError) as cm:
with self.assertRaises(ValidationError) as cm:
Model(data=6)
self.assertIn("matches multiple oneOf schemas", str(cm.exception))
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(data=5)
def test_oneof_with_objects(self):
@@ -147,7 +150,7 @@ class TestOneOfTypeParser(TestCase):
obj2 = Model(contact_info={"phone": "123-456-7890"})
self.assertEqual(obj2.contact_info.phone, "123-456-7890")
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(contact_info={"email": "user@example.com", "phone": "123-456-7890"})
def test_oneof_with_discriminator_basic(self):
@@ -190,14 +193,14 @@ class TestOneOfTypeParser(TestCase):
self.assertEqual(dog.pet.type, "dog")
self.assertEqual(dog.pet.barks, False)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(pet={"type": "cat", "barks": True})
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(pet={"type": "bird", "flies": True})
def test_oneof_with_invalid_types(self):
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(
{
"title": "Pet",
@@ -301,13 +304,13 @@ class TestOneOfTypeParser(TestCase):
Model = SchemaConverter.build(schema)
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(shape={"type": "triangle", "base": 5, "height": 3})
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(shape={"type": "circle", "side": 5})
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(shape={"radius": 5})
def test_oneof_missing_properties(self):
@@ -324,7 +327,7 @@ class TestOneOfTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(schema)
def test_oneof_invalid_properties(self):
@@ -336,7 +339,7 @@ class TestOneOfTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(schema)
def test_oneof_with_default_value(self):
@@ -373,12 +376,12 @@ class TestOneOfTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(schema)
def test_oneof_discriminator_without_property_name(self):
# Should throw because the spec determines propertyName is required for discriminator
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(
{
"title": "Test",
@@ -409,7 +412,7 @@ class TestOneOfTypeParser(TestCase):
def test_oneof_discriminator_with_invalid_discriminator(self):
# Should throw because a valid discriminator is required
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
SchemaConverter.build(
{
"title": "Test",
@@ -465,8 +468,9 @@ class TestOneOfTypeParser(TestCase):
self.assertEqual(obj2.value, "very long string")
# Invalid: Medium string (matches BOTH schemas - violates oneOf)
with self.assertRaises(ValueError) as cm:
with self.assertRaises(ValidationError) as cm:
Model(value="hello") # 5 chars: matches maxLength=6 AND minLength=4
self.assertIn("matches multiple oneOf schemas", str(cm.exception))
def test_oneof_shapes_discriminator_from_docs(self):
@@ -515,5 +519,5 @@ class TestOneOfTypeParser(TestCase):
self.assertEqual(rectangle.shape.height, 20)
# Invalid: Wrong properties for the type
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
Model(shape={"type": "circle", "width": 10})

View File

@@ -1,5 +1,8 @@
from jambo.exceptions import InternalAssertionException, InvalidSchemaException
from jambo.parser import ObjectTypeParser, RefTypeParser
from pydantic import ValidationError
from typing import ForwardRef
from unittest import TestCase
@@ -16,7 +19,7 @@ class TestRefTypeParser(TestCase):
"required": ["name", "age"],
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
RefTypeParser().from_properties(
"person",
properties,
@@ -40,7 +43,7 @@ class TestRefTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InternalAssertionException):
RefTypeParser().from_properties(
"person",
properties,
@@ -63,7 +66,7 @@ class TestRefTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InternalAssertionException):
RefTypeParser().from_properties(
"person",
properties,
@@ -77,7 +80,7 @@ class TestRefTypeParser(TestCase):
"$ref": "https://example.com/schemas/person.json",
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
RefTypeParser().from_properties(
"person",
properties,
@@ -110,7 +113,7 @@ class TestRefTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
ObjectTypeParser().from_properties(
"person",
properties,
@@ -126,7 +129,7 @@ class TestRefTypeParser(TestCase):
"$defs": {},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
RefTypeParser().from_properties(
"person",
properties,
@@ -142,7 +145,7 @@ class TestRefTypeParser(TestCase):
"$defs": {"person": None},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
RefTypeParser().from_properties(
"person",
properties,
@@ -232,7 +235,7 @@ class TestRefTypeParser(TestCase):
"required": ["name", "age"],
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
ObjectTypeParser().from_properties(
"person",
properties,
@@ -264,7 +267,7 @@ class TestRefTypeParser(TestCase):
)
# checks if when created via FowardRef the model is validated correctly.
with self.assertRaises(ValueError):
with self.assertRaises(ValidationError):
model(
name="John",
age=30,
@@ -421,7 +424,7 @@ class TestRefTypeParser(TestCase):
},
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
ref_strategy, ref_name, ref_property = RefTypeParser()._parse_from_strategy(
"invalid_strategy",
"person",

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import StringTypeParser
from pydantic import AnyUrl, EmailStr
@@ -62,7 +63,7 @@ class TestStringTypeParser(TestCase):
"minLength": 5,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_string_parser_with_default_invalid_maxlength(self):
@@ -75,7 +76,7 @@ class TestStringTypeParser(TestCase):
"minLength": 1,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_string_parser_with_default_invalid_minlength(self):
@@ -88,7 +89,7 @@ class TestStringTypeParser(TestCase):
"minLength": 2,
}
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
parser.from_properties("placeholder", properties)
def test_string_parser_with_email_format(self):
@@ -183,7 +184,7 @@ class TestStringTypeParser(TestCase):
"format": "unsupported-format",
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(InvalidSchemaException) as context:
parser.from_properties("placeholder", properties)
self.assertEqual(

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser import StringTypeParser
from jambo.parser._type_parser import GenericTypeParser
@@ -17,5 +18,5 @@ class TestGenericTypeParser(TestCase):
StringTypeParser.json_schema_type = "type:string"
def test_get_impl_invalid_type(self):
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSchemaException):
GenericTypeParser._get_impl({"type": "invalid_type"})