~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/regressiontests/delete_regress/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.conf import settings
 
2
from django.db import models, backend, connection, transaction
 
3
from django.db.models import sql, query
 
4
from django.test import TransactionTestCase
 
5
 
 
6
class Book(models.Model):
 
7
    pagecount = models.IntegerField()
 
8
 
 
9
# Can't run this test under SQLite, because you can't
 
10
# get two connections to an in-memory database.
 
11
if settings.DATABASE_ENGINE != 'sqlite3':
 
12
    class DeleteLockingTest(TransactionTestCase):
 
13
        def setUp(self):
 
14
            # Create a second connection to the database
 
15
            self.conn2 = backend.DatabaseWrapper({
 
16
                'DATABASE_HOST': settings.DATABASE_HOST,
 
17
                'DATABASE_NAME': settings.DATABASE_NAME,
 
18
                'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
 
19
                'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
 
20
                'DATABASE_PORT': settings.DATABASE_PORT,
 
21
                'DATABASE_USER': settings.DATABASE_USER,
 
22
                'TIME_ZONE': settings.TIME_ZONE,
 
23
            })
 
24
 
 
25
            # Put both DB connections into managed transaction mode
 
26
            transaction.enter_transaction_management()
 
27
            transaction.managed(True)
 
28
            self.conn2._enter_transaction_management(True)
 
29
 
 
30
        def tearDown(self):
 
31
            # Close down the second connection.
 
32
            transaction.leave_transaction_management()
 
33
            self.conn2.close()
 
34
 
 
35
        def test_concurrent_delete(self):
 
36
            "Deletes on concurrent transactions don't collide and lock the database. Regression for #9479"
 
37
 
 
38
            # Create some dummy data
 
39
            b1 = Book(id=1, pagecount=100)
 
40
            b2 = Book(id=2, pagecount=200)
 
41
            b3 = Book(id=3, pagecount=300)
 
42
            b1.save()
 
43
            b2.save()
 
44
            b3.save()
 
45
 
 
46
            transaction.commit()
 
47
 
 
48
            self.assertEquals(3, Book.objects.count())
 
49
 
 
50
            # Delete something using connection 2.
 
51
            cursor2 = self.conn2.cursor()
 
52
            cursor2.execute('DELETE from delete_regress_book WHERE id=1')
 
53
            self.conn2._commit();
 
54
 
 
55
            # Now perform a queryset delete that covers the object
 
56
            # deleted in connection 2. This causes an infinite loop
 
57
            # under MySQL InnoDB unless we keep track of already
 
58
            # deleted objects.
 
59
            Book.objects.filter(pagecount__lt=250).delete()
 
60
            transaction.commit()
 
61
            self.assertEquals(1, Book.objects.count())