edq.testing.unittest

 1import typing
 2import unittest
 3
 4import edq.util.json
 5import edq.util.reflection
 6
 7FORMAT_STR: str = "\n--- Expected ---\n%s\n--- Actual ---\n%s\n---\n"
 8
 9class BaseTest(unittest.TestCase):
10    """
11    A base class for unit tests.
12    """
13
14    maxDiff = None
15    """ Don't limit the size of diffs. """
16
17    def assertJSONDictEqual(self, a: typing.Any, b: typing.Any) -> None:  # pylint: disable=invalid-name
18        """
19        Like unittest.TestCase.assertDictEqual(),
20        but will try to convert each comparison argument to a dict if it is not already,
21        and uses an assertion message containing the full JSON representation of the arguments.
22
23        """
24
25        if (not isinstance(a, dict)):
26            if (isinstance(a, edq.util.json.DictConverter)):
27                a = a.to_dict()
28            else:
29                a = vars(a)
30
31        if (not isinstance(b, dict)):
32            if (isinstance(b, edq.util.json.DictConverter)):
33                b = b.to_dict()
34            else:
35                b = vars(b)
36
37        a_json = edq.util.json.dumps(a, indent = 4)
38        b_json = edq.util.json.dumps(b, indent = 4)
39
40        super().assertDictEqual(a, b, FORMAT_STR % (a_json, b_json))
41
42    def assertJSONListEqual(self, a: typing.List[typing.Any], b: typing.List[typing.Any]) -> None:  # pylint: disable=invalid-name
43        """
44        Call assertListEqual(), but supply a message containing the full JSON representation of the arguments.
45        """
46
47        a_json = edq.util.json.dumps(a, indent = 4)
48        b_json = edq.util.json.dumps(b, indent = 4)
49
50        super().assertListEqual(a, b, FORMAT_STR % (a_json, b_json))
51
52    def format_error_string(self, ex: typing.Union[BaseException, None]) -> str:
53        """
54        Format an error string from an exception so it can be checked for testing.
55        The type of the error will be included,
56        and any nested errors will be joined together.
57        """
58
59        parts = []
60
61        while (ex is not None):
62            type_name = edq.util.reflection.get_qualified_name(ex)
63            message = str(ex)
64
65            parts.append(f"{type_name}: {message}")
66
67            ex = ex.__cause__
68
69        return "; ".join(parts)
FORMAT_STR: str = '\n--- Expected ---\n%s\n--- Actual ---\n%s\n---\n'
class BaseTest(unittest.case.TestCase):
10class BaseTest(unittest.TestCase):
11    """
12    A base class for unit tests.
13    """
14
15    maxDiff = None
16    """ Don't limit the size of diffs. """
17
18    def assertJSONDictEqual(self, a: typing.Any, b: typing.Any) -> None:  # pylint: disable=invalid-name
19        """
20        Like unittest.TestCase.assertDictEqual(),
21        but will try to convert each comparison argument to a dict if it is not already,
22        and uses an assertion message containing the full JSON representation of the arguments.
23
24        """
25
26        if (not isinstance(a, dict)):
27            if (isinstance(a, edq.util.json.DictConverter)):
28                a = a.to_dict()
29            else:
30                a = vars(a)
31
32        if (not isinstance(b, dict)):
33            if (isinstance(b, edq.util.json.DictConverter)):
34                b = b.to_dict()
35            else:
36                b = vars(b)
37
38        a_json = edq.util.json.dumps(a, indent = 4)
39        b_json = edq.util.json.dumps(b, indent = 4)
40
41        super().assertDictEqual(a, b, FORMAT_STR % (a_json, b_json))
42
43    def assertJSONListEqual(self, a: typing.List[typing.Any], b: typing.List[typing.Any]) -> None:  # pylint: disable=invalid-name
44        """
45        Call assertListEqual(), but supply a message containing the full JSON representation of the arguments.
46        """
47
48        a_json = edq.util.json.dumps(a, indent = 4)
49        b_json = edq.util.json.dumps(b, indent = 4)
50
51        super().assertListEqual(a, b, FORMAT_STR % (a_json, b_json))
52
53    def format_error_string(self, ex: typing.Union[BaseException, None]) -> str:
54        """
55        Format an error string from an exception so it can be checked for testing.
56        The type of the error will be included,
57        and any nested errors will be joined together.
58        """
59
60        parts = []
61
62        while (ex is not None):
63            type_name = edq.util.reflection.get_qualified_name(ex)
64            message = str(ex)
65
66            parts.append(f"{type_name}: {message}")
67
68            ex = ex.__cause__
69
70        return "; ".join(parts)

A base class for unit tests.

maxDiff = None

Don't limit the size of diffs.

def assertJSONDictEqual(self, a: Any, b: Any) -> None:
18    def assertJSONDictEqual(self, a: typing.Any, b: typing.Any) -> None:  # pylint: disable=invalid-name
19        """
20        Like unittest.TestCase.assertDictEqual(),
21        but will try to convert each comparison argument to a dict if it is not already,
22        and uses an assertion message containing the full JSON representation of the arguments.
23
24        """
25
26        if (not isinstance(a, dict)):
27            if (isinstance(a, edq.util.json.DictConverter)):
28                a = a.to_dict()
29            else:
30                a = vars(a)
31
32        if (not isinstance(b, dict)):
33            if (isinstance(b, edq.util.json.DictConverter)):
34                b = b.to_dict()
35            else:
36                b = vars(b)
37
38        a_json = edq.util.json.dumps(a, indent = 4)
39        b_json = edq.util.json.dumps(b, indent = 4)
40
41        super().assertDictEqual(a, b, FORMAT_STR % (a_json, b_json))

Like unittest.TestCase.assertDictEqual(), but will try to convert each comparison argument to a dict if it is not already, and uses an assertion message containing the full JSON representation of the arguments.

def assertJSONListEqual(self, a: List[Any], b: List[Any]) -> None:
43    def assertJSONListEqual(self, a: typing.List[typing.Any], b: typing.List[typing.Any]) -> None:  # pylint: disable=invalid-name
44        """
45        Call assertListEqual(), but supply a message containing the full JSON representation of the arguments.
46        """
47
48        a_json = edq.util.json.dumps(a, indent = 4)
49        b_json = edq.util.json.dumps(b, indent = 4)
50
51        super().assertListEqual(a, b, FORMAT_STR % (a_json, b_json))

Call assertListEqual(), but supply a message containing the full JSON representation of the arguments.

def format_error_string(self, ex: Optional[BaseException]) -> str:
53    def format_error_string(self, ex: typing.Union[BaseException, None]) -> str:
54        """
55        Format an error string from an exception so it can be checked for testing.
56        The type of the error will be included,
57        and any nested errors will be joined together.
58        """
59
60        parts = []
61
62        while (ex is not None):
63            type_name = edq.util.reflection.get_qualified_name(ex)
64            message = str(ex)
65
66            parts.append(f"{type_name}: {message}")
67
68            ex = ex.__cause__
69
70        return "; ".join(parts)

Format an error string from an exception so it can be checked for testing. The type of the error will be included, and any nested errors will be joined together.