11 lines
15 KiB
JSON
11 lines
15 KiB
JSON
|
{
|
||
|
"extension": ".py",
|
||
|
"source": "from decimal import Decimal\nimport datetime\nimport unittest\n\nimport pytz\n\nfrom dsmr_parser import obis_references as obis\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_V4_2\n\n\nclass TelegramParserV4_2Test(unittest.TestCase):\n \"\"\" Test parsing of a DSMR v4.2 telegram. \"\"\"\n\n def test_parse(self):\n parser = TelegramParser(telegram_specifications.V4)\n try:\n result = parser.parse(TELEGRAM_V4_2, throw_ex=True)\n except Exception as ex:\n assert False, f\"parse trigged an exception {ex}\"\n\n # P1_MESSAGE_HEADER (1-3:0.2.8)\n assert isinstance(result[obis.P1_MESSAGE_HEADER], CosemObject)\n assert result[obis.P1_MESSAGE_HEADER].unit is None\n assert isinstance(result[obis.P1_MESSAGE_HEADER].value, str)\n assert result[obis.P1_MESSAGE_HEADER].value == '42'\n\n # P1_MESSAGE_TIMESTAMP (0-0:1.0.0)\n assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP], CosemObject)\n assert result[obis.P1_MESSAGE_TIMESTAMP].unit is None\n assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP].value, datetime.datetime)\n assert result[obis.P1_MESSAGE_TIMESTAMP].value == \\\n datetime.datetime(2016, 11, 13, 19, 57, 57, tzinfo=pytz.UTC)\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('1581.123')\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('1435.706')\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('0')\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('0')\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 == '3960221976967177082151037881335713'\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('2.027')\n\n # CURRENT_ELEC
|
||
|
"review": "# Grade: 8\n\n## Bugs\n- No identified bugs.\n\n## Optimizations\n- The `assert` statements could be replaced with `self.assertEqual()` or other appropriate `unittest` functions to take full advantage of Python's `unittest` capabilities.\n- Consolidate repetitive code by potentially using loops or helper functions, especially for testing similar properties across multiple data points.\n- Consider using `setUp` and `tearDown` methods in `unittest` to initialize and clean up the test environment, which can improve readability and maintenability.\n\n## Good points\n- The test comprehensively covers a wide range of data points for parsing DSMR v4.2 telegrams.\n- Includes tests for both valid and corrupted checksums, enhancing the robustness of the test suite.\n- Uses comprehensive assertions to check the correctness of parsed values against expected results.\n- Proper use of `Decimal` for financial and energy data, ensuring precision.\n\n## Summary\nThe `TelegramParserV4_2Test` class provides a thorough suite of tests for parsing DSMR v4.2 telegrams. The integration with obis references and assertions for each data field ensures data integrity in parsing test cases. Corrections are suggested for employing `unittest` assert methods for improved diagnostics and potentially restructuring for common test logic to enhance maintainability.\n\n## Open source alternatives\n- **dsmr-reader**: A web application that reads DSMR telegrams and offers a user-friendly interface to analyze energy usage.\n- **SMAP**: A Simple Measurement and Actuation Profile system to manage and process smart meter data inputs.",
|
||
|
"filename": "test_parse_v4_2.py",
|
||
|
"path": "test/test_parse_v4_2.py",
|
||
|
"directory": "test",
|
||
|
"grade": 8,
|
||
|
"size": 13023,
|
||
|
"line_count": 249
|
||
|
}
|