Added a test case for empty receipts.

This commit is contained in:
s3lph 2018-12-23 22:50:26 +01:00
parent 3d25540a1f
commit ea70484d00

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)