Renamed DatabaseFacade to MatematDatabase and added additional documentation.

This commit is contained in:
s3lph 2018-06-06 13:54:36 +02:00
parent 8b98d5383d
commit 8eda31cbbe
3 changed files with 18 additions and 6 deletions

View file

@ -1,3 +1,3 @@
from .wrapper import DatabaseWrapper
from .facade import DatabaseFacade as Database
from .facade import MatematDatabase

View file

@ -8,21 +8,33 @@ from matemat.exceptions import AuthenticationError, DatabaseConsistencyError
from matemat.db import DatabaseWrapper
class DatabaseFacade(object):
class MatematDatabase(object):
"""
This class provides a facade that abstracts every (TM) needed database access in high level functions.
Usage example (creating a user, changing the users touchkey, login as that user and delete the user:
with MatematDatabase('/srv/matemat/production.sqlite3') as db:
user: User = db.create_user('testuser', 'supersecurepassword')
db.change_touchkey(user, 'supersecurepassword', '048cdef')
user2: User = db.login('testuser', touchkey='048cdef')
db.delete_user(user2)
"""
def __init__(self, filename: str) -> None:
"""
Create a new database facade. This does not connect to the SQLite3 database yet. To actually open the
connection, use the Context Manager 'with' syntax (PEP 343), e.g.:
with Database('path/to/database.sqlite3') as db:
with MatematDatabase('path/to/database.sqlite3') as db:
db.foo()
:param filename: The SQLite3 database file to use.
"""
self.db: DatabaseWrapper = DatabaseWrapper(filename)
def __enter__(self) -> 'DatabaseFacade':
def __enter__(self) -> 'MatematDatabase':
# Pass context manager stuff through to the database wrapper
self.db.__enter__()
return self

View file

@ -3,14 +3,14 @@ import unittest
import bcrypt
from matemat.db import Database
from matemat.db import MatematDatabase
from matemat.exceptions import AuthenticationError, DatabaseConsistencyError
class DatabaseTest(unittest.TestCase):
def setUp(self) -> None:
self.db = Database(':memory:')
self.db = MatematDatabase(':memory:')
def test_create_user(self) -> None:
with self.db as db: