From f4c70096e252a357e83832fecac230d337e05a31 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Tue, 28 Jun 2022 23:47:51 +0200 Subject: [PATCH] Try to fix tests --- app/main.py | 24 ++++++++++++++++++++++++ app/models.py | 8 ++++---- data/tests.toml | 1 + tests/conftest.py | 16 +++++++++------- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/app/main.py b/app/main.py index 2dfc38c..c4cd531 100644 --- a/app/main.py +++ b/app/main.py @@ -443,6 +443,28 @@ def outbox_by_public_id( replies_tree = boxes.get_replies_tree(db, maybe_object) + likes = ( + db.query(models.InboxObject) + .filter( + models.InboxObject.ap_type == "Like", + models.InboxObject.activity_object_ap_id == maybe_object.ap_id, + ) + .options(joinedload(models.InboxObject.actor)) + .order_by(models.InboxObject.ap_published_at.desc()) + .limit(10) + ) + + shares = ( + db.query(models.InboxObject) + .filter( + models.InboxObject.ap_type == "Announce", + models.InboxObject.activity_object_ap_id == maybe_object.ap_id, + ) + .options(joinedload(models.InboxObject.actor)) + .order_by(models.InboxObject.ap_published_at.desc()) + .limit(10) + ) + return templates.render_template( db, request, @@ -450,6 +472,8 @@ def outbox_by_public_id( { "replies_tree": replies_tree, "outbox_object": maybe_object, + "likes": likes, + "shares": shares, }, ) diff --git a/app/models.py b/app/models.py index b4c0606..f5f67e5 100644 --- a/app/models.py +++ b/app/models.py @@ -58,14 +58,14 @@ class InboxObject(Base, BaseObject): is_hidden_from_stream = Column(Boolean, nullable=False, default=False) ap_actor_id = Column(String, nullable=False) - ap_type = Column(String, nullable=False) + ap_type = Column(String, nullable=False, index=True) ap_id = Column(String, nullable=False, unique=True, index=True) ap_context = Column(String, nullable=True) ap_published_at = Column(DateTime(timezone=True), nullable=False) ap_object: Mapped[ap.RawObject] = Column(JSON, nullable=False) # Only set for activities - activity_object_ap_id = Column(String, nullable=True) + activity_object_ap_id = Column(String, nullable=True, index=True) visibility = Column(Enum(ap.VisibilityEnum), nullable=False) @@ -134,12 +134,12 @@ class OutboxObject(Base, BaseObject): public_id = Column(String, nullable=False, index=True) - ap_type = Column(String, nullable=False) + ap_type = Column(String, nullable=False, index=True) ap_id = Column(String, nullable=False, unique=True, index=True) ap_context = Column(String, nullable=True) ap_object: Mapped[ap.RawObject] = Column(JSON, nullable=False) - activity_object_ap_id = Column(String, nullable=True) + activity_object_ap_id = Column(String, nullable=True, index=True) # Source content for activities (like Notes) source = Column(String, nullable=True) diff --git a/data/tests.toml b/data/tests.toml index 9efcb98..dabae6a 100644 --- a/data/tests.toml +++ b/data/tests.toml @@ -11,5 +11,6 @@ debug = true # In-mem DB sqlalchemy_database_url = "sqlite:///file:pytest?mode=memory&cache=shared&uri=true" +# sqlalchemy_database_url = "sqlite:///data/pytest.db" key_path = "tests/test.key" media_db_path = "tests/media.db" diff --git a/tests/conftest.py b/tests/conftest.py index 9774702..f175631 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,22 +8,24 @@ from app.database import Base 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) +# _Session = orm.sessionmaker(bind=engine, autocommit=False, autoflush=False) def _get_db_for_testing() -> Generator[orm.Session, None, None]: - session = _Session() - try: - yield session - finally: - session.close() + # try: + yield _Session # type: ignore + # finally: + # session.close() @pytest.fixture def db() -> Generator: Base.metadata.create_all(bind=engine) - yield orm.scoped_session(orm.sessionmaker(bind=engine)) + # sess = orm.sessionmaker(bind=engine)() + yield _Session + # yield orm.scoped_session(orm.sessionmaker(bind=engine)) try: Base.metadata.drop_all(bind=engine) except Exception: