forked from s3lph/matemat
Renamed DatabaseFacade to MatematDatabase and added additional documentation.
This commit is contained in:
parent
8b98d5383d
commit
8eda31cbbe
3 changed files with 18 additions and 6 deletions
|
@ -1,3 +1,3 @@
|
||||||
|
|
||||||
from .wrapper import DatabaseWrapper
|
from .wrapper import DatabaseWrapper
|
||||||
from .facade import DatabaseFacade as Database
|
from .facade import MatematDatabase
|
||||||
|
|
|
@ -8,21 +8,33 @@ from matemat.exceptions import AuthenticationError, DatabaseConsistencyError
|
||||||
from matemat.db import DatabaseWrapper
|
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:
|
def __init__(self, filename: str) -> None:
|
||||||
"""
|
"""
|
||||||
Create a new database facade. This does not connect to the SQLite3 database yet. To actually open the
|
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.:
|
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()
|
db.foo()
|
||||||
|
|
||||||
:param filename: The SQLite3 database file to use.
|
:param filename: The SQLite3 database file to use.
|
||||||
"""
|
"""
|
||||||
self.db: DatabaseWrapper = DatabaseWrapper(filename)
|
self.db: DatabaseWrapper = DatabaseWrapper(filename)
|
||||||
|
|
||||||
def __enter__(self) -> 'DatabaseFacade':
|
def __enter__(self) -> 'MatematDatabase':
|
||||||
# Pass context manager stuff through to the database wrapper
|
# Pass context manager stuff through to the database wrapper
|
||||||
self.db.__enter__()
|
self.db.__enter__()
|
||||||
return self
|
return self
|
||||||
|
|
|
@ -3,14 +3,14 @@ import unittest
|
||||||
|
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
|
||||||
from matemat.db import Database
|
from matemat.db import MatematDatabase
|
||||||
from matemat.exceptions import AuthenticationError, DatabaseConsistencyError
|
from matemat.exceptions import AuthenticationError, DatabaseConsistencyError
|
||||||
|
|
||||||
|
|
||||||
class DatabaseTest(unittest.TestCase):
|
class DatabaseTest(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
self.db = Database(':memory:')
|
self.db = MatematDatabase(':memory:')
|
||||||
|
|
||||||
def test_create_user(self) -> None:
|
def test_create_user(self) -> None:
|
||||||
with self.db as db:
|
with self.db as db:
|
||||||
|
|
Loading…
Reference in a new issue