summaryrefslogtreecommitdiffstats
path: root/tests/errors/test_exceptions.py
blob: 2fba25a6f98050db2e5f5e7d79d7b47492d41bf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from testtools import ExpectedException

from jenkins_jobs import errors
from tests import base


def dispatch(exc, *args):
    component_type = "type"  # noqa
    name = "name"

    for value in [component_type, name]:
        # prevent pep8 F841 "Unused Variable"
        pass

    raise exc(*args)


def gen_xml(exc, *args):
    data = {'module': 'data'}  # noqa

    raise exc(*args)


class TestInvalidAttributeError(base.BaseTestCase):

    def test_no_valid_values(self):
        # When given no valid values, InvalidAttributeError simply displays a
        # message indicating the invalid value, the component type, the
        # component name, and the attribute name.
        message = "'{0}' is an invalid value for attribute {1}.{2}".format(
            "fnord", "type.name", "fubar")
        with ExpectedException(errors.InvalidAttributeError, message):
            dispatch(errors.InvalidAttributeError, "fubar", "fnord")

    def test_with_valid_values(self):
        # When given valid values, InvalidAttributeError displays a message
        # indicating the invalid value, the component type, the component name,
        # and the attribute name; additionally, it lists the valid values for
        # the current component type & name.
        valid_values = ['herp', 'derp']
        message = "'{0}' is an invalid value for attribute {1}.{2}".format(
            "fnord", "type.name", "fubar")
        message += "\nValid values include: {0}".format(
            ', '.join("'{0}'".format(value) for value in valid_values))

        with ExpectedException(errors.InvalidAttributeError, message):
            dispatch(errors.InvalidAttributeError, "fubar", "fnord",
                     valid_values)


class TestMissingAttributeError(base.BaseTestCase):

    def test_with_single_missing_attribute(self):
        # When passed a single missing attribute, display a message indicating
        #  * the missing attribute
        #  * which component type and component name is missing it.
        missing_attribute = 'herp'
        message = "Missing {0} from an instance of '{1}'".format(
            missing_attribute, 'type.name')

        with ExpectedException(errors.MissingAttributeError, message):
            dispatch(errors.MissingAttributeError, missing_attribute)

        with ExpectedException(errors.MissingAttributeError,
                               message.replace('type.name', 'module')):
            gen_xml(errors.MissingAttributeError, missing_attribute)

    def test_with_multiple_missing_attributes(self):
        # When passed multiple missing attributes, display a message indicating
        #  * the missing attributes
        #  * which component type and component name is missing it.
        missing_attribute = ['herp', 'derp']
        message = "One of {0} must be present in '{1}'".format(
            ', '.join("'{0}'".format(value) for value in missing_attribute),
            'type.name')

        with ExpectedException(errors.MissingAttributeError, message):
            dispatch(errors.MissingAttributeError, missing_attribute)

        with ExpectedException(errors.MissingAttributeError,
                               message.replace('type.name', 'module')):
            gen_xml(errors.MissingAttributeError, missing_attribute)