Implements Better String Tests

This commit is contained in:
2025-04-12 19:37:53 -03:00
parent ec9171ba8f
commit 76b40847ce
3 changed files with 116 additions and 14 deletions

View File

@@ -69,10 +69,10 @@ class TestArrayTypeParser(TestCase):
with self.assertRaises(ValueError) as context:
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"All items in the default list must be of type str",
)
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()
@@ -82,9 +82,9 @@ class TestArrayTypeParser(TestCase):
with self.assertRaises(ValueError) as context:
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception), "Default value must be a list, got str"
)
self.assertEqual(
str(context.exception), "Default value must be a list, got str"
)
def test_array_parser_with_invalid_default_min(self):
parser = ArrayTypeParser()
@@ -94,9 +94,9 @@ class TestArrayTypeParser(TestCase):
with self.assertRaises(ValueError) as context:
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception), "Default list is below minItems limit of 2"
)
self.assertEqual(
str(context.exception), "Default list is below minItems limit of 2"
)
def test_array_parser_with_invalid_default_max(self):
parser = ArrayTypeParser()
@@ -110,6 +110,6 @@ class TestArrayTypeParser(TestCase):
with self.assertRaises(ValueError) as context:
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception), "Default list exceeds maxItems limit of 3"
)
self.assertEqual(
str(context.exception), "Default list exceeds maxItems limit of 3"
)

View File

@@ -0,0 +1,102 @@
from unittest import TestCase
from jambo.parser import StringTypeParser
class TestStringTypeParser(TestCase):
def test_string_parser_no_options(self):
parser = StringTypeParser()
properties = {"type": "string"}
type_parsing, type_validator = parser.from_properties("placeholder", properties)
self.assertEqual(type_parsing, str)
def test_string_parser_with_options(self):
parser = StringTypeParser()
properties = {
"type": "string",
"maxLength": 10,
"minLength": 1,
"pattern": "^[a-zA-Z]+$",
}
type_parsing, type_validator = parser.from_properties("placeholder", properties)
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-Z]+$")
def test_string_parser_with_default_value(self):
parser = StringTypeParser()
properties = {
"type": "string",
"default": "default_value",
"maxLength": 20,
"minLength": 5,
}
type_parsing, type_validator = parser.from_properties("placeholder", properties)
self.assertEqual(type_parsing, str)
self.assertEqual(type_validator["default"], "default_value")
self.assertEqual(type_validator["max_length"], 20)
self.assertEqual(type_validator["min_length"], 5)
def test_string_parser_with_invalid_default_value_type(self):
parser = StringTypeParser()
properties = {
"type": "string",
"default": 12345, # Invalid default value
"maxLength": 20,
"minLength": 5,
}
with self.assertRaises(ValueError) as context:
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()
properties = {
"type": "string",
"default": "default_value",
"maxLength": 2,
"minLength": 1,
}
with self.assertRaises(ValueError) as context:
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()
properties = {
"type": "string",
"default": "a",
"maxLength": 20,
"minLength": 2,
}
with self.assertRaises(ValueError) as context:
parser.from_properties("placeholder", properties)
self.assertEqual(
str(context.exception),
"Default value for placeholder is below minLength limit of 2",
)