Skip to content

Commit

Permalink
+test_wrong_messages()
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Jan 2, 2018
1 parent 519c378 commit 6411148
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
14 changes: 14 additions & 0 deletions django_tools/unittest_utils/unittest_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ def assert_endswith(self, text, prefix):
if not text.endswith(prefix):
self.fail("String %r doesn't ends with %r" % (text, prefix))

def assert_exception_startswith(self, context_manager, text):
"""
e.g.:
with self.assertRaises(AssertionError) as cm:
do_something()
self.assert_exception_startswith(cm, "First part of the error message")
"""
exception_text = context_manager.exception.args[0]
if not exception_text.startswith(text):
msg="%r doesn't starts with %r" % (exception_text, text)
raise self.failureException(msg)

def get_admin_url(self, obj, suffix):
opts = obj._meta
change_url = urlresolvers.reverse(
Expand Down
6 changes: 0 additions & 6 deletions django_tools_tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ def assert_extra_permission(self, permission):
self.assert_permissiontestmodel(permission)
self.assertEqual(permission.codename, "extra_permission")

def assert_exception_startswith(self, context_manager, text):
exception_text = context_manager.exception.args[0]
if not exception_text.startswith(text):
msg="%r doesn't starts with %r" % (exception_text, text)
raise self.failureException(msg)

#-------------------------------------------------------------------------

def test_setup(self):
Expand Down
14 changes: 14 additions & 0 deletions django_tools_tests/test_unittest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,17 @@ def test_get_messages_without_context(self):
status_code=302,
messages = ['redirect-response-message'],
)

def test_wrong_messages(self):
response = self.client.get("/create_message_normal_response/this-is-it/")
self.assertMessages(response, ['this-is-it'])

with self.assertRaises(AssertionError) as cm:
self.assertResponse(response,
messages=['this-is-not-it'],
browser_traceback=False,
)

self.assert_exception_startswith(cm,
"Lists differ: ['this-is-it'] != ['this-is-not-it']"
)

0 comments on commit 6411148

Please sign in to comment.