Skip to content

Commit

Permalink
Rename sqlite database location config option to 'path' to match othe…
Browse files Browse the repository at this point in the history
…r config (#1481)
  • Loading branch information
jacobtomlinson committed May 18, 2020
1 parent f1041c0 commit 962ad80
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
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"

0 comments on commit 962ad80

Please sign in to comment.