Implements Feature Complete AnyOf Keyword

This commit is contained in:
2025-04-19 05:57:43 -03:00
parent 5fdb4fa724
commit 5c3d3a39ba
17 changed files with 133 additions and 90 deletions

View File

@@ -1,5 +1,7 @@
from jambo.parser.anyof_type_parser import AnyOfTypeParser
from typing_extensions import Annotated
from typing import Union, get_args, get_origin
from unittest import TestCase
@@ -21,5 +23,41 @@ class TestAnyOfTypeParser(TestCase):
# check union type has string and int
self.assertEqual(get_origin(type_parsing), Union)
self.assertIn(str, get_args(type_parsing))
self.assertIn(int, get_args(type_parsing))
type_1, type_2 = get_args(type_parsing)
self.assertEqual(get_origin(type_1), Annotated)
self.assertIn(str, get_args(type_1))
self.assertEqual(get_origin(type_2), Annotated)
self.assertIn(int, get_args(type_2))
def test_any_of_string_or_int_with_default(self):
"""
Tests the AnyOfTypeParser with a string or int type and a default value.
"""
properties = {
"anyOf": [
{"type": "string"},
{"type": "integer"},
],
"default": 42,
}
type_parsing, type_validator = AnyOfTypeParser.from_properties(
"placeholder", properties
)
# check union type has string and int
self.assertEqual(get_origin(type_parsing), Union)
type_1, type_2 = get_args(type_parsing)
self.assertEqual(get_origin(type_1), Annotated)
self.assertIn(str, get_args(type_1))
self.assertEqual(get_origin(type_2), Annotated)
self.assertIn(int, get_args(type_2))
self.assertEqual(type_validator["default"], 42)

View File

@@ -12,7 +12,7 @@ class TestBoolTypeParser(TestCase):
type_parsing, type_validator = parser.from_properties("placeholder", properties)
self.assertEqual(type_parsing, bool)
self.assertEqual(type_validator, {})
self.assertEqual(type_validator, {"default": None})
def test_bool_parser_with_default(self):
parser = BooleanTypeParser()

View File

@@ -12,7 +12,7 @@ class TestFloatTypeParser(TestCase):
type_parsing, type_validator = parser.from_properties("placeholder", properties)
self.assertEqual(type_parsing, float)
self.assertEqual(type_validator, {})
self.assertEqual(type_validator, {"default": None})
def test_float_parser_with_options(self):
parser = FloatTypeParser()

View File

@@ -12,7 +12,7 @@ class TestIntTypeParser(TestCase):
type_parsing, type_validator = parser.from_properties("placeholder", properties)
self.assertEqual(type_parsing, int)
self.assertEqual(type_validator, {})
self.assertEqual(type_validator, {"default": None})
def test_int_parser_with_options(self):
parser = IntTypeParser()

View File

@@ -57,14 +57,9 @@ class TestStringTypeParser(TestCase):
"minLength": 5,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value for placeholder must be a string, but got <int>.",
)
def test_string_parser_with_default_invalid_maxlength(self):
parser = StringTypeParser()
@@ -75,14 +70,9 @@ class TestStringTypeParser(TestCase):
"minLength": 1,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value for placeholder exceeds maxLength limit of 2",
)
def test_string_parser_with_default_invalid_minlength(self):
parser = StringTypeParser()
@@ -93,10 +83,5 @@ class TestStringTypeParser(TestCase):
"minLength": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value for placeholder is below minLength limit of 2",
)