* Code Cleanup (#158) * print coverage report * create sonar-project property file * install all py dependencies in one step * code cleanup * reduce cognitive complexity * do not build on *.yml changes * optimise versionstring handling (#159) - Reading the version string from the image updates it even if the image is re-pulled without re-deployment * fix linter warning * exclude *.pyi filese * ignore some rules for tests * cleanup (#160) * Sonar qube 3 (#163) fix SonarQube warnings in modbus.py * Sonar qube 3 (#164) * fix SonarQube warnings * Sonar qube 3 (#165) * cleanup * Add support for TSUN Titan inverter Fixes #161 * fix SonarQube warnings * fix error * rename field "config" * SonarQube reads flake8 output * don't stop on flake8 errors * flake8 scan only app/src for SonarQube * update flake8 run * ignore flake8 C901 * cleanup * fix linter warnings * ignore changed *.yml files * read sensor list solarman data packets * catch 'No route to' error and log only in debug mode * fix unit tests * add sensor_list configuration * adapt unit tests * fix SonarQube warnings * Sonar qube 3 (#166) * add unittests for mqtt.py * add mock * move test requirements into a file * fix unit tests * fix formating * initial version * fix SonarQube warning * Sonar qube 4 (#169) * add unit test for inverter.py * fix SonarQube warning * Sonar qube 5 (#170) * fix SonarLints warnings * use random IP adresses for unit tests * Docker: The description ist missing (#171) Fixes #167 * S allius/issue167 (#172) * cleanup * Sonar qube 6 (#174) * test class ModbusConn * Sonar qube 3 (#178) * add more unit tests * GEN3: don't crash on overwritten msg in the receive buffer * improve test coverage und reduce test delays * reduce cognitive complexity * fix merge * fix merge conflikt * fix merge conflict * S allius/issue182 (#183) * GEN3: After inverter firmware update the 'Unknown Msg Type' increases continuously Fixes #182 * add support for Controller serial no and MAC * test hardening * GEN3: add support for new messages of version 3 firmwares * bump libraries to latest versions - bump aiomqtt to version 2.3.0 - bump aiohttp to version 3.10.5 * improve test coverage * reduce cognective complexity * fix target preview * remove dubbled fixtures * increase test coverage * Update README.md (#185) update badges * S allius/issue186 (#187) * Parse more values in Server Mode Fixes #186 * read OUTPUT_COEFFICIENT and MAC_ADDR in SrvMode * fix unit test * increase test coverage * S allius/issue186 (#188) * increase test coverage * update changelog * add dokumentation * change default config * Update README.md (#189) Config file is now foldable
237 lines
6.5 KiB
Python
237 lines
6.5 KiB
Python
# test_with_pytest.py
|
|
import pytest
|
|
import asyncio
|
|
|
|
from mock import patch
|
|
from enum import Enum
|
|
from app.src.infos import Infos
|
|
from app.src.config import Config
|
|
from app.src.inverter import Inverter
|
|
from app.src.singleton import Singleton
|
|
from app.src.gen3plus.connection_g3p import ConnectionG3P
|
|
from app.src.gen3plus.inverter_g3p import InverterG3P
|
|
|
|
from app.tests.test_modbus_tcp import patch_mqtt_err, patch_mqtt_except, test_port, test_hostname
|
|
|
|
|
|
pytest_plugins = ('pytest_asyncio',)
|
|
|
|
# initialize the proxy statistics
|
|
Infos.static_init()
|
|
|
|
@pytest.fixture
|
|
def config_conn():
|
|
Config.act_config = {
|
|
'mqtt':{
|
|
'host': test_hostname,
|
|
'port': test_port,
|
|
'user': '',
|
|
'passwd': ''
|
|
},
|
|
'ha':{
|
|
'auto_conf_prefix': 'homeassistant',
|
|
'discovery_prefix': 'homeassistant',
|
|
'entity_prefix': 'tsun',
|
|
'proxy_node_id': 'test_1',
|
|
'proxy_unique_id': ''
|
|
},
|
|
'solarman':{'enabled': True, 'host': 'test_cloud.local', 'port': 1234}, 'inverters':{'allow_all':True}
|
|
}
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
def module_init():
|
|
Singleton._instances.clear()
|
|
yield
|
|
|
|
@pytest.fixture
|
|
def patch_conn_init():
|
|
with patch.object(ConnectionG3P, '__init__', return_value= None) as conn:
|
|
yield conn
|
|
|
|
@pytest.fixture
|
|
def patch_conn_close():
|
|
with patch.object(ConnectionG3P, 'close') as conn:
|
|
yield conn
|
|
|
|
class FakeReader():
|
|
def __init__(self):
|
|
self.on_recv = asyncio.Event()
|
|
async def read(self, max_len: int):
|
|
await self.on_recv.wait()
|
|
return b''
|
|
def feed_eof(self):
|
|
return
|
|
|
|
|
|
class FakeWriter():
|
|
def write(self, buf: bytes):
|
|
return
|
|
def get_extra_info(self, sel: str):
|
|
if sel == 'peername':
|
|
return 'remote.intern'
|
|
elif sel == 'sockname':
|
|
return 'sock:1234'
|
|
assert False
|
|
def is_closing(self):
|
|
return False
|
|
def close(self):
|
|
return
|
|
async def wait_closed(self):
|
|
return
|
|
|
|
class TestType(Enum):
|
|
RD_TEST_0_BYTES = 1
|
|
RD_TEST_TIMEOUT = 2
|
|
RD_TEST_EXCEPT = 3
|
|
|
|
|
|
test = TestType.RD_TEST_0_BYTES
|
|
|
|
@pytest.fixture
|
|
def patch_open_connection():
|
|
async def new_conn(conn):
|
|
await asyncio.sleep(0)
|
|
return FakeReader(), FakeWriter()
|
|
|
|
def new_open(host: str, port: int):
|
|
global test
|
|
if test == TestType.RD_TEST_TIMEOUT:
|
|
raise ConnectionRefusedError
|
|
elif test == TestType.RD_TEST_EXCEPT:
|
|
raise ValueError("Value cannot be negative") # Compliant
|
|
return new_conn(None)
|
|
|
|
with patch.object(asyncio, 'open_connection', new_open) as conn:
|
|
yield conn
|
|
|
|
|
|
def test_method_calls(patch_conn_init, patch_conn_close):
|
|
spy1 = patch_conn_init
|
|
spy2 = patch_conn_close
|
|
reader = FakeReader()
|
|
writer = FakeWriter()
|
|
addr = ('proxy.local', 10000)
|
|
inverter = InverterG3P(reader, writer, addr, client_mode=False)
|
|
inverter.l_addr = ''
|
|
inverter.r_addr = ''
|
|
|
|
spy1.assert_called_once()
|
|
spy1.assert_called_once_with(reader, writer, addr, None, server_side=True, client_mode=False)
|
|
|
|
inverter.close()
|
|
spy2.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_remote_conn(config_conn, patch_open_connection, patch_conn_close):
|
|
_ = config_conn
|
|
_ = patch_open_connection
|
|
assert asyncio.get_running_loop()
|
|
|
|
spy1 = patch_conn_close
|
|
|
|
inverter = InverterG3P(FakeReader(), FakeWriter(), ('proxy.local', 10000), client_mode=False)
|
|
|
|
await inverter.async_create_remote()
|
|
await asyncio.sleep(0)
|
|
assert inverter.remote_stream
|
|
inverter.close()
|
|
spy1.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_remote_except(config_conn, patch_open_connection, patch_conn_close):
|
|
_ = config_conn
|
|
_ = patch_open_connection
|
|
assert asyncio.get_running_loop()
|
|
|
|
spy1 = patch_conn_close
|
|
|
|
global test
|
|
test = TestType.RD_TEST_TIMEOUT
|
|
|
|
inverter = InverterG3P(FakeReader(), FakeWriter(), ('proxy.local', 10000), client_mode=False)
|
|
|
|
await inverter.async_create_remote()
|
|
await asyncio.sleep(0)
|
|
assert inverter.remote_stream==None
|
|
|
|
test = TestType.RD_TEST_EXCEPT
|
|
await inverter.async_create_remote()
|
|
await asyncio.sleep(0)
|
|
assert inverter.remote_stream==None
|
|
inverter.close()
|
|
spy1.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_mqtt_publish(config_conn, patch_open_connection, patch_conn_close):
|
|
_ = config_conn
|
|
_ = patch_open_connection
|
|
assert asyncio.get_running_loop()
|
|
|
|
spy1 = patch_conn_close
|
|
|
|
Inverter.class_init()
|
|
|
|
inverter = InverterG3P(FakeReader(), FakeWriter(), ('proxy.local', 10000), client_mode=False)
|
|
inverter._SolarmanV5__set_serial_no(snr= 123344)
|
|
|
|
inverter.new_data['inverter'] = True
|
|
inverter.db.db['inverter'] = {}
|
|
await inverter.async_publ_mqtt()
|
|
assert inverter.new_data['inverter'] == False
|
|
|
|
inverter.new_data['env'] = True
|
|
inverter.db.db['env'] = {}
|
|
await inverter.async_publ_mqtt()
|
|
assert inverter.new_data['env'] == False
|
|
|
|
Infos.new_stat_data['proxy'] = True
|
|
await inverter.async_publ_mqtt()
|
|
assert Infos.new_stat_data['proxy'] == False
|
|
|
|
inverter.close()
|
|
spy1.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_mqtt_err(config_conn, patch_open_connection, patch_mqtt_err, patch_conn_close):
|
|
_ = config_conn
|
|
_ = patch_open_connection
|
|
_ = patch_mqtt_err
|
|
assert asyncio.get_running_loop()
|
|
|
|
spy1 = patch_conn_close
|
|
|
|
Inverter.class_init()
|
|
|
|
inverter = InverterG3P(FakeReader(), FakeWriter(), ('proxy.local', 10000), client_mode=False)
|
|
inverter._SolarmanV5__set_serial_no(snr= 123344)
|
|
|
|
inverter.new_data['inverter'] = True
|
|
inverter.db.db['inverter'] = {}
|
|
await inverter.async_publ_mqtt()
|
|
assert inverter.new_data['inverter'] == True
|
|
|
|
inverter.close()
|
|
spy1.assert_called_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_mqtt_except(config_conn, patch_open_connection, patch_mqtt_except, patch_conn_close):
|
|
_ = config_conn
|
|
_ = patch_open_connection
|
|
_ = patch_mqtt_except
|
|
assert asyncio.get_running_loop()
|
|
|
|
spy1 = patch_conn_close
|
|
|
|
Inverter.class_init()
|
|
|
|
inverter = InverterG3P(FakeReader(), FakeWriter(), ('proxy.local', 10000), client_mode=False)
|
|
inverter._SolarmanV5__set_serial_no(snr= 123344)
|
|
|
|
inverter.new_data['inverter'] = True
|
|
inverter.db.db['inverter'] = {}
|
|
await inverter.async_publ_mqtt()
|
|
assert inverter.new_data['inverter'] == True
|
|
|
|
inverter.close()
|
|
spy1.assert_called_once()
|