add unit tests for modbus

This commit is contained in:
Stefan Allius
2024-05-05 20:20:19 +02:00
parent 29ee540a19
commit bf0f152d5a
4 changed files with 37 additions and 10 deletions

View File

@@ -1,7 +1,12 @@
# test_with_pytest.py
# import pytest, logging
from app.src.modbus import Modbus
from app.src.infos import Infos
class TestHelper(Modbus):
def __init__(self):
super().__init__()
self.db = Infos()
def test_modbus_crc():
mb = Modbus()
@@ -19,3 +24,18 @@ def test_build_modbus_pdu():
assert pdu == b'\x01\x06\x20\x00\x00\x12\x02\x07'
assert mb.check_crc(pdu)
def test_build_recv():
mb = TestHelper()
pdu = mb.build_msg(1,3,0x300e,0x2)
assert pdu == b'\x01\x03\x30\x0e\x00\x02\xaa\xc8'
assert mb.check_crc(pdu)
call = 0
for key, update in mb.recv_resp(mb.db, b'\x01\x03\x04\x01\x2c\x00\x46\xbb\xf4'):
if key == 'grid':
assert update == True
elif key == 'inverter':
assert update == True
else:
assert False
call += 1
assert 2 == call