jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/409633 )
Change subject: TestLogentriesBase._test_logevent: Try logentry.comment() and catch KeyError ......................................................................
TestLogentriesBase._test_logevent: Try logentry.comment() and catch KeyError
- The `hasattr(logentry, 'comment')` always returns True because `comment` is a method name. It's the internal LogDict object that does not have the key. - Document the KeyError in the comment method docstring.
Bug: T186985 Change-Id: Ide544cf575cbb80b6432955fedf894b4d6eb6d3b --- M pywikibot/logentries.py M tests/logentry_tests.py 2 files changed, 8 insertions(+), 4 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/logentries.py b/pywikibot/logentries.py index e114eb0..fc278f5 100644 --- a/pywikibot/logentries.py +++ b/pywikibot/logentries.py @@ -143,7 +143,11 @@ return self._timestamp
def comment(self): - """Return the logentry's comment.""" + """Return the logentry's comment. + + @raise KeyError: The log entry has no 'comment' key. + @rtype: str + """ return self.data['comment']
diff --git a/tests/logentry_tests.py b/tests/logentry_tests.py index e32f3d3..faf0921 100644 --- a/tests/logentry_tests.py +++ b/tests/logentry_tests.py @@ -75,10 +75,10 @@ else: self.assertNotIn(logentry.type(), logentry.data) self.assertIsInstance(logentry.action(), unicode) - if hasattr(logentry, 'comment'): + try: self.assertIsInstance(logentry.comment(), unicode) - else: - self.assertRaises(KeyError, logentry.comment) + except KeyError as e: + self.assertRegex(str(e), "Log entry ([^)]+) has no 'comment' key") self.assertIsInstance(logentry.logid(), int) self.assertIsInstance(logentry.ns(), int) self.assertIsInstance(logentry.pageid(), int)
pywikibot-commits@lists.wikimedia.org