11 lines
26 KiB
JSON
Raw Normal View History

2024-12-29 19:47:35 +00:00
{
"extension": ".py",
"source": "import json\nimport unittest\nimport datetime\nimport pytz\n\nfrom dsmr_parser import telegram_specifications, obis_references\n\nfrom dsmr_parser.objects import CosemObject\nfrom dsmr_parser.objects import MBusObject\nfrom dsmr_parser.objects import ProfileGenericObject\nfrom dsmr_parser.parsers import TelegramParser\nfrom test.example_telegrams import TELEGRAM_V4_2, TELEGRAM_V5_TWO_MBUS, TELEGRAM_V5\nfrom decimal import Decimal\n\n\nclass TelegramTest(unittest.TestCase):\n \"\"\" Test instantiation of Telegram object \"\"\"\n\n def __init__(self, *args, **kwargs):\n self.item_names_tested = []\n super(TelegramTest, self).__init__(*args, **kwargs)\n\n def verify_telegram_item(self, telegram, testitem_name, object_type, unit_val, value_type, value_val):\n testitem = eval(\"telegram.{}\".format(testitem_name))\n assert isinstance(testitem, object_type)\n assert testitem.unit == unit_val\n assert isinstance(testitem.value, value_type)\n assert testitem.value == value_val\n self.item_names_tested.append(testitem_name)\n\n def test_instantiate(self):\n parser = TelegramParser(telegram_specifications.V4)\n telegram = parser.parse(TELEGRAM_V4_2)\n\n # P1_MESSAGE_HEADER (1-3:0.2.8)\n self.verify_telegram_item(telegram,\n 'P1_MESSAGE_HEADER',\n object_type=CosemObject,\n unit_val=None,\n value_type=str,\n value_val='42')\n\n # P1_MESSAGE_TIMESTAMP (0-0:1.0.0)\n self.verify_telegram_item(telegram,\n 'P1_MESSAGE_TIMESTAMP',\n CosemObject,\n unit_val=None,\n value_type=datetime.datetime,\n value_val=datetime.datetime(2016, 11, 13, 19, 57, 57, tzinfo=pytz.UTC))\n\n # ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)\n self.verify_telegram_item(telegram,\n 'ELECTRICITY_USED_TARIFF_1',\n object_type=CosemObject,\n unit_val='kWh',\n value_type=Decimal,\n value_val=Decimal('1581.123'))\n\n # ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)\n self.verify_telegram_item(telegram,\n 'ELECTRICITY_USED_TARIFF_2',\n object_type=CosemObject,\n unit_val='kWh',\n value_type=Decimal,\n value_val=Decimal('1435.706'))\n\n # ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)\n self.verify_telegram_item(telegram,\n 'ELECTRICITY_DELIVERED_TARIFF_1',\n object_type=CosemObject,\n unit_val='kWh',\n value_type=Decimal,\n value_val=Decimal('0'))\n\n # ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)\n self.verify_telegram_item(telegram,\n 'ELECTRICITY_DELIVERED_TARIFF_2',\n object_type=CosemObject,\n unit_val='kWh',\n value_type=Decimal,\n value_val=Decimal('0'))\n\n # ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)\n self.verify_telegram_item(telegram,\n 'ELECTRICITY_ACTIVE_TARIFF',\n object_type=CosemObject,\n unit_val=None,\n value_type=str,\n value_val='0002')\n\n # EQUIPMENT_IDENTIFIER (0-0:96.1.1)\n self.verify_telegram_item(telegram,\n
"review": "**7**\n\n### Bugs\n- No specific bugs found in the code.\n\n### Optimizations\n- Consider replacing `eval()` with a safer alternative to access object attributes to avoid potential security issues.\n- Instead of using manual assertions, consider using unittest's built-in methods such as `assertIsInstance()`, `assertEqual()`, etc., which provide better readability and error messages.\n\n### Good points\n- Extensive use of unit tests covering various scenarios to ensure the correctness of the parser.\n- Use of helper function `verify_telegram_item` reduces code duplication and enhances readability.\n- The code follows a consistent pattern in its testing methods, making it easy to follow.\n- The use of descriptive variable names aids in understanding the purpose of the tests.\n- The tests ensure that the data is correctly parsed and transformed into the expected output formats, including JSON and string representations.\n\n### Summary\nThe code presented is a test suite for a DSMR (Dutch Smart Meter Requirements) telegram parser. The suite thoroughly tests the parsing of V4 and V5 telegram data into expected object structures, values, and types. It covers a wide range of test cases to ensure the parser accurately processes DSMR telegrams into both object attributes and JSON outputs. Although the test suite is well-structured and effective, using `eval()` can pose a security risk, and employing unittest's built-in assertion methods could improve the code's clarity and maintainability.\n\n### Open source alternatives\n- **PyDSMR**: An open-source library to parse DSMR data, focusing on extensibility and Pythonic conventions.\n- **dsmr-parser**: A simple DSMR parser library for extracting information from DSMR telegrams.",
"filename": "test_telegram.py",
"path": "test/objects/test_telegram.py",
"directory": "objects",
"grade": 7,
"size": 23715,
"line_count": 487
}