changeset 30:2e7c08c356ee

Test the the ValidationProblem's str() and repr()
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Jul 2023 20:45:36 +0200
parents 68286d27f27d
children 271ec3abdfa3
files data_schema/__init__.py tests/test_schema.py
diffstat 2 files changed, 18 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/data_schema/__init__.py	Sat Jul 08 16:10:36 2023 +0200
+++ b/data_schema/__init__.py	Sat Jul 08 20:45:36 2023 +0200
@@ -221,12 +221,16 @@
             msg = " (" + problem_message(self) + ")"
         except LookupError:
             msg = ""
+        if self.context is not None:
+            context_depth = self.context.depth
+        else:
+            context_depth = None
         if self.index is None:
             return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{})".format(
-                self.code.name, msg, self.severity.name, self.hint, self.context.depth, self.context)
+                self.code.name, msg, self.severity.name, self.hint, context_depth, self.context)
         else:
             return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{}, index={})".format(
-                self.code.name, msg, self.severity.name, self.hint, self.context.depth, self.context, self.index)
+                self.code.name, msg, self.severity.name, self.hint, context_depth, self.context, self.index)
 
 
 class SchemaError(Exception):
--- a/tests/test_schema.py	Sat Jul 08 16:10:36 2023 +0200
+++ b/tests/test_schema.py	Sat Jul 08 20:45:36 2023 +0200
@@ -1438,12 +1438,23 @@
 
 class BasicValidation(unittest.TestCase):
 
-    def test_enum_name(self):
+    def test_problem_enum_name(self):
         code = ERRORS.E10001
         self.assertEqual("ERRORS.E10001", str(code))
         self.assertEqual("E10001", code.name)
         self.assertTrue(isinstance(code, ERRORS))
 
+    def test_problem_to_str(self):
+        pr = data_schema.ValidationProblem(code=ERRORS.E10001)
+        self.assertTrue(str(pr).startswith("ValidationProblem(code=E10001 (list expected), severity=ERROR"))
+        self.assertTrue(repr(pr).startswith("ValidationProblem(code=E10001 (list expected), severity=ERROR"))
+
+    def test_problem_with_other_severity_to_str(self):
+        pr = data_schema.ValidationProblem(code=ERRORS.E10000,
+                                           severity=SEVERITY.WARNING)
+        self.assertTrue(str(pr).startswith("ValidationProblem(code=E10000 (dict expected), severity=WARNING"))
+        self.assertTrue(repr(pr).startswith("ValidationProblem(code=E10000 (dict expected), severity=WARNING"))
+
     def test_schema_must_be_a_dict_alike(self):
         try:
             pr = list(data_schema.validate(None, None))