def test_cant_void_twice(self): """ Voiding a `Transaction` more than once is not permitted. """ amount = D(100) txn = TransactionFactory(self.creation_user, ledger_entries=[ LedgerEntry(amount=debit(amount), ledger=self.ar_ledger), LedgerEntry(amount=credit(amount), ledger=self.rev_ledger), ]) void_transaction(txn, self.creation_user) self.assertRaises( UnvoidableTransactionException, void_transaction, txn, self.creation_user)
def test_void_with_overridden_notes_and_type(self): """ Test voiding while setting notes and type. """ amount = D(100) evidence = UserFactory.create_batch(3) transaction = create_transaction( user=UserFactory(), evidence=evidence, ledger_entries=[ LedgerEntry( ledger=self.ar_ledger, amount=credit(amount), ), LedgerEntry( ledger=self.rev_ledger, amount=debit(amount), ), ], type=self.ttype, ) voiding_transaction = void_transaction( transaction, self.creation_user, notes='test notes', ) self.assertEqual(voiding_transaction.notes, 'test notes') self.assertEqual(voiding_transaction.type, transaction.type)
def test_can_void_void(self): """ A void can be voided, thus restoring the original transaction. """ amount = D(100) txn = TransactionFactory(self.creation_user, ledger_entries=[ LedgerEntry(amount=debit(amount), ledger=self.ar_ledger), LedgerEntry(amount=credit(amount), ledger=self.rev_ledger), ]) void_txn = void_transaction(txn, self.creation_user) self.assertEqual(void_txn.voids, txn) void_void_txn = (void_transaction(void_txn, self.creation_user)) self.assertEqual(void_void_txn.voids, void_txn) self.assertEqual(self.ar_ledger.get_balance(), amount) self.assertEqual(self.rev_ledger.get_balance(), -amount)
def test_ledger_balance_update(self): self.add_transaction([self.order_1]) self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount)), (self.order_1, self.cash_ledger, debit(self.amount)), (self.order_2, self.ar_ledger, None), (self.order_2, self.cash_ledger, None), ) self.add_transaction([self.order_2]) self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount)), (self.order_1, self.cash_ledger, debit(self.amount)), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), ) self.add_transaction([self.order_1]) self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 2), (self.order_1, self.cash_ledger, debit(self.amount) * 2), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), ) transaction = self.add_transaction([self.order_1, self.order_2]) self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 3), (self.order_1, self.cash_ledger, debit(self.amount) * 3), (self.order_2, self.ar_ledger, credit(self.amount) * 2), (self.order_2, self.cash_ledger, debit(self.amount) * 2), ) void_transaction(transaction, self.user) self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 2), (self.order_1, self.cash_ledger, debit(self.amount) * 2), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), )
def test_non_void(self): """ Test Transaction.objects.non_void filter. """ transaction_1 = self.add_transaction() self.assertFalse(self.filtered_out_by_non_void(transaction_1)) transaction_2 = self.add_transaction() self.assertFalse(self.filtered_out_by_non_void(transaction_2)) voiding_transaction = void_transaction(transaction_2, self.user) self.assertFalse(self.filtered_out_by_non_void(transaction_1)) self.assertTrue(self.filtered_out_by_non_void(transaction_2)) self.assertTrue(self.filtered_out_by_non_void(voiding_transaction))
def test_auto_timestamp(self): """ If a posted_timestamp isn't specified we assume the posted_timestamp is the same as the transaction we're voiding. """ amount = D(100) charge_txn = TransactionFactory(self.creation_user, ledger_entries=[ LedgerEntry(amount=debit(amount), ledger=self.ar_ledger), LedgerEntry(amount=credit(amount), ledger=self.rev_ledger), ]) void_txn = void_transaction(charge_txn, self.creation_user) self.assertEqual( charge_txn.posted_timestamp, void_txn.posted_timestamp)
def test_given_timestamp(self): """ If a posted_timestamp is given for the void, then use it """ amount = D(100) charge_txn = TransactionFactory(self.creation_user, ledger_entries=[ LedgerEntry(amount=debit(amount), ledger=self.ar_ledger), LedgerEntry(amount=credit(amount), ledger=self.rev_ledger), ]) now = datetime.now() void_txn = void_transaction( charge_txn, self.creation_user, posted_timestamp=now) self.assertEqual(now, void_txn.posted_timestamp)
def test_void_with_non_default_type(self): """ Test voiding a `Transaction` with a non-default `type`. """ amount = D(100) txn = TransactionFactory(self.creation_user, ledger_entries=[ LedgerEntry(amount=debit(amount), ledger=self.ar_ledger), LedgerEntry(amount=credit(amount), ledger=self.rev_ledger), ]) new_ttype = TransactionTypeFactory() void_txn = void_transaction(txn, self.creation_user, type=new_ttype) self.assertEqual(void_txn.voids, txn) self.assertEqual(self.ar_ledger.get_balance(), D(0)) self.assertEqual(self.rev_ledger.get_balance(), D(0)) self.assertEqual(void_txn.type, new_ttype) self.assertNotEqual(void_txn.type, txn.type)
def test_simple_void(self): """ Test voiding a `Transaction`. """ amount = D(100) evidence = UserFactory.create_batch(3) transaction = create_transaction( user=UserFactory(), evidence=evidence, ledger_entries=[ LedgerEntry( ledger=self.ar_ledger, amount=credit(amount), ), LedgerEntry( ledger=self.rev_ledger, amount=debit(amount), ), ], ) self.assertEqual(self.ar_ledger.get_balance(), credit(amount)) self.assertEqual(self.rev_ledger.get_balance(), debit(amount)) voiding_transaction = void_transaction(transaction, self.creation_user) self.assertEqual( set(tro.related_object for tro in voiding_transaction.related_objects.all()), set(evidence), ) self.assertEqual(self.ar_ledger.get_balance(), D(0)) self.assertEqual(self.rev_ledger.get_balance(), D(0)) self.assertEqual(voiding_transaction.voids, transaction) self.assertEqual( voiding_transaction.posted_timestamp, transaction.posted_timestamp) self.assertEqual( voiding_transaction.type, transaction.type) self.assertEqual( voiding_transaction.notes, 'Voiding transaction {}'.format(transaction))
def test_rebuild_ledger_balance(self): rebuild_ledger_balances() self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, None), (self.order_1, self.cash_ledger, None), (self.order_2, self.ar_ledger, None), (self.order_2, self.cash_ledger, None), ) self.add_transaction([self.order_1]) rebuild_ledger_balances() self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount)), (self.order_1, self.cash_ledger, debit(self.amount)), (self.order_2, self.ar_ledger, None), (self.order_2, self.cash_ledger, None), ) self.add_transaction([self.order_2]) rebuild_ledger_balances() self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount)), (self.order_1, self.cash_ledger, debit(self.amount)), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), ) self.add_transaction([self.order_1]) rebuild_ledger_balances() self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 2), (self.order_1, self.cash_ledger, debit(self.amount) * 2), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), ) transaction = self.add_transaction([self.order_1, self.order_2]) self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 3), (self.order_1, self.cash_ledger, debit(self.amount) * 3), (self.order_2, self.ar_ledger, credit(self.amount) * 2), (self.order_2, self.cash_ledger, debit(self.amount) * 2), ) void_transaction(transaction, self.user) rebuild_ledger_balances() self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 2), (self.order_1, self.cash_ledger, debit(self.amount) * 2), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), ) LedgerBalance.objects.update(balance=Decimal('1.00')) LedgerBalance.objects.first().delete() rebuild_ledger_balances() self.assert_objects_have_ledger_balances( (self.order_1, self.ar_ledger, credit(self.amount) * 2), (self.order_1, self.cash_ledger, debit(self.amount) * 2), (self.order_2, self.ar_ledger, credit(self.amount)), (self.order_2, self.cash_ledger, debit(self.amount)), )