|
{
|
|
"extension": ".py",
|
|
"source": "from unittest.mock import Mock\n\nimport unittest\n\nfrom dsmr_parser import obis_references as obis\nfrom dsmr_parser.clients.protocol import create_dsmr_protocol\nfrom dsmr_parser.objects import Telegram\n\nTELEGRAM_V2_2 = (\n '/ISk5\\2MT382-1004\\r\\n'\n '\\r\\n'\n '0-0:96.1.1(00000000000000)\\r\\n'\n '1-0:1.8.1(00001.001*kWh)\\r\\n'\n '1-0:1.8.2(00001.001*kWh)\\r\\n'\n '1-0:2.8.1(00001.001*kWh)\\r\\n'\n '1-0:2.8.2(00001.001*kWh)\\r\\n'\n '0-0:96.14.0(0001)\\r\\n'\n '1-0:1.7.0(0001.01*kW)\\r\\n'\n '1-0:2.7.0(0000.00*kW)\\r\\n'\n '0-0:17.0.0(0999.00*kW)\\r\\n'\n '0-0:96.3.10(1)\\r\\n'\n '0-0:96.13.1()\\r\\n'\n '0-0:96.13.0()\\r\\n'\n '0-1:24.1.0(3)\\r\\n'\n '0-1:96.1.0(000000000000)\\r\\n'\n '0-1:24.3.0(161107190000)(00)(60)(1)(0-1:24.2.1)(m3)\\r\\n'\n '(00001.001)\\r\\n'\n '0-1:24.4.0(1)\\r\\n'\n '!\\r\\n'\n)\n\n\nclass ProtocolTest(unittest.TestCase):\n\n def setUp(self):\n new_protocol, _ = create_dsmr_protocol('2.2',\n telegram_callback=Mock(),\n keep_alive_interval=1)\n self.protocol = new_protocol()\n\n def test_complete_packet(self):\n \"\"\"Protocol should assemble incoming lines into complete packet.\"\"\"\n\n self.protocol.data_received(TELEGRAM_V2_2.encode('ascii'))\n\n telegram = self.protocol.telegram_callback.call_args_list[0][0][0]\n assert isinstance(telegram, Telegram)\n\n assert float(telegram[obis.CURRENT_ELECTRICITY_USAGE].value) == 1.01\n assert telegram[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'\n\n assert float(telegram[obis.GAS_METER_READING].value) == 1.001\n assert telegram[obis.GAS_METER_READING].unit == 'm3'\n\n def test_receive_packet(self):\n \"\"\"Protocol packet reception.\"\"\"\n\n mock_transport = Mock()\n self.protocol.connection_made(mock_transport)\n assert not self.protocol._active\n\n self.protocol.data_received(TELEGRAM_V2_2.encode('ascii'))\n assert self.protocol._active\n\n # 1st call of keep_alive resets 'active' flag\n self.protocol.keep_alive()\n assert not self.protocol._active\n\n # 2nd call of keep_alive should close the transport\n self.protocol.keep_alive()\n mock_transport.close.assert_called_once()\n\n self.protocol.connection_lost(None)\n",
|
|
"review": "**Grade: 7**\n\n### Bugs\n- No significant bugs were found in the code.\n\n### Optimizations\n- Use `self.assertIsInstance` instead of the `assert` statement for consistency and better error messages within unittest framework.\n- Instead of `assert`, use `self.assertEqual`, `self.assertAlmostEqual`, and `self.assertFalse` for assertions to fit within the unittest framework's conventions, which provide more informative output.\n- Applying consistent naming conventions to better clarify method purposes, such as prefixing test methods with `test_`.\n\n### Good points\n- The use of `unittest` framework which is widely accepted and provides a structured way to test in Python.\n- Code is organized with setup and teardown functionalities method which ensures each test runs in isolation, maintaining test integrity.\n- Mocks are appropriately used to simulate and assert interactions in tests.\n\n### Summary\nThe code presents a well-structured unit test for a protocol that processes data telegrams. It leverages Python's `unittest` library to ensure the functionalities of receiving and processing data packets are verified. While it is mostly well-written, it could benefit from adhering more rigorously to unittest's built-in methods for making assertions versus using base `assert` statements, as well as slightly improving method naming conventions for clarity. \n\n### Open source alternatives\n- `pytest`: A powerful alternative to `unittest`, known for its simple syntax and ability to use fixtures, which can simplify setup and teardown processes.\n- `nose2`: Another testing framework that extends unittest and offers plugin support for extensibility.",
|
|
"filename": "test_protocol.py",
|
|
"path": "test/test_protocol.py",
|
|
"directory": "test",
|
|
"grade": 7,
|
|
"size": 2327,
|
|
"line_count": 74
|
|
} |