Ticket #36398: tests.2.py

File tests.2.py, 1.6 KB (added by OutOfFocus4, 4 months ago)
Line 
1from django.db.transaction import atomic
2from django.test import TransactionTestCase
3
4from .models import City, Country, Person, PersonProfile
5
6
7class ForeignKeySelectForUpdateTestCase(TransactionTestCase):
8 def setUp(self):
9 Country.objects.create(pk=1, name="Belgium")
10 Country.objects.create(pk=2, name="France")
11 City.objects.bulk_create(
12 (
13 City(pk=1, name="Liberchies", country_id=1),
14 City(pk=2, name="Samois-sur-Seine", country_id=2),
15 )
16 )
17 Person.objects.bulk_create(
18 (
19 Person(pk=3, name="Reinhardt", born_id=1, died_id=2),
20 Person(pk=4, name="Lon Sonterre", born_id=2),
21 )
22 )
23 PersonProfile.objects.bulk_create(
24 (PersonProfile(pk=3, person_id=4), PersonProfile(pk=5, person_id=3))
25 )
26
27 def test_select_for_update_reverse_one_to_one(self):
28 with atomic():
29 data = [
30 *Person.objects.values_list("profile__pk", flat=True)
31 .order_by("profile__pk")
32 .select_for_update(of=("self",))
33 .filter(pk=3)
34 ]
35 self.assertListEqual(data, [5])
36
37 def test_select_for_update_forward_nullable_foreign_key(self):
38 with atomic():
39 data = [
40 *Person.objects.select_for_update(of=("self",))
41 .values_list("died__name", flat=True)
42 .order_by("died__name")
43 .filter(pk=3)
44 ]
45 self.assertListEqual(data, ["Samois-sur-Seine"])
Back to Top