Skip to content

Commit

Permalink
Add TestMentionService
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Feb 3, 2025
1 parent 70be762 commit 09d0686
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion h/presenters/mention_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, mention: Mention):
def asdict(self) -> dict[str, Any]:
return {
"userid": self._mention.user.userid,
"username": self._mention.user.username,
"username": self._mention.username,
"display_name": self._mention.user.display_name,
"link": self._mention.user.uri,
}
7 changes: 7 additions & 0 deletions tests/common/fixtures/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from h.services import MentionService
from h.services.analytics import AnalyticsService
from h.services.annotation_delete import AnnotationDeleteService
from h.services.annotation_json import AnnotationJSONService
Expand Down Expand Up @@ -84,6 +85,7 @@
"user_signup_service",
"user_unique_service",
"user_update_service",
"mention_service",
)


Expand Down Expand Up @@ -306,3 +308,8 @@ def user_unique_service(mock_service):
@pytest.fixture
def user_update_service(mock_service):
return mock_service(UserUpdateService, name="user_update")


@pytest.fixture
def mention_service(mock_service):
return mock_service(MentionService)
26 changes: 26 additions & 0 deletions tests/unit/h/presenters/mention_json_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from h.models import Mention
from h.presenters.mention_json import MentionJSONPresenter


class TestMentionJSONPresenter:
def test_as_dict(self, user, annotation):
mention = Mention(annotation=annotation, user=user, username=user.username)

data = MentionJSONPresenter(mention).asdict()

assert data == {
"userid": user.userid,
"username": user.username,
"display_name": user.display_name,
"link": user.uri,
}

@pytest.fixture
def user(self, factories):
return factories.User.build()

@pytest.fixture
def annotation(self, factories):
return factories.Annotation.build()
17 changes: 15 additions & 2 deletions tests/unit/h/services/annotation_json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def test_present_all_for_user(

annotation_read_service.get_annotations_by_id.assert_called_once_with(
ids=sentinel.annotation_ids,
eager_load=[Annotation.document, Annotation.moderation, Annotation.group],
eager_load=[
Annotation.document,
Annotation.moderation,
Annotation.group,
Annotation.mentions,
],
)
flag_service.all_flagged.assert_called_once_with(user, sentinel.annotation_ids)
flag_service.flag_counts.assert_called_once_with(sentinel.annotation_ids)
Expand All @@ -201,13 +206,19 @@ def test_present_all_for_user(

@pytest.fixture
def service(
self, annotation_read_service, links_service, flag_service, user_service
self,
annotation_read_service,
links_service,
flag_service,
user_service,
mention_service,
):
return AnnotationJSONService(
annotation_read_service=annotation_read_service,
links_service=links_service,
flag_service=flag_service,
user_service=user_service,
mention_service=mention_service,
)

@pytest.fixture
Expand Down Expand Up @@ -246,6 +257,7 @@ def test_it(
flag_service,
links_service,
user_service,
mention_service,
):
service = factory(sentinel.context, pyramid_request)

Expand All @@ -254,6 +266,7 @@ def test_it(
links_service=links_service,
flag_service=flag_service,
user_service=user_service,
mention_service=mention_service,
)
assert service == AnnotationJSONService.return_value

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/h/services/annotation_write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,15 @@ def svc(
queue_service,
annotation_read_service,
annotation_metadata_service,
mention_service,
):
return AnnotationWriteService(
db_session=db_session,
has_permission=has_permission,
queue_service=queue_service,
annotation_read_service=annotation_read_service,
annotation_metadata_service=annotation_metadata_service,
mention_service=mention_service,
)

@pytest.fixture
Expand Down Expand Up @@ -321,6 +323,7 @@ def test_it(
queue_service,
annotation_read_service,
annotation_metadata_service,
mention_service,
):
svc = service_factory(sentinel.context, pyramid_request)

Expand All @@ -330,6 +333,7 @@ def test_it(
queue_service=queue_service,
annotation_read_service=annotation_read_service,
annotation_metadata_service=annotation_metadata_service,
mention_service=mention_service,
)
assert svc == AnnotationWriteService.return_value

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/h/services/mention_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from h.services import MentionService


class TestMentionService:
def test_update_mentions(
self, db_session, mention_service, user_service, annotation, user
):
annotation.text = f'Hello <a data-hyp-mention data-userid="{user.userid}">@{user.display_name}</a>'
user_service.fetch_all.return_value = [user]

mention_service.update_mentions(annotation)

assert len(annotation.mentions) == 1
assert annotation.mentions[0].user == user

@pytest.fixture
def annotation(self, factories):
return factories.Annotation()

@pytest.fixture
def user(self, factories):
return factories.User()


@pytest.fixture
def mention_service(db_session, user_service):
return MentionService(db_session, user_service)

0 comments on commit 09d0686

Please sign in to comment.