Add init options to skip data initialization and remove manual cleanup in test cases.
This commit is contained in:
@@ -54,12 +54,19 @@ def __validate_username(ctx: click.core.Context, param: click.core.Option,
|
||||
@click.option("-u", "--username", metavar="USERNAME", prompt=True,
|
||||
help="The username.", callback=__validate_username,
|
||||
default=lambda: os.getlogin())
|
||||
@click.option("--skip-accounts", is_flag=True, default=False,
|
||||
help="Skip initializing accounts.")
|
||||
@click.option("--skip-currencies", is_flag=True, default=False,
|
||||
help="Skip initializing currencies.")
|
||||
@with_appcontext
|
||||
def init_db_command(username: str) -> None:
|
||||
def init_db_command(username: str, skip_accounts: bool,
|
||||
skip_currencies: bool) -> None:
|
||||
"""Initializes the accounting database."""
|
||||
db.create_all()
|
||||
init_base_accounts_command()
|
||||
if not skip_accounts:
|
||||
init_accounts_command(username)
|
||||
if not skip_currencies:
|
||||
init_currencies_command(username)
|
||||
db.session.commit()
|
||||
click.echo("Accounting database initialized.")
|
||||
|
||||
@@ -72,14 +72,10 @@ class AccountTestCase(unittest.TestCase):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
self.__app: Flask = create_test_app()
|
||||
self.__app: Flask = create_test_app(is_skip_accounts=True)
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import Account, AccountL10n
|
||||
AccountL10n.query.delete()
|
||||
Account.query.delete()
|
||||
db.session.commit()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
|
||||
@@ -65,15 +65,9 @@ class CurrencyTestCase(unittest.TestCase):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
self.__app: Flask = create_test_app()
|
||||
self.__app: Flask = create_test_app(is_skip_currencies=True)
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import Currency, CurrencyL10n
|
||||
CurrencyL10n.query.delete()
|
||||
Currency.query.delete()
|
||||
db.session.commit()
|
||||
|
||||
self.__client: httpx.Client = get_client(self.__app, "editor")
|
||||
"""The user client."""
|
||||
self.__csrf_token: str = get_csrf_token(self.__client)
|
||||
|
||||
@@ -42,9 +42,6 @@ class DescriptionEditorTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
|
||||
@@ -52,9 +52,6 @@ class CashReceiptJournalEntryTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
@@ -686,9 +683,6 @@ class CashDisbursementJournalEntryTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
@@ -1295,10 +1289,6 @@ class TransferJournalEntryTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, \
|
||||
JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
@@ -2185,9 +2175,6 @@ class JournalEntryReorderTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
|
||||
@@ -49,9 +49,6 @@ class OffsetTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
|
||||
@@ -45,8 +45,6 @@ class OptionTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import Option
|
||||
Option.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
|
||||
@@ -45,11 +45,6 @@ class ReportTestCase(unittest.TestCase):
|
||||
self.__app: Flask = create_test_app()
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
|
||||
self.__client: httpx.Client = get_client(self.__app, "editor")
|
||||
"""The user client."""
|
||||
self.__csrf_token: str = get_csrf_token(self.__client)
|
||||
|
||||
@@ -40,10 +40,15 @@ db: SQLAlchemy = SQLAlchemy()
|
||||
"""The database instance."""
|
||||
|
||||
|
||||
def create_app(is_testing: bool = False) -> Flask:
|
||||
def create_app(is_testing: bool = False, is_skip_accounts: bool = False,
|
||||
is_skip_currencies: bool = False) -> Flask:
|
||||
"""Create and configure the application.
|
||||
|
||||
:param is_testing: True if we are running for testing, or False otherwise.
|
||||
:param is_skip_accounts: True to skip account initialization, or False
|
||||
otherwise.
|
||||
:param is_skip_currencies: True to skip currency initialization, or False
|
||||
otherwise.
|
||||
:return: The application.
|
||||
"""
|
||||
import accounting
|
||||
@@ -117,15 +122,20 @@ def create_app(is_testing: bool = False) -> Flask:
|
||||
accounting.init_app(app, user_utils=UserUtilities())
|
||||
|
||||
with app.app_context():
|
||||
init_db(app)
|
||||
init_db(app, is_skip_accounts, is_skip_currencies)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def init_db(app: Flask) -> None:
|
||||
def init_db(app: Flask, is_skip_accounts: bool,
|
||||
is_skip_currencies: bool) -> None:
|
||||
"""Initializes the database.
|
||||
|
||||
:param app: The Flask application.
|
||||
:param is_skip_accounts: True to skip account initialization, or False
|
||||
otherwise.
|
||||
:param is_skip_currencies: True to skip currency initialization, or False
|
||||
otherwise.
|
||||
:return: None.
|
||||
"""
|
||||
db.create_all()
|
||||
@@ -135,7 +145,12 @@ def init_db(app: Flask) -> None:
|
||||
db.session.add(User(username=username))
|
||||
db.session.commit()
|
||||
runner: FlaskCliRunner = app.test_cli_runner()
|
||||
result: Result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
|
||||
args: list[str] = ["accounting-init-db", "-u", "editor"]
|
||||
if is_skip_accounts:
|
||||
args += ["--skip-accounts"]
|
||||
if is_skip_currencies:
|
||||
args += ["--skip-currencies"]
|
||||
result: Result = runner.invoke(args=args)
|
||||
assert result.exit_code == 0, result.output + str(result.exception)
|
||||
|
||||
|
||||
|
||||
@@ -46,9 +46,6 @@ class UnmatchedOffsetTestCase(unittest.TestCase):
|
||||
"""The Flask application."""
|
||||
|
||||
with self.__app.app_context():
|
||||
from accounting.models import JournalEntry, JournalEntryLineItem
|
||||
JournalEntry.query.delete()
|
||||
JournalEntryLineItem.query.delete()
|
||||
self.__encoded_next_uri: str = encode_next(NEXT_URI)
|
||||
"""The encoded next URI."""
|
||||
|
||||
|
||||
@@ -60,12 +60,18 @@ class Accounts:
|
||||
RENT_INCOME: str = "7482-001"
|
||||
|
||||
|
||||
def create_test_app() -> Flask:
|
||||
def create_test_app(is_skip_accounts: bool = False,
|
||||
is_skip_currencies: bool = False) -> Flask:
|
||||
"""Creates and returns the testing Flask application.
|
||||
|
||||
:param is_skip_accounts: True to skip account initialization, or False
|
||||
otherwise.
|
||||
:param is_skip_currencies: True to skip currency initialization, or False
|
||||
otherwise.
|
||||
:return: The testing Flask application.
|
||||
"""
|
||||
app: Flask = create_app(is_testing=True)
|
||||
app: Flask = create_app(is_testing=True, is_skip_accounts=is_skip_accounts,
|
||||
is_skip_currencies=is_skip_currencies)
|
||||
|
||||
@app.get("/.csrf-token")
|
||||
def get_csrf_token_view() -> str:
|
||||
|
||||
Reference in New Issue
Block a user