2015-08-31 20:37:01 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import asyncio
|
|
|
|
import sqlite3
|
2021-03-27 12:16:42 +00:00
|
|
|
from amqtt.plugins.manager import BaseContext
|
|
|
|
from amqtt.plugins.persistence import SQLitePlugin
|
2015-08-31 20:37:01 +00:00
|
|
|
|
2021-03-14 21:16:51 +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):
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
|
|
|
|
def test_create_tables(self):
|
|
|
|
dbfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test.db")
|
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
2021-03-14 20:44:41 +00:00
|
|
|
context.config = {"persistence": {"file": dbfile}}
|
2017-08-06 22:51:23 +00:00
|
|
|
SQLitePlugin(context)
|
2015-08-31 20:37:01 +00:00
|
|
|
|
|
|
|
conn = sqlite3.connect(dbfile)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
rows = cursor.execute("SELECT name FROM sqlite_master where type = 'table'")
|
|
|
|
tables = []
|
|
|
|
for row in rows:
|
|
|
|
tables.append(row[0])
|
|
|
|
self.assertIn("session", tables)
|
|
|
|
|
2015-09-03 19:33:37 +00:00
|
|
|
# def test_save_session(self):
|
|
|
|
# dbfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test.db")
|
|
|
|
# context = BaseContext()
|
|
|
|
# context.logger = logging.getLogger(__name__)
|
|
|
|
# context.config = {
|
|
|
|
# 'persistence': {
|
|
|
|
# 'file': dbfile
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# sql_plugin = SQLitePlugin(context)
|
|
|
|
# s = Session()
|
|
|
|
# s.client_id = 'test_save_session'
|
|
|
|
# ret = self.loop.run_until_complete(sql_plugin.save_session(session=s))
|
|
|
|
#
|
|
|
|
# conn = sqlite3.connect(dbfile)
|
|
|
|
# cursor = conn.cursor()
|
|
|
|
# row = cursor.execute("SELECT client_id FROM session where client_id = 'test_save_session'").fetchone()
|
2021-03-06 17:37:23 +00:00
|
|
|
# assert len(row) == 1
|
2017-07-26 12:01:17 +00:00
|
|
|
# self.assertEqual(row[0], s.client_id)
|