Source code for intranet.apps.dashboard.tests
from datetime import date
from django.contrib.auth import get_user_model
from ...test.ion_test import IonTestCase
from ...utils.date import get_senior_graduation_year
from ..eighth.models import EighthActivity, EighthBlock, EighthScheduledActivity, EighthSignup, EighthSponsor
from .views import gen_schedule, gen_sponsor_schedule
[docs]class DashboardTest(IonTestCase):
"""Test for dashboard module"""
[docs] def setUp(self) -> None:
self.user = get_user_model().objects.get_or_create(
username="awilliam", graduation_year=get_senior_graduation_year() + 1, user_type="student"
)[0]
[docs] def test_gen_schedule(self):
"""Tests the gen_schedule method."""
self.make_admin()
schedule, no_signup_today = gen_schedule(self.user)
# There are no eighth blocks and no signups, so this should be None, False
self.assertIsNone(schedule)
self.assertFalse(no_signup_today)
# Add some blocks
today = date.today()
block1 = EighthBlock.objects.create(date=today, block_letter="A")
block2 = EighthBlock.objects.create(date=today, block_letter="B")
schedule, no_signup_today = gen_schedule(self.user)
# There are several eighth blocks but no signups, so this should be not None, then True
self.assertIsNotNone(schedule)
self.assertTrue(no_signup_today)
# Add an activity and schedule it
eighthactivity = EighthActivity.objects.create(name="Testing Ion", description="lol")
act_block1 = EighthScheduledActivity.objects.create(block=block1, activity=eighthactivity, capacity=10)
act_block2 = EighthScheduledActivity.objects.create(block=block2, activity=eighthactivity, capacity=10)
# Sign up this user for block1
EighthSignup.objects.create(user=self.user, scheduled_activity=act_block1)
schedule, no_signup_today = gen_schedule(self.user)
# There are several eighth blocks but no signups, so this should be not None, then True for block2
self.assertIsNotNone(schedule)
self.assertTrue(no_signup_today)
# The first item in `schedule` should be block1 and should be flagged "open"
self.assertEqual(block1, schedule[0]["block"])
self.assertEqual("Testing Ion", schedule[0]["current_signup"])
self.assertIn("open", schedule[0]["flags"])
# The second item in `schedule` should be block2 and should be flagged "warning" and "open"
self.assertEqual(block2, schedule[1]["block"])
self.assertIsNone(schedule[1]["current_signup"])
self.assertIn("warning", schedule[1]["flags"])
self.assertIn("open", schedule[1]["flags"])
# Lock block1 and it should show as locked
block1.locked = True
block1.save()
schedule, no_signup_today = gen_schedule(self.user)
self.assertIn("locked", schedule[0]["flags"])
# Sign up for block2, but then cancel the activity
signup = EighthSignup.objects.create(user=self.user, scheduled_activity=act_block2)
act_block2.cancel()
schedule, no_signup_today = gen_schedule(self.user)
# block2 should show as "warning", "cancelled", and "open"
for flag in ["warning", "cancelled", "open"]:
self.assertIn(flag, schedule[1]["flags"])
# Clean up: remove the blocks, signup, activity, etc.
block1.delete()
block2.delete()
signup.delete()
act_block2.delete()
act_block1.delete()