mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -1,6 +1,4 @@
|
||||
from django.contrib.contenttypes.fields import (
|
||||
GenericForeignKey, GenericRelation,
|
||||
)
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
|
||||
@@ -24,7 +22,7 @@ class Person(models.Model):
|
||||
|
||||
class Book(models.Model):
|
||||
pagecount = models.IntegerField()
|
||||
owner = models.ForeignKey('Child', models.CASCADE, null=True)
|
||||
owner = models.ForeignKey("Child", models.CASCADE, null=True)
|
||||
|
||||
|
||||
class Toy(models.Model):
|
||||
@@ -33,13 +31,13 @@ class Toy(models.Model):
|
||||
|
||||
class Child(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
toys = models.ManyToManyField(Toy, through='PlayedWith')
|
||||
toys = models.ManyToManyField(Toy, through="PlayedWith")
|
||||
|
||||
|
||||
class PlayedWith(models.Model):
|
||||
child = models.ForeignKey(Child, models.CASCADE)
|
||||
toy = models.ForeignKey(Toy, models.CASCADE)
|
||||
date = models.DateField(db_column='date_col')
|
||||
date = models.DateField(db_column="date_col")
|
||||
|
||||
|
||||
class PlayedWithNote(models.Model):
|
||||
@@ -57,8 +55,12 @@ class Email(Contact):
|
||||
|
||||
class Researcher(models.Model):
|
||||
contacts = models.ManyToManyField(Contact, related_name="research_contacts")
|
||||
primary_contact = models.ForeignKey(Contact, models.SET_NULL, null=True, related_name='primary_contacts')
|
||||
secondary_contact = models.ForeignKey(Contact, models.SET_NULL, null=True, related_name='secondary_contacts')
|
||||
primary_contact = models.ForeignKey(
|
||||
Contact, models.SET_NULL, null=True, related_name="primary_contacts"
|
||||
)
|
||||
secondary_contact = models.ForeignKey(
|
||||
Contact, models.SET_NULL, null=True, related_name="secondary_contacts"
|
||||
)
|
||||
|
||||
|
||||
class Food(models.Model):
|
||||
@@ -89,6 +91,7 @@ class Item(models.Model):
|
||||
version = models.ForeignKey(Version, models.CASCADE)
|
||||
location = models.ForeignKey(Location, models.SET_NULL, blank=True, null=True)
|
||||
|
||||
|
||||
# Models for #16128
|
||||
|
||||
|
||||
@@ -141,4 +144,4 @@ class OrderedPerson(models.Model):
|
||||
lives_in = models.ForeignKey(House, models.CASCADE)
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
ordering = ["name"]
|
||||
|
||||
@@ -3,23 +3,50 @@ import datetime
|
||||
from django.db import connection, models, transaction
|
||||
from django.db.models import Exists, OuterRef
|
||||
from django.test import (
|
||||
SimpleTestCase, TestCase, TransactionTestCase, skipUnlessDBFeature,
|
||||
SimpleTestCase,
|
||||
TestCase,
|
||||
TransactionTestCase,
|
||||
skipUnlessDBFeature,
|
||||
)
|
||||
|
||||
from .models import (
|
||||
Award, AwardNote, Book, Child, Contact, Eaten, Email, File, Food, FooFile,
|
||||
FooFileProxy, FooImage, FooPhoto, House, Image, Item, Location, Login,
|
||||
OrderedPerson, OrgUnit, Person, Photo, PlayedWith, PlayedWithNote, Policy,
|
||||
Researcher, Toy, Version,
|
||||
Award,
|
||||
AwardNote,
|
||||
Book,
|
||||
Child,
|
||||
Contact,
|
||||
Eaten,
|
||||
Email,
|
||||
File,
|
||||
Food,
|
||||
FooFile,
|
||||
FooFileProxy,
|
||||
FooImage,
|
||||
FooPhoto,
|
||||
House,
|
||||
Image,
|
||||
Item,
|
||||
Location,
|
||||
Login,
|
||||
OrderedPerson,
|
||||
OrgUnit,
|
||||
Person,
|
||||
Photo,
|
||||
PlayedWith,
|
||||
PlayedWithNote,
|
||||
Policy,
|
||||
Researcher,
|
||||
Toy,
|
||||
Version,
|
||||
)
|
||||
|
||||
|
||||
# Can't run this test under SQLite, because you can't
|
||||
# get two connections to an in-memory database.
|
||||
@skipUnlessDBFeature('test_db_allows_multiple_connections')
|
||||
@skipUnlessDBFeature("test_db_allows_multiple_connections")
|
||||
class DeleteLockingTest(TransactionTestCase):
|
||||
|
||||
available_apps = ['delete_regress']
|
||||
available_apps = ["delete_regress"]
|
||||
|
||||
def setUp(self):
|
||||
# Create a second connection to the default database
|
||||
@@ -62,9 +89,9 @@ class DeleteCascadeTests(TestCase):
|
||||
Django cascades deletes through generic-related objects to their
|
||||
reverse relations.
|
||||
"""
|
||||
person = Person.objects.create(name='Nelson Mandela')
|
||||
award = Award.objects.create(name='Nobel', content_object=person)
|
||||
AwardNote.objects.create(note='a peace prize', award=award)
|
||||
person = Person.objects.create(name="Nelson Mandela")
|
||||
award = Award.objects.create(name="Nobel", content_object=person)
|
||||
AwardNote.objects.create(note="a peace prize", award=award)
|
||||
self.assertEqual(AwardNote.objects.count(), 1)
|
||||
person.delete()
|
||||
self.assertEqual(Award.objects.count(), 0)
|
||||
@@ -78,10 +105,12 @@ class DeleteCascadeTests(TestCase):
|
||||
from one of the participants in the M2M, to the through model, to its
|
||||
related model.
|
||||
"""
|
||||
juan = Child.objects.create(name='Juan')
|
||||
paints = Toy.objects.create(name='Paints')
|
||||
played = PlayedWith.objects.create(child=juan, toy=paints, date=datetime.date.today())
|
||||
PlayedWithNote.objects.create(played=played, note='the next Jackson Pollock')
|
||||
juan = Child.objects.create(name="Juan")
|
||||
paints = Toy.objects.create(name="Paints")
|
||||
played = PlayedWith.objects.create(
|
||||
child=juan, toy=paints, date=datetime.date.today()
|
||||
)
|
||||
PlayedWithNote.objects.create(played=played, note="the next Jackson Pollock")
|
||||
self.assertEqual(PlayedWithNote.objects.count(), 1)
|
||||
paints.delete()
|
||||
self.assertEqual(PlayedWith.objects.count(), 0)
|
||||
@@ -98,7 +127,7 @@ class DeleteCascadeTests(TestCase):
|
||||
|
||||
class DeleteCascadeTransactionTests(TransactionTestCase):
|
||||
|
||||
available_apps = ['delete_regress']
|
||||
available_apps = ["delete_regress"]
|
||||
|
||||
def test_inheritance(self):
|
||||
"""
|
||||
@@ -137,6 +166,7 @@ class LargeDeleteTests(TestCase):
|
||||
|
||||
def noop(*args, **kwargs):
|
||||
pass
|
||||
|
||||
models.signals.post_delete.connect(noop, sender=Book)
|
||||
Book.objects.all().delete()
|
||||
models.signals.post_delete.disconnect(noop, sender=Book)
|
||||
@@ -149,6 +179,7 @@ class ProxyDeleteTest(TestCase):
|
||||
|
||||
See #16128.
|
||||
"""
|
||||
|
||||
def create_image(self):
|
||||
"""Return an Image referenced by both a FooImage and a FooFile."""
|
||||
# Create an Image
|
||||
@@ -244,7 +275,7 @@ class ProxyDeleteTest(TestCase):
|
||||
self.assertEqual(len(FooFileProxy.objects.all()), 0)
|
||||
|
||||
def test_19187_values(self):
|
||||
msg = 'Cannot call delete() after .values() or .values_list()'
|
||||
msg = "Cannot call delete() after .values() or .values_list()"
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
Image.objects.values().delete()
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
@@ -261,21 +292,20 @@ class Ticket19102Tests(TestCase):
|
||||
Note that .values() is not tested here on purpose. .values().delete()
|
||||
doesn't work for non fast-path deletes at all.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.o1 = OrgUnit.objects.create(name='o1')
|
||||
cls.o2 = OrgUnit.objects.create(name='o2')
|
||||
cls.l1 = Login.objects.create(description='l1', orgunit=cls.o1)
|
||||
cls.l2 = Login.objects.create(description='l2', orgunit=cls.o2)
|
||||
cls.o1 = OrgUnit.objects.create(name="o1")
|
||||
cls.o2 = OrgUnit.objects.create(name="o2")
|
||||
cls.l1 = Login.objects.create(description="l1", orgunit=cls.o1)
|
||||
cls.l2 = Login.objects.create(description="l2", orgunit=cls.o2)
|
||||
|
||||
@skipUnlessDBFeature("update_can_self_select")
|
||||
def test_ticket_19102_annotate(self):
|
||||
with self.assertNumQueries(1):
|
||||
Login.objects.order_by('description').filter(
|
||||
Login.objects.order_by("description").filter(
|
||||
orgunit__name__isnull=False
|
||||
).annotate(
|
||||
n=models.Count('description')
|
||||
).filter(
|
||||
).annotate(n=models.Count("description")).filter(
|
||||
n=1, pk=self.l1.pk
|
||||
).delete()
|
||||
self.assertFalse(Login.objects.filter(pk=self.l1.pk).exists())
|
||||
@@ -284,39 +314,27 @@ class Ticket19102Tests(TestCase):
|
||||
@skipUnlessDBFeature("update_can_self_select")
|
||||
def test_ticket_19102_extra(self):
|
||||
with self.assertNumQueries(1):
|
||||
Login.objects.order_by('description').filter(
|
||||
Login.objects.order_by("description").filter(
|
||||
orgunit__name__isnull=False
|
||||
).extra(
|
||||
select={'extraf': '1'}
|
||||
).filter(
|
||||
pk=self.l1.pk
|
||||
).delete()
|
||||
).extra(select={"extraf": "1"}).filter(pk=self.l1.pk).delete()
|
||||
self.assertFalse(Login.objects.filter(pk=self.l1.pk).exists())
|
||||
self.assertTrue(Login.objects.filter(pk=self.l2.pk).exists())
|
||||
|
||||
@skipUnlessDBFeature("update_can_self_select")
|
||||
def test_ticket_19102_select_related(self):
|
||||
with self.assertNumQueries(1):
|
||||
Login.objects.filter(
|
||||
pk=self.l1.pk
|
||||
).filter(
|
||||
Login.objects.filter(pk=self.l1.pk).filter(
|
||||
orgunit__name__isnull=False
|
||||
).order_by(
|
||||
'description'
|
||||
).select_related('orgunit').delete()
|
||||
).order_by("description").select_related("orgunit").delete()
|
||||
self.assertFalse(Login.objects.filter(pk=self.l1.pk).exists())
|
||||
self.assertTrue(Login.objects.filter(pk=self.l2.pk).exists())
|
||||
|
||||
@skipUnlessDBFeature("update_can_self_select")
|
||||
def test_ticket_19102_defer(self):
|
||||
with self.assertNumQueries(1):
|
||||
Login.objects.filter(
|
||||
pk=self.l1.pk
|
||||
).filter(
|
||||
Login.objects.filter(pk=self.l1.pk).filter(
|
||||
orgunit__name__isnull=False
|
||||
).order_by(
|
||||
'description'
|
||||
).only('id').delete()
|
||||
).order_by("description").only("id").delete()
|
||||
self.assertFalse(Login.objects.filter(pk=self.l1.pk).exists())
|
||||
self.assertTrue(Login.objects.filter(pk=self.l2.pk).exists())
|
||||
|
||||
@@ -326,10 +344,10 @@ class DeleteTests(TestCase):
|
||||
# When a subquery is performed by deletion code, the subquery must be
|
||||
# cleared of all ordering. There was a but that caused _meta ordering
|
||||
# to be used. Refs #19720.
|
||||
h = House.objects.create(address='Foo')
|
||||
OrderedPerson.objects.create(name='Jack', lives_in=h)
|
||||
OrderedPerson.objects.create(name='Bob', lives_in=h)
|
||||
OrderedPerson.objects.filter(lives_in__address='Foo').delete()
|
||||
h = House.objects.create(address="Foo")
|
||||
OrderedPerson.objects.create(name="Jack", lives_in=h)
|
||||
OrderedPerson.objects.create(name="Bob", lives_in=h)
|
||||
OrderedPerson.objects.filter(lives_in__address="Foo").delete()
|
||||
self.assertEqual(OrderedPerson.objects.count(), 0)
|
||||
|
||||
def test_foreign_key_delete_nullifies_correct_columns(self):
|
||||
@@ -338,8 +356,8 @@ class DeleteTests(TestCase):
|
||||
same model (Contact), deleting an instance of the target model
|
||||
(contact1) nullifies the correct fields of Researcher.
|
||||
"""
|
||||
contact1 = Contact.objects.create(label='Contact 1')
|
||||
contact2 = Contact.objects.create(label='Contact 2')
|
||||
contact1 = Contact.objects.create(label="Contact 1")
|
||||
contact2 = Contact.objects.create(label="Contact 2")
|
||||
researcher1 = Researcher.objects.create(
|
||||
primary_contact=contact1,
|
||||
secondary_contact=contact2,
|
||||
@@ -357,23 +375,25 @@ class DeleteTests(TestCase):
|
||||
self.assertIsNone(researcher2.secondary_contact)
|
||||
|
||||
def test_self_reference_with_through_m2m_at_second_level(self):
|
||||
toy = Toy.objects.create(name='Paints')
|
||||
child = Child.objects.create(name='Juan')
|
||||
toy = Toy.objects.create(name="Paints")
|
||||
child = Child.objects.create(name="Juan")
|
||||
Book.objects.create(pagecount=500, owner=child)
|
||||
PlayedWith.objects.create(child=child, toy=toy, date=datetime.date.today())
|
||||
Book.objects.filter(Exists(
|
||||
Book.objects.filter(
|
||||
pk=OuterRef('pk'),
|
||||
owner__toys=toy.pk,
|
||||
),
|
||||
)).delete()
|
||||
Book.objects.filter(
|
||||
Exists(
|
||||
Book.objects.filter(
|
||||
pk=OuterRef("pk"),
|
||||
owner__toys=toy.pk,
|
||||
),
|
||||
)
|
||||
).delete()
|
||||
self.assertIs(Book.objects.exists(), False)
|
||||
|
||||
|
||||
class DeleteDistinct(SimpleTestCase):
|
||||
def test_disallowed_delete_distinct(self):
|
||||
msg = 'Cannot call delete() after .distinct().'
|
||||
msg = "Cannot call delete() after .distinct()."
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
Book.objects.distinct().delete()
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
Book.objects.distinct('id').delete()
|
||||
Book.objects.distinct("id").delete()
|
||||
|
||||
Reference in New Issue
Block a user