From 3273fd84bf646b60926688efbfd43b771363e06e Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 3 Jun 2025 03:00:49 -0300 Subject: [PATCH] Fixes Test and Reports --- tests/parser/test_type_parser.py | 55 +++++++++++++++++++------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/tests/parser/test_type_parser.py b/tests/parser/test_type_parser.py index bceeae4..e26a3d5 100644 --- a/tests/parser/test_type_parser.py +++ b/tests/parser/test_type_parser.py @@ -1,38 +1,49 @@ from jambo.parser._type_parser import GenericTypeParser +import gc +from contextlib import contextmanager 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): - 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): # 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"}) def test_invalid_json_schema_type(self): - self.InvalidGenericTypeParser.json_schema_type = None - # 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"}) def test_invalid_mappings_properties_builder(self): - parser = self.InvalidGenericTypeParser() - with self.assertRaises(NotImplementedError): + with ( + with_test_parser() as InvalidGenericTypeParser, + self.assertRaises(NotImplementedError), + ): + parser = InvalidGenericTypeParser() parser.mappings_properties_builder({}, required=False)