tests.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import datetime
  2. from django.conf import settings
  3. from django.db import backend, connection, transaction, DEFAULT_DB_ALIAS
  4. from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
  5. from models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
  6. PlayedWithNote, Contact, Email, Researcher, Food, Eaten)
  7. # Can't run this test under SQLite, because you can't
  8. # get two connections to an in-memory database.
  9. class DeleteLockingTest(TransactionTestCase):
  10. def setUp(self):
  11. # Create a second connection to the default database
  12. conn_settings = settings.DATABASES[DEFAULT_DB_ALIAS]
  13. self.conn2 = backend.DatabaseWrapper({
  14. 'HOST': conn_settings['HOST'],
  15. 'NAME': conn_settings['NAME'],
  16. 'OPTIONS': conn_settings['OPTIONS'],
  17. 'PASSWORD': conn_settings['PASSWORD'],
  18. 'PORT': conn_settings['PORT'],
  19. 'USER': conn_settings['USER'],
  20. 'TIME_ZONE': settings.TIME_ZONE,
  21. })
  22. # Put both DB connections into managed transaction mode
  23. transaction.enter_transaction_management()
  24. transaction.managed(True)
  25. self.conn2._enter_transaction_management(True)
  26. def tearDown(self):
  27. # Close down the second connection.
  28. transaction.leave_transaction_management()
  29. self.conn2.close()
  30. @skipUnlessDBFeature('test_db_allows_multiple_connections')
  31. def test_concurrent_delete(self):
  32. "Deletes on concurrent transactions don't collide and lock the database. Regression for #9479"
  33. # Create some dummy data
  34. b1 = Book(id=1, pagecount=100)
  35. b2 = Book(id=2, pagecount=200)
  36. b3 = Book(id=3, pagecount=300)
  37. b1.save()
  38. b2.save()
  39. b3.save()
  40. transaction.commit()
  41. self.assertEqual(3, Book.objects.count())
  42. # Delete something using connection 2.
  43. cursor2 = self.conn2.cursor()
  44. cursor2.execute('DELETE from delete_regress_book WHERE id=1')
  45. self.conn2._commit();
  46. # Now perform a queryset delete that covers the object
  47. # deleted in connection 2. This causes an infinite loop
  48. # under MySQL InnoDB unless we keep track of already
  49. # deleted objects.
  50. Book.objects.filter(pagecount__lt=250).delete()
  51. transaction.commit()
  52. self.assertEqual(1, Book.objects.count())
  53. transaction.commit()
  54. class DeleteCascadeTests(TestCase):
  55. def test_generic_relation_cascade(self):
  56. """
  57. Django cascades deletes through generic-related objects to their
  58. reverse relations.
  59. """
  60. person = Person.objects.create(name='Nelson Mandela')
  61. award = Award.objects.create(name='Nobel', content_object=person)
  62. note = AwardNote.objects.create(note='a peace prize',
  63. award=award)
  64. self.assertEqual(AwardNote.objects.count(), 1)
  65. person.delete()
  66. self.assertEqual(Award.objects.count(), 0)
  67. # first two asserts are just sanity checks, this is the kicker:
  68. self.assertEqual(AwardNote.objects.count(), 0)
  69. def test_fk_to_m2m_through(self):
  70. """
  71. If an M2M relationship has an explicitly-specified through model, and
  72. some other model has an FK to that through model, deletion is cascaded
  73. from one of the participants in the M2M, to the through model, to its
  74. related model.
  75. """
  76. juan = Child.objects.create(name='Juan')
  77. paints = Toy.objects.create(name='Paints')
  78. played = PlayedWith.objects.create(child=juan, toy=paints,
  79. date=datetime.date.today())
  80. note = PlayedWithNote.objects.create(played=played,
  81. note='the next Jackson Pollock')
  82. self.assertEqual(PlayedWithNote.objects.count(), 1)
  83. paints.delete()
  84. self.assertEqual(PlayedWith.objects.count(), 0)
  85. # first two asserts just sanity checks, this is the kicker:
  86. self.assertEqual(PlayedWithNote.objects.count(), 0)
  87. class DeleteCascadeTransactionTests(TransactionTestCase):
  88. def test_inheritance(self):
  89. """
  90. Auto-created many-to-many through tables referencing a parent model are
  91. correctly found by the delete cascade when a child of that parent is
  92. deleted.
  93. Refs #14896.
  94. """
  95. r = Researcher.objects.create()
  96. email = Email.objects.create(
  97. label="office-email", email_address="carl@science.edu"
  98. )
  99. r.contacts.add(email)
  100. email.delete()
  101. def test_to_field(self):
  102. """
  103. Cascade deletion works with ForeignKey.to_field set to non-PK.
  104. """
  105. apple = Food.objects.create(name="apple")
  106. eaten = Eaten.objects.create(food=apple, meal="lunch")
  107. apple.delete()
  108. class LargeDeleteTests(TestCase):
  109. def test_large_deletes(self):
  110. "Regression for #13309 -- if the number of objects > chunk size, deletion still occurs"
  111. for x in range(300):
  112. track = Book.objects.create(pagecount=x+100)
  113. Book.objects.all().delete()
  114. self.assertEqual(Book.objects.count(), 0)