Better Defaults Validation Implementation

This commit is contained in:
2025-04-19 15:44:27 -03:00
parent 5c3d3a39ba
commit c6a37dab74
8 changed files with 67 additions and 122 deletions

View File

@@ -66,38 +66,25 @@ class TestArrayTypeParser(TestCase):
properties = {"items": {"type": "string"}, "default": ["a", 1, "c"]}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"All items in the default list must be of type str",
)
def test_array_parser_with_invalid_default_type(self):
parser = ArrayTypeParser()
properties = {"items": {"type": "string"}, "default": "not_a_list"}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception), "Default value must be a list, got str"
)
def test_array_parser_with_invalid_default_min(self):
parser = ArrayTypeParser()
properties = {"items": {"type": "string"}, "default": ["a"], "minItems": 2}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception), "Default list is below minItems limit of 2"
)
def test_array_parser_with_invalid_default_max(self):
parser = ArrayTypeParser()
@@ -107,9 +94,5 @@ class TestArrayTypeParser(TestCase):
"maxItems": 3,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception), "Default list exceeds maxItems limit of 3"
)

View File

@@ -61,14 +61,9 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value must be a number, got str",
)
def test_float_parser_with_default_invalid_maximum(self):
parser = FloatTypeParser()
@@ -80,14 +75,9 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value exceeds maximum limit of 10.5",
)
def test_float_parser_with_default_invalid_minimum(self):
parser = FloatTypeParser()
@@ -99,14 +89,9 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value is below minimum limit of 1.0",
)
def test_float_parser_with_default_invalid_exclusive_maximum(self):
parser = FloatTypeParser()
@@ -118,14 +103,9 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value exceeds exclusive maximum limit of 10.5",
)
def test_float_parser_with_default_invalid_exclusive_minimum(self):
parser = FloatTypeParser()
@@ -137,14 +117,9 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 0.5,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value is below exclusive minimum limit of 1.0",
)
def test_float_parser_with_default_invalid_multiple(self):
parser = FloatTypeParser()
@@ -156,10 +131,5 @@ class TestFloatTypeParser(TestCase):
"multipleOf": 2.0,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value 5.0 is not a multiple of 2.0",
)

View File

@@ -61,14 +61,9 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value must be a number, got str",
)
def test_int_parser_with_default_invalid_maximum(self):
parser = IntTypeParser()
@@ -80,14 +75,9 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value exceeds maximum limit of 10",
)
def test_int_parser_with_default_invalid_minimum(self):
parser = IntTypeParser()
@@ -99,14 +89,9 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value is below minimum limit of 1",
)
def test_int_parser_with_default_invalid_exclusive_maximum(self):
parser = IntTypeParser()
@@ -118,14 +103,9 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value exceeds exclusive maximum limit of 10",
)
def test_int_parser_with_default_invalid_exclusive_minimum(self):
parser = IntTypeParser()
@@ -137,14 +117,9 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value is below exclusive minimum limit of 1",
)
def test_int_parser_with_default_invalid_multipleOf(self):
parser = IntTypeParser()
@@ -156,10 +131,5 @@ class TestIntTypeParser(TestCase):
"multipleOf": 2,
}
with self.assertRaises(ValueError) as context:
with self.assertRaises(ValueError):
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value 5 is not a multiple of 2",
)