{
"extension": ".py",
"source": "import unittest\n\nfrom decimal import Decimal\n\nfrom dsmr_parser.objects import CosemObject, MBusObject\nfrom dsmr_parser.parsers import TelegramParser\nfrom dsmr_parser import telegram_specifications\nfrom dsmr_parser import obis_references as obis\nfrom test.example_telegrams import TELEGRAM_V3\n\n\nclass TelegramParserV3Test(unittest.TestCase):\n \"\"\" Test parsing of a DSMR v3 telegram. \"\"\"\n\n def test_parse(self):\n parser = TelegramParser(telegram_specifications.V3)\n try:\n result = parser.parse(TELEGRAM_V3, throw_ex=True)\n except Exception as ex:\n assert False, f\"parse trigged an exception {ex}\"\n\n # ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)\n assert result[obis.ELECTRICITY_USED_TARIFF_1].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1].value, Decimal)\n assert result[obis.ELECTRICITY_USED_TARIFF_1].value == Decimal('12345.678')\n\n # ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2], CosemObject)\n assert result[obis.ELECTRICITY_USED_TARIFF_2].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2].value, Decimal)\n assert result[obis.ELECTRICITY_USED_TARIFF_2].value == Decimal('12345.678')\n\n # ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1], CosemObject)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value, Decimal)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value == Decimal('12345.678')\n\n # ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2], CosemObject)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value, Decimal)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value == Decimal('12345.678')\n\n # ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)\n assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF], CosemObject)\n assert result[obis.ELECTRICITY_ACTIVE_TARIFF].unit is None\n assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF].value, str)\n assert result[obis.ELECTRICITY_ACTIVE_TARIFF].value == '0002'\n\n # EQUIPMENT_IDENTIFIER (0-0:96.1.1)\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER], CosemObject)\n assert result[obis.EQUIPMENT_IDENTIFIER].unit is None\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER].value, str)\n assert result[obis.EQUIPMENT_IDENTIFIER].value == '4B384547303034303436333935353037'\n\n # CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)\n assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE], CosemObject)\n assert result[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'\n assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE].value, Decimal)\n assert result[obis.CURRENT_ELECTRICITY_USAGE].value == Decimal('1.19')\n\n # CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)\n assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY], CosemObject)\n assert result[obis.CURRENT_ELECTRICITY_DELIVERY].unit == 'kW'\n assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY].value, Decimal)\n assert result[obis.CURRENT_ELECTRICITY_DELIVERY].value == Decimal('0')\n\n # TEXT_MESSAGE_CODE (0-0:96.13.1)\n assert isinstance(result[obis.TEXT_MESSAGE_CODE], CosemObject)\n assert result[obis.TEXT_MESSAGE_CODE].unit is None\n assert isinstance(result[obis.TEXT_MESSAGE_CODE].value, int)\n assert result[obis.TEXT_MESSAGE_CODE].value == 303132333435363738\n\n # TEXT_MESSAGE (0-0:96.13.0)\n assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)\n assert result[obis.TEXT_MESSAGE].unit is None\n assert isinstance(result[obis.TEXT_MESSAGE].value, str)\n assert result[obis.TEXT_MESSAGE].value == \\\n '303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E3F' \\\n '303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E3F' \\\n '303132333435363738393A3B3C3D3E3F'\n\n # DEVICE_TYPE (0-x:24.1.0)\n assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)\n assert result[obis.DEVICE_TYPE].unit is None\n assert isinstance(result[obis.DEVICE_TYPE].value, str)\n assert result[obis.DEVICE_TYPE].value == '03'\n\n # EQUIPMENT_IDENTIFIER_GAS (0-x:96.1.0)\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)\n assert result[obis.EQUIPMENT_IDENTIFIER_GAS].unit is None\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS].value, str)\n assert result[obis.EQUIPMENT_IDENTIFIER_GAS].value == '3232323241424344313233343536373839'\n\n # GAS_METER_READING (0-1:24.3.0)\n assert isinstance(result[obis.GAS_METER_READING], MBusObject)\n assert result[obis.GAS_METER_READING].unit == 'm3'\n assert isinstance(result[obis.GAS_METER_READING].value, Decimal)\n assert result[obis.GAS_METER_READING].value == Decimal('1.001')\n",
"review": "# 8\n\n## Bugs\n- None observed in the current code.\n\n## Optimizations\n- Use `self.assert*` methods from `unittest` instead of plain `assert` statements for consistency with the `unittest` framework.\n- Break down the test into smaller sub-tests using `subTest` for each section relating to different OBIS references. This improves maintainability and readability.\n- Consider using parameterized tests to reduce redundancy when asserting similar conditions across different OBIS references.\n\n## Good points\n- Comprehensive testing of various aspects of DSMR v3 telegram parsing, ensuring all critical data points are covered.\n- Use of exception handling to detect and report parsing errors during test execution.\n- Clear and logical organization of the tests, providing a good structure and flow.\n \n## Summary\nThe code provides a detailed unit test for parsing DSMR v3 telegrams, covering numerous OBIS references and possible values effectively. There are no evident bugs, but the use of the `unittest` framework could be optimized by replacing inline assertions with framework-specific methods for better integration and error reporting. The tests are well-structured and provide thorough coverage of the parsing functionality.\n\n## Open source alternatives\n- The `dmsr_parser` library seems like a specific utility for DSMR telegram parsing, and there might not be direct open-source alternatives that perform the exact function unless they're part of broader smart meter or energy management suites. Some commonly used data parsing libraries like `pandas` could potentially be adapted for similar purposes with custom function implementations.",
"filename": "test_parse_v3.py",
"path": "test/test_parse_v3.py",
"directory": "test",
"grade": 8,
"size": 5305,
"line_count": 102
}