Implements Better Tests #9
@@ -0,0 +1,6 @@
|
||||
from .schema_converter import SchemaConverter
|
||||
|
||||
|
||||
__all__ = [
|
||||
SchemaConverter # Exports the schema converter class for external use
|
||||
]
|
||||
|
||||
0
tests/parser/__init__.py
Normal file
0
tests/parser/__init__.py
Normal file
115
tests/parser/test_array_type_parser.py
Normal file
115
tests/parser/test_array_type_parser.py
Normal file
@@ -0,0 +1,115 @@
|
||||
from typing import get_args
|
||||
from unittest import TestCase
|
||||
|
||||
from jambo.parser import ArrayTypeParser
|
||||
|
||||
|
||||
class TestArrayTypeParser(TestCase):
|
||||
def test_array_parser_no_options(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {"items": {"type": "string"}}
|
||||
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
element_type = get_args(type_parsing)[0]
|
||||
|
||||
self.assertEqual(type_parsing.__origin__, list)
|
||||
self.assertEqual(element_type, str)
|
||||
|
||||
def test_array_parser_with_options_unique(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {"items": {"type": "string"}, "uniqueItems": True}
|
||||
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
self.assertEqual(type_parsing.__origin__, set)
|
||||
|
||||
def test_array_parser_with_options_max_min(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {"items": {"type": "string"}, "maxItems": 10, "minItems": 1}
|
||||
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
self.assertEqual(type_parsing.__origin__, list)
|
||||
self.assertEqual(type_validator["max_length"], 10)
|
||||
self.assertEqual(type_validator["min_length"], 1)
|
||||
|
||||
def test_array_parser_with_options_default_list(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {"items": {"type": "string"}, "default": ["a", "b", "c"]}
|
||||
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
self.assertEqual(type_parsing.__origin__, list)
|
||||
self.assertEqual(type_validator["default_factory"](), ["a", "b", "c"])
|
||||
|
||||
def test_array_parse_with_options_default_set(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {
|
||||
"items": {"type": "string"},
|
||||
"uniqueItems": True,
|
||||
"default": ["a", "b", "c"],
|
||||
}
|
||||
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
self.assertEqual(type_parsing.__origin__, set)
|
||||
self.assertEqual(type_validator["default_factory"](), {"a", "b", "c"})
|
||||
|
||||
def test_array_parser_with_invalid_default_elem_type(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {"items": {"type": "string"}, "default": ["a", 1, "c"]}
|
||||
|
||||
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",
|
||||
)
|
||||
|
||||
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:
|
||||
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:
|
||||
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()
|
||||
|
||||
properties = {
|
||||
"items": {"type": "string"},
|
||||
"default": ["a", "b", "c", "d"],
|
||||
"maxItems": 3,
|
||||
}
|
||||
|
||||
with self.assertRaises(ValueError) as context:
|
||||
parser.from_properties("placeholder", properties)
|
||||
|
||||
self.assertEqual(
|
||||
str(context.exception), "Default list exceeds maxItems limit of 3"
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
from jambo.schema_converter import SchemaConverter
|
||||
from jambo import SchemaConverter
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ from jambo.parser import (
|
||||
)
|
||||
|
||||
import unittest
|
||||
from typing import get_args
|
||||
|
||||
|
||||
class TestTypeParser(unittest.TestCase):
|
||||
@@ -98,42 +97,3 @@ class TestTypeParser(unittest.TestCase):
|
||||
|
||||
self.assertEqual(obj.name, "name")
|
||||
self.assertEqual(obj.age, 10)
|
||||
|
||||
def test_array_of_string_parser(self):
|
||||
parser = ArrayTypeParser()
|
||||
expected_definition = (list[str], {})
|
||||
|
||||
properties = {"items": {"type": "string"}}
|
||||
|
||||
self.assertEqual(
|
||||
parser.from_properties("placeholder", properties), expected_definition
|
||||
)
|
||||
|
||||
def test_array_of_object_parser(self):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"age": {"type": "integer"},
|
||||
},
|
||||
},
|
||||
"maxItems": 10,
|
||||
"minItems": 1,
|
||||
"uniqueItems": True,
|
||||
}
|
||||
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
self.assertEqual(type_parsing.__origin__, set)
|
||||
self.assertEqual(type_validator["max_length"], 10)
|
||||
self.assertEqual(type_validator["min_length"], 1)
|
||||
|
||||
Model = get_args(type_parsing)[0]
|
||||
obj = Model(name="name", age=10)
|
||||
|
||||
self.assertEqual(obj.name, "name")
|
||||
self.assertEqual(obj.age, 10)
|
||||
|
||||
Reference in New Issue
Block a user