Skip to content

Commit

Permalink
Updating documentation and improving coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-code committed Jan 5, 2020
1 parent 4f4a9dd commit 7428d0a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Technology Stack
----------------

* Python_ 3.6.x / 3.7.x
* `Django Web Framework`_ 2.0.x
* `Django Web Framework`_ 2.2.x
* PostgreSQL_
* `Redis 5.0`_
* Daphne_
Expand Down
19 changes: 2 additions & 17 deletions bootcamp/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core import serializers
from django.db import models
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -46,23 +45,9 @@ def mark_all_as_unread(self, recipient=None):

return qs.update(unread=True)

def serialize_latest_notifications(self, recipient=None):
"""Returns a serialized version of the most recent unread elements in
the queryset"""
qs = self.unread()[:5]
if recipient:
qs = qs.filter(recipient=recipient)[:5]

notification_dic = serializers.serialize("json", qs)
return notification_dic

def get_most_recent(self, recipient=None):
def get_most_recent(self):
"""Returns the most recent unread elements in the queryset"""
qs = self.unread()[:5]
if recipient:
qs = qs.filter(recipient=recipient)[:5]

return qs
return self.unread()[:5]


class Notification(models.Model):
Expand Down
100 changes: 95 additions & 5 deletions bootcamp/notifications/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class NotificationsModelsTest(TestCase):
def setUp(self):
self.user = self.make_user("test_user")
self.other_user = self.make_user("other_test_user")
self.first_news = News.objects.create(
user=self.user, content="This is a short content."
)
self.second_news = News.objects.create(
user=self.other_user,
content="This is an answer to the first news.",
reply=True,
parent=self.first_news,
)
self.first_notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="L"
)
Expand All @@ -17,17 +26,28 @@ def setUp(self):
self.third_notification = Notification.objects.create(
actor=self.other_user, recipient=self.user, verb="A"
)
self.fourth_notification = Notification.objects.create(
actor=self.other_user,
recipient=self.user,
action_object=self.first_news,
verb="A",
)

def test_return_values(self):
assert isinstance(self.first_notification, Notification)
assert isinstance(self.second_notification, Notification)
assert isinstance(self.third_notification, Notification)
assert isinstance(self.fourth_notification, Notification)
assert str(self.first_notification) == "test_user liked 0 minutes ago"
assert str(self.second_notification) == "test_user commented 0 minutes ago"
assert str(self.third_notification) == "other_test_user answered 0 minutes ago"
assert (
str(self.fourth_notification)
== "other_test_user answered This is a short content. 0 minutes ago"
)

def test_return_unread(self):
assert Notification.objects.unread().count() == 3
assert Notification.objects.unread().count() == 4
assert self.first_notification in Notification.objects.unread()

def test_mark_as_read_and_return(self):
Expand All @@ -39,16 +59,16 @@ def test_mark_as_read_and_return(self):

def test_mark_all_as_read(self):
Notification.objects.mark_all_as_read()
assert Notification.objects.read().count() == 3
assert Notification.objects.read().count() == 4
Notification.objects.mark_all_as_unread(self.other_user)
assert Notification.objects.read().count() == 1
assert Notification.objects.read().count() == 2
Notification.objects.mark_all_as_unread()
assert Notification.objects.unread().count() == 3
assert Notification.objects.unread().count() == 4
Notification.objects.mark_all_as_read(self.other_user)
assert Notification.objects.read().count() == 2

def test_get_most_recent(self):
assert Notification.objects.get_most_recent().count() == 3
assert Notification.objects.get_most_recent().count() == 4

def test_single_notification(self):
Notification.objects.mark_all_as_read()
Expand All @@ -65,3 +85,73 @@ def test_list_notification(self):
Notification.objects.mark_all_as_read()
notification_handler(self.user, [self.user, self.other_user], "C")
assert Notification.objects.unread().count() == 2

def test_icon_comment(self):
notification_one = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="C"
)
notification_two = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="A"
)
notification_three = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="K"
)
assert notification_one.get_icon() == "fa-comment"
assert notification_two.get_icon() == "fa-comment"
assert notification_three.get_icon() == "fa-comment"

def test_icon_users(self):
notification_one = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="I"
)
notification_two = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="U"
)
notification_three = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="O"
)
assert notification_one.get_icon() == "fa-users"
assert notification_two.get_icon() == "fa-users"
assert notification_three.get_icon() == "fa-users"

def test_icon_hearth(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="L"
)
assert notification.get_icon() == "fa-heart"

def test_icon_star(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="F"
)
assert notification.get_icon() == "fa-star"

def test_icon_check_circle(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="W"
)
assert notification.get_icon() == "fa-check-circle"

def test_icon_pencil(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="E"
)
assert notification.get_icon() == "fa-pencil"

def test_icon_plus(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="V"
)
assert notification.get_icon() == "fa-plus"

def test_icon_share(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="S"
)
assert notification.get_icon() == "fa-share-alt"

def test_icon_reply(self):
notification = Notification.objects.create(
actor=self.user, recipient=self.other_user, verb="R"
)
assert notification.get_icon() == "fa-reply"

0 comments on commit 7428d0a

Please sign in to comment.