# HG changeset patch # User Franz Glasner # Date 1688841936 -7200 # Node ID 2e7c08c356ee9f57eddd11f82dac4fc289aefa27 # Parent 68286d27f27d94fb101e4c34872c997e9ba3795a Test the the ValidationProblem's str() and repr() diff -r 68286d27f27d -r 2e7c08c356ee data_schema/__init__.py --- 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): diff -r 68286d27f27d -r 2e7c08c356ee tests/test_schema.py --- 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))