Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rm03 committed Nov 5, 2023
1 parent 4788272 commit 3e1646b
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 8 deletions.
20 changes: 13 additions & 7 deletions backend/announcements/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone


class Audience(models.Model):
Expand Down Expand Up @@ -53,13 +54,18 @@ class Announcement(models.Model):
default=ANNOUNCEMENT_NOTICE,
)
audiences = models.ManyToManyField("Audience", related_name="announcements")
release_time = models.DateTimeField(auto_now_add=True)
release_time = models.DateTimeField(default=timezone.now)
end_time = models.DateTimeField(null=True, blank=True)

def __str__(self):
return f"""[{self.get_announcement_type_display()}
for {','.join([audience.name for audience in self.audiences.all()])}]
starting at {self.release_time.strftime('%m-%d-%Y %H:%M:%S')}
{f'to {self.end_time.strftime("%m-%d-%Y %H:%M:%S")}'
if self.end_time else ''}
| {f'{self.title}: ' if self.title else ''} {self.message}"""
rtime = self.release_time.strftime("%m-%d-%Y %H:%M:%S")
etime = (
f" to {self.end_time.strftime('%m-%d-%Y %H:%M:%S')}"
if self.end_time
else ""
)
aud_str = ",".join([audience.name for audience in self.audiences.all()])
title_str = f"{self.title}: " if self.title else ""

return f"[{self.get_announcement_type_display()} for {aud_str}] \
starting at {rtime}{etime} | {title_str}{self.message}"
5 changes: 4 additions & 1 deletion backend/announcements/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def get_queryset(self):
audiences = self.request.query_params.get("audience")

if active == "true":
queryset = queryset.filter(Q(end_time__gte=timezone.now()) | Q(end_time__isnull=True))
queryset = queryset.filter(
Q(release_time__lte=timezone.now())
& (Q(end_time__gte=timezone.now()) | Q(end_time__isnull=True))
)

if audiences:
audience_names = audiences.split(",")
Expand Down
43 changes: 43 additions & 0 deletions backend/tests/announcements/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from announcements.models import Announcement, Audience
from django.test import TestCase
from django.utils import timezone


class AudienceTestCase(TestCase):
def setUp(self):
self.audience_name = "CLUBS"
self.audience = Audience.objects.create(name=Audience.AUDIENCE_CLUBS)

def test_str(self):
self.assertEqual(str(self.audience), self.audience_name)


class AnnouncementTestCase(TestCase):
def setUp(self):
self.audience_name = "CLUBS"
self.title = "Test Announcement"
self.message = "This is a test"
self.announcement_type = "Issue"
self.release_time = timezone.datetime(
year=3000, month=12, day=31, tzinfo=timezone.get_current_timezone()
)
self.end_time = timezone.datetime(
year=3001, month=1, day=1, tzinfo=timezone.get_current_timezone()
)
self.audience = Audience.objects.create(name=Audience.AUDIENCE_CLUBS)
self.announcement = Announcement.objects.create(
title=self.title,
message=self.message,
announcement_type=Announcement.ANNOUNCEMENT_ISSUE,
release_time=self.release_time,
end_time=self.end_time,
)
self.announcement.audiences.add(self.audience)

def test_str(self):
self.assertEqual(
str(self.announcement),
f"[{self.announcement_type} for {self.audience_name}] \
starting at {self.release_time.strftime('%m-%d-%Y %H:%M:%S')} to \
{self.end_time.strftime('%m-%d-%Y %H:%M:%S')} | {self.title}: {self.message}",
)
15 changes: 15 additions & 0 deletions backend/tests/announcements/test_populate_audiences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from announcements.models import Audience
from django.core.management import call_command
from django.test import TestCase


class PopulateAudiencesTestCase(TestCase):
def test_populate_audiences(self):
call_command("populate_audiences")
self.assertTrue(Audience.objects.all().count() > 0)

def test_populate_twice(self):
call_command("populate_audiences")
count = Audience.objects.all().count()
call_command("populate_audiences")
self.assertEqual(Audience.objects.all().count(), count)
43 changes: 43 additions & 0 deletions backend/tests/announcements/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from announcements.models import Announcement, Audience
from announcements.serializers import AnnouncementSerializer, AudienceSerializer
from django.test import TestCase
from django.utils import timezone


class AudienceSerializerTestCase(TestCase):
def setUp(self):
self.audience = Audience.objects.create(name=Audience.AUDIENCE_CLUBS)
self.serializer = AudienceSerializer(self.audience)

def test_serializer(self):
data = {"name": self.audience.name}
self.assertEqual(self.serializer.data, data)


class AnnouncementSerializerTestCase(TestCase):
def setUp(self):
self.audience_clubs = Audience.objects.create(name=Audience.AUDIENCE_CLUBS)
self.audience_ohq = Audience.objects.create(name=Audience.AUDIENCE_OHQ)
self.announcement = Announcement.objects.create(
title="Test message",
message="This is a test",
announcement_type=Announcement.ANNOUNCEMENT_NOTICE,
release_time=timezone.datetime(
year=3000, month=12, day=31, tzinfo=timezone.get_current_timezone()
),
)
self.announcement.audiences.add(self.audience_clubs)
self.announcement.audiences.add(self.audience_ohq)
self.serializer = AnnouncementSerializer(self.announcement)

def test_serializer(self):
data = {
"id": self.announcement.id,
"title": self.announcement.title,
"message": self.announcement.message,
"announcement_type": self.announcement.announcement_type,
"release_time": self.announcement.release_time.isoformat(),
"end_time": None,
"audiences": [Audience.AUDIENCE_CLUBS, Audience.AUDIENCE_OHQ],
}
self.assertEqual(self.serializer.data, data)
134 changes: 134 additions & 0 deletions backend/tests/announcements/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from announcements.models import Announcement, Audience
from announcements.serializers import AnnouncementSerializer
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.utils import timezone


class AnnouncementsFilterTestCase(TestCase):
def setUp(self):
self.client = Client()
self.audience_clubs = Audience.objects.create(name=Audience.AUDIENCE_CLUBS)
self.audience_ohq = Audience.objects.create(name=Audience.AUDIENCE_OHQ)
self.announcement1 = Announcement.objects.create(
title="Test message",
message="This is a test",
announcement_type=Announcement.ANNOUNCEMENT_NOTICE,
release_time=timezone.datetime(
year=1000, month=12, day=31, tzinfo=timezone.get_current_timezone()
),
end_time=timezone.datetime(
year=1001, month=12, day=31, tzinfo=timezone.get_current_timezone()
),
)
self.announcement2 = Announcement.objects.create(
title="Test message 2",
message="This is also a test",
announcement_type=Announcement.ANNOUNCEMENT_NOTICE,
end_time=timezone.datetime(
year=3000, month=12, day=31, tzinfo=timezone.get_current_timezone()
),
)
self.announcement1.audiences.add(self.audience_clubs)
self.announcement2.audiences.add(self.audience_ohq)

def test_get_no_filter(self):
response = self.client.get("/announcements/")
self.assertIn(AnnouncementSerializer(self.announcement1).data, response.json())
self.assertIn(AnnouncementSerializer(self.announcement2).data, response.json())

def test_filter_active(self):
response = self.client.get("/announcements/?active=true")
self.assertNotIn(
AnnouncementSerializer(self.announcement1).data, response.json()
)
self.assertIn(AnnouncementSerializer(self.announcement2).data, response.json())

def test_filter_audience(self):
response = self.client.get("/announcements/?audience=clubs")
self.assertIn(AnnouncementSerializer(self.announcement1).data, response.json())
self.assertNotIn(
AnnouncementSerializer(self.announcement2).data, response.json()
)


class AnnouncementsPermissionTestCase(TestCase):
def setUp(self):
self.client = Client()

def test_invalid_permission(self):
response = self.client.post(
"/announcements/",
{
"title": "Maintenance Alert",
"message": "We apologize for any inconvenience caused.",
"audiences": [
"CLUBS",
"COURSE_PLAN",
"COURSE_ALERT"
],
},
)
self.assertEqual(response.status_code, 403)


class AnnouncementsModifyTestCase(TestCase):
def setUp(self):
self.client = Client()
for x in [
"MOBILE",
"OHQ",
"CLUBS",
"COURSE_PLAN",
"COURSE_REVIEW",
"COURSE_ALERT",
]:
Audience.objects.get_or_create(name=x)
self.announcement = Announcement.objects.create(
title="Test message",
message="This is a test",
announcement_type=Announcement.ANNOUNCEMENT_NOTICE,
release_time=timezone.datetime(
year=1000, month=12, day=31, tzinfo=timezone.get_current_timezone()
),
end_time=timezone.datetime(
year=1001, month=12, day=31, tzinfo=timezone.get_current_timezone()
),
)
self.user = get_user_model().objects.create(
pennid=1,
username="student",
password="secret",
first_name="First",
last_name="Last",
email="[email protected]",
is_superuser=1,
)
self.client.force_login(self.user)

def test_create_announcement(self):
response = self.client.post(
"/announcements/",
{
"title": "Maintenance Alert",
"message": "We apologize for any inconvenience caused.",
"audiences": ["CLUBS", "COURSE_PLAN", "COURSE_ALERT"],
},
)
self.assertEqual(response.status_code, 201)
self.assertTrue(Announcement.objects.filter(title="Maintenance Alert").exists())

def test_update_announcement(self):
response = self.client.patch(
f"/announcements/{self.announcement.id}/",
{"title": "Wow!"},
content_type="application/json",
)
self.assertEqual(response.status_code, 200)
self.assertTrue(Announcement.objects.filter(title="Wow!").exists())
self.assertFalse(Announcement.objects.filter(title="Test message").exists())

def test_delete_announcement(self):
response = self.client.delete(f"/announcements/{self.announcement.id}/")
self.assertEqual(response.status_code, 204)
self.assertFalse(Announcement.objects.filter(title="Test message").exists())

0 comments on commit 3e1646b

Please sign in to comment.