Skip to content

Commit bc008f5

Browse files
committed
Confirm Lab 2.2
1 parent 0eeb5c6 commit bc008f5

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

project/config/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@
183183
INSTALLED_APPS += ["debug_toolbar"]
184184
MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
185185
INTERNAL_IPS = ["127.0.0.1", "10.0.2.2"]
186+
# Crank this way up to gather more calls.
187+
DEBUG_TOOLBAR_CONFIG = {"PROFILER_THRESHOLD_RATIO": 75000}
186188

187189
LOGGING = {
188190
"version": 1,

project/tests/test_lab2_2.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from unittest.mock import patch
2+
3+
from django.contrib.auth.models import User
4+
from django.test import Client, TestCase, tag
5+
from django.urls import reverse
6+
7+
from project.newsletter.models import Category, Post, Subscription
8+
9+
10+
@tag("lab_test")
11+
class TestAnalytics(TestCase):
12+
def test_verify_broken(self):
13+
author = User.objects.create_superuser(username="u1")
14+
categories = [
15+
Category.objects.create(slug=f"c{i}", title=f"Cat {i}") for i in range(5)
16+
]
17+
for i in range(5):
18+
post = Post.objects.create(
19+
author=author, slug=f"post{i}", title=f"Post {i}"
20+
)
21+
post.categories.set(categories)
22+
subscriber = User.objects.create(username=f"user{i}")
23+
subscription = Subscription.objects.create(user=subscriber)
24+
subscription.categories.set(categories)
25+
client = Client()
26+
client.force_login(author)
27+
28+
with self.assertNumQueries(8):
29+
# This should contain duplicate queries on category
30+
client.get(reverse("newsletter:analytics"))
31+
32+
@patch("project.newsletter.views.determine_buckets")
33+
def test_count_breakdown_calls(self, determine_buckets):
34+
determine_buckets.return_value = (False, False, False)
35+
author = User.objects.create_superuser(username="u1")
36+
categories = [
37+
Category.objects.create(slug=f"c{i}", title=f"Cat {i}") for i in range(5)
38+
]
39+
for i in range(5):
40+
post = Post.objects.create(
41+
author=author, slug=f"post{i}", title=f"Post {i}"
42+
)
43+
post.categories.set(categories)
44+
subscriber = User.objects.create(username=f"user{i}")
45+
subscription = Subscription.objects.create(user=subscriber)
46+
subscription.categories.set(categories)
47+
client = Client()
48+
client.force_login(author)
49+
50+
client.get(reverse("newsletter:analytics"))
51+
self.assertEqual(determine_buckets.call_count, 10)
52+
determine_buckets.reset_mock()
53+
54+
# Add more data and verify determine_buckets is called more.
55+
post = Post.objects.create(author=author, slug="post6", title="Post 6")
56+
post.categories.set(categories)
57+
subscriber = User.objects.create(username="user6")
58+
subscription = Subscription.objects.create(user=subscriber)
59+
subscription.categories.set(categories)
60+
client.get(reverse("newsletter:analytics"))
61+
self.assertEqual(determine_buckets.call_count, 12)

0 commit comments

Comments
 (0)