1
0
Fork 0
forked from s3lph/matemat

Full branch coverage in database facade. Also removed a redundant consistency check.

This commit is contained in:
s3lph 2018-08-21 02:34:05 +02:00
parent bf50d65389
commit 4c2c454f12
2 changed files with 16 additions and 4 deletions

View file

@ -300,10 +300,6 @@ class MatematDatabase(object):
'is_admin': is_admin,
'is_member': is_member
})
affected = c.execute('SELECT changes()').fetchone()[0]
if affected != 1:
raise DatabaseConsistencyError(
f'change_user should affect 1 users row, but affected {affected}')
# Only update the actual user object after the changes in the database succeeded
user.name = name
user.email = email

View file

@ -84,6 +84,12 @@ class DatabaseTest(unittest.TestCase):
with self.db as db:
with db.transaction() as c:
u = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com')
# Attempt touchkey login without a set touchkey
try:
db.login('testuser', touchkey='0123')
self.fail()
except AuthenticationError as e:
self.assertEqual('Touchkey not set', e.msg)
# Add a touchkey without using the provided function
c.execute('''UPDATE users SET touchkey = :tkhash WHERE user_id = :user_id''', {
'tkhash': crypt.crypt('0123', crypt.mksalt()),
@ -172,6 +178,9 @@ class DatabaseTest(unittest.TestCase):
self.assertFalse(checkuser.is_admin)
self.assertFalse(checkuser.is_member)
self.assertEqual(4200, checkuser.balance)
# Balance change without an agent must fail
with self.assertRaises(ValueError):
db.change_user(user, None, balance=0)
user.id = -1
with self.assertRaises(DatabaseConsistencyError):
db.change_user(user, agent, is_member='True')
@ -360,3 +369,10 @@ class DatabaseTest(unittest.TestCase):
self.assertEqual(60, c.fetchone()[0])
c.execute('''SELECT stock FROM products WHERE product_id = ?''', [fritzmate.id])
self.assertEqual(10, c.fetchone()[0])
user1.id = -1
clubmate.id = -1
with self.assertRaises(DatabaseConsistencyError):
db.increment_consumption(user1, florapowermate)
with self.assertRaises(DatabaseConsistencyError):
db.increment_consumption(user2, clubmate)