Changeset - 8abbe3462fda
[Not reviewed]
0 5 0
Joar Wandborg - 10 years ago 2013-12-26 20:48:45
joar@wandborg.se
[tests] Check transactions for errors before being added

- Added support for Exception-specific HTTP response codes for
AccountinExceptions.
5 files changed with 52 insertions and 10 deletions:
0 comments (0 inline, 0 general)
accounting/decorators.py
Show inline comments
...
 
@@ -21,3 +21,7 @@ def jsonify_exceptions(func):
 
        except AccountingException as exc:
 
            return jsonify(error=exc)
 
            response = jsonify(error=exc)
 

	
 
            response.status_code = exc.http_code
 

	
 
            return response
 

	
accounting/exceptions.py
Show inline comments
...
 
@@ -9,2 +9,3 @@ class AccountingException(Exception):
 
    '''
 
    http_code = 500
 
    def __init__(self, message, **kw):
...
 
@@ -16,3 +17,3 @@ class AccountingException(Exception):
 
class TransactionNotFound(AccountingException):
 
    pass
 
    http_code = 404
 

	
...
 
@@ -20,3 +21,3 @@ class TransactionNotFound(AccountingException):
 
class LedgerNotBalanced(AccountingException):
 
    pass
 
    http_code = 400
 

	
...
 
@@ -24,2 +25,2 @@ class LedgerNotBalanced(AccountingException):
 
class TransactionIDCollision(AccountingException):
 
    pass
 
    http_code = 400
accounting/storage/ledgercli.py
Show inline comments
...
 
@@ -175,2 +175,14 @@ class Ledger(Storage):
 

	
 
        # Check to see that no errors were introduced
 
        try:
 
            self.get_transactions()
 
        except AccountingException as exc:
 
            # TODO: Do a hard reset on the repository using Repository.reset,
 
            # this is on hold because of
 
            # https://github.com/libgit2/pygit2/issues/271.
 
            # This solution will work in the meantime
 
            self.delete_transaction(transaction.id)
 
            setattr(exc, 'transaction', transaction)
 
            raise exc
 

	
 
        self.commit_changes('Added transaction %s' % transaction.id)
accounting/tests/test_transactions.py
Show inline comments
...
 
@@ -11,2 +11,3 @@ import uuid
 
from datetime import datetime
 
from decimal import Decimal
 

	
...
 
@@ -19,3 +20,3 @@ from accounting.models import Transaction, Posting, Amount
 

	
 
logging.basicConfig(level=logging.DEBUG)
 
#logging.basicConfig(level=logging.DEBUG)
 

	
...
 
@@ -265,9 +266,32 @@ class TransactionTestCase(unittest.TestCase):
 

	
 
        self._post_json('/transaction', transaction)
 
        response = self._post_json('/transaction', transaction, expect=400)
 

	
 
        response = self._get_json('/transaction')
 
        self.assertEqual(response['error']['type'], 'LedgerNotBalanced')
 

	
 
        import pdb; pdb.set_trace()
 
    def test_update_transaction_amounts(self):
 
        transaction = self._add_simple_transaction()
 
        response = self._get_json(
 
            '/transaction/' + transaction.id)
 

	
 
        transaction = response['transaction']
 

	
 
        for posting in transaction.postings:
 
            posting.amount.amount *= Decimal(1.50)
 

	
 
        response = self._post_json('/transaction/' + transaction.id,
 
                                   {'transaction': transaction})
 

	
 
        self.assertEqual(response['status'], 'OK')
 

	
 
        response = self._get_json('/transaction/' + transaction.id)
 

	
 
        self.assertEqual(response['transaction'], transaction)
 

	
 
    def test_delete_nonexistent_transaction(self):
 
        response = self._open_json('DELETE', '/transaction/I-do-not-exist',
 
                                   expect=404)
 

	
 
        self.assertEqual(response['error']['type'], 'TransactionNotFound')
 

	
 
    def test_update_transaction_amounts(self): pass
 
    def test_post_transaction_with_metadata(self): pass
 

	
accounting/transport.py
Show inline comments
...
 
@@ -46,3 +46,4 @@ class AccountingEncoder(json.JSONEncoder):
 
                type=o.__class__.__name__,
 
                message=o.message
 
                message=o.message,
 
                transaction=getattr(o, 'transaction', None)
 
            )
0 comments (0 inline, 0 general)