|
3 | 3 | from django.test import TestCase |
4 | 4 | from django.utils import timezone |
5 | 5 | from datetime import timedelta |
| 6 | +from django.core.cache import cache |
6 | 7 |
|
7 | 8 | from courses.models import ( |
8 | 9 | Course, |
@@ -125,3 +126,29 @@ def test_leaderboard(self): |
125 | 126 | self.assertEqual(enrollment.position_on_leaderboard, rank) |
126 | 127 |
|
127 | 128 | 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