Merge branch 'hotfix-receipt-crash' into 'staging'

Hotfix: Receipt crash

See merge request s3lph/matemat!51
This commit is contained in:
s3lph 2018-12-23 21:53:36 +00:00
commit ee87b4fb65
2 changed files with 17 additions and 13 deletions

View file

@ -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)

View file

@ -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)