mirror of https://github.com/django/django.git
Migrated expressions_regress doctests. Thanks to Stephan Jaekel.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13950 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
75536fef1f
commit
d0da38410b
|
@ -1,12 +1,7 @@
|
|||
"""
|
||||
Spanning tests for all the operations that F() expressions can perform.
|
||||
Model for testing arithmetic expressions.
|
||||
"""
|
||||
from django.conf import settings
|
||||
from django.db import models, DEFAULT_DB_ALIAS
|
||||
|
||||
#
|
||||
# Model for testing arithmetic expressions.
|
||||
#
|
||||
from django.db import models
|
||||
|
||||
class Number(models.Model):
|
||||
integer = models.IntegerField(db_column='the_integer')
|
||||
|
@ -15,123 +10,3 @@ class Number(models.Model):
|
|||
def __unicode__(self):
|
||||
return u'%i, %.3f' % (self.integer, self.float)
|
||||
|
||||
|
||||
__test__ = {'API_TESTS': """
|
||||
>>> from django.db.models import F
|
||||
|
||||
>>> Number(integer=-1).save()
|
||||
>>> Number(integer=42).save()
|
||||
>>> Number(integer=1337).save()
|
||||
|
||||
We can fill a value in all objects with an other value of the same object.
|
||||
|
||||
>>> Number.objects.update(float=F('integer'))
|
||||
3
|
||||
>>> Number.objects.all()
|
||||
[<Number: -1, -1.000>, <Number: 42, 42.000>, <Number: 1337, 1337.000>]
|
||||
|
||||
We can increment a value of all objects in a query set.
|
||||
|
||||
>>> Number.objects.filter(integer__gt=0).update(integer=F('integer') + 1)
|
||||
2
|
||||
>>> Number.objects.all()
|
||||
[<Number: -1, -1.000>, <Number: 43, 42.000>, <Number: 1338, 1337.000>]
|
||||
|
||||
We can filter for objects, where a value is not equals the value of an other field.
|
||||
|
||||
>>> Number.objects.exclude(float=F('integer'))
|
||||
[<Number: 43, 42.000>, <Number: 1338, 1337.000>]
|
||||
|
||||
Complex expressions of different connection types are possible.
|
||||
|
||||
>>> n = Number.objects.create(integer=10, float=123.45)
|
||||
|
||||
>>> Number.objects.filter(pk=n.pk).update(float=F('integer') + F('float') * 2)
|
||||
1
|
||||
>>> Number.objects.get(pk=n.pk)
|
||||
<Number: 10, 256.900>
|
||||
|
||||
# All supported operators work as expected.
|
||||
|
||||
>>> n = Number.objects.create(integer=42, float=15.5)
|
||||
|
||||
# Left hand operators
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') + 15, float=F('float') + 42.7)
|
||||
>>> Number.objects.get(pk=n.pk) # LH Addition of floats and integers
|
||||
<Number: 57, 58.200>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') - 15, float=F('float') - 42.7)
|
||||
>>> Number.objects.get(pk=n.pk) # LH Subtraction of floats and integers
|
||||
<Number: 27, -27.200>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') * 15, float=F('float') * 42.7)
|
||||
>>> Number.objects.get(pk=n.pk) # Multiplication of floats and integers
|
||||
<Number: 630, 661.850>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') / 2, float=F('float') / 42.7)
|
||||
>>> Number.objects.get(pk=n.pk) # LH Division of floats and integers
|
||||
<Number: 21, 0.363>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') % 20)
|
||||
>>> Number.objects.get(pk=n.pk) # LH Modulo arithmetic on integers
|
||||
<Number: 2, 15.500>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') & 56)
|
||||
>>> Number.objects.get(pk=n.pk) # LH Bitwise ands on integers
|
||||
<Number: 40, 15.500>
|
||||
|
||||
# Right hand operators
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 + F('integer'), float=42.7 + F('float'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Addition of floats and integers
|
||||
<Number: 57, 58.200>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 - F('integer'), float=42.7 - F('float'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Subtraction of floats and integers
|
||||
<Number: -27, 27.200>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 * F('integer'), float=42.7 * F('float'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Multiplication of floats and integers
|
||||
<Number: 630, 661.850>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=640 / F('integer'), float=42.7 / F('float'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Division of floats and integers
|
||||
<Number: 15, 2.755>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=69 % F('integer'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Modulo arithmetic on integers
|
||||
<Number: 27, 15.500>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 & F('integer'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Bitwise ands on integers
|
||||
<Number: 10, 15.500>
|
||||
"""}
|
||||
|
||||
# Oracle doesn't support the Bitwise OR operator.
|
||||
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.oracle':
|
||||
__test__['API_TESTS'] += """
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') | 48)
|
||||
>>> Number.objects.get(pk=n.pk) # LH Bitwise or on integers
|
||||
<Number: 58, 15.500>
|
||||
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
|
||||
>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 | F('integer'))
|
||||
>>> Number.objects.get(pk=n.pk) # RH Bitwise or on integers
|
||||
<Number: 47, 15.500>
|
||||
|
||||
"""
|
||||
|
|
|
@ -0,0 +1,195 @@
|
|||
"""
|
||||
Spanning tests for all the operations that F() expressions can perform.
|
||||
"""
|
||||
from django.test import TestCase, Approximate
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models, DEFAULT_DB_ALIAS
|
||||
from django.db.models import F
|
||||
|
||||
from regressiontests.expressions_regress.models import Number
|
||||
|
||||
class ExpressionsRegressTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
Number(integer=-1).save()
|
||||
Number(integer=42).save()
|
||||
Number(integer=1337).save()
|
||||
self.assertEqual(Number.objects.update(float=F('integer')), 3)
|
||||
|
||||
def test_fill_with_value_from_same_object(self):
|
||||
"""
|
||||
We can fill a value in all objects with an other value of the
|
||||
same object.
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
Number.objects.all(),
|
||||
[
|
||||
'<Number: -1, -1.000>',
|
||||
'<Number: 42, 42.000>',
|
||||
'<Number: 1337, 1337.000>'
|
||||
]
|
||||
)
|
||||
|
||||
def test_increment_value(self):
|
||||
"""
|
||||
We can increment a value of all objects in a query set.
|
||||
"""
|
||||
self.assertEqual(
|
||||
Number.objects.filter(integer__gt=0)
|
||||
.update(integer=F('integer') + 1),
|
||||
2)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Number.objects.all(),
|
||||
[
|
||||
'<Number: -1, -1.000>',
|
||||
'<Number: 43, 42.000>',
|
||||
'<Number: 1338, 1337.000>'
|
||||
]
|
||||
)
|
||||
|
||||
def test_filter_not_equals_other_field(self):
|
||||
"""
|
||||
We can filter for objects, where a value is not equals the value
|
||||
of an other field.
|
||||
"""
|
||||
self.assertEqual(
|
||||
Number.objects.filter(integer__gt=0)
|
||||
.update(integer=F('integer') + 1),
|
||||
2)
|
||||
self.assertQuerysetEqual(
|
||||
Number.objects.exclude(float=F('integer')),
|
||||
[
|
||||
'<Number: 43, 42.000>',
|
||||
'<Number: 1338, 1337.000>'
|
||||
]
|
||||
)
|
||||
|
||||
def test_complex_expressions(self):
|
||||
"""
|
||||
Complex expressions of different connection types are possible.
|
||||
"""
|
||||
n = Number.objects.create(integer=10, float=123.45)
|
||||
self.assertEqual(Number.objects.filter(pk=n.pk)
|
||||
.update(float=F('integer') + F('float') * 2),
|
||||
1)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=n.pk).integer, 10)
|
||||
self.assertEqual(Number.objects.get(pk=n.pk).float, Approximate(256.900, places=3))
|
||||
|
||||
class ExpressionOperatorTests(TestCase):
|
||||
def setUp(self):
|
||||
self.n = Number.objects.create(integer=42, float=15.5)
|
||||
|
||||
def test_lefthand_addition(self):
|
||||
# LH Addition of floats and integers
|
||||
Number.objects.filter(pk=self.n.pk).update(
|
||||
integer=F('integer') + 15,
|
||||
float=F('float') + 42.7
|
||||
)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 57)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(58.200, places=3))
|
||||
|
||||
def test_lefthand_subtraction(self):
|
||||
# LH Subtraction of floats and integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=F('integer') - 15,
|
||||
float=F('float') - 42.7)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 27)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(-27.200, places=3))
|
||||
|
||||
def test_lefthand_multiplication(self):
|
||||
# Multiplication of floats and integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=F('integer') * 15,
|
||||
float=F('float') * 42.7)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 630)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(661.850, places=3))
|
||||
|
||||
def test_lefthand_division(self):
|
||||
# LH Division of floats and integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=F('integer') / 2,
|
||||
float=F('float') / 42.7)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 21)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(0.363, places=3))
|
||||
|
||||
def test_lefthand_modulo(self):
|
||||
# LH Modulo arithmetic on integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=F('integer') % 20)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 2)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
|
||||
|
||||
def test_lefthand_bitwise_and(self):
|
||||
# LH Bitwise ands on integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=F('integer') & 56)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 40)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
|
||||
|
||||
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.oracle':
|
||||
def test_lefthand_bitwise_or(self):
|
||||
# LH Bitwise or on integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=F('integer') | 48)
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 58)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
|
||||
|
||||
def test_right_hand_addition(self):
|
||||
# Right hand operators
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=15 + F('integer'),
|
||||
float=42.7 + F('float'))
|
||||
|
||||
# RH Addition of floats and integers
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 57)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(58.200, places=3))
|
||||
|
||||
def test_right_hand_subtraction(self):
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=15 - F('integer'),
|
||||
float=42.7 - F('float'))
|
||||
|
||||
# RH Subtraction of floats and integers
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, -27)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(27.200, places=3))
|
||||
|
||||
def test_right_hand_multiplication(self):
|
||||
# RH Multiplication of floats and integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=15 * F('integer'),
|
||||
float=42.7 * F('float'))
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 630)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(661.850, places=3))
|
||||
|
||||
def test_right_hand_division(self):
|
||||
# RH Division of floats and integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=640 / F('integer'),
|
||||
float=42.7 / F('float'))
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 15)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(2.755, places=3))
|
||||
|
||||
def test_right_hand_modulo(self):
|
||||
# RH Modulo arithmetic on integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=69 % F('integer'))
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 27)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
|
||||
|
||||
def test_right_hand_bitwise_and(self):
|
||||
# RH Bitwise ands on integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=15 & F('integer'))
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 10)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
|
||||
|
||||
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.oracle':
|
||||
def test_right_hand_bitwise_or(self):
|
||||
# RH Bitwise or on integers
|
||||
Number.objects.filter(pk=self.n.pk).update(integer=15 | F('integer'))
|
||||
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 47)
|
||||
self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
|
||||
|
Loading…
Reference in New Issue