From f8ea863b8027b5b2a98a0f89e954a39928ebe7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 9 Apr 2023 11:35:28 +0800 Subject: [PATCH] Moved the add_journal_entry and match_journal_entry_detail functions from testlib_journal_entry.py to testlib.py. They are used by everyone, and testlib_journal_entry.py is only for test_journal_entry.py to shorten the code in one single file. --- tests/test_account.py | 4 ++-- tests/test_currency.py | 4 ++-- tests/test_description_editor.py | 4 ++-- tests/test_journal_entry.py | 7 ++++--- tests/test_offset.py | 4 ++-- tests/testlib.py | 34 ++++++++++++++++++++++++++++++++ tests/testlib_journal_entry.py | 34 -------------------------------- tests/testlib_offset.py | 3 +-- 8 files changed, 47 insertions(+), 47 deletions(-) diff --git a/tests/test_account.py b/tests/test_account.py index fb97a5e..f76f45e 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -27,8 +27,8 @@ from flask import Flask from flask.testing import FlaskCliRunner from test_site import db -from testlib import NEXT_URI, create_test_app, get_client, set_locale -from testlib_journal_entry import add_journal_entry +from testlib import NEXT_URI, create_test_app, get_client, set_locale, \ + add_journal_entry class AccountData: diff --git a/tests/test_currency.py b/tests/test_currency.py index a5f5f3c..d1d0199 100644 --- a/tests/test_currency.py +++ b/tests/test_currency.py @@ -28,8 +28,8 @@ from flask import Flask from flask.testing import FlaskCliRunner from test_site import db -from testlib import NEXT_URI, create_test_app, get_client, set_locale -from testlib_journal_entry import add_journal_entry +from testlib import NEXT_URI, create_test_app, get_client, set_locale, \ + add_journal_entry class CurrencyData: diff --git a/tests/test_description_editor.py b/tests/test_description_editor.py index 2ddbd47..c9541eb 100644 --- a/tests/test_description_editor.py +++ b/tests/test_description_editor.py @@ -24,8 +24,8 @@ from click.testing import Result from flask import Flask from flask.testing import FlaskCliRunner -from testlib import NEXT_URI, Accounts, create_test_app, get_client -from testlib_journal_entry import add_journal_entry +from testlib import NEXT_URI, Accounts, create_test_app, get_client, \ + add_journal_entry class DescriptionEditorTestCase(unittest.TestCase): diff --git a/tests/test_journal_entry.py b/tests/test_journal_entry.py index 2a0ff54..e39d587 100644 --- a/tests/test_journal_entry.py +++ b/tests/test_journal_entry.py @@ -27,11 +27,12 @@ from flask import Flask from flask.testing import FlaskCliRunner from test_site import db -from testlib import NEXT_URI, Accounts, create_test_app, get_client +from testlib import NEXT_URI, Accounts, create_test_app, get_client, \ + add_journal_entry, match_journal_entry_detail from testlib_journal_entry import NON_EMPTY_NOTE, EMPTY_NOTE, \ get_add_form, get_unchanged_update_form, get_update_form, \ - match_journal_entry_detail, set_negative_amount, \ - remove_debit_in_a_currency, remove_credit_in_a_currency, add_journal_entry + set_negative_amount, remove_debit_in_a_currency, \ + remove_credit_in_a_currency PREFIX: str = "/accounting/journal-entries" """The URL prefix for the journal entry management.""" diff --git a/tests/test_offset.py b/tests/test_offset.py index b17f47c..7d0b71c 100644 --- a/tests/test_offset.py +++ b/tests/test_offset.py @@ -28,8 +28,8 @@ from flask import Flask from flask.testing import FlaskCliRunner from test_site import db -from testlib import Accounts, create_test_app, get_client -from testlib_journal_entry import match_journal_entry_detail +from testlib import Accounts, create_test_app, get_client, \ + match_journal_entry_detail from testlib_offset import JournalEntryData, JournalEntryCurrencyData, \ JournalEntryLineItemData, BaseTestData diff --git a/tests/testlib.py b/tests/testlib.py index 222ca5a..7214d7c 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -17,6 +17,7 @@ """The common test libraries. """ +import re import typing as t import httpx @@ -117,3 +118,36 @@ def set_locale(client: httpx.Client, csrf_token: str, "next": "/next"}) assert response.status_code == 302 assert response.headers["Location"] == "/next" + + +def add_journal_entry(client: httpx.Client, form: dict[str, str]) -> int: + """Adds a transfer journal entry. + + :param client: The client. + :param form: The form data. + :return: The newly-added journal entry ID. + """ + prefix: str = "/accounting/journal-entries" + journal_entry_type: str = "transfer" + if len({x for x in form if "-debit-" in x}) == 0: + journal_entry_type = "receipt" + elif len({x for x in form if "-credit-" in x}) == 0: + journal_entry_type = "disbursement" + store_uri = f"{prefix}/store/{journal_entry_type}" + response: httpx.Response = client.post(store_uri, data=form) + assert response.status_code == 302 + return match_journal_entry_detail(response.headers["Location"]) + + +def match_journal_entry_detail(location: str) -> int: + """Validates if the redirect location is the journal entry detail, and + returns the journal entry ID on success. + + :param location: The redirect location. + :return: The journal entry ID. + :raise AssertionError: When the location is not the journal entry detail. + """ + m: re.Match = re.match( + r"^/accounting/journal-entries/(\d+)\?next=%2F_next", location) + assert m is not None + return int(m.group(1)) diff --git a/tests/testlib_journal_entry.py b/tests/testlib_journal_entry.py index 0427009..32d2936 100644 --- a/tests/testlib_journal_entry.py +++ b/tests/testlib_journal_entry.py @@ -22,7 +22,6 @@ from datetime import date from decimal import Decimal from secrets import randbelow -import httpx from flask import Flask from test_site import db @@ -375,39 +374,6 @@ def __get_currency_prefix(form: dict[str, str], code: str) -> str: return m.group(1) -def add_journal_entry(client: httpx.Client, form: dict[str, str]) -> int: - """Adds a transfer journal entry. - - :param client: The client. - :param form: The form data. - :return: The newly-added journal entry ID. - """ - prefix: str = "/accounting/journal-entries" - journal_entry_type: str = "transfer" - if len({x for x in form if "-debit-" in x}) == 0: - journal_entry_type = "receipt" - elif len({x for x in form if "-credit-" in x}) == 0: - journal_entry_type = "disbursement" - store_uri = f"{prefix}/store/{journal_entry_type}" - response: httpx.Response = client.post(store_uri, data=form) - assert response.status_code == 302 - return match_journal_entry_detail(response.headers["Location"]) - - -def match_journal_entry_detail(location: str) -> int: - """Validates if the redirect location is the journal entry detail, and - returns the journal entry ID on success. - - :param location: The redirect location. - :return: The journal entry ID. - :raise AssertionError: When the location is not the journal entry detail. - """ - m: re.Match = re.match( - r"^/accounting/journal-entries/(\d+)\?next=%2F_next", location) - assert m is not None - return int(m.group(1)) - - def set_negative_amount(form: dict[str, str]) -> None: """Sets a negative amount in the form data, keeping the balance. diff --git a/tests/testlib_offset.py b/tests/testlib_offset.py index 50eaaaf..caaae9a 100644 --- a/tests/testlib_offset.py +++ b/tests/testlib_offset.py @@ -27,8 +27,7 @@ import httpx from flask import Flask from test_site import db -from testlib import NEXT_URI -from testlib_journal_entry import match_journal_entry_detail +from testlib import NEXT_URI, match_journal_entry_detail class JournalEntryLineItemData: