amqtt/tests/plugins/test_persistence.py

57 wiersze
2.0 KiB
Python
Czysty Zwykły widok Historia

import asyncio
2015-08-31 20:37:01 +00:00
import logging
2024-12-29 18:23:27 +00:00
from pathlib import Path
2015-08-31 20:37:01 +00:00
import sqlite3
import unittest
from amqtt.contexts import BaseContext
2021-03-27 12:16:42 +00:00
from amqtt.plugins.persistence import SQLitePlugin
2024-12-29 18:23:27 +00:00
from amqtt.session import Session
2015-08-31 20:37:01 +00:00
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
2015-08-31 20:37:01 +00:00
logging.basicConfig(level=logging.DEBUG, format=formatter)
class TestSQLitePlugin(unittest.TestCase):
2024-12-29 18:23:27 +00:00
def setUp(self) -> None:
2015-08-31 20:37:01 +00:00
self.loop = asyncio.new_event_loop()
2024-12-29 18:23:27 +00:00
def test_create_tables(self) -> None:
dbfile = Path(__file__).resolve().parent / "test.db"
2015-08-31 20:37:01 +00:00
context = BaseContext()
context.logger = logging.getLogger(__name__)
2024-12-29 18:23:27 +00:00
context.config = {"persistence": {"file": str(dbfile)}} # Ensure string path for config
SQLitePlugin(context)
2015-08-31 20:37:01 +00:00
2024-12-29 18:23:27 +00:00
try:
conn = sqlite3.connect(str(dbfile)) # Convert Path to string for sqlite connection
cursor = conn.cursor()
rows = cursor.execute("SELECT name FROM sqlite_master WHERE type = 'table'")
tables = [row[0] for row in rows] # List comprehension for brevity
assert "session" in tables
finally:
conn.close()
def test_save_session(self) -> None:
dbfile = Path(__file__).resolve().parent / "test.db"
context = BaseContext()
context.logger = logging.getLogger(__name__)
context.config = {"persistence": {"file": str(dbfile)}} # Ensure string path for config
sql_plugin = SQLitePlugin(context)
s = Session()
s.client_id = "test_save_session"
self.loop.run_until_complete(sql_plugin.save_session(session=s))
try:
conn = sqlite3.connect(str(dbfile)) # Convert Path to string for sqlite connection
cursor = conn.cursor()
row = cursor.execute("SELECT client_id FROM session WHERE client_id = 'test_save_session'").fetchone()
assert row is not None
assert row[0] == s.client_id
finally:
conn.close()