Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Jun 8, 2024
1 parent 592230f commit 5106597
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ format:
venv/bin/ruff format

test:
@bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest"
@bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG"

test_single:
@bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_not_exist_error.py"
Expand Down
21 changes: 14 additions & 7 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

from flask import Flask, g, request

from .factory import app_config, create_celery, create_flask_app, babel, oss
from .factory import (
app_config,
create_celery,
create_flask_app,
init_flask_app,
babel,
oss,
gs_vision,
)

from app.constants.locale import Locale
from app.core.rbac import AllowApplyType, ApplicationCheckType
from app.services.google_storage import GoogleStorage
from app.utils.logging import configure_root_logger, configure_extra_logs

configure_root_logger()
flask_app = create_flask_app(Flask(__name__))
configure_extra_logs(flask_app)
celery = create_celery(flask_app)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

Expand All @@ -23,7 +26,11 @@
FILE_PATH = os.path.abspath(os.path.join(APP_PATH, "..", "files")) # 一般文件
TMP_PATH = os.path.abspath(os.path.join(FILE_PATH, "tmp")) # 临时文件存放地址
STORAGE_PATH = os.path.abspath(os.path.join(APP_PATH, "..", "storage")) # 储存地址
gs_vision = GoogleStorage()

flask_app = create_flask_app(Flask(__name__))
configure_extra_logs(flask_app)
celery = create_celery(flask_app)
init_flask_app(flask_app)


def create_default_team(admin_user):
Expand Down
7 changes: 5 additions & 2 deletions app/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
from flask.logging import default_handler as flask_default_handler
from flask_apikit import APIKit
from flask_babel import Babel
from app.services.google_storage import GoogleStorage
import app.config as _app_config
from app.services.oss import OSS
from .apis import register_apis

from app.models import connect_db

logger = logging.getLogger(__name__)

# singleton modules
babel = Babel()
apikit = APIKit()
# 插件
oss = OSS()
gs_vision = GoogleStorage()

app_config = {
k: getattr(_app_config, k) for k in dir(_app_config) if not k.startswith("_")
Expand All @@ -32,7 +35,7 @@ def create_flask_app(app: Flask) -> Flask:
return app


def init_flask_app(app: Flask) -> Flask:
def init_flask_app(app: Flask):
register_apis(app)
babel.init_app(app)
apikit.init_app(app)
Expand Down
11 changes: 5 additions & 6 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from mongoengine import connection

from app import create_app, FILE_PATH
from app import create_app, FILE_PATH, init_db
from app.models.site_setting import SiteSetting
from app.models.user import User
from app.models.team import Team
Expand All @@ -23,14 +23,13 @@

def create_test_app():
# 先创建app连接上数据库
create_app()
app = create_app()
# 不是_test结尾则停止测试,防止数据覆盖
if not connection.get_db().name.endswith("_test"):
raise RuntimeError("Please use *_test database")
# 清库,防止测试交叉影响
raise AssertionError("Please use *_test database")
# reset the db
connection.get_db().client.drop_database(connection.get_db().name)
# 再次创建app,保证角色之类初始化成功
app = create_app()
init_db(app)
return app


Expand Down

0 comments on commit 5106597

Please sign in to comment.