12
12
13
13
import can
14
14
from can .bus import BusState
15
+ from can .exceptions import CanInitializationError
15
16
from can .interfaces .pcan .basic import *
16
17
from can .interfaces .pcan import PcanBus , PcanError
17
18
@@ -26,6 +27,8 @@ def setUp(self) -> None:
26
27
self .mock_pcan .Initialize .return_value = PCAN_ERROR_OK
27
28
self .mock_pcan .InitializeFD = Mock (return_value = PCAN_ERROR_OK )
28
29
self .mock_pcan .SetValue = Mock (return_value = PCAN_ERROR_OK )
30
+ self .mock_pcan .GetValue = self ._mockGetValue
31
+ self .PCAN_API_VERSION_SIM = "4.2"
29
32
30
33
self .bus = None
31
34
@@ -34,6 +37,17 @@ def tearDown(self) -> None:
34
37
self .bus .shutdown ()
35
38
self .bus = None
36
39
40
+ def _mockGetValue (self , channel , parameter ):
41
+ """
42
+ This method is used as mock for GetValue method of PCANBasic object.
43
+ Only a subset of parameters are supported.
44
+ """
45
+ if parameter == PCAN_API_VERSION :
46
+ return PCAN_ERROR_OK , self .PCAN_API_VERSION_SIM .encode ("ascii" )
47
+ raise NotImplementedError (
48
+ f"No mock return value specified for parameter { parameter } "
49
+ )
50
+
37
51
def test_bus_creation (self ) -> None :
38
52
self .bus = can .Bus (bustype = "pcan" )
39
53
self .assertIsInstance (self .bus , PcanBus )
@@ -52,6 +66,24 @@ def test_bus_creation_fd(self) -> None:
52
66
self .mock_pcan .Initialize .assert_not_called ()
53
67
self .mock_pcan .InitializeFD .assert_called_once ()
54
68
69
+ def test_api_version_low (self ) -> None :
70
+ self .PCAN_API_VERSION_SIM = "1.0"
71
+ with self .assertLogs ("can.pcan" , level = "WARNING" ) as cm :
72
+ self .bus = can .Bus (bustype = "pcan" )
73
+ found_version_warning = False
74
+ for i in cm .output :
75
+ if "version" in i and "pcan" in i :
76
+ found_version_warning = True
77
+ self .assertTrue (
78
+ found_version_warning ,
79
+ f"No warning was logged for incompatible api version { cm .output } " ,
80
+ )
81
+
82
+ def test_api_version_read_fail (self ) -> None :
83
+ self .mock_pcan .GetValue = Mock (return_value = (PCAN_ERROR_ILLOPERATION , None ))
84
+ with self .assertRaises (CanInitializationError ):
85
+ self .bus = can .Bus (bustype = "pcan" )
86
+
55
87
@parameterized .expand (
56
88
[
57
89
("no_error" , PCAN_ERROR_OK , PCAN_ERROR_OK , "some ok text 1" ),
@@ -108,8 +140,11 @@ def test_reset(self, name, status, expected_result) -> None:
108
140
)
109
141
def test_get_device_number (self , name , status , expected_result ) -> None :
110
142
with self .subTest (name ):
111
- self .mock_pcan .GetValue = Mock (return_value = (status , 1 ))
112
143
self .bus = can .Bus (bustype = "pcan" , fd = True )
144
+ # Mock GetValue after creation of bus to use first mock of
145
+ # GetValue in constructor
146
+ self .mock_pcan .GetValue = Mock (return_value = (status , 1 ))
147
+
113
148
self .assertEqual (self .bus .get_device_number (), expected_result )
114
149
self .mock_pcan .GetValue .assert_called_once_with (
115
150
PCAN_USBBUS1 , PCAN_DEVICE_NUMBER
0 commit comments