Implements Better Tests #9

Merged
HideyoshiNakazone merged 4 commits from better-tests into main 2025-04-13 05:15:08 +00:00
3 changed files with 116 additions and 14 deletions
Showing only changes of commit 76b40847ce - Show all commits

View File

@@ -24,7 +24,7 @@ class StringTypeParser(GenericTypeParser):
if not isinstance(default_value, str):
raise ValueError(
f"Default value for {name} must be a string, "
f"but got {type(properties['default'])}."
f"but got <{type(properties['default']).__name__}>."
)
if len(default_value) > properties.get("maxLength", float("inf")):

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",
)