From 3d25540a1f2791942eaee02d6650efe8f04f3aa4 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 23 Dec 2018 22:43:19 +0100 Subject: [PATCH 1/2] Fixed a crash in receipt generation. --- matemat/db/facade.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matemat/db/facade.py b/matemat/db/facade.py index 1c8061f..feb174a 100644 --- a/matemat/db/facade.py +++ b/matemat/db/facade.py @@ -647,7 +647,7 @@ class MatematDatabase(object): else: t = Transaction(ta_id, user, value, old_balance, datetime.fromtimestamp(date)) transactions.append(t) - if write: + if write and len(transactions) > 0: cursor.execute(''' INSERT INTO receipts (user_id, first_ta_id, last_ta_id) VALUES (:user_id, :first_ta, :last_ta) From ea70484d00992f302625468cc991bad8d53b1ae0 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 23 Dec 2018 22:50:26 +0100 Subject: [PATCH 2/2] Added a test case for empty receipts. --- matemat/db/test/test_facade.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/matemat/db/test/test_facade.py b/matemat/db/test/test_facade.py index 2ca28bf..27b7e24 100644 --- a/matemat/db/test/test_facade.py +++ b/matemat/db/test/test_facade.py @@ -506,6 +506,10 @@ 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. + receipt2: Receipt = db.create_receipt(user, write=True) + self.assertEqual(-1, receipt2.id) + self.assertEqual(0, len(receipt2.transactions)) with db.transaction() as c: c.execute('SELECT COUNT(receipt_id) FROM receipts') @@ -521,7 +525,7 @@ class DatabaseTest(unittest.TestCase): FROM users WHERE user_id = :id ''', [user.id]) - receipt2: Receipt = db.create_receipt(user, write=False) + receipt3: Receipt = db.create_receipt(user, write=False) with db.transaction() as c: c.execute('SELECT COUNT(receipt_id) FROM receipts') @@ -551,27 +555,27 @@ class DatabaseTest(unittest.TestCase): self.assertEqual(1337, t12.value) self.assertEqual(4000, t12.old_balance) - self.assertEqual(user, receipt2.user) - self.assertEqual(3, len(receipt2.transactions)) + self.assertEqual(user, receipt3.user) + self.assertEqual(3, len(receipt3.transactions)) - self.assertIsInstance(receipt2.transactions[0], Consumption) - t20: Consumption = receipt2.transactions[0] - self.assertEqual(user, receipt2.user) + self.assertIsInstance(receipt3.transactions[0], Consumption) + t20: Consumption = receipt3.transactions[0] + self.assertEqual(user, receipt3.user) self.assertEqual(-200, t20.value) self.assertEqual(5337, t20.old_balance) self.assertEqual('Flora Power Mate', t20.product) - self.assertIsInstance(receipt2.transactions[1], Modification) - t21: Modification = receipt2.transactions[1] - self.assertEqual(user, receipt2.user) + self.assertIsInstance(receipt3.transactions[1], Modification) + t21: Modification = receipt3.transactions[1] + self.assertEqual(user, receipt3.user) self.assertEqual(-937, t21.value) self.assertEqual(5137, t21.old_balance) self.assertEqual(admin.name, t21.agent) self.assertEqual(None, t21.reason) - self.assertIs(type(receipt2.transactions[2]), Transaction) - t22: Transaction = receipt2.transactions[2] - self.assertEqual(user, receipt2.user) + self.assertIs(type(receipt3.transactions[2]), Transaction) + t22: Transaction = receipt3.transactions[2] + self.assertEqual(user, receipt3.user) self.assertEqual(500, t22.value) self.assertEqual(4200, t22.old_balance)