From 4c2c454f1206dd73441c69f56d9abf25db7dfef4 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 21 Aug 2018 02:34:05 +0200 Subject: [PATCH] Full branch coverage in database facade. Also removed a redundant consistency check. --- matemat/db/facade.py | 4 ---- matemat/db/test/test_facade.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index b7f4291..5e8fffc 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -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 diff --git a/matemat/db/test/test_facade.py b/matemat/db/test/test_facade.py index 5843a29..d4ab500 100644 --- a/matemat/db/test/test_facade.py +++ b/matemat/db/test/test_facade.py @@ -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)