diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2018-01-12 16:18:12 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2018-01-12 16:18:12 +0100 |
commit | acdef0d40a355fbccf89317a3084d4a2a94ef920 (patch) | |
tree | 4988a9896bc841217967305b3db868eab8f4bf26 /talerbank/app/tests.py | |
parent | b61c34235450154882d271ffb566db677170bbf8 (diff) | |
download | bank-acdef0d40a355fbccf89317a3084d4a2a94ef920.tar.gz bank-acdef0d40a355fbccf89317a3084d4a2a94ef920.tar.bz2 bank-acdef0d40a355fbccf89317a3084d4a2a94ef920.zip |
Closing #5149.
Diffstat (limited to 'talerbank/app/tests.py')
-rw-r--r-- | talerbank/app/tests.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py index 8f5fd56..e7f830b 100644 --- a/talerbank/app/tests.py +++ b/talerbank/app/tests.py @@ -613,3 +613,100 @@ class MeasureHistory(TestCase): allowed_time_per_record = 0.003 self.assertLess( total_time, self.ntransfers*allowed_time_per_record) + +class BalanceTestCase(TestCase): + + def setUp(self): + self.the_bank = BankAccount( + user=User.objects.create_user( + username='U0', password='U0PASS'), + amount=Amount(settings.TALER_CURRENCY, 3)) + self.the_bank.save() + + user = BankAccount( + user=User.objects.create_user(username='U'), + amount=Amount(settings.TALER_CURRENCY, 10)) + user.save() + + # bank: 3, user: 10 (START). + + # bank: 2, user: 11 + wire_transfer(Amount(settings.TALER_CURRENCY, 1), + self.the_bank, + user, + "mock") + + # bank: 4, user: 9 + wire_transfer(Amount(settings.TALER_CURRENCY, 2), + user, + self.the_bank, + "mock") + + # bank: -1, user: 14 + wire_transfer(Amount(settings.TALER_CURRENCY, 5), + self.the_bank, + user, + "mock") + + # bank: 7, user: 6 (END) + wire_transfer(Amount(settings.TALER_CURRENCY, 8), + user, + self.the_bank, + "mock") + + # bank: -3, user: 16 (END) + wire_transfer(Amount(settings.TALER_CURRENCY, 10), + user, + self.the_bank, + "mock") + + + self.client = Client() + + def tearDown(self): + clear_db() + + def test_balance(self): + self.client.login(username="U0", + password="U0PASS") + response = self.client.get( + reverse("history", urlconf=urls), + {"auth": "basic", + "delta": 30, + "direction": "both", + "account_number": 55}, # unused + **{"HTTP_X_TALER_BANK_USERNAME": "U0", + "HTTP_X_TALER_BANK_PASSWORD": "U0PASS"}) + data = response.content.decode("utf-8") + self.assertEqual(response.status_code, 200) + entries = json.loads(data) + + + acc_in = Amount(settings.TALER_CURRENCY) + acc_out = Amount(settings.TALER_CURRENCY) + + for entry in entries["data"]: + if entry["sign"] == "+": + acc_in.add(Amount(**entry["amount"])) + if entry["sign"] == "-": + acc_out.add(Amount(**entry["amount"])) + + expected_amount = Amount(settings.TALER_CURRENCY, 3) + try: + debit = False + acc_in.subtract(acc_out) + expected_amount.add(acc_in) + except ValueError: + # "out" is bigger than "in" + LOGGER.info("out > in") + acc_out.subtract(acc_in) + try: + expected_amount.subtract(acc_out) + except ValueError: + # initial amount wasn't enough to cover expenses + debit = True + acc_out.subtract(expected_amount) + expected_amount = acc_out + + self.assertEqual( + Amount.cmp(expected_amount, self.the_bank.amount), 0) |