Better Object Internal Structure and Type Selection #16
@@ -1,38 +1,49 @@
|
|||||||
from jambo.parser._type_parser import GenericTypeParser
|
from jambo.parser._type_parser import GenericTypeParser
|
||||||
|
|
||||||
|
import gc
|
||||||
|
from contextlib import contextmanager
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def with_test_parser():
|
||||||
|
class InvalidGenericTypeParser(GenericTypeParser):
|
||||||
|
mapped_type = str
|
||||||
|
json_schema_type = "type:invalid"
|
||||||
|
|
||||||
|
def from_properties(
|
||||||
|
self, name: str, properties: dict[str, any], required: bool = False
|
||||||
|
): ...
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield InvalidGenericTypeParser
|
||||||
|
finally:
|
||||||
|
del InvalidGenericTypeParser
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
|
||||||
class TestGenericTypeParser(TestCase):
|
class TestGenericTypeParser(TestCase):
|
||||||
def setUp(self):
|
|
||||||
class InvalidGenericTypeParser(GenericTypeParser):
|
|
||||||
mapped_type = str
|
|
||||||
json_schema_type = "type:invalid"
|
|
||||||
|
|
||||||
def from_properties(
|
|
||||||
self, name: str, properties: dict[str, any], required: bool = False
|
|
||||||
): ...
|
|
||||||
|
|
||||||
if hasattr(self, "InvalidGenericTypeParser"):
|
|
||||||
delattr(self, "InvalidGenericTypeParser")
|
|
||||||
self.InvalidGenericTypeParser = InvalidGenericTypeParser
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
del self.InvalidGenericTypeParser
|
|
||||||
|
|
||||||
def test_invalid_get_impl(self):
|
def test_invalid_get_impl(self):
|
||||||
# Assuming GenericTypeParser is imported from the module
|
# Assuming GenericTypeParser is imported from the module
|
||||||
with self.assertRaises(ValueError):
|
with (
|
||||||
|
with_test_parser(),
|
||||||
|
self.assertRaises(ValueError),
|
||||||
|
):
|
||||||
GenericTypeParser._get_impl({"type": "another_invalid_type"})
|
GenericTypeParser._get_impl({"type": "another_invalid_type"})
|
||||||
|
|
||||||
def test_invalid_json_schema_type(self):
|
def test_invalid_json_schema_type(self):
|
||||||
self.InvalidGenericTypeParser.json_schema_type = None
|
|
||||||
|
|
||||||
# This is more for the developer's sanity check
|
# This is more for the developer's sanity check
|
||||||
with self.assertRaises(RuntimeError):
|
with (
|
||||||
|
with_test_parser() as InvalidGenericTypeParser,
|
||||||
|
self.assertRaises(RuntimeError),
|
||||||
|
):
|
||||||
|
InvalidGenericTypeParser.json_schema_type = None
|
||||||
GenericTypeParser._get_impl({"type": "another_invalid_type"})
|
GenericTypeParser._get_impl({"type": "another_invalid_type"})
|
||||||
|
|
||||||
def test_invalid_mappings_properties_builder(self):
|
def test_invalid_mappings_properties_builder(self):
|
||||||
parser = self.InvalidGenericTypeParser()
|
with (
|
||||||
with self.assertRaises(NotImplementedError):
|
with_test_parser() as InvalidGenericTypeParser,
|
||||||
|
self.assertRaises(NotImplementedError),
|
||||||
|
):
|
||||||
|
parser = InvalidGenericTypeParser()
|
||||||
parser.mappings_properties_builder({}, required=False)
|
parser.mappings_properties_builder({}, required=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user