Skip to content

Commit

Permalink
Added more tests to querysets
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroLQueiroz committed Oct 20, 2023
1 parent 85e1881 commit d971ce4
Showing 1 changed file with 129 additions and 2 deletions.
131 changes: 129 additions & 2 deletions notifications/tests/test_querysets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured
from django.test import override_settings
from swapper import load_model

Expand Down Expand Up @@ -43,7 +44,7 @@ def test_sent_unsent_methods(emailed, method):
),
)
@pytest.mark.django_db
def test_query_read_unread_methods(read, method):
def test_read_unread_methods(read, method):
NotificationFactory.create_batch(3, unread=read)
assert method().count() == 3

Expand All @@ -69,7 +70,7 @@ def test_query_read_unread_methods(read, method):
)
@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True})
@pytest.mark.django_db
def test_query_read_unread_with_deleted_notifications(read, method):
def test_read_unread_with_deleted_notifications(read, method):
NotificationFactory.create_batch(3, unread=read)
assert method().count() == 3

Expand Down Expand Up @@ -114,3 +115,129 @@ def test_mark_all_as_read_unread_with_recipient(status, method, check_method):

method(recipient=recipient)
assert check_method().count() == 2


@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True})
@pytest.mark.parametrize(
"deleted,method",
(
(True, Notification.objects.deleted),
(False, Notification.objects.active),
),
)
@pytest.mark.django_db
def test_deleted_active_methods(deleted, method):
NotificationFactory.create_batch(3, deleted=deleted)
assert method().count() == 3

first_notification = Notification.objects.first()
first_notification.deleted = not deleted
first_notification.save()
assert method().count() == 2

Notification.objects.all().update(deleted=not deleted)
assert method().count() == 0

first_notification.deleted = deleted
first_notification.save()
assert method().count() == 1


@pytest.mark.parametrize(
"method",
(
Notification.objects.deleted,
Notification.objects.active,
),
)
@pytest.mark.django_db
def test_deleted_active_methods_without_soft_delete(method):
with pytest.raises(ImproperlyConfigured):
assert method()


@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True})
@pytest.mark.parametrize(
"status,method,check_method",
(
(False, Notification.objects.mark_all_as_deleted, Notification.objects.deleted),
(True, Notification.objects.mark_all_as_active, Notification.objects.active),
),
)
@pytest.mark.django_db
def test_mark_all_as_deleted_active(status, method, check_method):
NotificationFactory.create_batch(3, deleted=status)
assert Notification.objects.count() == 3
assert check_method().count() == 0

method()
assert check_method().count() == 3


@override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True})
@pytest.mark.parametrize(
"status,method,check_method",
(
(False, Notification.objects.mark_all_as_deleted, Notification.objects.deleted),
(True, Notification.objects.mark_all_as_active, Notification.objects.active),
),
)
@pytest.mark.django_db
def test_mark_all_as_deleted_active_with_recipient(status, method, check_method):
recipient = Recipient()
NotificationFactory.create_batch(2, deleted=status, recipient=recipient)
NotificationFactory.create_batch(1, deleted=status)
assert Notification.objects.count() == 3
assert check_method().count() == 0

method(recipient=recipient)
assert check_method().count() == 2


@pytest.mark.parametrize(
"method",
(
Notification.objects.mark_all_as_deleted,
Notification.objects.mark_all_as_active,
),
)
@pytest.mark.django_db
def test_mark_all_as_deleted_active_without_soft_delete(method):
with pytest.raises(ImproperlyConfigured):
method()


@pytest.mark.parametrize(
"status,method,check_method",
(
(False, Notification.objects.mark_as_sent, Notification.objects.sent),
(True, Notification.objects.mark_as_unsent, Notification.objects.unsent),
),
)
@pytest.mark.django_db
def test_mark_as_sent_unsent_method(status, method, check_method):
NotificationFactory.create_batch(3, emailed=status)
assert Notification.objects.count() == 3
assert check_method().count() == 0

method()
assert check_method().count() == 3


@pytest.mark.parametrize(
"status,method,check_method",
(
(False, Notification.objects.mark_as_sent, Notification.objects.sent),
(True, Notification.objects.mark_as_unsent, Notification.objects.unsent),
),
)
@pytest.mark.django_db
def test_mark_as_sent_unsent_with_recipient(status, method, check_method):
recipient = Recipient()
NotificationFactory.create_batch(2, emailed=status, recipient=recipient)
NotificationFactory.create_batch(1, emailed=status)
assert Notification.objects.count() == 3
assert check_method().count() == 0

method(recipient=recipient)
assert check_method().count() == 2

0 comments on commit d971ce4

Please sign in to comment.