Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename sqlite database location config option to 'path' to match other config #1481

Merged
merged 1 commit into from May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/databases/sqlite.md
Expand Up @@ -7,7 +7,7 @@ A database module for opsdroid to persist memory in a [SQLite](https://www.sqlit
```yaml
databases:
sqlite:
file: "my_file.db" # (optional) default "~/.opsdroid/sqlite.db"
path: "my_file.db" # (optional) default "~/.opsdroid/sqlite.db"
table: "my_table" # (optional) default "opsdroid"
```

Expand Down
17 changes: 9 additions & 8 deletions opsdroid/database/sqlite/__init__.py
Expand Up @@ -9,7 +9,7 @@
from opsdroid.helper import JSONEncoder, JSONDecoder

_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = {"file": str, "table": str}
CONFIG_SCHEMA = {"path": str, "file": str, "table": str}

# pylint: disable=too-few-public-methods
# As the current module needs only one public method to register json types
Expand Down Expand Up @@ -39,8 +39,14 @@ def __init__(self, config, opsdroid=None):
self.name = "sqlite"
self.config = config
self.conn_args = {"isolation_level": None}
self.db_file = None
self.table = None
if "file" in self.config:
self.db_file = self.config["file"]
_LOGGER.warn("The option 'file' is deprecated, please use 'path' instead.")
else:
self.db_file = self.config.get(
"path", os.path.join(DEFAULT_ROOT_PATH, "sqlite.db")
)
self.table = self.config.get("table", "opsdroid")
_LOGGER.debug(_("Loaded sqlite database connector"))

async def connect(self):
Expand All @@ -55,11 +61,6 @@ async def connect(self):
opsdroid (OpsDroid): An instance of opsdroid core.

"""
self.db_file = self.config.get(
"file", os.path.join(DEFAULT_ROOT_PATH, "sqlite.db")
)
self.table = self.config.get("table", "opsdroid")

self.client = await aiosqlite.connect(self.db_file, **self.conn_args)

cur = await self.client.cursor()
Expand Down
19 changes: 11 additions & 8 deletions tests/test_database_sqlite.py
Expand Up @@ -25,14 +25,13 @@ def test_init(self):

This method will test the initialisation of the database
class. It will assert if the database class properties are
declared and equated to None.
declared.

"""
database = DatabaseSqlite({"file": "sqlite.db"})
database = DatabaseSqlite({"path": "sqlite.db"})
self.assertEqual(None, database.client)
self.assertEqual(None, database.database)
self.assertEqual(None, database.db_file)
self.assertEqual(None, database.table)
self.assertEqual("sqlite.db", database.db_file)
self.assertEqual("opsdroid", database.table)
self.assertEqual({"isolation_level": None}, database.conn_args)


Expand All @@ -50,7 +49,7 @@ async def test_connect(self):
As the database is created `opsdroid` table is created first.

"""
database = DatabaseSqlite({"file": "sqlite.db"})
database = DatabaseSqlite({"path": "sqlite.db"})
opsdroid = amock.CoroutineMock()
opsdroid.eventloop = self.loop

Expand All @@ -71,7 +70,7 @@ async def test_disconnect(self):
This method will test the database disconnection of sqlite database.

"""
database = DatabaseSqlite({"file": "sqlite.db"})
database = DatabaseSqlite({"path": "sqlite.db"})
opsdroid = amock.CoroutineMock()
opsdroid.eventloop = self.loop

Expand All @@ -92,7 +91,7 @@ async def test_get_put_and_delete(self):
followed by the `delete` operation which deletes the key.

"""
database = DatabaseSqlite({"file": "sqlite.db"})
database = DatabaseSqlite({"path": "sqlite.db"})
opsdroid = amock.CoroutineMock()
opsdroid.eventloop = self.loop

Expand All @@ -110,3 +109,7 @@ async def test_get_put_and_delete(self):
self.assertEqual("opsdroid", table)
self.assertEqual({}, data)
self.assertEqual("Connection", client)

async def test_deprecated_path(self):
database = DatabaseSqlite({"file": "sqlite.db"})
assert database.db_file == "sqlite.db"