diff --git a/tests/exceptions/__init__.py b/tests/exceptions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/exceptions/test_internal_assertion_exception.py b/tests/exceptions/test_internal_assertion_exception.py new file mode 100644 index 0000000..4a6d4de --- /dev/null +++ b/tests/exceptions/test_internal_assertion_exception.py @@ -0,0 +1,21 @@ +from jambo.exceptions.internal_assertion_exception import InternalAssertionException + +from unittest import TestCase + + +class TestInternalAssertionException(TestCase): + def test_inheritance(self): + self.assertTrue(issubclass(InternalAssertionException, AssertionError)) + + def test_message(self): + message = "This is an internal assertion error." + + expected_message = ( + f"Internal Assertion Failed: {message}\n" + "This is likely a bug in Jambo. Please report it at" + ) + + with self.assertRaises(InternalAssertionException) as ctx: + raise InternalAssertionException(message) + + self.assertEqual(str(ctx.exception), expected_message) diff --git a/tests/exceptions/test_invalid_schema_exception.py b/tests/exceptions/test_invalid_schema_exception.py new file mode 100644 index 0000000..8f96fd1 --- /dev/null +++ b/tests/exceptions/test_invalid_schema_exception.py @@ -0,0 +1,44 @@ +from jambo.exceptions.invalid_schema_exception import InvalidSchemaException + +from unittest import TestCase + + +class TestInternalAssertionException(TestCase): + def test_inheritance(self): + self.assertTrue(issubclass(InvalidSchemaException, ValueError)) + + def test_message(self): + message = "This is an internal assertion error." + + expected_message = f"Invalid JSON Schema: {message}" + + with self.assertRaises(InvalidSchemaException) as ctx: + raise InvalidSchemaException(message) + + self.assertEqual(str(ctx.exception), expected_message) + + def test_invalid_field(self): + message = "This is an internal assertion error." + invalid_field = "testField" + + expected_message = ( + f"Invalid JSON Schema: {message} (invalid field: {invalid_field})" + ) + + with self.assertRaises(InvalidSchemaException) as ctx: + raise InvalidSchemaException(message, invalid_field=invalid_field) + + self.assertEqual(str(ctx.exception), expected_message) + + def test_cause(self): + message = "This is an internal assertion error." + cause = ValueError("Underlying cause") + + expected_message = ( + f"Invalid JSON Schema: {message} (caused by ValueError: Underlying cause)" + ) + + with self.assertRaises(InvalidSchemaException) as ctx: + raise InvalidSchemaException(message, cause=cause) + + self.assertEqual(str(ctx.exception), expected_message) diff --git a/tests/exceptions/test_unsupported_schema_exception.py b/tests/exceptions/test_unsupported_schema_exception.py new file mode 100644 index 0000000..e01e3e3 --- /dev/null +++ b/tests/exceptions/test_unsupported_schema_exception.py @@ -0,0 +1,31 @@ +from jambo.exceptions.unsupported_schema_exception import UnsupportedSchemaException + +from unittest import TestCase + + +class TestUnsupportedSchemaException(TestCase): + def test_inheritance(self): + self.assertTrue(issubclass(UnsupportedSchemaException, ValueError)) + + def test_message(self): + message = "This is an internal assertion error." + + expected_message = f"Unsupported JSON Schema: {message}" + + with self.assertRaises(UnsupportedSchemaException) as ctx: + raise UnsupportedSchemaException(message) + + self.assertEqual(str(ctx.exception), expected_message) + + def test_unsupported_field(self): + message = "This is an internal assertion error." + invalid_field = "testField" + + expected_message = ( + f"Unsupported JSON Schema: {message} (unsupported field: {invalid_field})" + ) + + with self.assertRaises(UnsupportedSchemaException) as ctx: + raise UnsupportedSchemaException(message, unsupported_field=invalid_field) + + self.assertEqual(str(ctx.exception), expected_message) diff --git a/tests/parser/test_array_type_parser.py b/tests/parser/test_array_type_parser.py index 1cac217..dc9212f 100644 --- a/tests/parser/test_array_type_parser.py +++ b/tests/parser/test_array_type_parser.py @@ -19,6 +19,17 @@ class TestArrayTypeParser(TestCase): self.assertEqual(type_parsing.__origin__, list) self.assertEqual(element_type, str) + def test_array_parser_with_no_items(self): + parser = ArrayTypeParser() + + properties = { + "default": ["a", "b", "c", "d"], + "maxItems": 3, + } + + with self.assertRaises(InvalidSchemaException): + parser.from_properties("placeholder", properties) + def test_array_parser_with_options_unique(self): parser = ArrayTypeParser() diff --git a/tests/parser/test_oneof_type_parser.py b/tests/parser/test_oneof_type_parser.py index 99e9074..14bf942 100644 --- a/tests/parser/test_oneof_type_parser.py +++ b/tests/parser/test_oneof_type_parser.py @@ -20,6 +20,17 @@ class TestOneOfTypeParser(TestCase): ref_cache={}, ) + with self.assertRaises(InvalidSchemaException): + OneOfTypeParser().from_properties_impl( + "test_field", + { + "oneOf": [], # should throw because oneOf must be a list with at least one item + }, + required=True, + context={}, + ref_cache={}, + ) + with self.assertRaises(InvalidSchemaException): SchemaConverter.build( {