Skip to content

Commit

Permalink
pretty good tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Aug 11, 2023
1 parent 4376575 commit c6ca78d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions papermerge/search/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
tags=["search"]
)

engine = create_engine(settings.SEARCH_URL)


@router.get("/")
def search(
q: str,
user: User = Depends(current_user)
) -> List[IndexEntity]:
engine = create_engine(settings.SEARCH_URL)
session = Session(engine)

sq = Search(IndexEntity).query(q)

results: List[IndexEntity] = session.exec(sq)
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest
from django.conf import settings
from fastapi import FastAPI
from fastapi.testclient import TestClient
from salinic import Session, create_engine
from salinic.engine import AccessMode

from papermerge.core.models import User
from papermerge.core.routers import register_routers as reg_core_routers
Expand Down Expand Up @@ -46,3 +49,10 @@ def auth_api_client(user: User):
test_client=test_client,
user=user
)


@pytest.fixture()
def session(tmp_path) -> Session:
engine = create_engine(settings.SEARCH_URL, mode=AccessMode.RW)

return Session(engine)
50 changes: 45 additions & 5 deletions tests/search/test_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import pytest
from fastapi.testclient import TestClient
from salinic.session import Session

from papermerge.core.models import User
from papermerge.search.schema import IndexEntity
from papermerge.test.types import AuthTestClient


@pytest.mark.django_db(transaction=True)
def test_basic(
auth_api_client: AuthTestClient,
session: Session
):
"""Checks that search returns only documents belonging to current user """
current_user: User = auth_api_client.user

# document belong to current user
doc_1 = IndexEntity(
id='one',
document_id='doc_1',
title='Anmeldung',
user_id=str(current_user.id),
parent_id='parent_id',
entity_type='page',
breadcrumb=[('home', 'home_id')]
)
session.add(doc_1)

# document does not belong to current user
doc_2 = IndexEntity(
id='two',
document_id='doc_2',
title='Anmeldung',
user_id='i-belong-to-other-user',
parent_id='parent_id_2',
entity_type='page',
breadcrumb=[('home', 'home_id')]
)
session.add(doc_2)

response = auth_api_client.get("/search?q=Anmeldung")

@pytest.mark.django_db
def test_basic(auth_api_client: TestClient):
response = auth_api_client.get("/search?q=one")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}

returned_models = response.json()
assert len(returned_models) == 1

returned_model = returned_models[0]
assert returned_model['id'] == doc_1.model_dump()['id']

0 comments on commit c6ca78d

Please sign in to comment.