forked from s3lph/matemat
fix: unittests, codestyle
This commit is contained in:
parent
d54aa2bc57
commit
677c1e681b
3 changed files with 12 additions and 10 deletions
|
@ -153,16 +153,17 @@ class MatematDatabase(object):
|
||||||
raise ValueError(f'A user with the name \'{username}\' already exists.')
|
raise ValueError(f'A user with the name \'{username}\' already exists.')
|
||||||
# Insert the user into the database.
|
# Insert the user into the database.
|
||||||
c.execute('''
|
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)
|
logout_after_purchase)
|
||||||
VALUES (:username, :email, :pwhash, 0, :admin, :member, STRFTIME('%s', 'now'), STRFTIME('%s', 'now'),
|
VALUES (:username, :email, :pwhash, 0, :admin, :member, STRFTIME('%s', 'now'), STRFTIME('%s', 'now'),
|
||||||
logout_after_purchase)
|
:logout_after_purchase)
|
||||||
''', {
|
''', {
|
||||||
'username': username,
|
'username': username,
|
||||||
'email': email,
|
'email': email,
|
||||||
'pwhash': pwhash,
|
'pwhash': pwhash,
|
||||||
'admin': admin,
|
'admin': admin,
|
||||||
'member': member
|
'member': member,
|
||||||
|
'logout_after_purchase': logout_after_purchase
|
||||||
})
|
})
|
||||||
# Fetch the new user's rowid.
|
# Fetch the new user's rowid.
|
||||||
c.execute('SELECT last_insert_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: int = kwargs['balance'] if 'balance' in kwargs else user.balance
|
||||||
balance_reason: Optional[str] = kwargs['balance_reason'] if 'balance_reason' in kwargs else None
|
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
|
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:
|
with self.db.transaction() as c:
|
||||||
c.execute('SELECT balance FROM users WHERE user_id = :user_id', {'user_id': user.id})
|
c.execute('SELECT balance FROM users WHERE user_id = :user_id', {'user_id': user.id})
|
||||||
row = c.fetchone()
|
row = c.fetchone()
|
||||||
|
|
|
@ -342,9 +342,9 @@ class DatabaseTest(unittest.TestCase):
|
||||||
def test_transfer(self) -> None:
|
def test_transfer(self) -> None:
|
||||||
with self.db as db:
|
with self.db as db:
|
||||||
with db.transaction() as c:
|
with db.transaction() as c:
|
||||||
user = db.create_user('testuser', '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)
|
user2 = db.create_user('testuser2', 'supersecurepassword', 'testuser@example.com', True, True, False)
|
||||||
user3 = db.create_user('testuser3', 'supersecurepassword', 'testuser@example.com', True, True)
|
user3 = db.create_user('testuser3', 'supersecurepassword', 'testuser@example.com', True, True, True)
|
||||||
c.execute('''SELECT balance FROM users WHERE user_id = ?''', [user.id])
|
c.execute('''SELECT balance FROM users WHERE user_id = ?''', [user.id])
|
||||||
self.assertEqual(0, c.fetchone()[0])
|
self.assertEqual(0, c.fetchone()[0])
|
||||||
c.execute('''SELECT balance FROM users WHERE user_id = ?''', [user2.id])
|
c.execute('''SELECT balance FROM users WHERE user_id = ?''', [user2.id])
|
||||||
|
|
|
@ -53,12 +53,12 @@ class DatabaseTest(unittest.TestCase):
|
||||||
with self.db as db:
|
with self.db as db:
|
||||||
with db.transaction() as c:
|
with db.transaction() as c:
|
||||||
c.execute('''
|
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 = db._sqlite_db.cursor()
|
||||||
c.execute("SELECT * FROM users")
|
c.execute("SELECT * FROM users")
|
||||||
user = c.fetchone()
|
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:
|
def test_transaction_rollback(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -68,7 +68,7 @@ class DatabaseTest(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
with db.transaction() as c:
|
with db.transaction() as c:
|
||||||
c.execute('''
|
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')
|
raise ValueError('This should trigger a rollback')
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
|
|
Loading…
Reference in a new issue