Skip to content

Commit

Permalink
Write unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vzahradnik committed Aug 25, 2023
1 parent e6b8082 commit 31220b6
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 3 deletions.
7 changes: 7 additions & 0 deletions 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
48 changes: 48 additions & 0 deletions 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"
10 changes: 10 additions & 0 deletions 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)})",)
12 changes: 12 additions & 0 deletions 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"
17 changes: 17 additions & 0 deletions 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)
3 changes: 0 additions & 3 deletions 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():
Expand Down
16 changes: 16 additions & 0 deletions 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"
18 changes: 18 additions & 0 deletions 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

0 comments on commit 31220b6

Please sign in to comment.