diff --git a/jambo/utils/properties_builder/numeric_properties_builder.py b/jambo/utils/properties_builder/numeric_properties_builder.py index 0c52abe..f38dea1 100644 --- a/jambo/utils/properties_builder/numeric_properties_builder.py +++ b/jambo/utils/properties_builder/numeric_properties_builder.py @@ -22,22 +22,22 @@ def numeric_properties_builder(properties): f"Default value must be a number, got {type(default_value).__name__}" ) - if default_value >= properties.get("maximum", float("inf")): + if default_value > properties.get("maximum", float("inf")): raise ValueError( f"Default value exceeds maximum limit of {properties.get('maximum')}" ) - if default_value <= properties.get("minimum", float("-inf")): + if default_value < properties.get("minimum", float("-inf")): raise ValueError( f"Default value is below minimum limit of {properties.get('minimum')}" ) - if default_value > properties.get("exclusiveMaximum", float("inf")): + if default_value >= properties.get("exclusiveMaximum", float("inf")): raise ValueError( f"Default value exceeds exclusive maximum limit of {properties.get('exclusiveMaximum')}" ) - if default_value < properties.get("exclusiveMinimum", float("-inf")): + if default_value <= properties.get("exclusiveMinimum", float("-inf")): raise ValueError( f"Default value is below exclusive minimum limit of {properties.get('exclusiveMinimum')}" ) diff --git a/tests/parser/test_bool_type_parser.py b/tests/parser/test_bool_type_parser.py new file mode 100644 index 0000000..761ddc0 --- /dev/null +++ b/tests/parser/test_bool_type_parser.py @@ -0,0 +1,28 @@ +from unittest import TestCase + +from jambo.parser import BooleanTypeParser + + +class TestBoolTypeParser(TestCase): + def test_bool_parser_no_options(self): + parser = BooleanTypeParser() + + properties = {"type": "boolean"} + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, bool) + self.assertEqual(type_validator, {}) + + def test_bool_parser_with_default(self): + parser = BooleanTypeParser() + + properties = { + "type": "boolean", + "default": True, + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, bool) + self.assertEqual(type_validator["default"], True) diff --git a/tests/parser/test_float_type_parser.py b/tests/parser/test_float_type_parser.py new file mode 100644 index 0000000..c8e3ad5 --- /dev/null +++ b/tests/parser/test_float_type_parser.py @@ -0,0 +1,165 @@ +from unittest import TestCase + +from jambo.parser import FloatTypeParser + + +class TestFloatTypeParser(TestCase): + def test_float_parser_no_options(self): + parser = FloatTypeParser() + + properties = {"type": "number"} + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, float) + self.assertEqual(type_validator, {}) + + def test_float_parser_with_options(self): + parser = FloatTypeParser() + + properties = { + "type": "number", + "maximum": 10.5, + "minimum": 1.0, + "multipleOf": 0.5, + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, float) + self.assertEqual(type_validator["le"], 10.5) + self.assertEqual(type_validator["ge"], 1.0) + self.assertEqual(type_validator["multiple_of"], 0.5) + + def test_float_parser_with_default(self): + parser = FloatTypeParser() + + properties = { + "type": "number", + "default": 5.0, + "maximum": 10.5, + "minimum": 1.0, + "multipleOf": 0.5, + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, float) + self.assertEqual(type_validator["default"], 5.0) + self.assertEqual(type_validator["le"], 10.5) + self.assertEqual(type_validator["ge"], 1.0) + self.assertEqual(type_validator["multiple_of"], 0.5) + + def test_float_parser_with_default_invalid_type(self): + parser = FloatTypeParser() + + properties = { + "type": "number", + "default": "invalid", # Invalid default value + "maximum": 10.5, + "minimum": 1.0, + "multipleOf": 0.5, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "number", + "default": 15.0, + "maximum": 10.5, + "minimum": 1.0, + "multipleOf": 0.5, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "number", + "default": -5.0, + "maximum": 10.5, + "minimum": 1.0, + "multipleOf": 0.5, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "number", + "default": 10.5, + "exclusiveMaximum": 10.5, + "minimum": 1.0, + "multipleOf": 0.5, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "number", + "default": 1.0, + "maximum": 10.5, + "exclusiveMinimum": 1.0, + "multipleOf": 0.5, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "number", + "default": 5.0, + "maximum": 10.5, + "minimum": 1.0, + "multipleOf": 2.0, + } + + with self.assertRaises(ValueError) as context: + parser.from_properties("placeholder", properties) + + self.assertEqual( + str(context.exception), + "Default value 5.0 is not a multiple of 2.0", + ) diff --git a/tests/parser/test_int_type_parser.py b/tests/parser/test_int_type_parser.py new file mode 100644 index 0000000..e50b340 --- /dev/null +++ b/tests/parser/test_int_type_parser.py @@ -0,0 +1,165 @@ +from unittest import TestCase + +from jambo.parser import IntTypeParser + + +class TestIntTypeParser(TestCase): + def test_int_parser_no_options(self): + parser = IntTypeParser() + + properties = {"type": "integer"} + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, int) + self.assertEqual(type_validator, {}) + + def test_int_parser_with_options(self): + parser = IntTypeParser() + + properties = { + "type": "integer", + "maximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, int) + self.assertEqual(type_validator["le"], 10) + self.assertEqual(type_validator["ge"], 1) + self.assertEqual(type_validator["multiple_of"], 2) + + def test_int_parser_with_default(self): + parser = IntTypeParser() + + properties = { + "type": "integer", + "default": 6, + "maximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + type_parsing, type_validator = parser.from_properties("placeholder", properties) + + self.assertEqual(type_parsing, int) + self.assertEqual(type_validator["default"], 6) + self.assertEqual(type_validator["le"], 10) + self.assertEqual(type_validator["ge"], 1) + self.assertEqual(type_validator["multiple_of"], 2) + + def test_int_parser_with_default_invalid_type(self): + parser = IntTypeParser() + + properties = { + "type": "integer", + "default": "invalid", # Invalid default value + "maximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "integer", + "default": 15, + "maximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "integer", + "default": -5, + "maximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "integer", + "default": 10, + "exclusiveMaximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "integer", + "default": 1, + "exclusiveMinimum": 1, + "maximum": 10, + "multipleOf": 2, + } + + with self.assertRaises(ValueError) as context: + 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() + + properties = { + "type": "integer", + "default": 5, + "maximum": 10, + "minimum": 1, + "multipleOf": 2, + } + + with self.assertRaises(ValueError) as context: + parser.from_properties("placeholder", properties) + + self.assertEqual( + str(context.exception), + "Default value 5 is not a multiple of 2", + ) diff --git a/tests/test_type_parser.py b/tests/test_type_parser.py index 3b36df7..051f9a1 100644 --- a/tests/test_type_parser.py +++ b/tests/test_type_parser.py @@ -62,24 +62,6 @@ class TestTypeParser(unittest.TestCase): self.assertEqual(type_validator["lt"], 11) self.assertEqual(type_validator["multiple_of"], 2) - def test_string_parser(self): - parser = StringTypeParser() - - type_parsing, type_validator = parser.from_properties( - "placeholder", - { - "type": "string", - "maxLength": 10, - "minLength": 1, - "pattern": "[a-zA-Z0-9]", - }, - ) - - self.assertEqual(type_parsing, str) - self.assertEqual(type_validator["max_length"], 10) - self.assertEqual(type_validator["min_length"], 1) - self.assertEqual(type_validator["pattern"], "[a-zA-Z0-9]") - def test_object_parser(self): parser = ObjectTypeParser()