From a8e0687095872d95ef573aac7331778c7bd7082f Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 9 Sep 2018 03:53:52 +0200 Subject: [PATCH] Fix: Fetch receipt_pref in list_users. --- matemat/db/facade.py | 10 +++++++--- matemat/db/test/test_facade.py | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 039961f..4447b39 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -86,15 +86,19 @@ class MatematDatabase(object): users: List[User] = [] with self.db.transaction(exclusive=False) as c: for row in c.execute(''' - SELECT user_id, username, email, is_admin, is_member, balance + SELECT user_id, username, email, is_admin, is_member, balance, receipt_pref FROM users WHERE touchkey IS NOT NULL OR NOT :must_have_touchkey ''', { 'must_have_touchkey': with_touchkey }): # Decompose each row and put the values into a User object - user_id, username, email, is_admin, is_member, balance = row - users.append(User(user_id, username, balance, email, is_admin, is_member)) + user_id, username, email, is_admin, is_member, balance, receipt_p = row + try: + receipt_pref: ReceiptPreference = ReceiptPreference(receipt_p) + except ValueError: + raise DatabaseConsistencyError(f'{receipt_p} is not a valid ReceiptPreference') + users.append(User(user_id, username, balance, email, is_admin, is_member, receipt_pref)) return users def get_user(self, uid: int) -> User: diff --git a/matemat/db/test/test_facade.py b/matemat/db/test/test_facade.py index 19e19c3..5441c00 100644 --- a/matemat/db/test/test_facade.py +++ b/matemat/db/test/test_facade.py @@ -58,7 +58,8 @@ class DatabaseTest(unittest.TestCase): testuser: User = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com', True, True) db.change_touchkey(testuser, '', 'touchkey', verify_password=False) db.create_user('anothertestuser', 'otherpassword', 'anothertestuser@example.com', False, True) - db.create_user('yatu', 'igotapasswordtoo', 'yatu@example.com', False, False) + u = db.create_user('yatu', 'igotapasswordtoo', 'yatu@example.com', False, False) + db.change_user(u, agent=None, receipt_pref=ReceiptPreference.WEEKLY) users = db.list_users() users_with_touchkey = db.list_users(with_touchkey=True) self.assertEqual(3, len(users)) @@ -76,6 +77,7 @@ class DatabaseTest(unittest.TestCase): self.assertEqual('yatu@example.com', user.email) self.assertFalse(user.is_member) self.assertFalse(user.is_admin) + self.assertEqual(ReceiptPreference.WEEKLY, user.receipt_pref) usercheck[user.id] = 1 self.assertEqual(3, len(usercheck)) self.assertEqual(1, len(users_with_touchkey))