Files
offline-db/content/tests/test_admin_performance2.py
Barış Keserandgoogle-labs-jules[bot] 780d2bd231 Fix admin N+1 and full scan risk in EpisodeAdmin and SeasonAdmin (#286)
- Removed `anime__title` cross-table relation from `search_fields` in `SeasonAdmin`.
- Removed `list_filter = ('anime',)` from `SeasonAdmin` and added `autocomplete_fields = ('anime',)`.
- Removed `list_filter = ('season__anime',)` from `EpisodeAdmin` which was performing full table loading.
- Replaced `- ` with `- [x] ` for the known `EpisodeAdmin` issue in `.jules/development-plan.md`.
- Updated test expectations in `test_admin_performance2.py` to reflect the reduction in query counts due to the fixes.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-03-12 20:30:39 +00:00

29 lines
1.0 KiB
Python

from django.test import TestCase, Client
from django.contrib.auth import get_user_model
from django.urls import reverse
from content.models import Anime, Season, Episode
User = get_user_model()
class EpisodeAdminPerformanceTest(TestCase):
def setUp(self):
self.admin_user = User.objects.create_superuser('admin_ep_test2', 'admin@example.com', 'password')
self.client = Client()
self.client.force_login(self.admin_user)
self.anime = Anime.objects.create(title="Test Anime")
self.season = Season.objects.create(anime=self.anime, number=1)
for i in range(10):
Episode.objects.create(season=self.season, number=i+1, title=f"Ep {i+1}")
def test_admin_changelist_performance(self):
url = reverse('admin:content_episode_changelist')
self.client.get(url)
# 1 auth, 1 session, 2 counts, 1 main fetch = 5
with self.assertNumQueries(5):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)