Merged the "init-db" console command to the Flask application initialization in the test site, to simplify the code.

This commit is contained in:
依瑪貓 2023-04-10 23:50:16 +08:00
parent c21ed59dfe
commit 700e4f822a
11 changed files with 32 additions and 56 deletions

View File

@ -78,10 +78,8 @@ class AccountTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import Account, AccountL10n
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
AccountL10n.query.delete()
Account.query.delete()

View File

@ -45,9 +45,8 @@ class BaseAccountTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
result: Result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
def test_nobody(self) -> None:

View File

@ -42,11 +42,7 @@ class ConsoleCommandTestCase(unittest.TestCase):
"""
self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
result: Result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0,
result.output + str(result.exception))
# Drop every accounting table, to see if accounting-init recreates
# them correctly.
tables: list[sa.Table] \

View File

@ -71,10 +71,8 @@ class CurrencyTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import Currency, CurrencyL10n
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
CurrencyL10n.query.delete()
Currency.query.delete()

View File

@ -42,10 +42,8 @@ class DescriptionEditorTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()

View File

@ -55,10 +55,8 @@ class CashReceiptJournalEntryTestCase(unittest.TestCase):
with self.app.app_context():
from accounting.models import BaseAccount, JournalEntry, \
JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()
@ -666,9 +664,8 @@ class CashDisbursementJournalEntryTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
@ -1254,9 +1251,8 @@ class TransferJournalEntryTestCase(unittest.TestCase):
with self.app.app_context():
from accounting.models import BaseAccount, JournalEntry, \
JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
@ -2120,10 +2116,8 @@ class JournalEntryReorderTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()

View File

@ -50,10 +50,8 @@ class OffsetTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()

View File

@ -52,10 +52,8 @@ class OptionTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import Option
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
Option.query.delete()

View File

@ -47,10 +47,8 @@ class ReportTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()
JournalEntryLineItem.query.delete()

View File

@ -21,9 +21,7 @@ import os
import typing as t
from secrets import token_urlsafe
import click
from flask import Flask, Blueprint, render_template, redirect, Response
from flask.cli import with_appcontext
from flask_babel_js import BabelJS
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
@ -63,7 +61,6 @@ def create_app(is_testing: bool = False) -> Flask:
db.init_app(app)
app.register_blueprint(bp, url_prefix="/")
app.cli.add_command(init_db_command)
from . import locale
locale.init_app(app)
@ -110,20 +107,23 @@ def create_app(is_testing: bool = False) -> Flask:
accounting.init_app(app, user_utils=UserUtilities())
with app.app_context():
init_db()
return app
@click.command("init-db")
@with_appcontext
def init_db_command() -> None:
"""Initializes the database."""
def init_db() -> None:
"""Initializes the database.
:return: None.
"""
db.create_all()
from .auth import User
for username in ["viewer", "editor", "admin", "nobody"]:
if User.query.filter(User.username == username).first() is None:
db.session.add(User(username=username))
db.session.commit()
click.echo("Database initialized successfully.")
@bp.get("/", endpoint="home")

View File

@ -46,9 +46,8 @@ class UnmatchedOffsetTestCase(unittest.TestCase):
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem
result: Result
result = runner.invoke(args="init-db")
self.assertEqual(result.exit_code, 0)
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete()