Switch to aiosqlite

This commit is contained in:
Thomas Sileo 2022-06-29 20:43:17 +02:00
parent 18bd2cb664
commit 1f54a6a6ac
21 changed files with 698 additions and 549 deletions

View file

@ -2,22 +2,25 @@ from typing import Generator
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import orm
from app.database import Base
from app.database import async_engine
from app.database import async_session
from app.database import engine
from app.database import get_db
from app.main import app
from tests.factories import _Session
# _Session = orm.sessionmaker(bind=engine, autocommit=False, autoflush=False)
def _get_db_for_testing() -> Generator[orm.Session, None, None]:
# try:
yield _Session # type: ignore
# finally:
# session.close()
@pytest.fixture
async def async_db_session():
async with async_session() as session:
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
@ -46,6 +49,6 @@ def exclude_fastapi_middleware():
@pytest.fixture
def client(db, exclude_fastapi_middleware) -> Generator:
app.dependency_overrides[get_db] = _get_db_for_testing
# app.dependency_overrides[get_db] = _get_db_for_testing
with TestClient(app) as c:
yield c

View file

@ -1,13 +1,18 @@
import httpx
import pytest
import respx
from sqlalchemy import func
from sqlalchemy import select
from sqlalchemy.orm import Session
from app import models
from app.actor import fetch_actor
from app.database import Session
from app.database import AsyncSession
from tests import factories
def test_fetch_actor(db: Session, respx_mock) -> None:
@pytest.mark.asyncio
async def test_fetch_actor(async_db_session: AsyncSession, respx_mock) -> None:
# Given a remote actor
ra = factories.RemoteActorFactory(
base_url="https://example.com",
@ -17,18 +22,22 @@ def test_fetch_actor(db: Session, respx_mock) -> None:
respx_mock.get(ra.ap_id).mock(return_value=httpx.Response(200, json=ra.ap_actor))
# When fetching this actor for the first time
saved_actor = fetch_actor(db, ra.ap_id)
saved_actor = await fetch_actor(async_db_session, ra.ap_id)
# Then it has been fetched and saved in DB
assert respx.calls.call_count == 1
assert db.query(models.Actor).one().ap_id == saved_actor.ap_id
assert (
await async_db_session.execute(select(models.Actor))
).scalar_one().ap_id == saved_actor.ap_id
# When fetching it a second time
actor_from_db = fetch_actor(db, ra.ap_id)
actor_from_db = await fetch_actor(async_db_session, ra.ap_id)
# Then it's read from the DB
assert actor_from_db.ap_id == ra.ap_id
assert db.query(models.Actor).count() == 1
assert (
await async_db_session.execute(select(func.count(models.Actor.id)))
).scalar_one() == 1
assert respx.calls.call_count == 1

View file

@ -1,9 +1,9 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app import activitypub as ap
from app import models
from app.config import generate_csrf_token
from app.database import Session
from app.utils.emoji import EMOJIS_BY_NAME
from tests.utils import generate_admin_session_cookies

View file

@ -3,12 +3,12 @@ from uuid import uuid4
import httpx
import respx
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app import activitypub as ap
from app import models
from app.actor import LOCAL_ACTOR
from app.ap_object import RemoteObject
from app.database import Session
from tests import factories
from tests.utils import mock_httpsig_checker

View file

@ -4,6 +4,7 @@ from uuid import uuid4
import httpx
import respx
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app import activitypub as ap
from app import models
@ -11,7 +12,6 @@ from app import webfinger
from app.actor import LOCAL_ACTOR
from app.ap_object import RemoteObject
from app.config import generate_csrf_token
from app.database import Session
from tests import factories
from tests.utils import generate_admin_session_cookies

View file

@ -1,13 +1,16 @@
from uuid import uuid4
import httpx
import pytest
import respx
from fastapi.testclient import TestClient
from sqlalchemy import select
from sqlalchemy.orm import Session
from app import models
from app.actor import LOCAL_ACTOR
from app.ap_object import RemoteObject
from app.database import Session
from app.database import AsyncSession
from app.outgoing_activities import _MAX_RETRIES
from app.outgoing_activities import new_outgoing_activity
from app.outgoing_activities import process_next_outgoing_activity
@ -36,8 +39,9 @@ def _setup_outbox_object() -> models.OutboxObject:
return outbox_object
def test_new_outgoing_activity(
db: Session,
@pytest.mark.asyncio
async def test_new_outgoing_activity(
async_db_session: AsyncSession,
client: TestClient,
respx_mock: respx.MockRouter,
) -> None:
@ -48,9 +52,13 @@ def test_new_outgoing_activity(
raise ValueError("Should never happen")
# When queuing the activity
outgoing_activity = new_outgoing_activity(db, inbox_url, outbox_object.id)
outgoing_activity = await new_outgoing_activity(
async_db_session, inbox_url, outbox_object.id
)
assert db.query(models.OutgoingActivity).one() == outgoing_activity
assert (
await async_db_session.execute(select(models.OutgoingActivity))
).scalar_one() == outgoing_activity
assert outgoing_activity.outbox_object_id == outbox_object.id
assert outgoing_activity.recipient == inbox_url

View file

@ -1,9 +1,9 @@
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app import activitypub as ap
from app.actor import LOCAL_ACTOR
from app.database import Session
_ACCEPTED_AP_HEADERS = [
"application/activity+json",