|
9 | 9 |
|
10 | 10 | import pytest |
11 | 11 | from django.conf import settings |
| 12 | +from django.contrib.contenttypes.models import ContentType |
12 | 13 | from django.contrib.sites import models as sites |
13 | 14 | from django.utils import timezone |
14 | 15 | from model_bakery import baker |
| 16 | +from notifications.models import Notification |
15 | 17 |
|
| 18 | +from recoco.apps.projects.models import Note |
16 | 19 | from recoco.apps.tasks import models as task_models |
| 20 | +from recoco.apps.tasks.models import Task, TaskFollowup |
17 | 21 |
|
18 | 22 | from .. import models |
19 | 23 |
|
@@ -111,4 +115,60 @@ def test_projectsite_queryset(): |
111 | 115 | assert project.project_sites.current().site == site |
112 | 116 |
|
113 | 117 |
|
| 118 | +@pytest.mark.django_db |
| 119 | +def test_project_queryset_unread_notifications(): |
| 120 | + project = baker.make(models.Project) |
| 121 | + user = baker.make(settings.AUTH_USER_MODEL) |
| 122 | + public_note = baker.make(Note, project=project, public=True) |
| 123 | + private_note = baker.make(Note, project=project, public=False) |
| 124 | + |
| 125 | + project_ct = ContentType.objects.get_for_model(models.Project) |
| 126 | + task_ct = ContentType.objects.get_for_model(Task) |
| 127 | + task_followup_ct = ContentType.objects.get_for_model(TaskFollowup) |
| 128 | + note_ct = ContentType.objects.get_for_model(Note) |
| 129 | + |
| 130 | + baker.make( |
| 131 | + Notification, |
| 132 | + recipient=user, |
| 133 | + target_object_id=project.id, |
| 134 | + target_content_type=project_ct, |
| 135 | + unread=True, |
| 136 | + action_object_content_type=task_ct, |
| 137 | + ) |
| 138 | + baker.make( |
| 139 | + Notification, |
| 140 | + recipient=user, |
| 141 | + target_object_id=project.id, |
| 142 | + target_content_type=project_ct, |
| 143 | + unread=True, |
| 144 | + action_object_content_type=task_followup_ct, |
| 145 | + ) |
| 146 | + baker.make( |
| 147 | + Notification, |
| 148 | + recipient=user, |
| 149 | + target_object_id=project.id, |
| 150 | + target_content_type=project_ct, |
| 151 | + unread=True, |
| 152 | + action_object_content_type=note_ct, |
| 153 | + action_object_object_id=public_note.id, |
| 154 | + ) |
| 155 | + baker.make( |
| 156 | + Notification, |
| 157 | + recipient=user, |
| 158 | + target_object_id=project.id, |
| 159 | + target_content_type=project_ct, |
| 160 | + unread=True, |
| 161 | + action_object_content_type=note_ct, |
| 162 | + action_object_object_id=private_note.id, |
| 163 | + ) |
| 164 | + |
| 165 | + annotated_project = models.Project.objects.with_unread_notifications( |
| 166 | + user.id |
| 167 | + ).first() |
| 168 | + assert annotated_project.action_notifications_count == 2 |
| 169 | + assert annotated_project.conversation_notifications_count == 1 |
| 170 | + assert annotated_project.private_conversation_notifications_count == 1 |
| 171 | + assert annotated_project.document_notifications_count == 0 |
| 172 | + |
| 173 | + |
114 | 174 | # eof |
0 commit comments