From 31220b6d0af27905774bdfcc6268b04eb91a0f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Z=C3=A1hradn=C3=ADk?= Date: Fri, 25 Aug 2023 19:50:37 +0200 Subject: [PATCH] Write unit tests --- tcmenu/tagval/protocol/command_protocol.py | 7 +++ tcmenu/tagval/protocol/tag_val_menu_fields.py | 48 +++++++++++++++++++ .../tagval/protocol/tc_protocol_exception.py | 10 ++++ test/tagval/commands/test_dialog_mode.py | 12 +++++ test/tagval/protocol/test_command_protocol.py | 17 +++++++ test/tagval/protocol/test_correlation_id.py | 3 -- .../protocol/test_tag_val_menu_fields.py | 16 +++++++ .../protocol/test_tc_protocol_exception.py | 18 +++++++ 8 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 tcmenu/tagval/protocol/command_protocol.py create mode 100644 tcmenu/tagval/protocol/tag_val_menu_fields.py create mode 100644 tcmenu/tagval/protocol/tc_protocol_exception.py create mode 100644 test/tagval/commands/test_dialog_mode.py create mode 100644 test/tagval/protocol/test_command_protocol.py create mode 100644 test/tagval/protocol/test_tag_val_menu_fields.py create mode 100644 test/tagval/protocol/test_tc_protocol_exception.py diff --git a/tcmenu/tagval/protocol/command_protocol.py b/tcmenu/tagval/protocol/command_protocol.py new file mode 100644 index 0000000..fea306e --- /dev/null +++ b/tcmenu/tagval/protocol/command_protocol.py @@ -0,0 +1,7 @@ +from enum import Enum + + +class CommandProtocol(Enum): + INVALID = 0 + TAG_VAL_PROTOCOL = 1 + RAW_BIN_PROTOCOL = 2 diff --git a/tcmenu/tagval/protocol/tag_val_menu_fields.py b/tcmenu/tagval/protocol/tag_val_menu_fields.py new file mode 100644 index 0000000..a2027fc --- /dev/null +++ b/tcmenu/tagval/protocol/tag_val_menu_fields.py @@ -0,0 +1,48 @@ +from enum import Enum + + +class TagValMenuFields(Enum): + """ + Field names are used to represent the possible field names that can be sent to a remote menu. These must + be the same at both sides to be understood. All fields starting with an upper or lower case letter are + reserved. Letters starting with digits 0 to 9 are not reserved. Fields must be exactly two letters. + """ + + KEY_NAME_FIELD: str = "NM" + KEY_UUID_FIELD: str = "UU" + KEY_SERIAL_NO: str = "US" + KEY_VER_FIELD: str = "VE" + HB_FREQUENCY_FIELD: str = "HI" + HB_MODE_FIELD: str = "HR" + KEY_PLATFORM_ID: str = "PF" + KEY_BOOT_TYPE_FIELD: str = "BT" + KEY_ID_FIELD: str = "ID" + KEY_CORRELATION_FIELD: str = "IC" + KEY_EEPROM_FIELD: str = "IE" + KEY_READONLY_FIELD: str = "RO" + KEY_VISIBLE_FIELD: str = "VI" + KEY_ALPHA_FIELD: str = "RA" + KEY_WIDTH_FIELD: str = "WI" + KEY_PARENT_ID_FIELD: str = "PI" + KEY_ANALOG_MAX_FIELD: str = "AM" + KEY_ANALOG_OFFSET_FIELD: str = "AO" + KEY_ANALOG_STEP_FIELD: str = "AS" + KEY_ANALOG_DIVISOR_FIELD: str = "AD" + KEY_ANALOG_UNIT_FIELD: str = "AU" + KEY_FLOAT_DECIMAL_PLACES: str = "FD" + KEY_NEGATIVE_ALLOWED: str = "NA" + KEY_REMOTE_NUM: str = "RN" + KEY_CURRENT_VAL: str = "VC" + KEY_BOOLEAN_NAMING: str = "BN" + KEY_NO_OF_CHOICES: str = "NC" + KEY_MAX_LENGTH: str = "ML" + KEY_EDIT_TYPE: str = "EM" + KEY_PREPEND_CHOICE: str = "C" # second char from A onwards. + KEY_PREPEND_NAMECHOICE: str = "c" # second char from A onwards. + KEY_CHANGE_TYPE: str = "TC" + KEY_ACK_STATUS: str = "ST" + KEY_MODE_FIELD: str = "MO" + KEY_BUFFER_FIELD: str = "BU" + KEY_HEADER_FIELD: str = "HF" + KEY_BUTTON1_FIELD: str = "B1" + KEY_BUTTON2_FIELD: str = "B2" diff --git a/tcmenu/tagval/protocol/tc_protocol_exception.py b/tcmenu/tagval/protocol/tc_protocol_exception.py new file mode 100644 index 0000000..58739ab --- /dev/null +++ b/tcmenu/tagval/protocol/tc_protocol_exception.py @@ -0,0 +1,10 @@ +class TcProtocolException(IOError): + """An exception that indicates a problem during protocol conversion.""" + + def __init__(self, message, cause=None): + super().__init__(message) + self.cause = cause + + # If there's a cause, modify the message to include it + if self.cause: + self.args = (f"{message} (caused by: {str(cause)})",) diff --git a/test/tagval/commands/test_dialog_mode.py b/test/tagval/commands/test_dialog_mode.py new file mode 100644 index 0000000..9058a8d --- /dev/null +++ b/test/tagval/commands/test_dialog_mode.py @@ -0,0 +1,12 @@ +from tcmenu.tagval.commands.dialog_mode import DialogMode + + +def test_enum_values(): + assert DialogMode.SHOW + assert DialogMode.HIDE + assert DialogMode.ACTION + + +def test_enum_uniqueness(): + values = [mode.value for mode in DialogMode] + assert len(values) == len(set(values)), "Enum values are not unique" diff --git a/test/tagval/protocol/test_command_protocol.py b/test/tagval/protocol/test_command_protocol.py new file mode 100644 index 0000000..2e6296a --- /dev/null +++ b/test/tagval/protocol/test_command_protocol.py @@ -0,0 +1,17 @@ +import pytest + +from tcmenu.tagval.protocol.command_protocol import CommandProtocol + + +def test_valid_command_protocol(): + assert CommandProtocol(0) == CommandProtocol.INVALID + assert CommandProtocol(0).value == 0 + assert CommandProtocol(1) == CommandProtocol.TAG_VAL_PROTOCOL + assert CommandProtocol(1).value == 1 + assert CommandProtocol(2) == CommandProtocol.RAW_BIN_PROTOCOL + assert CommandProtocol(2).value == 2 + + +def test_unsupported_command_protocol(): + with pytest.raises(ValueError): + CommandProtocol(10) diff --git a/test/tagval/protocol/test_correlation_id.py b/test/tagval/protocol/test_correlation_id.py index 7c3d270..313c94b 100644 --- a/test/tagval/protocol/test_correlation_id.py +++ b/test/tagval/protocol/test_correlation_id.py @@ -1,7 +1,4 @@ -import pytest - from tcmenu.tagval.protocol.correlation_id import CorrelationId -from tcmenu.tagval.protocol.message_field import MessageField def test_correlation_id_from_string(): diff --git a/test/tagval/protocol/test_tag_val_menu_fields.py b/test/tagval/protocol/test_tag_val_menu_fields.py new file mode 100644 index 0000000..a6f912b --- /dev/null +++ b/test/tagval/protocol/test_tag_val_menu_fields.py @@ -0,0 +1,16 @@ +from tcmenu.tagval.protocol.tag_val_menu_fields import TagValMenuFields + + +def test_enum_fields_length(): + for field in TagValMenuFields: + # Fields having prepend in their name have the second char appended later. + # Check whether the const has at least 1 character. + if "prepend" in field.name.lower(): + assert len(field.value) >= 1, f"{field.name} value should be at least length 1" + else: + assert len(field.value) == 2, f"{field.name} value should be of length 2" + + +def test_enum_fields_not_start_with_digit(): + for field in TagValMenuFields: + assert not field.value[0].isdigit(), f"{field.name} value should not start with a digit" diff --git a/test/tagval/protocol/test_tc_protocol_exception.py b/test/tagval/protocol/test_tc_protocol_exception.py new file mode 100644 index 0000000..3591adc --- /dev/null +++ b/test/tagval/protocol/test_tc_protocol_exception.py @@ -0,0 +1,18 @@ +from tcmenu.tagval.protocol.tc_protocol_exception import TcProtocolException + + +def test_message_only(): + e = TcProtocolException("A test message") + assert str(e) == "A test message" + + +def test_message_with_cause(): + cause = Exception("Cause for the error") + e = TcProtocolException("A test message", cause) + assert str(e) == "A test message (caused by: Cause for the error)" + + +def test_cause_attribute(): + cause = Exception("Cause for the error") + e = TcProtocolException("A test message", cause) + assert e.cause == cause