forked from s3lph/matemat
Merge branch 'staging' into 'master'
Fixed: Crash when creating a receipt with zero transactions See merge request s3lph/matemat!52
This commit is contained in:
commit
1e6e9aabb2
2 changed files with 17 additions and 13 deletions
|
@ -647,7 +647,7 @@ class MatematDatabase(object):
|
||||||
else:
|
else:
|
||||||
t = Transaction(ta_id, user, value, old_balance, datetime.fromtimestamp(date))
|
t = Transaction(ta_id, user, value, old_balance, datetime.fromtimestamp(date))
|
||||||
transactions.append(t)
|
transactions.append(t)
|
||||||
if write:
|
if write and len(transactions) > 0:
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT INTO receipts (user_id, first_ta_id, last_ta_id)
|
INSERT INTO receipts (user_id, first_ta_id, last_ta_id)
|
||||||
VALUES (:user_id, :first_ta, :last_ta)
|
VALUES (:user_id, :first_ta, :last_ta)
|
||||||
|
|
|
@ -506,6 +506,10 @@ class DatabaseTest(unittest.TestCase):
|
||||||
db.increment_consumption(user, product)
|
db.increment_consumption(user, product)
|
||||||
db.deposit(user, 1337)
|
db.deposit(user, 1337)
|
||||||
receipt1: Receipt = db.create_receipt(user, write=True)
|
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:
|
with db.transaction() as c:
|
||||||
c.execute('SELECT COUNT(receipt_id) FROM receipts')
|
c.execute('SELECT COUNT(receipt_id) FROM receipts')
|
||||||
|
@ -521,7 +525,7 @@ class DatabaseTest(unittest.TestCase):
|
||||||
FROM users
|
FROM users
|
||||||
WHERE user_id = :id
|
WHERE user_id = :id
|
||||||
''', [user.id])
|
''', [user.id])
|
||||||
receipt2: Receipt = db.create_receipt(user, write=False)
|
receipt3: Receipt = db.create_receipt(user, write=False)
|
||||||
|
|
||||||
with db.transaction() as c:
|
with db.transaction() as c:
|
||||||
c.execute('SELECT COUNT(receipt_id) FROM receipts')
|
c.execute('SELECT COUNT(receipt_id) FROM receipts')
|
||||||
|
@ -551,27 +555,27 @@ class DatabaseTest(unittest.TestCase):
|
||||||
self.assertEqual(1337, t12.value)
|
self.assertEqual(1337, t12.value)
|
||||||
self.assertEqual(4000, t12.old_balance)
|
self.assertEqual(4000, t12.old_balance)
|
||||||
|
|
||||||
self.assertEqual(user, receipt2.user)
|
self.assertEqual(user, receipt3.user)
|
||||||
self.assertEqual(3, len(receipt2.transactions))
|
self.assertEqual(3, len(receipt3.transactions))
|
||||||
|
|
||||||
self.assertIsInstance(receipt2.transactions[0], Consumption)
|
self.assertIsInstance(receipt3.transactions[0], Consumption)
|
||||||
t20: Consumption = receipt2.transactions[0]
|
t20: Consumption = receipt3.transactions[0]
|
||||||
self.assertEqual(user, receipt2.user)
|
self.assertEqual(user, receipt3.user)
|
||||||
self.assertEqual(-200, t20.value)
|
self.assertEqual(-200, t20.value)
|
||||||
self.assertEqual(5337, t20.old_balance)
|
self.assertEqual(5337, t20.old_balance)
|
||||||
self.assertEqual('Flora Power Mate', t20.product)
|
self.assertEqual('Flora Power Mate', t20.product)
|
||||||
|
|
||||||
self.assertIsInstance(receipt2.transactions[1], Modification)
|
self.assertIsInstance(receipt3.transactions[1], Modification)
|
||||||
t21: Modification = receipt2.transactions[1]
|
t21: Modification = receipt3.transactions[1]
|
||||||
self.assertEqual(user, receipt2.user)
|
self.assertEqual(user, receipt3.user)
|
||||||
self.assertEqual(-937, t21.value)
|
self.assertEqual(-937, t21.value)
|
||||||
self.assertEqual(5137, t21.old_balance)
|
self.assertEqual(5137, t21.old_balance)
|
||||||
self.assertEqual(admin.name, t21.agent)
|
self.assertEqual(admin.name, t21.agent)
|
||||||
self.assertEqual(None, t21.reason)
|
self.assertEqual(None, t21.reason)
|
||||||
|
|
||||||
self.assertIs(type(receipt2.transactions[2]), Transaction)
|
self.assertIs(type(receipt3.transactions[2]), Transaction)
|
||||||
t22: Transaction = receipt2.transactions[2]
|
t22: Transaction = receipt3.transactions[2]
|
||||||
self.assertEqual(user, receipt2.user)
|
self.assertEqual(user, receipt3.user)
|
||||||
self.assertEqual(500, t22.value)
|
self.assertEqual(500, t22.value)
|
||||||
self.assertEqual(4200, t22.old_balance)
|
self.assertEqual(4200, t22.old_balance)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue