From 677c1e681b128aba45f6f426374a9e5e76daead5 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 9 Apr 2024 22:40:38 +0200 Subject: [PATCH] fix: unittests, codestyle --- matemat/db/facade.py | 10 ++++++---- matemat/db/test/test_facade.py | 6 +++--- matemat/db/test/test_wrapper.py | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 9ed5b15..4a9d8dd 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -153,16 +153,17 @@ class MatematDatabase(object): raise ValueError(f'A user with the name \'{username}\' already exists.') # Insert the user into the database. c.execute(''' - INSERT INTO users (username, email, password, balance, is_admin, is_member, lastchange, created, + INSERT INTO users (username, email, password, balance, is_admin, is_member, lastchange, created, logout_after_purchase) VALUES (:username, :email, :pwhash, 0, :admin, :member, STRFTIME('%s', 'now'), STRFTIME('%s', 'now'), - logout_after_purchase) + :logout_after_purchase) ''', { 'username': username, 'email': email, 'pwhash': pwhash, 'admin': admin, - 'member': member + 'member': member, + 'logout_after_purchase': logout_after_purchase }) # Fetch the new user's rowid. c.execute('SELECT last_insert_rowid()') @@ -286,7 +287,8 @@ class MatematDatabase(object): balance: int = kwargs['balance'] if 'balance' in kwargs else user.balance balance_reason: Optional[str] = kwargs['balance_reason'] if 'balance_reason' in kwargs else None receipt_pref: ReceiptPreference = kwargs['receipt_pref'] if 'receipt_pref' in kwargs else user.receipt_pref - logout_after_purchase: bool = kwargs['logout_after_purchase'] if 'logout_after_purchase' in kwargs else user.logout_after_purchase + logout_after_purchase: bool = \ + kwargs['logout_after_purchase'] if 'logout_after_purchase' in kwargs else user.logout_after_purchase with self.db.transaction() as c: c.execute('SELECT balance FROM users WHERE user_id = :user_id', {'user_id': user.id}) row = c.fetchone() diff --git a/matemat/db/test/test_facade.py b/matemat/db/test/test_facade.py index 738f957..2fe4409 100644 --- a/matemat/db/test/test_facade.py +++ b/matemat/db/test/test_facade.py @@ -342,9 +342,9 @@ class DatabaseTest(unittest.TestCase): def test_transfer(self) -> None: with self.db as db: with db.transaction() as c: - user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com', True, True) - user2 = db.create_user('testuser2', 'supersecurepassword', 'testuser@example.com', True, True) - user3 = db.create_user('testuser3', 'supersecurepassword', 'testuser@example.com', True, True) + user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com', True, True, False) + user2 = db.create_user('testuser2', 'supersecurepassword', 'testuser@example.com', True, True, False) + user3 = db.create_user('testuser3', 'supersecurepassword', 'testuser@example.com', True, True, True) c.execute('''SELECT balance FROM users WHERE user_id = ?''', [user.id]) self.assertEqual(0, c.fetchone()[0]) c.execute('''SELECT balance FROM users WHERE user_id = ?''', [user2.id]) diff --git a/matemat/db/test/test_wrapper.py b/matemat/db/test/test_wrapper.py index d661c4f..27fc9e7 100644 --- a/matemat/db/test/test_wrapper.py +++ b/matemat/db/test/test_wrapper.py @@ -53,12 +53,12 @@ class DatabaseTest(unittest.TestCase): with self.db as db: with db.transaction() as c: c.execute(''' - INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0, 0) + INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0, 0, 0) ''') c = db._sqlite_db.cursor() c.execute("SELECT * FROM users") user = c.fetchone() - self.assertEqual((1, 'testuser', None, 'supersecurepassword', None, 1, 1, 0, 42, 0, 0), user) + self.assertEqual((1, 'testuser', None, 'supersecurepassword', None, 1, 1, 0, 42, 0, 0, 0), user) def test_transaction_rollback(self) -> None: """ @@ -68,7 +68,7 @@ class DatabaseTest(unittest.TestCase): try: with db.transaction() as c: c.execute(''' - INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0, 0) + INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42, 0, 0, 0) ''') raise ValueError('This should trigger a rollback') except ValueError as e: