Skip to content

Commit 40abb5d

Browse files
committed
Confirm Lab 2.3
1 parent 3a07faf commit 40abb5d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

project/tests/test_lab2_3.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from django.contrib.auth.models import User
2+
from django.test import Client, TestCase, tag
3+
from django.urls import reverse
4+
5+
from project.newsletter.models import Category, Post, Subscription
6+
7+
8+
@tag("lab_test")
9+
class TestAnalytics(TestCase):
10+
def test_verify_broken(self):
11+
author = User.objects.create_superuser(username="u1")
12+
categories = [
13+
Category.objects.create(slug=f"c{i}", title=f"Cat {i}") for i in range(5)
14+
]
15+
for i in range(5):
16+
post = Post.objects.create(
17+
author=author, slug=f"post{i}", title=f"Post {i}"
18+
)
19+
post.categories.set(categories)
20+
subscriber = User.objects.create(username=f"user{i}")
21+
subscription = Subscription.objects.create(user=subscriber)
22+
subscription.categories.set(categories)
23+
client = Client()
24+
client.force_login(author)
25+
26+
with self.assertNumQueries(6):
27+
# This should contain duplicate queries on category
28+
response = client.get(reverse("newsletter:analytics"))
29+
30+
self.assertEqual(
31+
response.context["aggregates"],
32+
{
33+
"Subscriptions": 25,
34+
"Subscriptions (30 days)": 25,
35+
"Subscriptions (90 days)": 25,
36+
"Subscriptions (180 days)": 25,
37+
"Posts": 5,
38+
"Posts (30 days)": 5,
39+
"Posts (90 days)": 5,
40+
"Posts (180 days)": 5,
41+
},
42+
)
43+
44+
self.assertEqual(
45+
response.context["subscription_category_aggregates"],
46+
{f"Cat {i}": 5 for i in range(5)},
47+
)
48+
self.assertEqual(
49+
response.context["post_category_aggregates"],
50+
{f"Cat {i}": 5 for i in range(5)},
51+
)

0 commit comments

Comments
 (0)