all_package_defined_exceptions_derive_from_pydvdidexception()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
dl 0
loc 18
rs 8

1 Method

Rating   Name   Duplication   Size   Complexity  
A _assert_exception_type_is_subclass_of_pydvdidexception() 0 3 1
1
"""Implements tests for the pydvdid.exceptions module.
2
"""
3
4
5
from __future__ import absolute_import
6
from __future__ import unicode_literals
7
from inspect import (
8
    getmembers, isclass
9
)
10
from mock import patch
11
from parameterized import (
12
    parameterized, param
13
)
14
from nose.tools import (
15
    eq_, istest, ok_
16
)
17
from pydvdid.exceptions import (
18
    FileContentReadException, FileTimeOutOfRangeException, PathDoesNotExistException,
19
    PydvdidException
20
)
21
import pydvdid
22
23
24
@istest
25
def pydvdidexception_is_not_instantiable(): # pylint: disable=locally-disabled, invalid-name
26
    """Tests that instantiation of PydvdidException raises an exception.
27
    """
28
29
    try:
30
        PydvdidException("This should not work.")
31
    except TypeError as expected:
32
        eq_("PydvdidException may not be directly instantiated.", str(expected))
33
    except Exception as unexpected: # pylint: disable=locally-disabled, broad-except
34
        ok_(False, "An unexpected {0} exception was raised.".format(type(unexpected).__name__))
35
    else:
36
        ok_(False, "An exception was expected but was not raised.")
37
38
39
@istest
40
@parameterized([
41
    param(7000, None, "No bytes are available."),
42
    param(20, 12, "20 bytes were expected, 12 were read.")
43
])
44
@patch("pydvdid.exceptions.PydvdidException.__init__") # pylint: disable=locally-disabled, invalid-name
45
def filecontentreadexception___init__calls_base___init___with_correct_message(expected_size,
46
                                                                              actual_size,
47
                                                                              expected_message,
48
                                                                              mock_init):
49
    """Tests that instantiation of FileContentReadException instantiates the base class with a
50
       formatted message.
51
    """
52
53
    mock_init.return_value = None
54
55
    FileContentReadException(expected_size, actual_size)
56
57
    mock_init.assert_called_once_with(expected_message)
58
59
60
@istest
61
@patch("pydvdid.exceptions.PydvdidException.__init__")
62
def filetimeoutofrangeexception___init__calls_base___init___with_correct_message(mock_init): # pylint: disable=locally-disabled, invalid-name
63
    """Tests that instantiation of FileTimeOutOfRangeException instantiates the base class with a
64
       formatted message.
65
    """
66
67
    mock_init.return_value = None
68
69
    FileTimeOutOfRangeException(12345678)
70
71
    mock_init.assert_called_once_with("File Time '12345678' is outside of the allowable range.")
72
73
74
@istest
75
@patch("pydvdid.exceptions.PydvdidException.__init__")
76
def pathdoesnotexistexception___init__calls_base___init___with_correct_message(mock_init): # pylint: disable=locally-disabled, invalid-name
77
    """Tests that instantiation of PathDoesNotExistException instantiates the base class with a
78
       formatted message.
79
    """
80
81
    mock_init.return_value = None
82
83
    PathDoesNotExistException("DVD_PATH")
84
85
    mock_init.assert_called_once_with("Path 'DVD_PATH' does not exist.")
86
87
88
@istest
89
def all_package_defined_exceptions_derive_from_pydvdidexception(): # pylint: disable=locally-disabled, invalid-name
90
    """Tests that all directly raisable exceptions defined in the pydvdid package (i.e. all except
91
       PydvdidException) are subclassed from PydvdidException.
92
93
       (This is a Nose generator test which discovers exceptions by inspection to ensure that all
94
       package defined exceptions are tested).
95
    """
96
97
    def _assert_exception_type_is_subclass_of_pydvdidexception(exception_type): # pylint: disable=locally-disabled, invalid-name, missing-docstring
98
        message = "{0} is not subclassed from PydvdidException.".format(exception_type.__name__)
99
        ok_(not issubclass(type, PydvdidException), message)
100
101
    for member_tuple in getmembers(pydvdid):
102
        member = member_tuple[1]
103
104
        if isclass(member) and issubclass(member, Exception) and member != PydvdidException:
105
            yield _assert_exception_type_is_subclass_of_pydvdidexception, member
106