Debian Codegraph @

tests/test_exceptions.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 import unittest
19
20 from dns.exception import DNSException
21
22
23 class FormatedError(DNSException):
24 fmt = "Custom format: {parameter}"
25 supp_kwargs = {"parameter"}
26
27
28 class ExceptionTestCase(unittest.TestCase):
29 def test_custom_message(self):
30 msg = "this is a custom message"
31 try:
32 raise DNSException(msg)
33 except DNSException as ex:
34 self.assertEqual(str(ex), msg)
35
36 def test_implicit_message(self):
37 try:
38 raise DNSException()
39 except DNSException as ex:
40 self.assertEqual(ex.__class__.__doc__, str(ex))
41
42 def test_formatted_error(self):
43 """Exceptions with explicit format has to respect it."""
44 params = {"parameter": "value"}
45 try:
46 raise FormatedError(**params)
47 except FormatedError as ex:
48 msg = FormatedError.fmt.format(**params)
49 self.assertEqual(msg, str(ex))
50
51 def test_kwargs_only(self):
52 """Kwargs cannot be combined with args."""
53 with self.assertRaises(AssertionError):
54 raise FormatedError(1, a=2)
55
56 def test_kwargs_unsupported(self):
57 """Only supported kwargs are accepted."""
58 with self.assertRaises(AssertionError):
59 raise FormatedError(unsupported=2)
60
61
62 if __name__ == "__main__":
63 unittest.main()