11 lines
16 KiB
JSON
11 lines
16 KiB
JSON
|
{
|
||
|
"extension": ".py",
|
||
|
"source": "from decimal import Decimal\n\nimport datetime\nimport unittest\n\nimport pytz\n\nfrom dsmr_parser import telegram_specifications\nfrom dsmr_parser.exceptions import InvalidChecksumError, ParseError\nfrom dsmr_parser.objects import CosemObject, MBusObject\nfrom dsmr_parser.parsers import TelegramParser\nfrom test.example_telegrams import TELEGRAM_V5\n\n\nclass TelegramParserV5Test(unittest.TestCase):\n \"\"\" Test parsing of a DSMR v5.x telegram. \"\"\"\n\n def test_parse(self):\n parser = TelegramParser(telegram_specifications.V5)\n try:\n telegram = parser.parse(TELEGRAM_V5, throw_ex=True)\n except Exception as ex:\n assert False, f\"parse trigged an exception {ex}\"\n print('test: ', type(telegram.P1_MESSAGE_HEADER), telegram.P1_MESSAGE_HEADER.__dict__)\n # P1_MESSAGE_HEADER (1-3:0.2.8)\n assert isinstance(telegram.P1_MESSAGE_HEADER, CosemObject)\n assert telegram.P1_MESSAGE_HEADER.unit is None\n assert isinstance(telegram.P1_MESSAGE_HEADER.value, str)\n assert telegram.P1_MESSAGE_HEADER.value == '50'\n\n # P1_MESSAGE_TIMESTAMP (0-0:1.0.0)\n assert isinstance(telegram.P1_MESSAGE_TIMESTAMP, CosemObject)\n assert telegram.P1_MESSAGE_TIMESTAMP.unit is None\n assert isinstance(telegram.P1_MESSAGE_TIMESTAMP.value, datetime.datetime)\n assert telegram.P1_MESSAGE_TIMESTAMP.value == \\\n datetime.datetime(2017, 1, 2, 18, 20, 2, tzinfo=pytz.UTC)\n\n # ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)\n assert isinstance(telegram.ELECTRICITY_USED_TARIFF_1, CosemObject)\n assert telegram.ELECTRICITY_USED_TARIFF_1.unit == 'kWh'\n assert isinstance(telegram.ELECTRICITY_USED_TARIFF_1.value, Decimal)\n assert telegram.ELECTRICITY_USED_TARIFF_1.value == Decimal('4.426')\n\n # ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)\n assert isinstance(telegram.ELECTRICITY_USED_TARIFF_2, CosemObject)\n assert telegram.ELECTRICITY_USED_TARIFF_2.unit == 'kWh'\n assert isinstance(telegram.ELECTRICITY_USED_TARIFF_2.value, Decimal)\n assert telegram.ELECTRICITY_USED_TARIFF_2.value == Decimal('2.399')\n\n # ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)\n assert isinstance(telegram.ELECTRICITY_DELIVERED_TARIFF_1, CosemObject)\n assert telegram.ELECTRICITY_DELIVERED_TARIFF_1.unit == 'kWh'\n assert isinstance(telegram.ELECTRICITY_DELIVERED_TARIFF_1.value, Decimal)\n assert telegram.ELECTRICITY_DELIVERED_TARIFF_1.value == Decimal('2.444')\n\n # ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)\n assert isinstance(telegram.ELECTRICITY_DELIVERED_TARIFF_2, CosemObject)\n assert telegram.ELECTRICITY_DELIVERED_TARIFF_2.unit == 'kWh'\n assert isinstance(telegram.ELECTRICITY_DELIVERED_TARIFF_2.value, Decimal)\n assert telegram.ELECTRICITY_DELIVERED_TARIFF_2.value == Decimal('0')\n\n # ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)\n assert isinstance(telegram.ELECTRICITY_ACTIVE_TARIFF, CosemObject)\n assert telegram.ELECTRICITY_ACTIVE_TARIFF.unit is None\n assert isinstance(telegram.ELECTRICITY_ACTIVE_TARIFF.value, str)\n assert telegram.ELECTRICITY_ACTIVE_TARIFF.value == '0002'\n\n # EQUIPMENT_IDENTIFIER (0-0:96.1.1)\n assert isinstance(telegram.EQUIPMENT_IDENTIFIER, CosemObject)\n assert telegram.EQUIPMENT_IDENTIFIER.unit is None\n assert isinstance(telegram.EQUIPMENT_IDENTIFIER.value, str)\n assert telegram.EQUIPMENT_IDENTIFIER.value == '4B384547303034303436333935353037'\n\n # CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)\n assert isinstance(telegram.CURRENT_ELECTRICITY_USAGE, CosemObject)\n assert telegram.CURRENT_ELECTRICITY_USAGE.unit == 'kW'\n assert isinstance(telegram.CURRENT_ELECTRICITY_USAGE.value, Decimal)\n assert telegram.CURRENT_ELECTRICITY_USAGE.value == Decimal('0.244')\n\n # CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)\n assert isinstance(telegram.CURRENT_ELECTRICITY_DELIVERY, CosemObject)\
|
||
|
"review": "**7**\n\n### Bugs\n- No direct identification of issues, but exceptions could be handled much better with more detailed information.\n- The gas meter reading asserts for unit contradiction, once as 'm3' and another time as `None`.\n \n### Optimizations\n- Consider using `self.assert` over Python's `assert` statements for better unittest integration.\n- The test could be refactored to be more DRY (Don't Repeat Yourself) by creating helper functions for shared assertion patterns.\n- Error messages in exceptions could be made more informative and specific to enhance debugging.\n- Improve exception messages to clearly communicate which part of the telegram caused the error.\n\n### Good points\n- Comprehensive range of tests covering different data points ensures thorough verification.\n- Assertions use a good combination of type checks and value comparisons.\n- Proper usage of Python's unittest framework provides a structured testing approach.\n \n### Summary\nThe provided code is robust in terms of testing a broad spectrum of possible DSMR telegram parsing scenarios and covers multiple cases such as valid data, checksum validation, and error handling for corrupted content. However, it lacks clarity in exception messages and can be refactored to reduce repetitive code. Moving from Python's generic `assert` to unittest's `self.assert` can better integrate with test runners and produce more informative output during failures.\n\n### Open source alternatives\n- **DSMR-reader**: An open source application to read and visualize DSMR data.\n- **Home Assistant DSMR integration**: A component for Home Assistant that supports parsing data from a smart meter via DSMR.",
|
||
|
"filename": "test_parse_v5.py",
|
||
|
"path": "test/test_parse_v5.py",
|
||
|
"directory": "test",
|
||
|
"grade": 7,
|
||
|
"size": 14292,
|
||
|
"line_count": 275
|
||
|
}
|