Skip to content

Commit 783ed2e

Browse files
Add test for cache invalidation and fix test cache clearing
Co-authored-by: alexeygrigorev <875246+alexeygrigorev@users.noreply.github.com>
1 parent a4cb3df commit 783ed2e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

courses/tests/test_course.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.test import TestCase, Client
22
from django.urls import reverse
33
from django.utils import timezone
4+
from django.core.cache import cache
45

56
from courses.models import (
67
User,
@@ -27,6 +28,9 @@
2728

2829
class CourseDetailViewTests(TestCase):
2930
def setUp(self):
31+
# Clear cache before each test to ensure fresh state
32+
cache.clear()
33+
3034
self.client = Client()
3135

3236
self.user = User.objects.create_user(**credentials)

courses/tests/test_leaderboard.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.test import TestCase
44
from django.utils import timezone
55
from datetime import timedelta
6+
from django.core.cache import cache
67

78
from courses.models import (
89
Course,
@@ -125,3 +126,29 @@ def test_leaderboard(self):
125126
self.assertEqual(enrollment.position_on_leaderboard, rank)
126127

127128
self.assertEqual(enrollment.total_score, score)
129+
130+
def test_leaderboard_cache_invalidation(self):
131+
"""Test that leaderboard cache is invalidated when update_leaderboard is called"""
132+
# Create some test data
133+
enrollment1 = self.create_student("student1")
134+
enrollment2 = self.create_student("student2")
135+
136+
homework = self.create_homework(1)
137+
self.submit_homework(homework, enrollment1, score=100)
138+
self.submit_homework(homework, enrollment2, score=50)
139+
140+
# Update leaderboard (this should populate the cache)
141+
update_leaderboard(self.course)
142+
143+
# Check the cache key
144+
cache_key = f"leaderboard:{self.course.id}"
145+
146+
# Manually set a value in cache to verify it gets invalidated
147+
cache.set(cache_key, "test_value", 3600)
148+
self.assertEqual(cache.get(cache_key), "test_value")
149+
150+
# Update leaderboard again (this should invalidate the cache)
151+
update_leaderboard(self.course)
152+
153+
# Cache should be invalidated (None)
154+
self.assertIsNone(cache.get(cache_key))

0 commit comments

Comments
 (0)