Implements Better Tests For: Int, Float, Bool
This commit is contained in:
@@ -22,22 +22,22 @@ def numeric_properties_builder(properties):
|
|||||||
f"Default value must be a number, got {type(default_value).__name__}"
|
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(
|
raise ValueError(
|
||||||
f"Default value exceeds maximum limit of {properties.get('maximum')}"
|
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(
|
raise ValueError(
|
||||||
f"Default value is below minimum limit of {properties.get('minimum')}"
|
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(
|
raise ValueError(
|
||||||
f"Default value exceeds exclusive maximum limit of {properties.get('exclusiveMaximum')}"
|
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(
|
raise ValueError(
|
||||||
f"Default value is below exclusive minimum limit of {properties.get('exclusiveMinimum')}"
|
f"Default value is below exclusive minimum limit of {properties.get('exclusiveMinimum')}"
|
||||||
)
|
)
|
||||||
|
|||||||
28
tests/parser/test_bool_type_parser.py
Normal file
28
tests/parser/test_bool_type_parser.py
Normal file
@@ -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)
|
||||||
165
tests/parser/test_float_type_parser.py
Normal file
165
tests/parser/test_float_type_parser.py
Normal file
@@ -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",
|
||||||
|
)
|
||||||
165
tests/parser/test_int_type_parser.py
Normal file
165
tests/parser/test_int_type_parser.py
Normal file
@@ -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",
|
||||||
|
)
|
||||||
@@ -62,24 +62,6 @@ class TestTypeParser(unittest.TestCase):
|
|||||||
self.assertEqual(type_validator["lt"], 11)
|
self.assertEqual(type_validator["lt"], 11)
|
||||||
self.assertEqual(type_validator["multiple_of"], 2)
|
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):
|
def test_object_parser(self):
|
||||||
parser = ObjectTypeParser()
|
parser = ObjectTypeParser()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user