1212
1313import can
1414from can .bus import BusState
15+ from can .exceptions import CanInitializationError
1516from can .interfaces .pcan .basic import *
1617from can .interfaces .pcan import PcanBus , PcanError
1718
@@ -26,6 +27,8 @@ def setUp(self) -> None:
2627 self .mock_pcan .Initialize .return_value = PCAN_ERROR_OK
2728 self .mock_pcan .InitializeFD = Mock (return_value = PCAN_ERROR_OK )
2829 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"
2932
3033 self .bus = None
3134
@@ -34,6 +37,17 @@ def tearDown(self) -> None:
3437 self .bus .shutdown ()
3538 self .bus = None
3639
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+
3751 def test_bus_creation (self ) -> None :
3852 self .bus = can .Bus (bustype = "pcan" )
3953 self .assertIsInstance (self .bus , PcanBus )
@@ -52,6 +66,24 @@ def test_bus_creation_fd(self) -> None:
5266 self .mock_pcan .Initialize .assert_not_called ()
5367 self .mock_pcan .InitializeFD .assert_called_once ()
5468
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+
5587 @parameterized .expand (
5688 [
5789 ("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:
108140 )
109141 def test_get_device_number (self , name , status , expected_result ) -> None :
110142 with self .subTest (name ):
111- self .mock_pcan .GetValue = Mock (return_value = (status , 1 ))
112143 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+
113148 self .assertEqual (self .bus .get_device_number (), expected_result )
114149 self .mock_pcan .GetValue .assert_called_once_with (
115150 PCAN_USBBUS1 , PCAN_DEVICE_NUMBER
0 commit comments