[test] add test special config
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline failed

This commit is contained in:
SouthFox 2023-07-30 20:50:08 +08:00
parent abdf1fea0f
commit b11a3ce322
5 changed files with 47 additions and 9 deletions

View file

@ -2,14 +2,11 @@ from pathlib import Path
import tomli import tomli
import pydantic import pydantic
import os
DEBUG = True DEBUG = True
MAIN_VERSION = "0.1.1" MAIN_VERSION = "0.1.1"
ROOT_DIR = Path().parent.resolve()
DB_PATH = ROOT_DIR / "data" / "database.db"
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
_CONFIG_FILE = "config.toml"
class Config(pydantic.BaseModel): class Config(pydantic.BaseModel):
domain: str domain: str
@ -32,6 +29,12 @@ class Config(pydantic.BaseModel):
inbox_retention_days: int = 15 inbox_retention_days: int = 15
post_token: str | None = None post_token: str | None = None
sqlalchemy_database: str | None = None
ROOT_DIR = Path().parent.resolve()
_CONFIG_FILE = os.getenv("FOXHOLE_CONFIG_FILE", "config.toml")
def load_config() -> Config: def load_config() -> Config:
try: try:
return Config.parse_obj( return Config.parse_obj(
@ -56,6 +59,10 @@ def get_version_commit() -> str:
CONFIG = load_config() CONFIG = load_config()
DOMAIN = CONFIG.domain DOMAIN = CONFIG.domain
DB_PATH = CONFIG.sqlalchemy_database or ROOT_DIR / "data" / "database.db"
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
_SCHEME = "https" if CONFIG.https else "http" _SCHEME = "https" if CONFIG.https else "http"
ID = f"{_SCHEME}://{DOMAIN}" ID = f"{_SCHEME}://{DOMAIN}"
BASE_URL = ID BASE_URL = ID

12
data/tests.toml Normal file
View file

@ -0,0 +1,12 @@
domain = "localhost:8000"
username = "test"
name = "test"
summary = "<p>Hello World!</p>"
https = false
icon_url = "https://localhost:8000/static/nopic.png"
secret = "0d0645798d20af6bd77a9303658f62d8"
debug = true
# In-mem DB
sqlalchemy_database = "file:pytest?mode=memory&cache=shared&uri=true"

View file

@ -168,3 +168,12 @@ def send_note(ctx):
print("Done!") print("Done!")
asyncio.run(_dodo()) asyncio.run(_dodo())
@task
def tests(ctx):
run(
f"FOXHOLE_CONFIG_FILE=tests.toml pytest -vvv",
pty=True,
echo=True,
)

View file

@ -14,10 +14,20 @@ from app.database import SessionLocal
from sqlalchemy import orm from sqlalchemy import orm
_Session = orm.scoped_session(SessionLocal)
@pytest_asyncio.fixture
async def async_db_session():
async with async_session() as session: # type: ignore
async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield session
async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
@pytest.fixture @pytest.fixture
def db(): def db():
_Session = orm.scoped_session(SessionLocal)
Base.metadata.create_all(bind=engine) Base.metadata.create_all(bind=engine)
with _Session() as db_session: with _Session() as db_session:
try: try:

View file

@ -21,6 +21,7 @@ from sqlalchemy import select
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_outbox_send_follow_request( async def test_outbox_send_follow_request(
db: Session, db: Session,
async_db_session,
client: TestClient, client: TestClient,
respx_mock: respx.MockRouter, respx_mock: respx.MockRouter,
) -> None: ) -> None:
@ -34,10 +35,9 @@ async def test_outbox_send_follow_request(
respx_mock.post(remote_ap_id + "/inbox").mock( respx_mock.post(remote_ap_id + "/inbox").mock(
return_value=httpx.Response(202)) return_value=httpx.Response(202))
from app.database import async_session
from app.boxes import send_follow from app.boxes import send_follow
async with async_session() as db_session: #type: ignore async with async_db_session as db_session: #type: ignore
await send_follow(db_session, remote_ap_id) await send_follow(db_session, remote_ap_id)
# And the Follow activity was created in the outbox # And the Follow activity was created in the outbox
@ -49,6 +49,7 @@ async def test_outbox_send_follow_request(
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_outbox_send_create_activity( async def test_outbox_send_create_activity(
db: Session, db: Session,
async_db_session,
client: TestClient, client: TestClient,
respx_mock: respx.MockRouter, respx_mock: respx.MockRouter,
) -> None: ) -> None:
@ -62,12 +63,11 @@ async def test_outbox_send_create_activity(
respx_mock.post(remote_ap_id + "/inbox").mock( respx_mock.post(remote_ap_id + "/inbox").mock(
return_value=httpx.Response(202)) return_value=httpx.Response(202))
from app.database import async_session
from app.boxes import _send_create from app.boxes import _send_create
from app.activitypub import VisibilityEnum from app.activitypub import VisibilityEnum
from app.orgpython import to_html from app.orgpython import to_html
async with async_session() as db_session: #type: ignore async with async_db_session as db_session: #type: ignore
content = "*Blod Text* =code Text= \n" content = "*Blod Text* =code Text= \n"
content = to_html(content) content = to_html(content)