From 93b6238c757da4cd42988db7ba8edac9996239a2 Mon Sep 17 00:00:00 2001 From: imperosol Date: Sun, 25 Oct 2026 12:04:31 +0100 Subject: [PATCH] Test `ClubNewsView` --- club/tests/test_club_news.py | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 club/tests/test_club_news.py diff --git a/club/tests/test_club_news.py b/club/tests/test_club_news.py new file mode 100644 index 00000000..75d58cb8 --- /dev/null +++ b/club/tests/test_club_news.py @@ -0,0 +1,60 @@ +from datetime import datetime + +from django.test import RequestFactory, TestCase +from django.urls import reverse +from django.utils.timezone import get_current_timezone +from model_bakery import baker + +from club.models import Club +from club.views import ClubNewsView +from com.models import News, NewsDate +from core.baker_recipes import subscriber_user +from core.models import User + + +class TestClubNews(TestCase): + @classmethod + def setUpTestData(cls): + cls.club = baker.make(Club) + cls.news = baker.make( + News, club=cls.club, is_published=True, author=baker.make(User), _quantity=5 + ) + d = datetime(year=2025, month=10, day=1, tzinfo=get_current_timezone()) + dates = [ + baker.prepare(NewsDate, news=cls.news[0], start_date=d), + baker.prepare(NewsDate, news=cls.news[0], start_date=d.replace(day=8)), + baker.prepare(NewsDate, news=cls.news[0], start_date=d.replace(day=15)), + baker.prepare(NewsDate, news=cls.news[1], start_date=d.replace(month=5)), + baker.prepare(NewsDate, news=cls.news[2], start_date=d.replace(month=4)), + baker.prepare(NewsDate, news=cls.news[3], start_date=d.replace(year=2024)), + baker.prepare(NewsDate, news=cls.news[4], start_date=d.replace(year=2020)), + ] + NewsDate.objects.bulk_create(dates) + cls.url = reverse("club:news_list", kwargs={"club_id": cls.club.id}) + + def test_load_page(self): + """Just test that the page loads.""" + self.client.force_login(subscriber_user.make()) + res = self.client.get(self.url) + assert res.status_code == 200 + + def test_page_data(self): + """Test that we display the good events.""" + # For this test, the object we want to test + # is built with `itertools.groupby`. + # However, it is consumed when rendering the template, + # making it unusable for later testing purposes, + # so we must use lower level django APIs in this test. + request = RequestFactory().get(self.url) + request.user = subscriber_user.make() + response = ClubNewsView.as_view()(request, club_id=self.club.id) + events = [ + (semester_code, list(dates)) + for semester_code, dates in response.context_data["events"] + ] + assert events == [ + ("A25", [self.news[0]]), + ("P25", [self.news[1], self.news[2]]), + ("A24", [self.news[3]]), + ("A20", [self.news[4]]), + ]