Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init(self):
config = {"example_item": "test"}
database = Database(config)
self.assertEqual("", database.name)
self.assertEqual("test", database.config["example_item"])
async def test_disconnect(self):
database = Database({})
try:
await database.disconnect()
except NotImplementedError:
self.fail("disconnect() raised NotImplementedError unexpectedly!")
"""A mocked database module."""
from opsdroid.database import Database
class DatabaseTest(Database):
"""The mocked database class."""
def __init__(self, config, opsdroid=None):
"""Start the class."""
assert opsdroid is not None
pass
async def test_put(self):
database = Database({})
with self.assertRaises(NotImplementedError):
await database.put("test", {})
async def test_unload(self):
with OpsDroid() as opsdroid:
mock_connector = Connector({}, opsdroid=opsdroid)
mock_connector.disconnect = amock.CoroutineMock()
opsdroid.connectors = [mock_connector]
mock_database = Database({})
mock_database.disconnect = amock.CoroutineMock()
opsdroid.memory.databases = [mock_database]
mock_skill = amock.Mock(config={"name": "mockskill"})
opsdroid.skills = [mock_skill]
opsdroid.web_server = Web(opsdroid)
opsdroid.web_server.stop = amock.CoroutineMock()
mock_web_server = opsdroid.web_server
opsdroid.cron_task = amock.CoroutineMock()
opsdroid.cron_task.cancel = amock.CoroutineMock()
mock_cron_task = opsdroid.cron_task
async def task():
await asyncio.sleep(0.5)
async def test_get(self):
database = Database({})
with self.assertRaises(NotImplementedError):
await database.get("test")
import logging
import json
import aiosqlite
from opsdroid.const import DEFAULT_ROOT_PATH
from opsdroid.database import Database
from opsdroid.helper import JSONEncoder, JSONDecoder
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = {"file": str, "table": str}
# pylint: disable=too-few-public-methods
# As the current module needs only one public method to register json types
class DatabaseSqlite(Database):
"""A sqlite database class.
SQLite Database class used to persist data in sqlite.
"""
def __init__(self, config, opsdroid=None):
"""Initialise the sqlite database.
Set basic properties of the database. Initialise properties like
name, connection arguments, database file, table name and config.
Args:
config (dict): The configuration of the database which consists
of `file` and `table` name of the sqlite database
specified in `configuration.yaml` file.
Iterates through all the database modules parsed
in the argument, connects and starts them.
Args:
databases (list): A list of all database modules to be started.
"""
if not databases:
_LOGGER.debug(databases)
_LOGGER.warning(_("All databases failed to load."))
for database_module in databases:
for name, cls in database_module["module"].__dict__.items():
if (
isinstance(cls, type)
and issubclass(cls, Database)
and cls is not Database
):
_LOGGER.debug(_("Adding database: %s."), name)
database = cls(database_module["config"], opsdroid=self)
self.memory.databases.append(database)
await database.connect()
Iterates through all the database modules parsed
in the argument, connects and starts them.
Args:
databases (list): A list of all database modules to be started.
"""
if not databases:
_LOGGER.debug(databases)
_LOGGER.warning(_("All databases failed to load."))
for database_module in databases:
for name, cls in database_module["module"].__dict__.items():
if (
isinstance(cls, type)
and issubclass(cls, Database)
and cls is not Database
):
_LOGGER.debug(_("Adding database: %s."), name)
database = cls(database_module["config"], opsdroid=self)
self.memory.databases.append(database)
await database.connect()
"""Module for storing data within Redis."""
import json
import logging
import aioredis
from aioredis import parser
from voluptuous import Any
from opsdroid.database import Database
from opsdroid.helper import JSONEncoder, JSONDecoder
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = {"host": str, "port": Any(int, str), "database": int, "password": str}
class RedisDatabase(Database):
"""Database class for storing data within a Redis instance."""
def __init__(self, config, opsdroid=None):
"""Initialise the redis database.
Set basic properties of the database. Initialise properties like
name, connection arguments, database file, table name and config.
Args:
config (dict): The configuration of the database which consists
of `file` and `table` name of the sqlite database
specified in `configuration.yaml` file.
opsdroid (OpsDroid): An instance of opsdroid.core.
"""
super().__init__(config, opsdroid=opsdroid)