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