Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(tests): notification bulk create in fixture #55

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ repos:

- repo: local
hooks:
# - id: pytest
# name: Pytest
# entry: poetry run pytest -v
# language: system
# types: [ python ]
# stages: [ commit ]
# pass_filenames: false
# always_run: true
- id: pytest
name: Pytest
entry: poetry run pytest -v
language: system
types: [ python ]
stages: [ commit ]
pass_filenames: false
always_run: true

- id: pylint
name: pylint
Expand Down
11 changes: 5 additions & 6 deletions django_notification/api/views/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
from rest_framework.serializers import Serializer
from rest_framework.viewsets import GenericViewSet

from django_notification.api.serializers.notification import \
NotificationSerializer
from django_notification.api.serializers.simple_notification import \
SimpleNotificationSerializer
from django_notification.api.serializers.notification import NotificationSerializer
from django_notification.api.serializers.simple_notification import (
SimpleNotificationSerializer,
)
from django_notification.decorators.action import conditional_action
from django_notification.mixins import (ConfigurableAttrsMixin,
DisableMethodsMixin)
from django_notification.mixins import ConfigurableAttrsMixin, DisableMethodsMixin
from django_notification.models.notification import Notification
from django_notification.settings.conf import config

Expand Down
11 changes: 5 additions & 6 deletions django_notification/api/views/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
from rest_framework.serializers import Serializer
from rest_framework.viewsets import GenericViewSet

from django_notification.api.serializers.notification import \
NotificationSerializer
from django_notification.api.serializers.simple_notification import \
SimpleNotificationSerializer
from django_notification.mixins import (ConfigurableAttrsMixin,
DisableMethodsMixin)
from django_notification.api.serializers.notification import NotificationSerializer
from django_notification.api.serializers.simple_notification import (
SimpleNotificationSerializer,
)
from django_notification.mixins import ConfigurableAttrsMixin, DisableMethodsMixin
from django_notification.models.notification import Notification
from django_notification.settings.conf import config

Expand Down
2 changes: 1 addition & 1 deletion django_notification/tests/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import views
from . import paginations
from . import throttlings
from . import throttlings
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from django_notification.api.serializers.simple_notification import (
SimpleNotificationSerializer,
)
from django_notification.utils.serialization.field_filters import filter_non_empty_fields
from django_notification.utils.serialization.field_filters import (
filter_non_empty_fields,
)
from django_notification.utils.serialization.notif_title_generator import generate_title
from pytest import mark
from django_notification.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from rest_framework.response import Response
from django.core.cache import cache

from django_notification.api.throttlings.role_base_throttle import RoleBasedUserRateThrottle
from django_notification.api.throttlings.role_base_throttle import (
RoleBasedUserRateThrottle,
)
from django_notification.settings.conf import config
from django_notification.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON

Expand Down
28 changes: 21 additions & 7 deletions django_notification/tests/api/views/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def setup_method(self) -> None:
"""
self.client = APIClient()

def test_get_queryset_for_staff(self, admin_user: User, notification: Notification) -> None:
def test_get_queryset_for_staff(
self, admin_user: User, notification: Notification
) -> None:
"""
Test that staff users can retrieve all seen notifications.

Expand All @@ -58,7 +60,9 @@ def test_get_queryset_for_staff(self, admin_user: User, notification: Notificati
== Notification.queryset.seen(seen_by=admin_user).count()
)

def test_get_queryset_for_non_staff(self, user: User, notification: Notification) -> None:
def test_get_queryset_for_non_staff(
self, user: User, notification: Notification
) -> None:
"""
Test that non-staff users retrieve notifications based on their group memberships.

Expand Down Expand Up @@ -100,7 +104,9 @@ def test_list_method_disabled(self, user: User) -> None:
assert response.status_code == 405 # Method Not Allowed

@patch.object(config, "api_allow_retrieve", False)
def test_retrieve_method_disabled(self, user: User, notification: Notification) -> None:
def test_retrieve_method_disabled(
self, user: User, notification: Notification
) -> None:
"""
Test that the retrieve method is disabled when `api_allow_retrieve` is False in the config.

Expand All @@ -119,7 +125,9 @@ def test_retrieve_method_disabled(self, user: User, notification: Notification)
assert response.status_code == 405 # Method Not Allowed

@patch.object(config, "include_serializer_full_details", True)
def test_full_details_serializer_used(self, user: User, notification: Notification) -> None:
def test_full_details_serializer_used(
self, user: User, notification: Notification
) -> None:
"""
Test that the full details serializer is used when `include_serializer_full_details` is True.

Expand Down Expand Up @@ -169,7 +177,9 @@ def test_clear_activities(self, user: User, notification: Notification) -> None:
Notification.queryset.sent().count() == DeletedNotification.objects.count()
)

def test_clear_notification(self, admin_user: User, notification: Notification) -> None:
def test_clear_notification(
self, admin_user: User, notification: Notification
) -> None:
"""
Test the clear_notification action to soft delete a specific notification.

Expand All @@ -192,7 +202,9 @@ def test_clear_notification(self, admin_user: User, notification: Notification)
assert DeletedNotification.objects.filter(notification=notification).exists()

@patch.object(config, "include_hard_delete", True)
def test_delete_activities(self, admin_user: User, notification: Notification) -> None:
def test_delete_activities(
self, admin_user: User, notification: Notification
) -> None:
"""
Test the delete_activities action to hard delete all notifications.

Expand All @@ -218,7 +230,9 @@ def test_delete_activities(self, admin_user: User, notification: Notification) -
assert not Notification.queryset.all_notifications()

@patch.object(config, "include_hard_delete", True)
def test_delete_notification(self, admin_user: User, notification: Notification) -> None:
def test_delete_notification(
self, admin_user: User, notification: Notification
) -> None:
"""
Test the delete_notification action to hard delete a specific notification.

Expand Down
24 changes: 18 additions & 6 deletions django_notification/tests/api/views/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def setup_method(self) -> None:
"""
self.client = APIClient()

def test_get_queryset_for_staff(self, admin_user: Type[User], notification: Notification) -> None:
def test_get_queryset_for_staff(
self, admin_user: Type[User], notification: Notification
) -> None:
"""
Test that staff users retrieve all unseen notifications.

Expand All @@ -62,7 +64,9 @@ def test_get_queryset_for_staff(self, admin_user: Type[User], notification: Noti
== Notification.queryset.unseen(unseen_by=admin_user).count()
)

def test_get_queryset_for_non_staff(self, user: Type[User], notification: Notification) -> None:
def test_get_queryset_for_non_staff(
self, user: Type[User], notification: Notification
) -> None:
"""
Test that non-staff users retrieve unseen notifications based on their group memberships.

Expand All @@ -85,7 +89,9 @@ def test_get_queryset_for_non_staff(self, user: Type[User], notification: Notifi
== Notification.queryset.unseen(unseen_by=user).count()
)

def test_retrieve_notification(self, user: Type[User], notification: Notification) -> None:
def test_retrieve_notification(
self, user: Type[User], notification: Notification
) -> None:
"""
Test the retrieve functionality to get a specific notification and mark it as seen.

Expand All @@ -106,7 +112,9 @@ def test_retrieve_notification(self, user: Type[User], notification: Notificatio
assert response.status_code == 200
assert Notification.queryset.seen(seen_by=user).exists()

def test_mark_all_as_seen(self, user: Type[User], notification: Notification) -> None:
def test_mark_all_as_seen(
self, user: Type[User], notification: Notification
) -> None:
"""
Test the mark_all_as_seen action to mark all notifications as seen for the user.

Expand Down Expand Up @@ -145,7 +153,9 @@ def test_list_method_disabled(self, user: Type[User]) -> None:
assert response.status_code == 405 # Method Not Allowed

@patch.object(config, "api_allow_retrieve", False)
def test_retrieve_method_disabled(self, user: Type[User], notification: Notification) -> None:
def test_retrieve_method_disabled(
self, user: Type[User], notification: Notification
) -> None:
"""
Test that the retrieve method is disabled when `api_allow_retrieve` is False in the config.

Expand All @@ -165,7 +175,9 @@ def test_retrieve_method_disabled(self, user: Type[User], notification: Notifica
assert response.status_code == 405 # Method Not Allowed

@patch.object(config, "include_serializer_full_details", True)
def test_full_details_serializer_used(self, user: Type[User], notification: Notification) -> None:
def test_full_details_serializer_used(
self, user: Type[User], notification: Notification
) -> None:
"""
Test that the full details serializer is used when `include_serializer_full_details` is True in the config.

Expand Down
2 changes: 1 addition & 1 deletion django_notification/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
notification_recipient,
notification_seen,
deleted_notification,
admin_user
admin_user,
)
4 changes: 3 additions & 1 deletion django_notification/tests/fixtures/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def notifications(db, user: User, qs_user: User, qs_group: Group) -> List[Notifi
timestamp=now(),
),
]
notifications = Notification.objects.bulk_create(notifications)
for notification in notifications:
notification.save()

notifications[0].recipient.add(qs_user)
notifications[1].recipient.add(qs_user)
notifications[2].recipient.add(qs_user)
Expand Down
16 changes: 12 additions & 4 deletions django_notification/tests/models/test_deleted_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_string_representation(
)
assert str(deleted_notification) == expected_str

def test_unique_together_constraint(self, notification: Notification, user: User) -> None:
def test_unique_together_constraint(
self, notification: Notification, user: User
) -> None:
"""
Test that the combination of user and notification is unique.

Expand All @@ -56,7 +58,9 @@ def test_unique_together_constraint(self, notification: Notification, user: User
notification=notification, user=user, deleted_at=now()
)

def test_save_method_with_valid_user(self, notification: Notification, user: User) -> None:
def test_save_method_with_valid_user(
self, notification: Notification, user: User
) -> None:
"""
Test the save method when the user is a recipient or group member of the notification.

Expand All @@ -78,7 +82,9 @@ def test_save_method_with_valid_user(self, notification: Notification, user: Use
notification=notification, user=user
).exists()

def test_save_method_with_invalid_user(self, notification: Notification, another_user: User) -> None:
def test_save_method_with_invalid_user(
self, notification: Notification, another_user: User
) -> None:
"""
Test that a `PermissionError` is raised when the user is not a recipient or group member.

Expand All @@ -96,7 +102,9 @@ def test_save_method_with_invalid_user(self, notification: Notification, another
)
deleted_notification.save()

def test_save_method_for_staff_user(self, notification: Notification, user: User) -> None:
def test_save_method_for_staff_user(
self, notification: Notification, user: User
) -> None:
"""
Test that a staff user can save `DeletedNotification` even if they are not a recipient or group member.

Expand Down
16 changes: 12 additions & 4 deletions django_notification/tests/models/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def test_title_generator_with_only_target(

assert title == f"{user} liked {user}"

def test_title_generator_with_no_target_or_action_object(self, notification: Notification, user: User) -> None:
def test_title_generator_with_no_target_or_action_object(
self, notification: Notification, user: User
) -> None:
"""
Test the title generator when neither target nor action object are present.

Expand All @@ -113,7 +115,9 @@ def test_mark_as_seen(self, notification: Notification, user: User) -> None:

assert notification.seen_by.filter(id=user.id).exists()

def test_mark_as_seen_with_group(self, notification: Notification, user: User, group: Group) -> None:
def test_mark_as_seen_with_group(
self, notification: Notification, user: User, group: Group
) -> None:
"""
Test the `mark_as_seen` method when the user belongs to a group that is a recipient.

Expand Down Expand Up @@ -149,7 +153,9 @@ def test_notification_str_representation(self, notification: Notification) -> No
expected_str = f"{notification.description} now"
assert str(notification) == expected_str

def test_add_recipients_to_notification(self, notification: Notification, user: User) -> None:
def test_add_recipients_to_notification(
self, notification: Notification, user: User
) -> None:
"""
Test adding recipients to the notification.

Expand All @@ -160,7 +166,9 @@ def test_add_recipients_to_notification(self, notification: Notification, user:
notification.recipient.add(user)
assert notification.recipient.filter(id=user.id).exists()

def test_add_groups_to_notification(self, notification: Notification, group: Group) -> None:
def test_add_groups_to_notification(
self, notification: Notification, group: Group
) -> None:
"""
Test adding groups to the notification.

Expand Down
1 change: 1 addition & 0 deletions django_notification/tests/repository/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TestNotificationQuerySet:
"""
Test suite for the `NotificationQuerySet`.
"""

def test_all_notifications_with_recipients(
self, notifications: List[Notification], qs_user: List[User]
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion django_notification/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("notification/", include("django_notification.api.routers.notification")),
]
]