Пример #1
0
class EntryTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username='******')
        self.user.save()

        self.group = Group(name='group1', slug='group1')
        self.group.save()

        self.account = Account(name='account1',
                               slug='account1',
                               group=self.group)
        self.account.save()

        self.transaction = Transaction(group=self.group)
        self.transaction.save()

        self.entry = TransactionEntry(account=self.account,
                                      debit=100,
                                      credit=100,
                                      transaction=self.transaction)

    def tearDown(self):
        self.transaction.delete()
        self.account.delete()
        self.group.delete()
        self.user.delete()

    def testDebitAndCreditInSameEntry(self):
        """Checks that setting both debit and credit does not fail"""

        self.entry.debit = 100
        self.entry.credit = 100
        self.entry.save()

    def testNegativeCredit(self):
        """Checks that inputing av negative credit raises an error"""

        self.entry.credit = -100
        with self.assertRaises(InvalidTransactionEntry):
            self.entry.save()

    def testNegativeDebit(self):
        """Checks that inputing av negative debit  raises an error"""

        self.entry.debit = -100
        with self.assertRaises(InvalidTransactionEntry):
            self.entry.save()

    def testDebitAndCreditSetToZero(self):
        """Checks that setting both debit and credit to zero raises error"""

        self.entry.debit = 0
        self.entry.credit = 0
        with self.assertRaises(InvalidTransactionEntry):
            self.entry.save()
Пример #2
0
class LogTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username='******')
        self.user.save()

        self.group = Group(name='Group 1', slug='group1')
        self.group.save()

        self.transaction = Transaction(group=self.group)
        self.transaction.set_pending(user=self.user)

    def tearDown(self):
        self.transaction.delete()
        self.group.delete()
        self.user.delete()

    def testLogEntryUniqePerType(self):
        """Checks that only one log entry of each type is allowed (except for
        pending)
        """

        for key, value in Transaction.TRANSACTION_STATE:
            log1 = TransactionLog(type=key,
                                  transaction=self.transaction,
                                  user=self.user)
            log2 = TransactionLog(type=key,
                                  transaction=self.transaction,
                                  user=self.user)
            if key != Transaction.PENDING_STATE:
                log1.save()
                with self.assertRaises(InvalidTransactionLog):
                    log2.save()

    def testLogEntryModify(self):
        """Checks that modifying log entry raises error"""

        log_entry = self.transaction.log_set.filter(
            type=Transaction.PENDING_STATE)[0]

        with self.assertRaises(InvalidTransactionLog):
            log_entry.save()

        for key, value in Transaction.TRANSACTION_STATE:
            log1 = TransactionLog(type=key,
                                  transaction=self.transaction,
                                  user=self.user)

            if key != Transaction.PENDING_STATE:
                log1.save()
                with self.assertRaises(InvalidTransactionLog):
                    log1.save()
Пример #3
0
class EntryTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username="******")
        self.user.save()

        self.group = Group(name="group1", slug="group1")
        self.group.save()

        self.account = Account(name="account1", slug="account1", group=self.group)
        self.account.save()

        self.transaction = Transaction(group=self.group)
        self.transaction.save()

        self.entry = TransactionEntry(account=self.account, debit=100, credit=100, transaction=self.transaction)

    def tearDown(self):
        self.transaction.delete()
        self.account.delete()
        self.group.delete()
        self.user.delete()

    def testDebitAndCreditInSameEntry(self):
        """Checks that setting both debit and credit does not fail"""

        self.entry.debit = 100
        self.entry.credit = 100
        self.entry.save()

    def testNegativeCredit(self):
        """Checks that inputing av negative credit raises an error"""

        self.entry.credit = -100
        with self.assertRaises(InvalidTransactionEntry):
            self.entry.save()

    def testNegativeDebit(self):
        """Checks that inputing av negative debit  raises an error"""

        self.entry.debit = -100
        with self.assertRaises(InvalidTransactionEntry):
            self.entry.save()

    def testDebitAndCreditSetToZero(self):
        """Checks that setting both debit and credit to zero raises error"""

        self.entry.debit = 0
        self.entry.credit = 0
        with self.assertRaises(InvalidTransactionEntry):
            self.entry.save()
Пример #4
0
class LogTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username="******")
        self.user.save()

        self.group = Group(name="Group 1", slug="group1")
        self.group.save()

        self.transaction = Transaction(group=self.group)
        self.transaction.set_pending(user=self.user)

    def tearDown(self):
        self.transaction.delete()
        self.group.delete()
        self.user.delete()

    def testLogEntryUniqePerType(self):
        """Checks that only one log entry of each type is allowed (except for
        pending)
        """

        for key, value in Transaction.TRANSACTION_STATE:
            log1 = TransactionLog(type=key, transaction=self.transaction, user=self.user)
            log2 = TransactionLog(type=key, transaction=self.transaction, user=self.user)
            if key != Transaction.PENDING_STATE:
                log1.save()
                with self.assertRaises(InvalidTransactionLog):
                    log2.save()

    def testLogEntryModify(self):
        """Checks that modifying log entry raises error"""

        log_entry = self.transaction.log_set.filter(type=Transaction.PENDING_STATE)[0]

        with self.assertRaises(InvalidTransactionLog):
            log_entry.save()

        for key, value in Transaction.TRANSACTION_STATE:
            log1 = TransactionLog(type=key, transaction=self.transaction, user=self.user)

            if key != Transaction.PENDING_STATE:
                log1.save()
                with self.assertRaises(InvalidTransactionLog):
                    log1.save()
Пример #5
0
class TransactionTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username='******')
        self.user.save()

        self.group = Group(name='Group 1', slug='group1')
        self.group.save()

        self.accounts = [
            Account(name='Account 1', slug='account1', group=self.group),
            Account(name='Account 2', slug='account2', group=self.group),
            Account(name='Account 3', slug='account3', group=self.group),
        ]
        for account in self.accounts:
            account.save()

        self.before = datetime.datetime.now()

        self.transaction = Transaction(group=self.group)
        self.transaction.save()
        self.transaction.entry_set.add(
            TransactionEntry(account=self.accounts[0], debit=100))
        self.transaction.entry_set.add(
            TransactionEntry(account=self.accounts[1], credit=100))

        self.transaction.set_pending(user=self.user)

        self.after = datetime.datetime.now()

    def tearDown(self):
        self.transaction.delete()
        for account in self.accounts:
            account.delete()
        self.group.delete()
        self.user.delete()

    def testEmptyTransaction(self):
        """Checks that empty transactions are accepted"""

        transaction = Transaction(group=self.group)
        transaction.save()
        transaction.delete()

    def testEqualDebitAndCreditAmount(self):
        """Checks that transaction only accept sum(debit)==sum(credit)"""

        transaction = Transaction(group=self.group)
        transaction.save()

        transaction.entry_set.add(
            TransactionEntry(account=self.accounts[1], debit=200))
        transaction.entry_set.add(
            TransactionEntry(account=self.accounts[0], credit=100))
        with self.assertRaises(InvalidTransaction):
            transaction.set_pending(user=self.user)

        transaction.delete()

    def testAccountOnlyOnceInTransaction(self):
        """Checks that multiple credit accounts are allowed in a transaction"""

        transaction = Transaction(group=self.group)
        transaction.save()

        transaction.entry_set.add(
            TransactionEntry(account=self.accounts[0], debit=200))
        transaction.entry_set.add(
            TransactionEntry(account=self.accounts[1], credit=100))
        transaction.entry_set.add(
            TransactionEntry(account=self.accounts[2], credit=100))

        transaction.delete()

    def testPendingLogEntry(self):
        """Checks that a pending log entry is created"""

        transaction = self.transaction

        self.assertEqual(transaction.is_pending(), True)
        self.assertEqual(transaction.log_set.count(), 1)
        self.assertEqual(
            transaction.log_set.filter(type=Transaction.PENDING_STATE).count(),
            1)

        pending = transaction.log_set.filter(
            type=Transaction.PENDING_STATE)[0].timestamp

        self.assert_(pending > self.before)
        self.assert_(pending < self.after)

    def testCommittedLogEntry(self):
        """Checks that a committed log entry is created"""

        transaction = self.transaction

        before = datetime.datetime.now()
        transaction.set_committed(user=self.user)
        after = datetime.datetime.now()

        self.assertEqual(transaction.is_committed(), True)
        self.assertEqual(transaction.log_set.count(), 2)
        self.assertEqual(
            transaction.log_set.filter(
                type=Transaction.COMMITTED_STATE).count(), 1)

        committed = transaction.log_set.filter(
            type=Transaction.COMMITTED_STATE)[0].timestamp

        self.assert_(committed > before)
        self.assert_(committed < after)

    def testRejectLogEntry(self):
        """Checks that pending transaction can be rejected"""

        transaction = self.transaction
        self.assertEqual(transaction.is_pending(), True)

        before = datetime.datetime.now()
        transaction.set_rejected(message='Reason for rejecting',
                                 user=self.user)
        after = datetime.datetime.now()

        self.assertEqual(transaction.is_rejected(), True)
        self.assertEqual(transaction.log_set.count(), 2)
        self.assertEqual(
            transaction.log_set.filter(
                type=Transaction.REJECTED_STATE).count(), 1)

        rejected = transaction.log_set.filter(
            type=Transaction.REJECTED_STATE)[0].timestamp
        self.assert_(rejected > before)
        self.assert_(rejected < after)

    def testRejectCommitedTransaction(self):
        """Tests that rejecting committed transaction fails"""

        transaction = self.transaction
        transaction.set_committed(user=self.user)

        with self.assertRaises(InvalidTransaction):
            transaction.set_rejected(message='Reason for rejecting',
                                     user=self.user)
Пример #6
0
class TransactionTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username="******")
        self.user.save()

        self.group = Group(name="Group 1", slug="group1")
        self.group.save()

        self.accounts = [
            Account(name="Account 1", slug="account1", group=self.group),
            Account(name="Account 2", slug="account2", group=self.group),
            Account(name="Account 3", slug="account3", group=self.group),
        ]
        for account in self.accounts:
            account.save()

        self.before = datetime.datetime.now()

        self.transaction = Transaction(group=self.group)
        self.transaction.save()
        self.transaction.entry_set.create(account=self.accounts[0], debit=100)
        self.transaction.entry_set.create(account=self.accounts[1], credit=100)

        self.transaction.set_pending(user=self.user)

        self.after = datetime.datetime.now()

    def tearDown(self):
        self.transaction.delete()
        for account in self.accounts:
            account.delete()
        self.group.delete()
        self.user.delete()

    def testEmptyTransaction(self):
        """Checks that empty transactions are accepted"""

        transaction = Transaction(group=self.group)
        transaction.save()
        transaction.delete()

    def testEqualDebitAndCreditAmount(self):
        """Checks that transaction only accept sum(debit)==sum(credit)"""

        transaction = Transaction(group=self.group)
        transaction.save()

        transaction.entry_set.create(account=self.accounts[1], debit=200)
        transaction.entry_set.create(account=self.accounts[0], credit=100)

        with pytest.raises(InvalidTransaction):
            transaction.set_pending(user=self.user)

        transaction.delete()

    def testAccountOnlyOnceInTransaction(self):
        """Checks that multiple credit accounts are allowed in a transaction"""

        transaction = Transaction(group=self.group)
        transaction.save()

        TransactionEntry.objects.create(account=self.accounts[0],
                                        debit=200,
                                        transaction=transaction)
        TransactionEntry.objects.create(account=self.accounts[1],
                                        credit=100,
                                        transaction=transaction)
        TransactionEntry.objects.create(account=self.accounts[2],
                                        credit=100,
                                        transaction=transaction)

        transaction.delete()

    def testPendingLogEntry(self):
        """Checks that a pending log entry is created"""

        transaction = self.transaction

        assert transaction.is_pending() is True
        assert transaction.log_set.count() == 1
        assert (transaction.log_set.filter(
            type=Transaction.PENDING_STATE).count() == 1)

        pending = transaction.log_set.filter(
            type=Transaction.PENDING_STATE)[0].timestamp

        assert pending > self.before
        assert pending < self.after

    def testCommittedLogEntry(self):
        """Checks that a committed log entry is created"""

        transaction = self.transaction

        before = datetime.datetime.now()
        transaction.set_committed(user=self.user)
        after = datetime.datetime.now()

        assert transaction.is_committed() is True
        assert transaction.log_set.count() == 2
        assert (transaction.log_set.filter(
            type=Transaction.COMMITTED_STATE).count() == 1)

        committed = transaction.log_set.filter(
            type=Transaction.COMMITTED_STATE)[0].timestamp

        assert committed > before
        assert committed < after

    def testRejectLogEntry(self):
        """Checks that pending transaction can be rejected"""

        transaction = self.transaction
        assert transaction.is_pending() is True

        before = datetime.datetime.now()
        transaction.set_rejected(message="Reason for rejecting",
                                 user=self.user)
        after = datetime.datetime.now()

        assert transaction.is_rejected() is True
        assert transaction.log_set.count() == 2
        assert (transaction.log_set.filter(
            type=Transaction.REJECTED_STATE).count() == 1)

        rejected = transaction.log_set.filter(
            type=Transaction.REJECTED_STATE)[0].timestamp
        assert rejected > before
        assert rejected < after

    def testRejectCommitedTransaction(self):
        """Tests that rejecting committed transaction fails"""

        transaction = self.transaction
        transaction.set_committed(user=self.user)

        with pytest.raises(InvalidTransaction):
            transaction.set_rejected(message="Reason for rejecting",
                                     user=self.user)
Пример #7
0
class TransactionTestCase(unittest.TestCase):
    def setUp(self):
        self.user = User(username="******")
        self.user.save()

        self.group = Group(name="Group 1", slug="group1")
        self.group.save()

        self.accounts = [
            Account(name="Account 1", slug="account1", group=self.group),
            Account(name="Account 2", slug="account2", group=self.group),
            Account(name="Account 3", slug="account3", group=self.group),
        ]
        for account in self.accounts:
            account.save()

        self.before = datetime.datetime.now()

        self.transaction = Transaction(group=self.group)
        self.transaction.save()
        self.transaction.entry_set.add(TransactionEntry(account=self.accounts[0], debit=100))
        self.transaction.entry_set.add(TransactionEntry(account=self.accounts[1], credit=100))

        self.transaction.set_pending(user=self.user)

        self.after = datetime.datetime.now()

    def tearDown(self):
        self.transaction.delete()
        for account in self.accounts:
            account.delete()
        self.group.delete()
        self.user.delete()

    def testEmptyTransaction(self):
        """Checks that empty transactions are accepted"""

        transaction = Transaction(group=self.group)
        transaction.save()
        transaction.delete()

    def testEqualDebitAndCreditAmount(self):
        """Checks that transaction only accept sum(debit)==sum(credit)"""

        transaction = Transaction(group=self.group)
        transaction.save()

        transaction.entry_set.add(TransactionEntry(account=self.accounts[1], debit=200))
        transaction.entry_set.add(TransactionEntry(account=self.accounts[0], credit=100))
        with self.assertRaises(InvalidTransaction):
            transaction.set_pending(user=self.user)

        transaction.delete()

    def testAccountOnlyOnceInTransaction(self):
        """Checks that multiple credit accounts are allowed in a transaction"""

        transaction = Transaction(group=self.group)
        transaction.save()

        transaction.entry_set.add(TransactionEntry(account=self.accounts[0], debit=200))
        transaction.entry_set.add(TransactionEntry(account=self.accounts[1], credit=100))
        transaction.entry_set.add(TransactionEntry(account=self.accounts[2], credit=100))

        transaction.delete()

    def testPendingLogEntry(self):
        """Checks that a pending log entry is created"""

        transaction = self.transaction

        self.assertEqual(transaction.is_pending(), True)
        self.assertEqual(transaction.log_set.count(), 1)
        self.assertEqual(transaction.log_set.filter(type=Transaction.PENDING_STATE).count(), 1)

        pending = transaction.log_set.filter(type=Transaction.PENDING_STATE)[0].timestamp

        self.assert_(pending > self.before)
        self.assert_(pending < self.after)

    def testCommittedLogEntry(self):
        """Checks that a committed log entry is created"""

        transaction = self.transaction

        before = datetime.datetime.now()
        transaction.set_committed(user=self.user)
        after = datetime.datetime.now()

        self.assertEqual(transaction.is_committed(), True)
        self.assertEqual(transaction.log_set.count(), 2)
        self.assertEqual(transaction.log_set.filter(type=Transaction.COMMITTED_STATE).count(), 1)

        committed = transaction.log_set.filter(type=Transaction.COMMITTED_STATE)[0].timestamp

        self.assert_(committed > before)
        self.assert_(committed < after)

    def testRejectLogEntry(self):
        """Checks that pending transaction can be rejected"""

        transaction = self.transaction
        self.assertEqual(transaction.is_pending(), True)

        before = datetime.datetime.now()
        transaction.set_rejected(message="Reason for rejecting", user=self.user)
        after = datetime.datetime.now()

        self.assertEqual(transaction.is_rejected(), True)
        self.assertEqual(transaction.log_set.count(), 2)
        self.assertEqual(transaction.log_set.filter(type=Transaction.REJECTED_STATE).count(), 1)

        rejected = transaction.log_set.filter(type=Transaction.REJECTED_STATE)[0].timestamp
        self.assert_(rejected > before)
        self.assert_(rejected < after)

    def testRejectCommitedTransaction(self):
        """Tests that rejecting committed transaction fails"""

        transaction = self.transaction
        transaction.set_committed(user=self.user)

        with self.assertRaises(InvalidTransaction):
            transaction.set_rejected(message="Reason for rejecting", user=self.user)