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.

This commit is contained in:
依瑪貓 2023-04-09 11:35:28 +08:00
parent 5ae0d03b32
commit f8ea863b80
8 changed files with 47 additions and 47 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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):

View File

@ -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."""

View File

@ -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

View File

@ -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))

View File

@ -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.

View File

@ -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: