Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Исправлено падение теста, тикет issue #187 (#208)
Browse files Browse the repository at this point in the history
* fixed(dbhandler): add singlton to DBHandler class

* fix: fixed type for __instance
  • Loading branch information
stepanskryabin authored Jun 8, 2022
1 parent 5d42cf9 commit 58aff06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
19 changes: 19 additions & 0 deletions mod/db/dbhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,28 @@ class DBHandler:
database tables
"""

__instance: Optional['DBHandler'] = None
_logger: Optional[str]
_loglevel: Optional[str]

def __new__(cls, *args, **kwargs):
"""
A function called when creating a new object.
If such an object already exists, returns it.
And if not, creates a new one.
Args:
cls: class DBHandler
*args:
**kwargs:
"""

if cls.__instance is None:
cls.__instance = super().__new__(cls)

return cls.__instance

def __init__(self,
uri: str = 'sqlite:/:memory:',
debug: bool = False,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_dbhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@


class TestDBHandlerMainMethods(unittest.TestCase):
db: DBHandler

@classmethod
def setUpClass(cls) -> None:
logger.remove()
cls.db = DBHandler(uri="sqlite:/:memory:")

def setUp(self):
self.db = DBHandler(uri="sqlite:/:memory:")
self.db.create_table()

def tearDown(self):
Expand Down
16 changes: 15 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,34 @@
import unittest

from loguru import logger
from mod.config.config import ConfigHandler
from mod.db.dbhandler import DBHandler
from server import app
from starlette.testclient import TestClient, WebSocketTestSession
from starlette.websockets import WebSocketDisconnect


class TestWebsocket(unittest.TestCase):
config: ConfigHandler
config_option: ConfigHandler
db: DBHandler
ws_client: TestClient

class DisconnectErr(Exception):
pass

@classmethod
def setUpClass(cls):
logger.remove()
cls.ws_client = TestClient(app)
cls.config = ConfigHandler()
cls.config_option = cls.config.read()
cls.db = DBHandler(uri=cls.config_option.uri)
cls.db.create_table()

@classmethod
def tearDownClass(cls) -> None:
del cls.db

def test_normal_connect(self):
with self.ws_client.websocket_connect("/ws") as connection:
Expand All @@ -59,7 +74,6 @@ def callback():
connection.exit_stack.callback(callback)
connection.close(1006)

@unittest.skip("Not working")
def test_send_normal_message(self):
with self.ws_client.websocket_connect("/ws") as connection:
connection.send_json({"type": "ping-pong",
Expand Down

0 comments on commit 58aff06

Please sign in to comment.