Added the "accounting-init-db" console command to the database initialization of the test site, for simplicity.

This commit is contained in:
依瑪貓 2023-04-10 23:58:08 +08:00
parent 700e4f822a
commit 4be1ead6b5
10 changed files with 10 additions and 76 deletions

View File

@ -21,9 +21,7 @@ import unittest
from datetime import timedelta, date from datetime import timedelta, date
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from test_site import db from test_site import db
from testlib import NEXT_URI, create_test_app, get_client, set_locale, \ from testlib import NEXT_URI, create_test_app, get_client, set_locale, \
@ -75,12 +73,8 @@ class AccountTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import Account, AccountL10n from accounting.models import Account, AccountL10n
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
AccountL10n.query.delete() AccountL10n.query.delete()
Account.query.delete() Account.query.delete()
db.session.commit() db.session.commit()

View File

@ -20,9 +20,7 @@
import unittest import unittest
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from testlib import create_test_app, get_client from testlib import create_test_app, get_client
@ -43,12 +41,6 @@ class BaseAccountTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
def test_nobody(self) -> None: def test_nobody(self) -> None:
"""Test the permission as nobody. """Test the permission as nobody.

View File

@ -21,9 +21,7 @@ import unittest
from datetime import timedelta, date from datetime import timedelta, date
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from test_site import db from test_site import db
from testlib import NEXT_URI, create_test_app, get_client, set_locale, \ from testlib import NEXT_URI, create_test_app, get_client, set_locale, \
@ -68,12 +66,8 @@ class CurrencyTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import Currency, CurrencyL10n from accounting.models import Currency, CurrencyL10n
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
CurrencyL10n.query.delete() CurrencyL10n.query.delete()
Currency.query.delete() Currency.query.delete()
db.session.commit() db.session.commit()

View File

@ -20,9 +20,7 @@
import unittest import unittest
from datetime import date from datetime import date
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
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 add_journal_entry
@ -39,12 +37,8 @@ class DescriptionEditorTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem from accounting.models import JournalEntry, JournalEntryLineItem
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()

View File

@ -22,9 +22,7 @@ from datetime import date, timedelta
from decimal import Decimal from decimal import Decimal
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from test_site import db 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, \
@ -51,13 +49,8 @@ class CashReceiptJournalEntryTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import BaseAccount, JournalEntry, \ from accounting.models import JournalEntry, JournalEntryLineItem
JournalEntryLineItem
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()
@ -661,13 +654,8 @@ class CashDisbursementJournalEntryTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem from accounting.models import JournalEntry, JournalEntryLineItem
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() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()
@ -1247,14 +1235,9 @@ class TransferJournalEntryTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import BaseAccount, JournalEntry, \ from accounting.models import JournalEntry, \
JournalEntryLineItem JournalEntryLineItem
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() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()
@ -2113,12 +2096,8 @@ class JournalEntryReorderTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem from accounting.models import JournalEntry, JournalEntryLineItem
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()

View File

@ -23,9 +23,7 @@ import unittest
from decimal import Decimal from decimal import Decimal
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from test_site import db from test_site import db
from testlib import Accounts, create_test_app, get_client, \ from testlib import Accounts, create_test_app, get_client, \
@ -47,12 +45,8 @@ class OffsetTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem from accounting.models import JournalEntry, JournalEntryLineItem
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()

View File

@ -21,9 +21,7 @@ import unittest
from datetime import datetime, timedelta from datetime import datetime, timedelta
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from test_site import db 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
@ -49,12 +47,8 @@ class OptionTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import Option from accounting.models import Option
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
Option.query.delete() Option.query.delete()
self.client, self.csrf_token = get_client(self.app, "admin") self.client, self.csrf_token = get_client(self.app, "admin")

View File

@ -21,9 +21,7 @@ import unittest
from datetime import date from datetime import date
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from testlib import create_test_app, get_client, Accounts, BaseTestData from testlib import create_test_app, get_client, Accounts, BaseTestData
@ -44,12 +42,8 @@ class ReportTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem from accounting.models import JournalEntry, JournalEntryLineItem
result: Result = runner.invoke(
args=["accounting-init-db", "-u", "editor"])
self.assertEqual(result.exit_code, 0)
JournalEntry.query.delete() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()

View File

@ -21,7 +21,9 @@ import os
import typing as t import typing as t
from secrets import token_urlsafe from secrets import token_urlsafe
from click.testing import Result
from flask import Flask, Blueprint, render_template, redirect, Response from flask import Flask, Blueprint, render_template, redirect, Response
from flask.testing import FlaskCliRunner
from flask_babel_js import BabelJS from flask_babel_js import BabelJS
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect from flask_wtf import CSRFProtect
@ -108,14 +110,15 @@ def create_app(is_testing: bool = False) -> Flask:
accounting.init_app(app, user_utils=UserUtilities()) accounting.init_app(app, user_utils=UserUtilities())
with app.app_context(): with app.app_context():
init_db() init_db(app)
return app return app
def init_db() -> None: def init_db(app: Flask) -> None:
"""Initializes the database. """Initializes the database.
:param app: The Flask application.
:return: None. :return: None.
""" """
db.create_all() db.create_all()
@ -124,6 +127,9 @@ def init_db() -> None:
if User.query.filter(User.username == username).first() is None: if User.query.filter(User.username == username).first() is None:
db.session.add(User(username=username)) db.session.add(User(username=username))
db.session.commit() db.session.commit()
runner: FlaskCliRunner = app.test_cli_runner()
result: Result = runner.invoke(args=["accounting-init-db", "-u", "editor"])
assert result.exit_code == 0, result.output + str(result.exception)
@bp.get("/", endpoint="home") @bp.get("/", endpoint="home")

View File

@ -20,9 +20,7 @@
import unittest import unittest
import httpx import httpx
from click.testing import Result
from flask import Flask from flask import Flask
from flask.testing import FlaskCliRunner
from test_site import db from test_site import db
from testlib import create_test_app, get_client, Accounts, \ from testlib import create_test_app, get_client, Accounts, \
@ -43,13 +41,8 @@ class UnmatchedOffsetTestCase(unittest.TestCase):
""" """
self.app: Flask = create_test_app() self.app: Flask = create_test_app()
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context(): with self.app.app_context():
from accounting.models import JournalEntry, JournalEntryLineItem from accounting.models import JournalEntry, JournalEntryLineItem
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() JournalEntry.query.delete()
JournalEntryLineItem.query.delete() JournalEntryLineItem.query.delete()