Skip to content

Commit

Permalink
Include unexpected non-text in warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfulton committed Mar 11, 2017
1 parent 752e4d2 commit ff47b70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion transaction/_transaction.py
Expand Up @@ -758,7 +758,7 @@ def text_or_warn(s):
if isinstance(s, text_type):
return s

warnings.warn("Expected text", DeprecationWarning, stacklevel=3)
warnings.warn("Expected text, got %r" % s, DeprecationWarning, stacklevel=3)
if isinstance(s, bytes):
return s.decode('utf-8', 'replace')
else:
Expand Down
21 changes: 13 additions & 8 deletions transaction/tests/test__transaction.py
Expand Up @@ -1014,16 +1014,21 @@ def test_note_42(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
txn.note(42)
self.assertNonTextDeprecationWarning(w)
self.assertNonTextDeprecationWarning(w, '42')
self.assertEqual(txn.description, u'42')

def assertNonTextDeprecationWarning(self, w):
def assertNonTextDeprecationWarning(self, w, expect=None):
[w] = w
self.assertEqual(
(DeprecationWarning, "Expected text",
os.path.splitext(__file__)[0]),
(w.category, str(w.message), os.path.splitext(w.filename)[0]),
)
prefix = "Expected text, got "
str_message = str(w.message)
if expect:
self.assertEqual(prefix + expect, str_message)
else:
self.assertTrue(str_message.startswith(prefix))

self.assertEqual((DeprecationWarning, os.path.splitext(__file__)[0]),
(w.category, os.path.splitext(w.filename)[0]),
)

def test_description_bytes(self):
txn = self._makeOne()
Expand All @@ -1038,7 +1043,7 @@ def test_description_42(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
txn.description = 42
self.assertNonTextDeprecationWarning(w)
self.assertNonTextDeprecationWarning(w, '42')
self.assertEqual(txn.description, u'42')

def test_description_None(self):
Expand Down

0 comments on commit ff47b70

Please sign in to comment.