diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2017-12-29 12:23:48 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2017-12-29 12:23:48 +0100 |
commit | e3c242043b199ddade6ae540bcc8284c0e377d5e (patch) | |
tree | f812a50aa868f86de30a3eaaa27d8d99b6402af7 | |
parent | c85ea8f4f1e521e71ec8dfd58313d1477a602596 (diff) | |
download | bank-e3c242043b199ddade6ae540bcc8284c0e377d5e.tar.gz bank-e3c242043b199ddade6ae540bcc8284c0e377d5e.tar.bz2 bank-e3c242043b199ddade6ae540bcc8284c0e377d5e.zip |
lint tests
-rw-r--r-- | talerbank/app/tests.py | 197 | ||||
-rw-r--r-- | talerbank/settings.py | 3 |
2 files changed, 117 insertions, 83 deletions
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py index a7d68f3..8f5fd56 100644 --- a/talerbank/app/tests.py +++ b/talerbank/app/tests.py @@ -17,16 +17,16 @@ import json import timeit import logging +from urllib.parse import unquote from django.db import connection from django.test import TestCase, Client from django.urls import reverse from django.conf import settings from django.contrib.auth.models import User from mock import patch, MagicMock -from urllib.parse import unquote from .models import BankAccount, BankTransaction from . import urls -from .views import wire_transfer, LoginFailed +from .views import wire_transfer from .amount import Amount, CurrencyMismatch, BadFormatAmount LOGGER = logging.getLogger() @@ -37,8 +37,11 @@ def clear_db(): BankAccount.objects.all().delete() BankTransaction.objects.all().delete() with connection.cursor() as cursor: - cursor.execute("ALTER SEQUENCE app_bankaccount_account_no_seq RESTART") - cursor.execute("ALTER SEQUENCE app_banktransaction_id_seq RESTART") + cursor.execute( + "ALTER SEQUENCE app_bankaccount_account_no_seq" \ + " RESTART") + cursor.execute( + "ALTER SEQUENCE app_banktransaction_id_seq RESTART") class WithdrawTestCase(TestCase): def setUp(self): @@ -57,9 +60,6 @@ class WithdrawTestCase(TestCase): self.exchange_bank_account.save() self.client = Client() - def tearDown(self): - clear_db() - @patch('talerbank.app.views.wire_transfer') @patch('hashlib.new') @patch('time.time') @@ -100,9 +100,9 @@ class WithdrawTestCase(TestCase): response = self.client.post( reverse("pin-verify", urlconf=urls), {"pin_1": "0"}) - - args, kwargs = mocked_wire_transfer.call_args + args, kwargs = mocked_wire_transfer.call_args + del kwargs self.assertTrue( args[0].dump() == amount.dump() \ and self.user_bank_account in args \ @@ -129,15 +129,15 @@ class InternalWireTransferTestCase(TestCase): def test_internal_wire_transfer(self): client = Client() client.login(username="give_money", password="gm") - response = client.post(reverse("profile", urlconf=urls), - {"amount": 3.0, - "receiver": self.take_money.account_no, - "subject": "charity"}) + response = client.post( + reverse("profile", urlconf=urls), + {"amount": 3.0, + "receiver": self.take_money.account_no, + "subject": "charity"}) take_money = BankAccount.objects.get(account_no=4) self.assertEqual(0, Amount.cmp( Amount(settings.TALER_CURRENCY, 3), - take_money.amount), - msg=self.take_money.amount.stringify(2)) + take_money.amount)) self.assertEqual(302, response.status_code) @@ -211,7 +211,9 @@ class LoginTestCase(TestCase): **{"HTTP_X_TALER_BANK_USERNAME": "Wrong", "HTTP_X_TALER_BANK_PASSWORD": "Credentials"}) data = response.content.decode("utf-8") - self.assertJSONEqual('{"error": "Wrong username/password", "ec": 5212}', json.loads(data)) + self.assertJSONEqual( + '{"error": "Wrong username/password", "ec": 5212}', + json.loads(data)) self.assertEqual(401, response.status_code) @@ -277,7 +279,8 @@ class RejectTestCase(TestCase): data='{"row_id": %d, \ "auth": {"type": "basic"}, \ "account_number": %d}' \ - % (jdata["row_id"], rejected.bankaccount.account_no), + % (jdata["row_id"], + rejected.bankaccount.account_no), content_type="application/json", **{"HTTP_X_TALER_BANK_USERNAME": "rejecting_user", "HTTP_X_TALER_BANK_PASSWORD": "rejecting_password"}) @@ -309,11 +312,13 @@ class AddIncomingTestCase(TestCase): "fraction": 0, \ "currency": "%s"}}' \ % settings.TALER_CURRENCY - response = client.post(reverse("add-incoming", urlconf=urls), - data=data, - content_type="application/json", - follow=True, **{"HTTP_X_TALER_BANK_USERNAME": "user_user", - "HTTP_X_TALER_BANK_PASSWORD": "user_password"}) + response = client.post( + reverse("add-incoming", urlconf=urls), + data=data, + content_type="application/json", + follow=True, + **{"HTTP_X_TALER_BANK_USERNAME": "user_user", + "HTTP_X_TALER_BANK_PASSWORD": "user_password"}) self.assertEqual(200, response.status_code) data = '{"auth": {"type": "basic"}, \ "credit_account": 1, \ @@ -324,11 +329,13 @@ class AddIncomingTestCase(TestCase): "fraction": 0, \ "currency": "%s"}}' \ % "WRONGCURRENCY" - response = client.post(reverse("add-incoming", urlconf=urls), - data=data, - content_type="application/json", - follow=True, **{"HTTP_X_TALER_BANK_USERNAME": "user_user", - "HTTP_X_TALER_BANK_PASSWORD": "user_password"}) + response = client.post( + reverse("add-incoming", urlconf=urls), + data=data, + content_type="application/json", + follow=True, + **{"HTTP_X_TALER_BANK_USERNAME": "user_user", + "HTTP_X_TALER_BANK_PASSWORD": "user_password"}) self.assertEqual(406, response.status_code) # Try to go debit data = '{"auth": {"type": "basic"}, \ @@ -340,11 +347,13 @@ class AddIncomingTestCase(TestCase): "fraction": 1, \ "currency": "%s"}}' \ % settings.TALER_CURRENCY - response = client.post(reverse("add-incoming", urlconf=urls), - data=data, - content_type="application/json", - follow=True, **{"HTTP_X_TALER_BANK_USERNAME": "user_user", - "HTTP_X_TALER_BANK_PASSWORD": "user_password"}) + response = client.post( + reverse("add-incoming", urlconf=urls), + data=data, + content_type="application/json", + follow=True, + **{"HTTP_X_TALER_BANK_USERNAME": "user_user", + "HTTP_X_TALER_BANK_PASSWORD": "user_password"}) self.assertEqual(403, response.status_code) class HistoryContext: @@ -369,7 +378,8 @@ class HistoryTestCase(TestCase): username='User0', password="Password0")) credit_account.save() - for subject in ("a", "b", "c", "d", "e", "f", "g", "h", "i"): + for subject in ( + "a", "b", "c", "d", "e", "f", "g", "h", "i"): wire_transfer(Amount(settings.TALER_CURRENCY, 1), debit_account, credit_account, subject) @@ -391,38 +401,41 @@ class HistoryTestCase(TestCase): clear_db() def test_history(self): - for ctx in (HistoryContext( - expected_resp={"status": 200}, - delta="4", direction="both"), - HistoryContext( - expected_resp={ - "fields": [("row_id", 6)], - "status": 200}, - delta="+1", start="5", direction="both"), - HistoryContext( - expected_resp={ - "fields": [("wt_subject", "h")], - "status": 200}, - delta="-1", start=9, direction="both"), - HistoryContext( - expected_resp={"status": 204}, - delta="1", start="11", direction="both"), - HistoryContext( - expected_resp={ - "status": 200, - "fields": [("wt_subject", "i"), ("sign", "cancel-")]}, - start=8, delta="+1", direction="cancel-"), - HistoryContext( - expected_resp={"status": 204}, - start=8, delta="+1", direction="cancel-", cancelled="omit"), - HistoryContext( - expected_resp={"status": 204}, - start=8, delta="-1", direction="cancel-"), - HistoryContext( - expected_resp={"status": 204}, - delta="+1", direction="cancel+"), - HistoryContext(expected_resp={"status": 200}, - delta="+1", direction="debit")): + for ctx in ( + HistoryContext( + expected_resp={"status": 200}, + delta="4", direction="both"), + HistoryContext( + expected_resp={ + "fields": [("row_id", 6)], + "status": 200}, + delta="+1", start="5", direction="both"), + HistoryContext( + expected_resp={ + "fields": [("wt_subject", "h")], + "status": 200}, + delta="-1", start=9, direction="both"), + HistoryContext( + expected_resp={"status": 204}, + delta="1", start="11", direction="both"), + HistoryContext( + expected_resp={ + "status": 200, + "fields": [("wt_subject", "i"), + ("sign", "cancel-")]}, + start=8, delta="+1", direction="cancel-"), + HistoryContext( + expected_resp={"status": 204}, + start=8, delta="+1", + direction="cancel-", cancelled="omit"), + HistoryContext( + expected_resp={"status": 204}, + start=8, delta="-1", direction="cancel-"), + HistoryContext( + expected_resp={"status": 204}, + delta="+1", direction="cancel+"), + HistoryContext(expected_resp={"status": 200}, + delta="+1", direction="debit")): response = self.client.get( reverse("history", urlconf=urls), ctx.urlargs, **{"HTTP_X_TALER_BANK_USERNAME": "User", @@ -442,9 +455,9 @@ class HistoryTestCase(TestCase): # extract expected data from response expected_data = {} response_data = {} - for k, v in ctx.expected_resp.get("fields", []): - response_data.update({k: data.get(k)}) - expected_data.update({k: v}) + for key, value in ctx.expected_resp.get("fields", []): + response_data.update({key: data.get(key)}) + expected_data.update({key: value}) self.assertEqual(expected_data, response_data) @@ -481,9 +494,11 @@ class DBCustomColumnTestCase(TestCase): def test_exists(self): user_bankaccount = BankAccount.objects.get( user=User.objects.get(username='U')) - self.assertTrue(isinstance(user_bankaccount.amount, Amount)) + self.assertTrue( + isinstance(user_bankaccount.amount, Amount)) -# This tests whether a bank account goes debit and then goes >=0 again +# This tests whether a bank account goes debit and then goes >=0 +# again class DebitTestCase(TestCase): def setUp(self): @@ -511,8 +526,10 @@ class DebitTestCase(TestCase): user_bankaccount, "Go green") tmp = Amount(settings.TALER_CURRENCY, 10) - self.assertEqual(0, Amount.cmp(user_bankaccount.amount, tmp)) - self.assertEqual(0, Amount.cmp(user_bankaccount0.amount, tmp)) + self.assertEqual( + 0, Amount.cmp(user_bankaccount.amount, tmp)) + self.assertEqual( + 0, Amount.cmp(user_bankaccount0.amount, tmp)) self.assertFalse(user_bankaccount.debit) self.assertTrue(user_bankaccount0.debit) @@ -525,19 +542,33 @@ class DebitTestCase(TestCase): tmp.value = 1 self.assertTrue(user_bankaccount.debit) self.assertFalse(user_bankaccount0.debit) - self.assertEqual(0, Amount.cmp(user_bankaccount.amount, tmp)) - self.assertEqual(0, Amount.cmp(user_bankaccount0.amount, tmp)) + self.assertEqual( + 0, Amount.cmp(user_bankaccount.amount, tmp)) + self.assertEqual( + 0, Amount.cmp(user_bankaccount0.amount, tmp)) class ParseAmountTestCase(TestCase): def test_parse_amount(self): ret = Amount.parse("KUDOS:4.0") - self.assertJSONEqual('{"value": 4, "fraction": 0, "currency": "KUDOS"}', ret.dump()) + self.assertJSONEqual( + '{"value": 4, \ + "fraction": 0, \ + "currency": "KUDOS"}', + ret.dump()) ret = Amount.parse("KUDOS:4.3") - self.assertJSONEqual('{"value": 4, "fraction": 30000000, "currency": "KUDOS"}', ret.dump()) + self.assertJSONEqual( + '{"value": 4, \ + "fraction": 30000000, \ + "currency": "KUDOS"}', + ret.dump()) ret = Amount.parse("KUDOS:4") - self.assertJSONEqual('{"value": 4, "fraction": 0, "currency": "KUDOS"}', ret.dump()) + self.assertJSONEqual( + '{"value": 4, "fraction": 0, "currency": "KUDOS"}', + ret.dump()) ret = Amount.parse("KUDOS:4.") # forbid? - self.assertJSONEqual('{"value": 4, "fraction": 0, "currency": "KUDOS"}', ret.dump()) + self.assertJSONEqual( + '{"value": 4, "fraction": 0, "currency": "KUDOS"}', + ret.dump()) try: Amount.parse("Buggy") except BadFormatAmount: @@ -574,9 +605,11 @@ class MeasureHistory(TestCase): # Measure the time extract_history() needs to retrieve # ~ntransfers records. - timer = timeit.Timer(stmt="extract_history(self.user_bankaccount0)", - setup="from talerbank.app.views import extract_history", - globals=locals()) + timer = timeit.Timer( + stmt="extract_history(self.user_bankaccount0)", + setup="from talerbank.app.views import extract_history", + globals=locals()) total_time = timer.timeit(number=1) allowed_time_per_record = 0.003 - self.assertLess(total_time, self.ntransfers*allowed_time_per_record) + self.assertLess( + total_time, self.ntransfers*allowed_time_per_record) diff --git a/talerbank/settings.py b/talerbank/settings.py index fe6ef83..e8f226d 100644 --- a/talerbank/settings.py +++ b/talerbank/settings.py @@ -104,7 +104,8 @@ if not DBNAME: LOGGER.info("dbname: %s" % DBNAME) -CHECK_DBSTRING_FORMAT = re.search("[a-z]+:///[a-z]+([\?][a-z]+=[a-z/]+)?", DBNAME) +CHECK_DBSTRING_FORMAT = re.search( + r"[a-z]+:///[a-z]+([\?][a-z]+=[a-z/]+)?", DBNAME) if not CHECK_DBSTRING_FORMAT: LOGGER.error("Bad db string given '%s', respect the format" \ "'dbtype:///dbname'" % DBNAME) |