Ticket #36398: tests.py

File tests.py, 1.9 KB (added by OutOfFocus4, 4 months ago)
Line 
1from django.contrib.auth import get_user_model
2from django.contrib.auth.models import Group
3from django.db.transaction import atomic
4from django.test import TransactionTestCase
5
6User = get_user_model()
7
8
9class ProofOfConceptTest(TransactionTestCase):
10 def setUp(self):
11 super().setUp()
12 groups = Group.objects.bulk_create(
13 (
14 Group(pk=1, name="Group 1"),
15 Group(pk=2, name="Group 2"),
16 Group(pk=3, name="Group 3"),
17 )
18 )
19 users = User.objects.bulk_create(
20 (
21 User(
22 pk=1,
23 password="!",
24 first_name="Camilla",
25 last_name="Millbern",
26 email="cmillbern@aol.com",
27 username="cmillbern",
28 ),
29 User(
30 pk=2,
31 password="!",
32 first_name="Shaina",
33 last_name="Sonnen",
34 email="shainasonnen@gmail.com",
35 username="ssonnen43",
36 ),
37 )
38 )
39 users[0].groups.set(groups)
40 users[1].groups.set(groups[:2])
41
42 def test_always_works(self):
43 with atomic():
44 groups = [
45 *User.objects.values_list("pk", "groups__name")
46 .order_by("groups__name")
47 .select_for_update(of=("self",))
48 .filter(pk=1)
49 ]
50 self.assertSequenceEqual(
51 groups, ((1, "Group 1"), (1, "Group 2"), (1, "Group 3"))
52 )
53
54 def test_proof_of_concept(self):
55 with atomic():
56 groups = [
57 *User.objects.values_list("groups__name", flat=True)
58 .order_by("groups__name")
59 .select_for_update(of=("self",))
60 .filter(pk=1)
61 ]
62 self.assertSequenceEqual(groups, ("Group 1", "Group 2", "Group 3"))
Back to Top