diff --git a/src/accounting/commands.py b/src/accounting/commands.py index d262ef1..4d321dd 100644 --- a/src/accounting/commands.py +++ b/src/accounting/commands.py @@ -54,13 +54,20 @@ 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() - init_accounts_command(username) - init_currencies_command(username) + if not skip_accounts: + init_accounts_command(username) + if not skip_currencies: + init_currencies_command(username) db.session.commit() click.echo("Accounting database initialized.") diff --git a/tests/test_account.py b/tests/test_account.py index 478451d..e17048e 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -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.""" diff --git a/tests/test_currency.py b/tests/test_currency.py index ea5d771..1830754 100644 --- a/tests/test_currency.py +++ b/tests/test_currency.py @@ -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) diff --git a/tests/test_description_editor.py b/tests/test_description_editor.py index d91428a..9835620 100644 --- a/tests/test_description_editor.py +++ b/tests/test_description_editor.py @@ -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.""" diff --git a/tests/test_journal_entry.py b/tests/test_journal_entry.py index ae4af0a..4dab71e 100644 --- a/tests/test_journal_entry.py +++ b/tests/test_journal_entry.py @@ -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.""" diff --git a/tests/test_offset.py b/tests/test_offset.py index e303041..372a06a 100644 --- a/tests/test_offset.py +++ b/tests/test_offset.py @@ -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.""" diff --git a/tests/test_option.py b/tests/test_option.py index 58e3d6f..9495b10 100644 --- a/tests/test_option.py +++ b/tests/test_option.py @@ -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.""" diff --git a/tests/test_report.py b/tests/test_report.py index 0dd1d73..d8fec7e 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -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) diff --git a/tests/test_site/__init__.py b/tests/test_site/__init__.py index fe0476c..96b844a 100644 --- a/tests/test_site/__init__.py +++ b/tests/test_site/__init__.py @@ -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) diff --git a/tests/test_unmatched_offset.py b/tests/test_unmatched_offset.py index 643ffc1..24977cf 100644 --- a/tests/test_unmatched_offset.py +++ b/tests/test_unmatched_offset.py @@ -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.""" diff --git a/tests/testlib.py b/tests/testlib.py index 636aece..0689079 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -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: