Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RawDataRecord with unit tests #297

Merged
merged 10 commits into from
Nov 28, 2024
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytest
from mock import Mock, patch

from uds.database.abstract_data_record import AbstractDataRecord
from uds.database.data_record import AbstractDataRecord
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved

SCRIPT_LOCATION = "uds.database.abstract_data_record"
SCRIPT_LOCATION = "uds.database.data_record"
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved


class TestAbstractDataRecord:
Expand Down
26 changes: 26 additions & 0 deletions tests/software_tests/database/data_record/test_raw_data_record.py
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved
n135c10r marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from uds.database.data_record import RawDataRecord
from uds.database.data_record.abstract_data_record import DataRecordType


def test_raw_data_record_initialization():
record = RawDataRecord(name="TestRawDataRecord", length=16)
assert record.name == "TestRawDataRecord"
assert record.length == 16
assert record.data_record_type == DataRecordType.RAW
assert record.is_reoccurring is False
assert record.min_occurrences == 1
assert record.max_occurrences == 1
assert record.contains == ()
assert record.decode(1234) == 1234
assert record.encode(1234) == 1234


def test_raw_data_record_invalid_name():
with pytest.raises(TypeError):
RawDataRecord(name=123, length=16)

def test_raw_data_record_invalid_length():
with pytest.raises(ValueError):
RawDataRecord(name="TestRecord", length=-1)
2 changes: 1 addition & 1 deletion uds/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
Tools for decoding and encoding information from/to diagnostic messages.
"""

from .abstract_data_record import AbstractDataRecord, DataRecordType, DecodedDataRecord
from .data_record import RawDataRecord
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions uds/database/data_record/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Package with implementation for all type of Data Records.
"""
__all__ = ["AbstractDataRecord", "DataRecordType", "DecodedDataRecord", "RawDataRecord"]

from .abstract_data_record import AbstractDataRecord, DataRecordType, DecodedDataRecord
from .raw_data_record import RawDataRecord
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
meaningful information (e.g. physical value, text).
"""

__all__ = ["DataRecordType", "AbstractDataRecord", "DecodedDataRecord"]
__all__ = ["DataRecordType", "AbstractDataRecord", "DecodedDataRecord", "DataRecordPhysicalValueAlias"]

from abc import ABC, abstractmethod
from typing import Tuple, TypedDict, Union, Optional
Expand All @@ -28,12 +28,12 @@ class DataRecordType(ValidatedEnum):
"""All Data Record types."""

# TODO: fill with following tasks:
# - https://github.com/mdabrowski1990/uds/issues/2
# - https://github.com/mdabrowski1990/uds/issues/6
# - https://github.com/mdabrowski1990/uds/issues/8
# - https://github.com/mdabrowski1990/uds/issues/9
# - https://github.com/mdabrowski1990/uds/issues/10

RAW: "DataRecordType" = "RAW" # type: ignore

class AbstractDataRecord(ABC):
"""Common implementation and interface for all Data Records."""
Expand Down
89 changes: 89 additions & 0 deletions uds/database/data_record/raw_data_record.py
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from typing import Optional, Tuple

from .abstract_data_record import AbstractDataRecord, DataRecordType, DataRecordPhysicalValueAlias, DecodedDataRecord


class RawDataRecord(AbstractDataRecord):
"""Common implementation and interface for Raw Data Record."""

def __init__(self, name: str, length: int) -> None:
"""
Initialization of Raw Data Record.

:param name: Name to assign to this Data Record.
:param length: Number of bits that this Raw Data Record is stored over.

:raise TypeError: Provided name is not str type.
:raise ValueError: Provided length is not a positive integer.
"""
super().__init__(name)
if not isinstance(length, int) or length <= 0:
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("Length must be a positive integer.")
self.__length = length

@property
def data_record_type(self) -> DataRecordType:
"""Type of this Data Record."""
return DataRecordType.RAW

@property
def length(self) -> int:
"""Get number of bits that this Data Record is stored over."""
return self.__length

@property
def is_reoccurring(self) -> bool:
"""
Whether this Data Record might occur multiple times.

Values meaning:
- False - exactly one occurrence in every diagnostic message
- True - number of occurrences might vary
"""
return False

@property
def min_occurrences(self) -> int:
"""
Minimal number of this Data Record occurrences.

.. note:: Relevant only if :attr:`~uds.database.data_record.raw_data_record.RawDataRecord.is_reoccurring`
equals True.
"""
return 1

@property
def max_occurrences(self) -> Optional[int]:
"""
Maximal number of this Data Record occurrences.

.. note:: Relevant only if :attr:`~uds.database.data_record.raw_data_record.RawDataRecord.is_reoccurring`
equals True.
.. warning:: No maximal number (infinite number of occurrences) is represented by None value.
"""
return 1

@property
def contains(self) -> Tuple[AbstractDataRecord, ...]:
"""Get Data Records contained by this Data Record."""
return ()

def decode(self, raw_value: int) -> DecodedDataRecord:
"""
Decode physical value for provided raw value.

:param raw_value: Raw (bit) value of Data Record.

:return: Dictionary with physical value for this Data Record.
"""
return DecodedDataRecord(name=self.name, raw_value=raw_value, physical_value="Raw Data Record")
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved

def encode(self, physical_value: DataRecordPhysicalValueAlias) -> int:
"""
Encode raw value for provided physical value.

:param physical_value: Physical (meaningful e.g. float, str type) value of this Data Record.

:return: Raw Value of this Data Record.
"""
return physical_value # type: ignore
mdabrowski1990 marked this conversation as resolved.
Show resolved Hide resolved
Loading