Skip to content

Commit

Permalink
Remove Any Packet feature (#232)
Browse files Browse the repository at this point in the history
Remove `AnyCanPacket` class and abandon `Any Packet` feature.
  • Loading branch information
mdabrowski1990 authored Oct 23, 2023
1 parent 156d9a9 commit 5069ec8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 357 deletions.
219 changes: 2 additions & 217 deletions tests/software_tests/packet/test_can_packet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from mock import patch, Mock, call

from uds.packet.can_packet import CanPacket, AnyCanPacket, \
CanPacketType, CanAddressingFormat, AbstractCanAddressingInformation, CanIdHandler, AddressingType, \
from uds.packet.can_packet import CanPacket, \
CanPacketType, CanAddressingFormat, AbstractCanAddressingInformation, AddressingType, \
DEFAULT_FILLER_BYTE, AmbiguityError
from uds.can import CanFlowStatus

Expand All @@ -15,17 +15,11 @@ class TestCanPacket:

def setup_method(self):
self.mock_can_packet = Mock(spec=CanPacket)
mock_can_id_handler_class = Mock(spec=CanIdHandler,
ADDRESSING_TYPE_NAME=CanIdHandler.ADDRESSING_TYPE_NAME,
TARGET_ADDRESS_NAME=CanIdHandler.TARGET_ADDRESS_NAME,
SOURCE_ADDRESS_NAME=CanIdHandler.SOURCE_ADDRESS_NAME)
# patching
self._patcher_warn = patch(f"{SCRIPT_LOCATION}.warn")
self.mock_warn = self._patcher_warn.start()
self._patcher_can_dlc_handler_class = patch(f"{SCRIPT_LOCATION}.CanDlcHandler")
self.mock_can_dlc_handler_class = self._patcher_can_dlc_handler_class.start()
self._patcher_can_id_handler_class = patch(f"{SCRIPT_LOCATION}.CanIdHandler", mock_can_id_handler_class)
self.mock_can_id_handler_class = self._patcher_can_id_handler_class.start()
self._patcher_ai_class = patch(f"{SCRIPT_LOCATION}.CanAddressingInformation")
self.mock_ai_class = self._patcher_ai_class.start()
self._patcher_normal_11bit_ai_class = patch(f"{SCRIPT_LOCATION}.Normal11BitCanAddressingInformation")
Expand Down Expand Up @@ -56,7 +50,6 @@ def setup_method(self):
def teardown_method(self):
self._patcher_warn.stop()
self._patcher_can_dlc_handler_class.stop()
self._patcher_can_id_handler_class.stop()
self._patcher_ai_class.stop()
self._patcher_normal_11bit_ai_class.stop()
self._patcher_normal_fixed_ai_class.stop()
Expand Down Expand Up @@ -810,162 +803,6 @@ def test_update_ai_data_byte(self, addressing_format, raw_frame_data, ai_data_by
address_extension=self.mock_can_packet.address_extension)


class TestAnyCanPacket:
"""Unit tests for `AnyCanPacket` class."""

def setup_method(self):
self.mock_any_can_packet = Mock(spec=AnyCanPacket)
# patching
self._patcher_validate_raw_bytes = patch(f"{SCRIPT_LOCATION}.validate_raw_bytes")
self.mock_validate_raw_bytes = self._patcher_validate_raw_bytes.start()
self._patcher_addressing_type_class = patch(f"{SCRIPT_LOCATION}.AddressingType")
self.mock_addressing_type_class = self._patcher_addressing_type_class.start()
self._patcher_addressing_format_class = patch(f"{SCRIPT_LOCATION}.CanAddressingFormat")
self.mock_addressing_format_class = self._patcher_addressing_format_class.start()
self._patcher_can_id_handler_class = patch(f"{SCRIPT_LOCATION}.CanIdHandler")
self.mock_can_id_handler_class = self._patcher_can_id_handler_class.start()
self._patcher_can_dlc_handler_class = patch(f"{SCRIPT_LOCATION}.CanDlcHandler")
self.mock_can_dlc_handler_class = self._patcher_can_dlc_handler_class.start()
self._patcher_ai_class = patch(f"{SCRIPT_LOCATION}.CanAddressingInformation")
self.mock_ai_class = self._patcher_ai_class.start()
self._patcher_can_packet_class = patch(f"{SCRIPT_LOCATION}.CanPacket")
self.mock_can_packet_class = self._patcher_can_packet_class.start()

def teardown_method(self):
self._patcher_validate_raw_bytes.stop()
self._patcher_addressing_type_class.stop()
self._patcher_addressing_format_class.stop()
self._patcher_can_id_handler_class.stop()
self._patcher_can_dlc_handler_class.stop()
self._patcher_ai_class.stop()
self._patcher_can_packet_class.stop()

# __init__

@pytest.mark.parametrize("raw_frame_data, addressing_format, addressing_type, can_id", [
("some data", "some format", "some addressing type", "some CAN ID"),
(range(10), CanAddressingFormat.EXTENDED_ADDRESSING, AddressingType.FUNCTIONAL, 0x987)
])
def test_init(self, raw_frame_data, addressing_format, addressing_type, can_id):
AnyCanPacket.__init__(self=self.mock_any_can_packet,
raw_frame_data=raw_frame_data,
addressing_format=addressing_format,
addressing_type=addressing_type,
can_id=can_id)
assert self.mock_any_can_packet.raw_frame_data == raw_frame_data
assert self.mock_any_can_packet.addressing_format == addressing_format
assert self.mock_any_can_packet.addressing_type == addressing_type
assert self.mock_any_can_packet.can_id == can_id

# raw_frame_data

@pytest.mark.parametrize("raw_frame_data", ["some raw data", list(range(10))])
def test_raw_frame_data__get(self, raw_frame_data):
self.mock_any_can_packet._AnyCanPacket__raw_frame_data = raw_frame_data
assert AnyCanPacket.raw_frame_data.fget(self.mock_any_can_packet) == raw_frame_data

@pytest.mark.parametrize("raw_frame_data", ["some raw data", list(range(10))])
def test_raw_frame_data__set(self, raw_frame_data):
AnyCanPacket.raw_frame_data.fset(self.mock_any_can_packet, value=raw_frame_data)
self.mock_validate_raw_bytes.assert_called_once_with(raw_frame_data, allow_empty=True)
self.mock_can_dlc_handler_class.validate_data_bytes_number.assert_called_once_with(len(raw_frame_data))
assert self.mock_any_can_packet._AnyCanPacket__raw_frame_data == tuple(raw_frame_data)

# can_id

@pytest.mark.parametrize("can_id", ["some CAN ID", 0x98321])
def test_can_id__get(self, can_id):
self.mock_any_can_packet._AnyCanPacket__can_id = can_id
assert AnyCanPacket.can_id.fget(self.mock_any_can_packet) == can_id

@pytest.mark.parametrize("can_id", ["some CAN ID", 0x98321])
def test_can_id__set(self, can_id):
AnyCanPacket.can_id.fset(self.mock_any_can_packet, value=can_id)
self.mock_can_id_handler_class.validate_can_id.assert_called_once_with(can_id)
assert self.mock_any_can_packet._AnyCanPacket__can_id == can_id

# addressing_format

@pytest.mark.parametrize("addressing_format", ["some addresisng format", CanAddressingFormat.EXTENDED_ADDRESSING])
def test_addressing_format__get(self, addressing_format):
self.mock_any_can_packet._AnyCanPacket__addressing_format = addressing_format
assert AnyCanPacket.addressing_format.fget(self.mock_any_can_packet) == addressing_format

@pytest.mark.parametrize("addressing_format", ["some addresisng format", CanAddressingFormat.EXTENDED_ADDRESSING])
def test_addressing_format__set(self, addressing_format):
AnyCanPacket.addressing_format.fset(self.mock_any_can_packet, value=addressing_format)
self.mock_addressing_format_class.validate_member.assert_called_once_with(addressing_format)
self.mock_addressing_format_class.assert_called_once_with(addressing_format)
assert self.mock_any_can_packet._AnyCanPacket__addressing_format == self.mock_addressing_format_class.return_value

# addressing_type

@pytest.mark.parametrize("addressing_type", ["some addresisng", AddressingType.PHYSICAL])
def test_addressing_type__get(self, addressing_type):
self.mock_any_can_packet._AnyCanPacket__addressing_type = addressing_type
assert AnyCanPacket.addressing_type.fget(self.mock_any_can_packet) == addressing_type

@pytest.mark.parametrize("addressing_type", ["some addresisng", AddressingType.PHYSICAL])
def test_addressing_type__set(self, addressing_type):
AnyCanPacket.addressing_type.fset(self.mock_any_can_packet, value=addressing_type)
self.mock_addressing_type_class.validate_member.assert_called_once_with(addressing_type)
self.mock_addressing_type_class.assert_called_once_with(addressing_type)
assert self.mock_any_can_packet._AnyCanPacket__addressing_type == self.mock_addressing_type_class.return_value

# dlc

@pytest.mark.parametrize("raw_frame_data", ["some raw data", list(range(10))])
def test_dlc__get(self, raw_frame_data):
self.mock_any_can_packet.raw_frame_data = raw_frame_data
assert AnyCanPacket.dlc.fget(self.mock_any_can_packet) \
== self.mock_can_dlc_handler_class.encode_dlc.return_value
self.mock_can_dlc_handler_class.encode_dlc.assert_called_once_with(len(raw_frame_data))

# packet_type

@pytest.mark.parametrize("ai_data_bytes_number, raw_frame_data", [
(0, []),
(1, [0x2F]),
])
def test_packet_type__get__none(self, ai_data_bytes_number, raw_frame_data):
self.mock_ai_class.get_ai_data_bytes_number.return_value = ai_data_bytes_number
self.mock_any_can_packet.raw_frame_data = raw_frame_data
assert AnyCanPacket.packet_type.fget(self.mock_any_can_packet) is None
self.mock_ai_class.get_ai_data_bytes_number.assert_called_once_with(
self.mock_any_can_packet.addressing_format)

@pytest.mark.parametrize("ai_data_bytes_number, raw_frame_data", [
(0, [0x2F]),
(1, [0x12, 0x34, 0x45]),
])
def test_packet_type__get(self, ai_data_bytes_number, raw_frame_data):
self.mock_ai_class.get_ai_data_bytes_number.return_value = ai_data_bytes_number
self.mock_any_can_packet.raw_frame_data = raw_frame_data
assert AnyCanPacket.packet_type.fget(self.mock_any_can_packet) is raw_frame_data[ai_data_bytes_number] >> 4
self.mock_ai_class.get_ai_data_bytes_number.assert_called_once_with(
self.mock_any_can_packet.addressing_format)

# get_addressing_information

@pytest.mark.parametrize("exception", [TypeError, ValueError, IndexError])
@patch(f"{SCRIPT_LOCATION}.AbstractCanPacketContainer.get_addressing_information")
def test_get_addressing_information__none(self, mock_get_addressing_information, exception):
mock_get_addressing_information.side_effect = exception
assert AnyCanPacket.get_addressing_information(self=self.mock_any_can_packet) == {
AbstractCanAddressingInformation.ADDRESSING_TYPE_NAME: None,
AbstractCanAddressingInformation.TARGET_ADDRESS_NAME: None,
AbstractCanAddressingInformation.SOURCE_ADDRESS_NAME: None,
AbstractCanAddressingInformation.ADDRESS_EXTENSION_NAME: None,
}
mock_get_addressing_information.assert_called_once_with()

@patch(f"{SCRIPT_LOCATION}.AbstractCanPacketContainer.get_addressing_information")
def test_get_addressing_information(self, mock_get_addressing_information):
assert AnyCanPacket.get_addressing_information(self=self.mock_any_can_packet) \
== mock_get_addressing_information.return_value
mock_get_addressing_information.assert_called_once_with()


@pytest.mark.integration
class TestCanPacketIntegration:
"""Integration tests for `CanPacket` class."""
Expand Down Expand Up @@ -1115,55 +952,3 @@ def test_change_addressing_information(self, init_kwargs, addressing_kwargs, ai_
can_packet = CanPacket(**init_kwargs)
can_packet.set_address_information(**addressing_kwargs)
assert can_packet.raw_frame_data[0] == ai_data_byte


@pytest.mark.integration
class TestAnyCanPacketIntegration:
"""Integration tests for `AnyCanPacket` class."""

# __init__

@pytest.mark.parametrize("init_kwargs, expected_attribute_values", [
({"raw_frame_data": [],
"addressing_format": CanAddressingFormat.EXTENDED_ADDRESSING,
"addressing_type": AddressingType.PHYSICAL,
"can_id": 0x100},
{"raw_frame_data": (),
"addressing_type": AddressingType.PHYSICAL,
"addressing_format": CanAddressingFormat.EXTENDED_ADDRESSING,
"can_id": 0x100,
"packet_type": None,
"dlc": 0,
"target_address": None,
"source_address": None,
"address_extension": None,
"payload": None,
"data_length": None,
"sequence_number": None,
"flow_status": None,
"block_size": None,
"st_min": None}),
({"raw_frame_data": [0xFE, 0xDC, 0xBA, 0x98],
"addressing_format": CanAddressingFormat.NORMAL_FIXED_ADDRESSING,
"addressing_type": AddressingType.FUNCTIONAL,
"can_id": 0x721},
{"raw_frame_data": (0xFE, 0xDC, 0xBA, 0x98),
"addressing_type": AddressingType.FUNCTIONAL,
"addressing_format": CanAddressingFormat.NORMAL_FIXED_ADDRESSING,
"can_id": 0x721,
"packet_type": 0xF,
"dlc": 4,
# "target_address": None, # exception would be raised as it cannot be decoded from invalid CAN ID
# "source_address": None, # exception would be raised as it cannot be decoded from invalid CAN ID
# "address_extension": None, # exception would be raised as it cannot be decoded from invalid AI
"payload": None,
"data_length": None,
"sequence_number": None,
"flow_status": None,
"block_size": None,
"st_min": None})
])
def test_init(self, init_kwargs, expected_attribute_values):
packet = AnyCanPacket(**init_kwargs)
for attr_name, attr_value in expected_attribute_values.items():
assert getattr(packet, attr_name) == attr_value
2 changes: 1 addition & 1 deletion uds/packet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
PacketsSequence, PacketsTuple, PacketsList, \
PacketsRecordsSequence, PacketsRecordsTuple, PacketsRecordsList
from .abstract_can_packet_container import AbstractCanPacketContainer
from .can_packet import CanPacket, AnyCanPacket
from .can_packet import CanPacket
from .can_packet_record import CanPacketRecord
Loading

0 comments on commit 5069ec8

Please sign in to comment.