From d3d28c620fd6ed3da136e348e13a73d5e78b9ccc Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Fri, 12 Apr 2024 08:58:59 +0200 Subject: [PATCH] Improve traceback for case missing an attribute --- src/zinolib/ritz.py | 8 ++++++-- tests/test_ritz.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/test_ritz.py diff --git a/src/zinolib/ritz.py b/src/zinolib/ritz.py index 4f303e5..1599802 100644 --- a/src/zinolib/ritz.py +++ b/src/zinolib/ritz.py @@ -276,8 +276,12 @@ def __getattr__(self, name): elif "downtime" == name: return self.get_downtime() else: - self.__getattribute__(name) - # raise AttributeError("%s instance of type %s has no attribute '%s'" % (self.__class__, self._attrs["type"], name)) + try: + self.__getattribute__(name) + except AttributeError as e: + msg = "%s of type %s has no attribute '%s'" + typename = self._attrs.get("type", "UNKNOWN") + raise AttributeError(msg % (self, typename, name)) return self def __getitem__(self, key): diff --git a/tests/test_ritz.py b/tests/test_ritz.py new file mode 100644 index 0000000..c2d8696 --- /dev/null +++ b/tests/test_ritz.py @@ -0,0 +1,29 @@ +import unittest + +from zinolib.ritz import Case + + +class CaseTest(unittest.TestCase): + class FakeZino: + def __init__(self, attrs=None): + if attrs: + self.attrs = attrs + else: + self.attrs = {} + + def get_attributes(self, caseid): + return self.attrs + + def test_golden_path_attribute_access(self): + zino = self.FakeZino({'foo': 'bar'}) + case = Case(zino, 1) + self.assertTrue(case._attrs) + self.assertEqual(case.foo, 'bar') + + def test_accessing_missing_attribute_should_fail_with_extra_info(self): + zino = self.FakeZino({'foo': 'bar'}) + case = Case(zino, 1) + expected_msg = "(1) of type UNKNOWN has no attribute 'xux'" + with self.assertRaises(AttributeError) as cm: + case.xux + self.assertEqual(cm.exception.args[0], expected_msg)