|
"source": "import unittest\n\nfrom decimal import Decimal\n\nfrom dsmr_parser.exceptions import InvalidChecksumError, ParseError\nfrom dsmr_parser.objects import CosemObject\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_ISKRA_IE\n\n\nclass TelegramParserIskraIETest(unittest.TestCase):\n \"\"\" Test parsing of a Iskra IE5 telegram. \"\"\"\n\n def test_parse(self):\n parser = TelegramParser(telegram_specifications.ISKRA_IE)\n try:\n result = parser.parse(TELEGRAM_ISKRA_IE, throw_ex=True)\n except Exception as ex:\n assert False, f\"parse trigged an exception {ex}\"\n\n # EQUIPMENT_IDENTIFIER_GAS (0-0: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 == '09610'\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('10.181')\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('10.182')\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('10.281')\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('10.282')\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 == '0001'\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('0.170')\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.270')\n\n # INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE (1-0:21.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value == Decimal('0.217')\n\n # INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE (1-0:41.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value == Decimal('0.417')\n\n # INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE (1-0:61.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value == Decimal('0.617')\n\n # INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE (1-0:22.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value == Decimal('0.227')\n\n # INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE (1-0:42.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value == Decimal('0.427')\n\n # INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE (1-0:62.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value == Decimal('0.627')\n\n # INSTANTANEOUS_VOLTAGE_L1 (1-0:32.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L1], CosemObject)\n assert result[obis.INSTANTANEOUS_VOLTAGE_L1].unit == 'V'\n assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L1].value, Decimal)\n assert result[obis.INSTANTANEOUS_VOLTAGE_L1].value == Decimal('242.5')\n\n # INSTANTANEOUS_VOLTAGE_L2 (1-0:52.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L2], CosemObject)\n assert result[obis.INSTANTANEOUS_VOLTAGE_L2].unit == 'V'\n assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L2].value, Decimal)\n assert result[obis.INSTANTANEOUS_VOLTAGE_L2].value == Decimal('241.7')\n\n # INSTANTANEOUS_VOLTAGE_L3 (1-0:72.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L3], CosemObject)\n assert result[obis.INSTANTANEOUS_VOLTAGE_L3].unit == 'V'\n assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L3].value, Decimal)\n assert result[obis.INSTANTANEOUS_VOLTAGE_L3].value == Decimal('243.3')\n\n # INSTANTANEOUS_CURRENT_L1 (1-0:31.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L1], CosemObject)\n assert result[obis.INSTANTANEOUS_CURRENT_L1].unit == 'A'\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L1].value, Decimal)\n assert result[obis.INSTANTANEOUS_CURRENT_L1].value == Decimal('0.000')\n\n # INSTANTANEOUS_CURRENT_L2 (1-0:51.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L2], CosemObject)\n assert result[obis.INSTANTANEOUS_CURRENT_L2].unit == 'A'\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L2].value, Decimal)\n assert result[obis.INSTANTANEOUS_CURRENT_L2].value == Decimal('0.000')\n\n # INSTANTANEOUS_CURRENT_L3 (1-0:71.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L3], CosemObject)\n assert result[obis.INSTANTANEOUS_CURRENT_L3].unit == 'A'\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L3].value, Decimal)\n assert result[obis.INSTANTANEOUS_CURRENT_L3].value == Decimal('0.000')\n\n # ACTUAL_SWITCH_POSITION (0-0:96.3.10)\n assert isinstance(result[obis.ACTUAL_SWITCH_POSITION], CosemObject)\n assert result[obis.ACTUAL_SWITCH_POSITION].unit is None\n assert isinstance(result[obis.ACTUAL_SWITCH_POSITION].value, str)\n assert result[obis.ACTUAL_SWITCH_POSITION].value == '1'\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 result[obis.TEXT_MESSAGE].value is None\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 result[obis.EQUIPMENT_IDENTIFIER].value is None\n\n def test_checksum_valid(self):\n # No exception is raised.\n TelegramParser.validate_checksum(TELEGRAM_ISKRA_IE)\n\n def test_checksum_invalid(self):\n # Remove the electricty used data value. This causes the checksum to not match anymore.\n corrupted_telegram = TELEGRAM_ISKRA_IE.replace(\n '1-0:1.8.1(000010.181*kWh)\\r\\n',\n ''\n )\n\n with self.assertRaises(InvalidChecksumError):\n TelegramParser.validate_checksum(corrupted_telegram)\n\n def test_checksum_missing(self):\n # Remove the checksum value causing a ParseError.\n corrupted_telegram = TELEGRAM_ISKRA_IE.replace('!AD3B\\r\\n', '')\n with self.assertRaises(ParseError):\n TelegramParser.validate_checksum(corrupted_telegram)\n",
|
|
"review": "# 8\n\n## Bugs\n- No significant bugs identified in the test code.\n\n## Optimizations\n- Use `self.assertIsInstance` and `self.assertEqual` instead of `assert` statements for better test framework integration.\n- Consider using `setUp` method to initialize common variables to reduce repetition.\n- Group similar assertions together for better readability and performance where possible.\n\n## Good points\n- The code is well-structured and highly readable.\n- Comprehensive test coverage for various scenarios including valid parsing and checksum cases as well as invalid ones.\n- Use of specific exceptions (`InvalidChecksumError`, `ParseError`) enhances clarity and debugging.\n- Efficient use of the `unittest` framework for structured testing.\n\n## Summary\nThe code is a well-developed test suite for the `TelegramParser` class related to parsing Iskra IE5 telegrams. It effectively checks both normal and edge cases, including parsing and validation operations. Optimization could be done by using more features from the `unittest` module to standardize and simplify assertion checks, thereby improving maintainability.\n\n## Open source alternatives\n- **PyDSM**: A Python library providing an interface for reading DSMR telegrams, capturing similar functionality.\n- **dsmr-parser-python**: Another Python parser specifically designed for DSMR telegrams, very similar to the approach used here.",
|