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)