Adds tests for select_for_update.

This commit is contained in:
Bertrand Bordage 2014-10-20 17:25:20 +02:00
parent 7877a13e82
commit a4f766fd14
2 changed files with 43 additions and 6 deletions

View file

@ -8,9 +8,10 @@ except ImportError: # For Python 2.6
from unittest2 import skip
from django.contrib.auth.models import Group, Permission, User
from django.db import connection
from django.db import connection, transaction
from django.db.models import Count
from django.test import TransactionTestCase
from django.db.transaction import TransactionManagementError
from django.test import TransactionTestCase, skipUnlessDBFeature
from .models import Test
@ -439,9 +440,24 @@ class ReadTestCase(TransactionTestCase):
def test_using(self):
pass
@skip(NotImplementedError)
@skipUnlessDBFeature('has_select_for_update')
def test_select_for_update(self):
pass
with self.assertRaises(TransactionManagementError):
list(Test.objects.select_for_update())
with self.assertNumQueries(1):
with transaction.atomic():
data1 = list(Test.objects.select_for_update())
self.assertListEqual(data1, [self.t1, self.t2])
self.assertListEqual([t.name for t in data1],
['test1', 'test2'])
with self.assertNumQueries(0):
with transaction.atomic():
data2 = list(Test.objects.select_for_update())
self.assertListEqual(data2, [self.t1, self.t2])
self.assertListEqual([t.name for t in data2],
['test1', 'test2'])
def test_extra_select(self):
"""

View file

@ -9,9 +9,9 @@ except ImportError: # For Python 2.6
from django import VERSION as django_version
from django.contrib.auth.models import User, Permission, Group
from django.core.exceptions import MultipleObjectsReturned
from django.db import connection
from django.db import connection, transaction
from django.db.models import Count
from django.test import TransactionTestCase
from django.test import TransactionTestCase, skipUnlessDBFeature
from .models import Test
@ -481,6 +481,27 @@ class WriteTestCase(TransactionTestCase):
.prefetch_related('owner__groups__permissions'))
self.assertEqual(data7[0].owner.username, 'modified_user')
@skipUnlessDBFeature('has_select_for_update')
def test_invalidate_select_for_update(self):
with self.assertNumQueries(1):
Test.objects.bulk_create([Test(name='test1'), Test(name='test2')])
with self.assertNumQueries(1):
with transaction.atomic():
data1 = list(Test.objects.select_for_update())
self.assertListEqual([t.name for t in data1],
['test1', 'test2'])
with self.assertNumQueries(1):
with transaction.atomic():
qs = Test.objects.select_for_update()
qs.update(name='test3')
with self.assertNumQueries(1):
with transaction.atomic():
data2 = list(Test.objects.select_for_update())
self.assertListEqual([t.name for t in data2], ['test3'] * 2)
@skip(NotImplementedError)
def test_invalidate_extra_select(self):
pass