|
| 1 | +from datetime import date, timedelta |
| 2 | +from random import randint |
| 3 | + |
1 | 4 | from django.utils import timezone
|
2 | 5 | from rest_framework.test import APITestCase
|
3 | 6 |
|
4 | 7 | from example.factories import CommentFactory, EntryFactory
|
5 |
| -from example.models import Author, Blog, Comment, Entry |
| 8 | +from example.models import Author, Blog, Comment, Entry, LabResults, ResearchProject |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class PerformanceTestCase(APITestCase):
|
@@ -84,3 +87,32 @@ def test_query_prefetch_uses_included_resources(self):
|
84 | 87 | "/entries?fields[entries]=comments&page[size]=25"
|
85 | 88 | )
|
86 | 89 | self.assertEqual(len(response.data["results"]), 25)
|
| 90 | + |
| 91 | + def test_query_prefetch_read_only(self): |
| 92 | + """We expect a read only list view with an include have five queries: |
| 93 | +
|
| 94 | + 1. Primary resource COUNT query |
| 95 | + 2. Primary resource SELECT |
| 96 | + 3. Authors prefetched |
| 97 | + 4. Author types prefetched |
| 98 | + 5. Entries prefetched |
| 99 | + """ |
| 100 | + project = ResearchProject.objects.create( |
| 101 | + topic="Mars Mission", supervisor="Elon Musk" |
| 102 | + ) |
| 103 | + |
| 104 | + LabResults.objects.bulk_create( |
| 105 | + [ |
| 106 | + LabResults( |
| 107 | + research_project=project, |
| 108 | + date=date.today() + timedelta(days=i), |
| 109 | + measurements=randint(0, 10000), |
| 110 | + author=self.author, |
| 111 | + ) |
| 112 | + for i in range(20) |
| 113 | + ] |
| 114 | + ) |
| 115 | + |
| 116 | + with self.assertNumQueries(5): |
| 117 | + response = self.client.get("/lab-results?include=author&page[size]=25") |
| 118 | + self.assertEqual(len(response.data["results"]), 20) |
0 commit comments