Refs #30446 -- Removed unnecessary Value(..., output_field) in docs and tests.

This commit is contained in:
Simon Charette 2020-06-06 14:34:38 -04:00 committed by Mariusz Felisiak
parent 1e38f1191d
commit 156a2138db
7 changed files with 34 additions and 103 deletions

View File

@ -100,7 +100,7 @@ An example::
>>> >>>
>>> from datetime import date, timedelta >>> from datetime import date, timedelta
>>> from django.db.models import Case, CharField, Value, When >>> from django.db.models import Case, Value, When
>>> Client.objects.create( >>> Client.objects.create(
... name='Jane Doe', ... name='Jane Doe',
... account_type=Client.REGULAR, ... account_type=Client.REGULAR,
@ -119,7 +119,6 @@ An example::
... When(account_type=Client.GOLD, then=Value('5%')), ... When(account_type=Client.GOLD, then=Value('5%')),
... When(account_type=Client.PLATINUM, then=Value('10%')), ... When(account_type=Client.PLATINUM, then=Value('10%')),
... default=Value('0%'), ... default=Value('0%'),
... output_field=CharField(),
... ), ... ),
... ).values_list('name', 'discount') ... ).values_list('name', 'discount')
<QuerySet [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')]> <QuerySet [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')]>
@ -141,7 +140,6 @@ the ``Client`` has been with us, we could do so using lookups::
... When(registered_on__lte=a_year_ago, then=Value('10%')), ... When(registered_on__lte=a_year_ago, then=Value('10%')),
... When(registered_on__lte=a_month_ago, then=Value('5%')), ... When(registered_on__lte=a_month_ago, then=Value('5%')),
... default=Value('0%'), ... default=Value('0%'),
... output_field=CharField(),
... ) ... )
... ).values_list('name', 'discount') ... ).values_list('name', 'discount')
<QuerySet [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]> <QuerySet [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]>

View File

@ -841,7 +841,7 @@ class AggregateTestCase(TestCase):
Book.objects.aggregate(fail=F('price')) Book.objects.aggregate(fail=F('price'))
def test_nonfield_annotation(self): def test_nonfield_annotation(self):
book = Book.objects.annotate(val=Max(Value(2, output_field=IntegerField()))).first() book = Book.objects.annotate(val=Max(Value(2))).first()
self.assertEqual(book.val, 2) self.assertEqual(book.val, 2)
book = Book.objects.annotate(val=Max(Value(2), output_field=IntegerField())).first() book = Book.objects.annotate(val=Max(Value(2), output_field=IntegerField())).first()
self.assertEqual(book.val, 2) self.assertEqual(book.val, 2)

View File

@ -374,7 +374,7 @@ class AggregationTests(TestCase):
# Conditional aggregation of a grouped queryset. # Conditional aggregation of a grouped queryset.
self.assertEqual( self.assertEqual(
Book.objects.annotate(c=Count('authors')).values('pk').aggregate(test=Sum( Book.objects.annotate(c=Count('authors')).values('pk').aggregate(test=Sum(
Case(When(c__gt=1, then=1), output_field=IntegerField()) Case(When(c__gt=1, then=1))
))['test'], ))['test'],
3 3
) )
@ -382,7 +382,7 @@ class AggregationTests(TestCase):
def test_sliced_conditional_aggregate(self): def test_sliced_conditional_aggregate(self):
self.assertEqual( self.assertEqual(
Author.objects.all()[:5].aggregate(test=Sum(Case( Author.objects.all()[:5].aggregate(test=Sum(Case(
When(age__lte=35, then=1), output_field=IntegerField() When(age__lte=35, then=1)
)))['test'], )))['test'],
3 3
) )

View File

@ -5,9 +5,9 @@ from unittest import skipIf
from django.core.exceptions import FieldDoesNotExist, FieldError from django.core.exceptions import FieldDoesNotExist, FieldError
from django.db import connection from django.db import connection
from django.db.models import ( from django.db.models import (
BooleanField, Case, CharField, Count, DateTimeField, Exists, BooleanField, Case, Count, DateTimeField, Exists, ExpressionWrapper, F,
ExpressionWrapper, F, FloatField, Func, IntegerField, Max, FloatField, Func, IntegerField, Max, NullBooleanField, OuterRef, Q,
NullBooleanField, OuterRef, Q, Subquery, Sum, Value, When, Subquery, Sum, Value, When,
) )
from django.db.models.expressions import RawSQL from django.db.models.expressions import RawSQL
from django.db.models.functions import Length, Lower from django.db.models.functions import Length, Lower
@ -115,8 +115,7 @@ class NonAggregateAnnotationTestCase(TestCase):
s3.books.add(cls.b3, cls.b4, cls.b6) s3.books.add(cls.b3, cls.b4, cls.b6)
def test_basic_annotation(self): def test_basic_annotation(self):
books = Book.objects.annotate( books = Book.objects.annotate(is_book=Value(1))
is_book=Value(1, output_field=IntegerField()))
for book in books: for book in books:
self.assertEqual(book.is_book, 1) self.assertEqual(book.is_book, 1)
@ -163,9 +162,7 @@ class NonAggregateAnnotationTestCase(TestCase):
self.assertTrue(all(not book.selected for book in books)) self.assertTrue(all(not book.selected for book in books))
def test_annotate_with_aggregation(self): def test_annotate_with_aggregation(self):
books = Book.objects.annotate( books = Book.objects.annotate(is_book=Value(1), rating_count=Count('rating'))
is_book=Value(1, output_field=IntegerField()),
rating_count=Count('rating'))
for book in books: for book in books:
self.assertEqual(book.is_book, 1) self.assertEqual(book.is_book, 1)
self.assertEqual(book.rating_count, 1) self.assertEqual(book.rating_count, 1)
@ -231,9 +228,7 @@ class NonAggregateAnnotationTestCase(TestCase):
self.assertCountEqual(lengths, [3, 7, 8]) self.assertCountEqual(lengths, [3, 7, 8])
def test_filter_annotation(self): def test_filter_annotation(self):
books = Book.objects.annotate( books = Book.objects.annotate(is_book=Value(1)).filter(is_book=1)
is_book=Value(1, output_field=IntegerField())
).filter(is_book=1)
for book in books: for book in books:
self.assertEqual(book.is_book, 1) self.assertEqual(book.is_book, 1)
@ -469,7 +464,7 @@ class NonAggregateAnnotationTestCase(TestCase):
qs = Employee.objects.extra( qs = Employee.objects.extra(
select={'random_value': '42'} select={'random_value': '42'}
).select_related('store').annotate( ).select_related('store').annotate(
annotated_value=Value(17, output_field=IntegerField()) annotated_value=Value(17),
) )
rows = [ rows = [
@ -493,7 +488,7 @@ class NonAggregateAnnotationTestCase(TestCase):
qs = Employee.objects.extra( qs = Employee.objects.extra(
select={'random_value': '42'} select={'random_value': '42'}
).select_related('store').annotate( ).select_related('store').annotate(
annotated_value=Value(17, output_field=IntegerField()) annotated_value=Value(17),
) )
rows = [ rows = [
@ -554,7 +549,7 @@ class NonAggregateAnnotationTestCase(TestCase):
function='COALESCE', function='COALESCE',
) )
).annotate( ).annotate(
tagline_lower=Lower(F('tagline'), output_field=CharField()) tagline_lower=Lower(F('tagline')),
).order_by('name') ).order_by('name')
# LOWER function supported by: # LOWER function supported by:
@ -661,7 +656,6 @@ class NonAggregateAnnotationTestCase(TestCase):
max_pages=Case( max_pages=Case(
When(book_contact_set__isnull=True, then=Value(0)), When(book_contact_set__isnull=True, then=Value(0)),
default=Max(F('book__pages')), default=Max(F('book__pages')),
output_field=IntegerField(),
), ),
).values('name', 'max_pages') ).values('name', 'max_pages')
self.assertCountEqual(qs, [ self.assertCountEqual(qs, [

View File

@ -729,7 +729,7 @@ class BasicExpressionsTests(TestCase):
self.assertEqual(qs.get().ceo_company, 'Test GmbH') self.assertEqual(qs.get().ceo_company, 'Test GmbH')
def test_pickle_expression(self): def test_pickle_expression(self):
expr = Value(1, output_field=IntegerField()) expr = Value(1)
expr.convert_value # populate cached property expr.convert_value # populate cached property
self.assertEqual(pickle.loads(pickle.dumps(expr)), expr) self.assertEqual(pickle.loads(pickle.dumps(expr)), expr)
@ -1545,7 +1545,7 @@ class FTimeDeltaTests(TestCase):
def test_time_subtraction(self): def test_time_subtraction(self):
Time.objects.create(time=datetime.time(12, 30, 15, 2345)) Time.objects.create(time=datetime.time(12, 30, 15, 2345))
queryset = Time.objects.annotate( queryset = Time.objects.annotate(
difference=F('time') - Value(datetime.time(11, 15, 0), output_field=TimeField()), difference=F('time') - Value(datetime.time(11, 15, 0)),
) )
self.assertEqual( self.assertEqual(
queryset.get().difference, queryset.get().difference,
@ -1631,7 +1631,7 @@ class FTimeDeltaTests(TestCase):
def test_date_minus_duration(self): def test_date_minus_duration(self):
more_than_4_days = Experiment.objects.filter( more_than_4_days = Experiment.objects.filter(
assigned__lt=F('completed') - Value(datetime.timedelta(days=4), output_field=DurationField()) assigned__lt=F('completed') - Value(datetime.timedelta(days=4))
) )
self.assertQuerysetEqual(more_than_4_days, ['e3', 'e4', 'e5'], lambda e: e.name) self.assertQuerysetEqual(more_than_4_days, ['e3', 'e4', 'e5'], lambda e: e.name)

View File

@ -6,9 +6,9 @@ from uuid import UUID
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django.db.models import ( from django.db.models import (
BinaryField, BooleanField, Case, CharField, Count, DecimalField, BinaryField, BooleanField, Case, Count, DecimalField, F,
DurationField, F, GenericIPAddressField, IntegerField, Max, Min, Q, Sum, GenericIPAddressField, IntegerField, Max, Min, Q, Sum, TextField, Value,
TextField, TimeField, UUIDField, Value, When, When,
) )
from django.test import SimpleTestCase, TestCase from django.test import SimpleTestCase, TestCase
@ -68,7 +68,6 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=Value('one')), When(integer=1, then=Value('one')),
When(integer=2, then=Value('two')), When(integer=2, then=Value('two')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 'one'), (2, 'two'), (3, 'other'), (2, 'two'), (3, 'other'), (3, 'other'), (4, 'other')], [(1, 'one'), (2, 'two'), (3, 'other'), (2, 'two'), (3, 'other'), (3, 'other'), (4, 'other')],
transform=attrgetter('integer', 'test') transform=attrgetter('integer', 'test')
@ -79,7 +78,6 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.annotate(test=Case( CaseTestModel.objects.annotate(test=Case(
When(integer=1, then=1), When(integer=1, then=1),
When(integer=2, then=2), When(integer=2, then=2),
output_field=IntegerField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 1), (2, 2), (3, None), (2, 2), (3, None), (3, None), (4, None)], [(1, 1), (2, 2), (3, None), (2, 2), (3, None), (3, None), (4, None)],
transform=attrgetter('integer', 'test') transform=attrgetter('integer', 'test')
@ -101,7 +99,6 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.annotate(f_test=Case( CaseTestModel.objects.annotate(f_test=Case(
When(integer2=F('integer'), then=Value('equal')), When(integer2=F('integer'), then=Value('equal')),
When(integer2=F('integer') + 1, then=Value('+1')), When(integer2=F('integer') + 1, then=Value('+1')),
output_field=CharField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')], [(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')],
transform=attrgetter('integer', 'f_test') transform=attrgetter('integer', 'f_test')
@ -135,7 +132,6 @@ class CaseExpressionTests(TestCase):
When(integer2=F('o2o_rel__integer'), then=Value('equal')), When(integer2=F('o2o_rel__integer'), then=Value('equal')),
When(integer2=F('o2o_rel__integer') + 1, then=Value('+1')), When(integer2=F('o2o_rel__integer') + 1, then=Value('+1')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, 'other')], [(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, 'other')],
transform=attrgetter('integer', 'join_test') transform=attrgetter('integer', 'join_test')
@ -148,7 +144,6 @@ class CaseExpressionTests(TestCase):
When(o2o_rel__integer=2, then=Value('two')), When(o2o_rel__integer=2, then=Value('two')),
When(o2o_rel__integer=3, then=Value('three')), When(o2o_rel__integer=3, then=Value('three')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 'one'), (2, 'two'), (3, 'three'), (2, 'two'), (3, 'three'), (3, 'three'), (4, 'one')], [(1, 'one'), (2, 'two'), (3, 'three'), (2, 'two'), (3, 'three'), (3, 'three'), (4, 'one')],
transform=attrgetter('integer', 'join_test') transform=attrgetter('integer', 'join_test')
@ -178,7 +173,6 @@ class CaseExpressionTests(TestCase):
f_test=Case( f_test=Case(
When(integer2=F('integer'), then=Value('equal')), When(integer2=F('integer'), then=Value('equal')),
When(integer2=F('f_plus_1'), then=Value('+1')), When(integer2=F('f_plus_1'), then=Value('+1')),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')], [(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')],
@ -195,7 +189,6 @@ class CaseExpressionTests(TestCase):
When(f_minus_2=0, then=Value('zero')), When(f_minus_2=0, then=Value('zero')),
When(f_minus_2=1, then=Value('one')), When(f_minus_2=1, then=Value('one')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[(1, 'negative one'), (2, 'zero'), (3, 'one'), (2, 'zero'), (3, 'one'), (3, 'one'), (4, 'other')], [(1, 'negative one'), (2, 'zero'), (3, 'one'), (2, 'zero'), (3, 'one'), (3, 'one'), (4, 'other')],
@ -226,7 +219,6 @@ class CaseExpressionTests(TestCase):
test=Case( test=Case(
When(integer2=F('min'), then=Value('min')), When(integer2=F('min'), then=Value('min')),
When(integer2=F('max'), then=Value('max')), When(integer2=F('max'), then=Value('max')),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[(1, 1, 'min'), (2, 3, 'max'), (3, 4, 'max'), (2, 2, 'min'), (3, 4, 'max'), (3, 3, 'min'), (4, 5, 'min')], [(1, 1, 'min'), (2, 3, 'max'), (3, 4, 'max'), (2, 2, 'min'), (3, 4, 'max'), (3, 3, 'min'), (4, 5, 'min')],
@ -242,7 +234,6 @@ class CaseExpressionTests(TestCase):
When(max=3, then=Value('max = 3')), When(max=3, then=Value('max = 3')),
When(max=4, then=Value('max = 4')), When(max=4, then=Value('max = 4')),
default=Value(''), default=Value(''),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[(1, 1, ''), (2, 3, 'max = 3'), (3, 4, 'max = 4'), (2, 3, 'max = 3'), [(1, 1, ''), (2, 3, 'max = 3'), (3, 4, 'max = 4'), (2, 3, 'max = 3'),
@ -256,7 +247,6 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=Value('one')), When(integer=1, then=Value('one')),
When(integer=2, then=Value('two')), When(integer=2, then=Value('two')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
)).exclude(test='other').order_by('pk'), )).exclude(test='other').order_by('pk'),
[(1, 'one'), (2, 'two'), (2, 'two')], [(1, 'one'), (2, 'two'), (2, 'two')],
transform=attrgetter('integer', 'test') transform=attrgetter('integer', 'test')
@ -269,7 +259,6 @@ class CaseExpressionTests(TestCase):
When(integer=2, then=Value('two')), When(integer=2, then=Value('two')),
When(integer=3, then=Value('three')), When(integer=3, then=Value('three')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
)).order_by('test').values_list('integer', flat=True)), )).order_by('test').values_list('integer', flat=True)),
[1, 4, 3, 3, 3, 2, 2] [1, 4, 3, 3, 3, 2, 2]
) )
@ -278,7 +267,7 @@ class CaseExpressionTests(TestCase):
objects = CaseTestModel.objects.annotate( objects = CaseTestModel.objects.annotate(
selected=Case( selected=Case(
When(pk__in=[], then=Value('selected')), When(pk__in=[], then=Value('selected')),
default=Value('not selected'), output_field=CharField() default=Value('not selected'),
) )
) )
self.assertEqual(len(objects), CaseTestModel.objects.count()) self.assertEqual(len(objects), CaseTestModel.objects.count())
@ -291,7 +280,6 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=2), When(integer=1, then=2),
When(integer=2, then=1), When(integer=2, then=1),
default=3, default=3,
output_field=IntegerField(),
) + 1, ) + 1,
).order_by('pk'), ).order_by('pk'),
[(1, 3), (2, 2), (3, 4), (2, 2), (3, 4), (3, 4), (4, 4)], [(1, 3), (2, 2), (3, 4), (2, 2), (3, 4), (3, 4), (4, 4)],
@ -305,7 +293,6 @@ class CaseExpressionTests(TestCase):
test=Case( test=Case(
When(integer=F('integer2'), then='pk'), When(integer=F('integer2'), then='pk'),
When(integer=4, then='pk'), When(integer=4, then='pk'),
output_field=IntegerField(),
), ),
).values('test')).order_by('pk'), ).values('test')).order_by('pk'),
[(1, 1), (2, 2), (3, 3), (4, 5)], [(1, 1), (2, 2), (3, 3), (4, 5)],
@ -327,7 +314,6 @@ class CaseExpressionTests(TestCase):
SOME_CASE = Case( SOME_CASE = Case(
When(pk=0, then=Value('0')), When(pk=0, then=Value('0')),
default=Value('1'), default=Value('1'),
output_field=CharField(),
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
CaseTestModel.objects.annotate(somecase=SOME_CASE).order_by('pk'), CaseTestModel.objects.annotate(somecase=SOME_CASE).order_by('pk'),
@ -340,19 +326,15 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.aggregate( CaseTestModel.objects.aggregate(
one=Sum(Case( one=Sum(Case(
When(integer=1, then=1), When(integer=1, then=1),
output_field=IntegerField(),
)), )),
two=Sum(Case( two=Sum(Case(
When(integer=2, then=1), When(integer=2, then=1),
output_field=IntegerField(),
)), )),
three=Sum(Case( three=Sum(Case(
When(integer=3, then=1), When(integer=3, then=1),
output_field=IntegerField(),
)), )),
four=Sum(Case( four=Sum(Case(
When(integer=4, then=1), When(integer=4, then=1),
output_field=IntegerField(),
)), )),
), ),
{'one': 1, 'two': 2, 'three': 3, 'four': 1} {'one': 1, 'two': 2, 'three': 3, 'four': 1}
@ -373,11 +355,9 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.aggregate( CaseTestModel.objects.aggregate(
equal=Sum(Case( equal=Sum(Case(
When(integer2=F('integer'), then=1), When(integer2=F('integer'), then=1),
output_field=IntegerField(),
)), )),
plus_one=Sum(Case( plus_one=Sum(Case(
When(integer2=F('integer') + 1, then=1), When(integer2=F('integer') + 1, then=1),
output_field=IntegerField(),
)), )),
), ),
{'equal': 3, 'plus_one': 4} {'equal': 3, 'plus_one': 4}
@ -389,7 +369,6 @@ class CaseExpressionTests(TestCase):
When(integer=2, then=3), When(integer=2, then=3),
When(integer=3, then=4), When(integer=3, then=4),
default=1, default=1,
output_field=IntegerField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 1), (2, 3), (3, 4), (3, 4)], [(1, 1), (2, 3), (3, 4), (3, 4)],
transform=attrgetter('integer', 'integer2') transform=attrgetter('integer', 'integer2')
@ -400,7 +379,6 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.filter(integer2=Case( CaseTestModel.objects.filter(integer2=Case(
When(integer=2, then=3), When(integer=2, then=3),
When(integer=3, then=4), When(integer=3, then=4),
output_field=IntegerField(),
)).order_by('pk'), )).order_by('pk'),
[(2, 3), (3, 4), (3, 4)], [(2, 3), (3, 4), (3, 4)],
transform=attrgetter('integer', 'integer2') transform=attrgetter('integer', 'integer2')
@ -422,7 +400,6 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.filter(string=Case( CaseTestModel.objects.filter(string=Case(
When(integer2=F('integer'), then=Value('2')), When(integer2=F('integer'), then=Value('2')),
When(integer2=F('integer') + 1, then=Value('3')), When(integer2=F('integer') + 1, then=Value('3')),
output_field=CharField(),
)).order_by('pk'), )).order_by('pk'),
[(3, 4, '3'), (2, 2, '2'), (3, 4, '3')], [(3, 4, '3'), (2, 2, '2'), (3, 4, '3')],
transform=attrgetter('integer', 'integer2', 'string') transform=attrgetter('integer', 'integer2', 'string')
@ -444,7 +421,6 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.filter(integer=Case( CaseTestModel.objects.filter(integer=Case(
When(integer2=F('o2o_rel__integer') + 1, then=2), When(integer2=F('o2o_rel__integer') + 1, then=2),
When(integer2=F('o2o_rel__integer'), then=3), When(integer2=F('o2o_rel__integer'), then=3),
output_field=IntegerField(),
)).order_by('pk'), )).order_by('pk'),
[(2, 3), (3, 3)], [(2, 3), (3, 3)],
transform=attrgetter('integer', 'integer2') transform=attrgetter('integer', 'integer2')
@ -456,7 +432,6 @@ class CaseExpressionTests(TestCase):
When(o2o_rel__integer=1, then=1), When(o2o_rel__integer=1, then=1),
When(o2o_rel__integer=2, then=3), When(o2o_rel__integer=2, then=3),
When(o2o_rel__integer=3, then=4), When(o2o_rel__integer=3, then=4),
output_field=IntegerField(),
)).order_by('pk'), )).order_by('pk'),
[(1, 1), (2, 3), (3, 4), (3, 4)], [(1, 1), (2, 3), (3, 4), (3, 4)],
transform=attrgetter('integer', 'integer2') transform=attrgetter('integer', 'integer2')
@ -485,7 +460,6 @@ class CaseExpressionTests(TestCase):
integer=Case( integer=Case(
When(integer2=F('integer'), then=2), When(integer2=F('integer'), then=2),
When(integer2=F('f_plus_1'), then=3), When(integer2=F('f_plus_1'), then=3),
output_field=IntegerField(),
), ),
).order_by('pk'), ).order_by('pk'),
[(3, 4), (2, 2), (3, 4)], [(3, 4), (2, 2), (3, 4)],
@ -501,7 +475,6 @@ class CaseExpressionTests(TestCase):
When(f_plus_1=3, then=3), When(f_plus_1=3, then=3),
When(f_plus_1=4, then=4), When(f_plus_1=4, then=4),
default=1, default=1,
output_field=IntegerField(),
), ),
).order_by('pk'), ).order_by('pk'),
[(1, 1), (2, 3), (3, 4), (3, 4)], [(1, 1), (2, 3), (3, 4), (3, 4)],
@ -612,7 +585,6 @@ class CaseExpressionTests(TestCase):
integer=Case( integer=Case(
When(integer2=F('o2o_rel__integer') + 1, then=2), When(integer2=F('o2o_rel__integer') + 1, then=2),
When(integer2=F('o2o_rel__integer'), then=3), When(integer2=F('o2o_rel__integer'), then=3),
output_field=IntegerField(),
), ),
) )
@ -624,7 +596,6 @@ class CaseExpressionTests(TestCase):
When(o2o_rel__integer=2, then=Value('two')), When(o2o_rel__integer=2, then=Value('two')),
When(o2o_rel__integer=3, then=Value('three')), When(o2o_rel__integer=3, then=Value('three')),
default=Value('other'), default=Value('other'),
output_field=CharField(),
), ),
) )
@ -644,9 +615,9 @@ class CaseExpressionTests(TestCase):
def test_update_binary(self): def test_update_binary(self):
CaseTestModel.objects.update( CaseTestModel.objects.update(
binary=Case( binary=Case(
When(integer=1, then=Value(b'one', output_field=BinaryField())), When(integer=1, then=b'one'),
When(integer=2, then=Value(b'two', output_field=BinaryField())), When(integer=2, then=b'two'),
default=Value(b'', output_field=BinaryField()), default=b'',
), ),
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
@ -725,10 +696,8 @@ class CaseExpressionTests(TestCase):
def test_update_duration(self): def test_update_duration(self):
CaseTestModel.objects.update( CaseTestModel.objects.update(
duration=Case( duration=Case(
# fails on sqlite if output_field is not set explicitly on all When(integer=1, then=timedelta(1)),
# Values containing timedeltas When(integer=2, then=timedelta(2)),
When(integer=1, then=Value(timedelta(1), output_field=DurationField())),
When(integer=2, then=Value(timedelta(2), output_field=DurationField())),
), ),
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
@ -808,7 +777,6 @@ class CaseExpressionTests(TestCase):
def test_update_generic_ip_address(self): def test_update_generic_ip_address(self):
CaseTestModel.objects.update( CaseTestModel.objects.update(
generic_ip_address=Case( generic_ip_address=Case(
# fails on postgresql if output_field is not set explicitly
When(integer=1, then=Value('1.1.1.1')), When(integer=1, then=Value('1.1.1.1')),
When(integer=2, then=Value('2.2.2.2')), When(integer=2, then=Value('2.2.2.2')),
output_field=GenericIPAddressField(), output_field=GenericIPAddressField(),
@ -915,8 +883,8 @@ class CaseExpressionTests(TestCase):
def test_update_string(self): def test_update_string(self):
CaseTestModel.objects.filter(string__in=['1', '2']).update( CaseTestModel.objects.filter(string__in=['1', '2']).update(
string=Case( string=Case(
When(integer=1, then=Value('1', output_field=CharField())), When(integer=1, then=Value('1')),
When(integer=2, then=Value('2', output_field=CharField())), When(integer=2, then=Value('2')),
), ),
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
@ -942,10 +910,8 @@ class CaseExpressionTests(TestCase):
def test_update_time(self): def test_update_time(self):
CaseTestModel.objects.update( CaseTestModel.objects.update(
time=Case( time=Case(
# fails on sqlite if output_field is not set explicitly on all When(integer=1, then=time(1)),
# Values containing times When(integer=2, then=time(2)),
When(integer=1, then=Value(time(1), output_field=TimeField())),
When(integer=2, then=Value(time(2), output_field=TimeField())),
), ),
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
@ -974,16 +940,8 @@ class CaseExpressionTests(TestCase):
def test_update_uuid(self): def test_update_uuid(self):
CaseTestModel.objects.update( CaseTestModel.objects.update(
uuid=Case( uuid=Case(
# fails on sqlite if output_field is not set explicitly on all When(integer=1, then=UUID('11111111111111111111111111111111')),
# Values containing UUIDs When(integer=2, then=UUID('22222222222222222222222222222222')),
When(integer=1, then=Value(
UUID('11111111111111111111111111111111'),
output_field=UUIDField(),
)),
When(integer=2, then=Value(
UUID('22222222222222222222222222222222'),
output_field=UUIDField(),
)),
), ),
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
@ -1022,7 +980,6 @@ class CaseExpressionTests(TestCase):
When(integer__lt=2, then=Value('less than 2')), When(integer__lt=2, then=Value('less than 2')),
When(integer__gt=2, then=Value('greater than 2')), When(integer__gt=2, then=Value('greater than 2')),
default=Value('equal to 2'), default=Value('equal to 2'),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[ [
@ -1038,7 +995,6 @@ class CaseExpressionTests(TestCase):
test=Case( test=Case(
When(integer=2, integer2=3, then=Value('when')), When(integer=2, integer2=3, then=Value('when')),
default=Value('default'), default=Value('default'),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[ [
@ -1054,7 +1010,6 @@ class CaseExpressionTests(TestCase):
test=Case( test=Case(
When(Q(integer=2) | Q(integer2=3), then=Value('when')), When(Q(integer=2) | Q(integer2=3), then=Value('when')),
default=Value('default'), default=Value('default'),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[ [
@ -1070,7 +1025,6 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=2), When(integer=1, then=2),
When(integer=2, then=1), When(integer=2, then=1),
default=3, default=3,
output_field=IntegerField(),
)).order_by('test', 'pk'), )).order_by('test', 'pk'),
[(2, 1), (2, 1), (1, 2)], [(2, 1), (2, 1), (1, 2)],
transform=attrgetter('integer', 'test') transform=attrgetter('integer', 'test')
@ -1082,7 +1036,6 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=2), When(integer=1, then=2),
When(integer=2, then=1), When(integer=2, then=1),
default=3, default=3,
output_field=IntegerField(),
)).order_by(F('test').asc(), 'pk'), )).order_by(F('test').asc(), 'pk'),
[(2, 1), (2, 1), (1, 2)], [(2, 1), (2, 1), (1, 2)],
transform=attrgetter('integer', 'test') transform=attrgetter('integer', 'test')
@ -1101,7 +1054,6 @@ class CaseExpressionTests(TestCase):
foo=Case( foo=Case(
When(fk_rel__pk=1, then=2), When(fk_rel__pk=1, then=2),
default=3, default=3,
output_field=IntegerField()
), ),
), ),
[(o, 3)], [(o, 3)],
@ -1113,7 +1065,6 @@ class CaseExpressionTests(TestCase):
foo=Case( foo=Case(
When(fk_rel__isnull=True, then=2), When(fk_rel__isnull=True, then=2),
default=3, default=3,
output_field=IntegerField()
), ),
), ),
[(o, 2)], [(o, 2)],
@ -1133,12 +1084,10 @@ class CaseExpressionTests(TestCase):
foo=Case( foo=Case(
When(fk_rel__pk=1, then=2), When(fk_rel__pk=1, then=2),
default=3, default=3,
output_field=IntegerField()
), ),
bar=Case( bar=Case(
When(fk_rel__pk=1, then=4), When(fk_rel__pk=1, then=4),
default=5, default=5,
output_field=IntegerField()
), ),
), ),
[(o, 3, 5)], [(o, 3, 5)],
@ -1150,12 +1099,10 @@ class CaseExpressionTests(TestCase):
foo=Case( foo=Case(
When(fk_rel__isnull=True, then=2), When(fk_rel__isnull=True, then=2),
default=3, default=3,
output_field=IntegerField()
), ),
bar=Case( bar=Case(
When(fk_rel__isnull=True, then=4), When(fk_rel__isnull=True, then=4),
default=5, default=5,
output_field=IntegerField()
), ),
), ),
[(o, 2, 4)], [(o, 2, 4)],
@ -1167,7 +1114,6 @@ class CaseExpressionTests(TestCase):
qs = CaseTestModel.objects.values_list('id', 'integer').annotate( qs = CaseTestModel.objects.values_list('id', 'integer').annotate(
cnt=Sum( cnt=Sum(
Case(When(~Q(fk_rel__integer=1), then=1), default=2), Case(When(~Q(fk_rel__integer=1), then=1), default=2),
output_field=IntegerField()
), ),
).order_by('integer') ).order_by('integer')
# The first o has 2 as its fk_rel__integer=1, thus it hits the # The first o has 2 as its fk_rel__integer=1, thus it hits the
@ -1189,12 +1135,10 @@ class CaseExpressionTests(TestCase):
qs = CaseTestModel.objects.values_list('id', 'integer').annotate( qs = CaseTestModel.objects.values_list('id', 'integer').annotate(
cnt=Sum( cnt=Sum(
Case(When(~Q(fk_rel__integer=1), then=1), default=2), Case(When(~Q(fk_rel__integer=1), then=1), default=2),
output_field=IntegerField()
), ),
).annotate( ).annotate(
cnt2=Sum( cnt2=Sum(
Case(When(~Q(fk_rel__integer=1), then=1), default=2), Case(When(~Q(fk_rel__integer=1), then=1), default=2),
output_field=IntegerField()
), ),
).order_by('integer') ).order_by('integer')
self.assertEqual(str(qs.query).count(' JOIN '), 1) self.assertEqual(str(qs.query).count(' JOIN '), 1)
@ -1231,7 +1175,6 @@ class CaseDocumentationExamples(TestCase):
When(account_type=Client.GOLD, then=Value('5%')), When(account_type=Client.GOLD, then=Value('5%')),
When(account_type=Client.PLATINUM, then=Value('10%')), When(account_type=Client.PLATINUM, then=Value('10%')),
default=Value('0%'), default=Value('0%'),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')], [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')],
@ -1247,7 +1190,6 @@ class CaseDocumentationExamples(TestCase):
When(registered_on__lte=a_year_ago, then=Value('10%')), When(registered_on__lte=a_year_ago, then=Value('10%')),
When(registered_on__lte=a_month_ago, then=Value('5%')), When(registered_on__lte=a_month_ago, then=Value('5%')),
default=Value('0%'), default=Value('0%'),
output_field=CharField(),
), ),
).order_by('pk'), ).order_by('pk'),
[('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')], [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')],
@ -1299,15 +1241,12 @@ class CaseDocumentationExamples(TestCase):
Client.objects.aggregate( Client.objects.aggregate(
regular=Sum(Case( regular=Sum(Case(
When(account_type=Client.REGULAR, then=1), When(account_type=Client.REGULAR, then=1),
output_field=IntegerField(),
)), )),
gold=Sum(Case( gold=Sum(Case(
When(account_type=Client.GOLD, then=1), When(account_type=Client.GOLD, then=1),
output_field=IntegerField(),
)), )),
platinum=Sum(Case( platinum=Sum(Case(
When(account_type=Client.PLATINUM, then=1), When(account_type=Client.PLATINUM, then=1),
output_field=IntegerField(),
)), )),
), ),
{'regular': 2, 'gold': 1, 'platinum': 3} {'regular': 2, 'gold': 1, 'platinum': 3}
@ -1360,9 +1299,9 @@ class CaseWhenTests(SimpleTestCase):
with self.assertRaisesMessage(TypeError, msg): with self.assertRaisesMessage(TypeError, msg):
When(condition=object()) When(condition=object())
with self.assertRaisesMessage(TypeError, msg): with self.assertRaisesMessage(TypeError, msg):
When(condition=Value(1, output_field=IntegerField())) When(condition=Value(1))
with self.assertRaisesMessage(TypeError, msg): with self.assertRaisesMessage(TypeError, msg):
When(Value(1, output_field=IntegerField()), string='1') When(Value(1), string='1')
with self.assertRaisesMessage(TypeError, msg): with self.assertRaisesMessage(TypeError, msg):
When() When()

View File

@ -170,7 +170,7 @@ class TestRangeContainsLookup(PostgreSQLTestCase):
self.aware_timestamps[1], self.aware_timestamps[1],
(self.timestamps[1], self.timestamps[2]), (self.timestamps[1], self.timestamps[2]),
(self.aware_timestamps[1], self.aware_timestamps[2]), (self.aware_timestamps[1], self.aware_timestamps[2]),
Value(self.dates[0], output_field=DateTimeField()), Value(self.dates[0]),
Func(F('dates'), function='lower', output_field=DateTimeField()), Func(F('dates'), function='lower', output_field=DateTimeField()),
F('timestamps_inner'), F('timestamps_inner'),
) )