update to 1.0.2

This commit is contained in:
Norbert
2024-07-12 12:13:55 +02:00
parent 3a0fc1f9cd
commit 577596d9f3
44 changed files with 5860 additions and 1957 deletions

View File

@@ -0,0 +1,51 @@
"""Module containing a connection class."""
from pathlib import Path
from .cursor import Cursor
from .error import Error
open_connections_file = Path(__file__).parent / "open_connections.txt"
class Connection:
"""Class mocking a connection to a MySQL server."""
def __init__(self, *args, consider_open=True, **kwargs):
self.open_connections += consider_open
self.open: bool = consider_open
def cursor(self) -> Cursor:
"""Return the cursor for the connection."""
self._cursor = Cursor()
return self._cursor
def close(self):
"""Close connection."""
if not self.open:
raise Error("Connection already closed")
self.open = False
self.open_connections -= 1
def commit(self):
"""Save all changes to the database."""
def is_connected(self) -> bool:
"""Return if the connection is open."""
return self.open
@property
def open_connections(self):
with open(open_connections_file, "r", encoding="utf-8") as f:
contents = f.read()
return int(contents)
@open_connections.setter
def open_connections(self, value: int):
value = value if value > 0 else 0
with open(open_connections_file, "w", encoding="utf-8") as f:
f.write(str(value))

View File

@@ -0,0 +1,10 @@
"""Module with mock implementation of interacting with a MySQL server."""
from .connection import Connection
from .error import Error
def connect(*args, **kwargs) -> Connection:
"""Mock connect to a MySQL server."""
return Connection()

View File

@@ -0,0 +1,56 @@
"""Module with cursor implementation."""
import json
from pathlib import Path
from .error import Error
config_file = Path(__file__).parent / "cursor_config.json"
class Cursor:
"""Cursor of a MySQL connection."""
def __init__(self, raise_err: bool = True):
if self._raise_error_in_init and raise_err:
raise Error
def execute(self, *args, **kwargs) -> None:
"""Mock execute a query."""
if self._raise_error:
raise Error
def executemany(self, *args, **kwargs) -> None:
"""Mock execute a query multiple times with different data."""
if self._raise_error:
raise Error
@property
def _raise_error(self):
return self._read_configs()["raise_error"]
@property
def _raise_error_in_init(self):
return self._read_configs()["raise_error_in_init"]
@_raise_error.setter
def _raise_error(self, value: int):
data = self._read_configs()
data["raise_error"] = value
self._save_configs(data)
@_raise_error_in_init.setter
def _raise_error_in_init(self, value: int):
data = self._read_configs()
data["raise_error_in_init"] = value
self._save_configs(data)
def _read_configs(self):
with open(config_file, "r", encoding="utf-8") as f:
return json.loads(f.read())
def _save_configs(self, data):
with open(config_file, "w", encoding="utf-8") as f:
f.write(json.dumps(data, indent=4))

View File

@@ -0,0 +1,4 @@
{
"raise_error_in_init": false,
"raise_error": false
}

View File

@@ -0,0 +1,5 @@
"""Module containing the basic error class of this package."""
class Error(Exception):
"""Basic error class of this package."""

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,30 @@
"""Class to mimick `pymodbus.client`. It is used when running the tests."""
from collections import namedtuple
from .registers_output import raw_holding_register_values, raw_input_register_values
from .utils import getnext
Registers = namedtuple("Registers", ["registers"])
class ModbusSerialClient:
"""Class mimicking `pymodbus.client.ModbusSerialClient`."""
def __init__(self, *_, **__) -> None:
self._holding_registers_gen = getnext(raw_holding_register_values)
self._input_registers_gen = getnext(raw_input_register_values)
def connect(self):
"""Mimick connecting the inverter and return the success code."""
return True
def read_holding_registers(self, count, *_, **__):
"""Read holding register values from inverter."""
return Registers([next(self._holding_registers_gen) for _ in range(count)])
def read_input_registers(self, count, *_, **__):
"""Read input register values from inverter."""
return Registers([next(self._input_registers_gen) for _ in range(count)])

View File

@@ -0,0 +1,806 @@
"""Raw register values, just read from the inverter."""
raw_input_register_values = [
2364,
40,
1243,
6295,
7371,
14,
6,
4996,
44,
2,
940,
484,
0,
0,
0,
0,
0,
0,
0,
0,
4281,
2,
128,
1,
29,
1,
0,
2,
98,
39141,
0,
0,
4,
45357,
0,
82,
52,
300,
12288,
0,
1,
0,
52,
10,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
47644,
31,
50010,
5,
0,
0,
0,
0,
569,
0,
3930,
5,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2359,
40,
937,
4997,
2323,
13,
45,
4998,
2354,
15,
261,
4998,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2,
0,
2,
0,
3696,
2,
1075,
0,
0,
0,
691,
0,
0,
0,
2,
0,
24610,
5,
680,
0,
4130,
0,
175,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
213,
190,
3344,
3329,
0,
94,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
64289,
65535,
65535,
65535,
2338,
0,
55563,
65535,
5000,
0,
60536,
65535,
0,
0,
0,
0,
245,
0,
12042,
0,
0,
0,
100,
40,
4,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]
raw_holding_register_values = [
18483,
13396,
12597,
18489,
12338,
12848,
13363,
21359,
27745,
30752,
8224,
8224,
8224,
8224,
8224,
8224,
8224,
8224,
8224,
8224,
8224,
1800,
900,
15,
900,
1955,
2645,
4950,
5050,
1,
0,
2599,
1955,
2645,
4750,
5150,
275,
100,
0,
100,
100,
100,
94,
90,
20,
50,
80,
100,
105,
100,
6,
0,
0,
2139,
2231,
2369,
2461,
0,
10,
0,
0,
20,
5,
0,
7500,
58036,
4755,
5005,
1955,
2530,
0,
0,
4755,
5005,
1955,
2530,
0,
1,
900,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
29,
0,
1,
1,
0,
6,
27,
8,
43,
58,
14,
19,
3,
24,
0,
0,
1,
0,
0,
200,
210,
0,
10240,
30,
25610,
12830,
0,
0,
0,
15127,
0,
0,
0,
0,
0,
30,
0,
0,
0,
0,
0,
0,
0,
2450,
0,
21328,
20056,
19521,
20808,
22360,
1,
0,
0,
0,
0,
0,
60000,
1500,
0,
20,
0,
15000,
0,
0,
300,
100,
100,
100,
300,
10,
10,
10,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
32,
0,
0,
1,
5020,
5,
0,
0,
0,
0,
2014,
4980,
2,
5,
0,
30,
5020,
4980,
0,
0,
0,
0,
30000,
900,
900,
30000,
30000,
1000,
900,
2200,
2530,
2576,
2070,
10,
0,
100,
100,
100,
0,
10,
1,
0,
0,
0,
0,
0,
3,
1,
0,
1,
1,
2,
0,
0,
0,
100,
0,
1,
0,
0,
0,
0,
1,
0,
2668,
1818,
0,
0,
0,
0,
0,
0,
30,
0,
0,
0,
2000,
80,
1000,
40,
30,
800,
0,
0,
0,
0,
0,
0,
0,
0,
0,
500,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
20,
95,
1000,
30,
60,
0,
15127,
0,
2000,
0,
0,
2000,
30,
80,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]

View File

@@ -0,0 +1,11 @@
"""Mock `pymodbus` utils."""
from typing import Any
def getnext(iterable: list, default: Any = 0):
"""Yield one item at a time and yield `default` when iterable is exhausted."""
yield from iterable
while True:
yield default