From 07a4d3131ee647e2be7278d43a04705c72a1ab65 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 29 Dec 2018 20:55:53 +0100 Subject: [PATCH] Fixed a small bug in the SQL queries and the unit tests --- matemat/db/facade.py | 2 +- matemat/db/test/test_facade.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 9ccfd12..41274ad 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -606,7 +606,7 @@ class MatematDatabase(object): transactions: List[Transaction] = [] with self.db.transaction() as cursor: cursor.execute(''' - SELECT COALESCE(MAX(r.date), u.created), MAX(r.last_ta_id) + SELECT COALESCE(MAX(r.date), u.created), COALESCE(MAX(r.last_ta_id), 0) FROM users AS u LEFT JOIN receipts AS r ON r.user_id = u.user_id diff --git a/matemat/db/test/test_facade.py b/matemat/db/test/test_facade.py index 27b7e24..29c38e2 100644 --- a/matemat/db/test/test_facade.py +++ b/matemat/db/test/test_facade.py @@ -506,14 +506,14 @@ class DatabaseTest(unittest.TestCase): db.increment_consumption(user, product) db.deposit(user, 1337) receipt1: Receipt = db.create_receipt(user, write=True) - # Attempt to create a receipt with zero transactions. Won't be written to DB. + # Attempt to create a receipt with zero transactions. Will carry NULL transaction IDs receipt2: Receipt = db.create_receipt(user, write=True) - self.assertEqual(-1, receipt2.id) + self.assertNotEqual(-1, receipt2.id) self.assertEqual(0, len(receipt2.transactions)) with db.transaction() as c: c.execute('SELECT COUNT(receipt_id) FROM receipts') - self.assertEqual(1, c.fetchone()[0]) + self.assertEqual(2, c.fetchone()[0]) db.increment_consumption(user, product) db.change_user(user, agent=admin, balance=4200) @@ -529,7 +529,7 @@ class DatabaseTest(unittest.TestCase): with db.transaction() as c: c.execute('SELECT COUNT(receipt_id) FROM receipts') - self.assertEqual(1, c.fetchone()[0]) + self.assertEqual(2, c.fetchone()[0]) self.assertEqual(user, receipt1.user) self.assertEqual(3, len(receipt1.transactions))