summaryrefslogtreecommitdiff
path: root/tests/expressions_case
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2019-08-20 08:54:41 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-02-04 13:20:06 +0100
commit335c9c94acf263901fb023404408880245b0c4b4 (patch)
tree691e9683de6c9840cd0a9a097d020c499ea735db /tests/expressions_case
parent469bf2db15597f2c87cb0f8f64132056d2467f15 (diff)
downloaddjango-335c9c94acf263901fb023404408880245b0c4b4.tar.gz
Simplified imports from django.db and django.contrib.gis.db.
Diffstat (limited to 'tests/expressions_case')
-rw-r--r--tests/expressions_case/tests.py174
1 files changed, 88 insertions, 86 deletions
diff --git a/tests/expressions_case/tests.py b/tests/expressions_case/tests.py
index d1a58f5d88..f85def932a 100644
--- a/tests/expressions_case/tests.py
+++ b/tests/expressions_case/tests.py
@@ -5,9 +5,11 @@ from operator import attrgetter, itemgetter
from uuid import UUID
from django.core.exceptions import FieldError
-from django.db import models
-from django.db.models import F, Max, Min, Q, Sum, Value
-from django.db.models.expressions import Case, When
+from django.db.models import (
+ BinaryField, Case, CharField, Count, DurationField, F,
+ GenericIPAddressField, IntegerField, Max, Min, Q, Sum, TextField,
+ TimeField, UUIDField, Value, When,
+)
from django.test import SimpleTestCase, TestCase
from .models import CaseTestModel, Client, FKCaseTestModel, O2OCaseTestModel
@@ -57,7 +59,7 @@ class CaseExpressionTests(TestCase):
# GROUP BY on Oracle fails with TextField/BinaryField; see #24096.
cls.non_lob_fields = [
f.name for f in CaseTestModel._meta.get_fields()
- if not (f.is_relation and f.auto_created) and not isinstance(f, (models.BinaryField, models.TextField))
+ if not (f.is_relation and f.auto_created) and not isinstance(f, (BinaryField, TextField))
]
def test_annotate(self):
@@ -66,7 +68,7 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=Value('one')),
When(integer=2, then=Value('two')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
)).order_by('pk'),
[(1, 'one'), (2, 'two'), (3, 'other'), (2, 'two'), (3, 'other'), (3, 'other'), (4, 'other')],
transform=attrgetter('integer', 'test')
@@ -77,7 +79,7 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.annotate(test=Case(
When(integer=1, then=1),
When(integer=2, then=2),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by('pk'),
[(1, 1), (2, 2), (3, None), (2, 2), (3, None), (3, None), (4, None)],
transform=attrgetter('integer', 'test')
@@ -99,7 +101,7 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.annotate(f_test=Case(
When(integer2=F('integer'), then=Value('equal')),
When(integer2=F('integer') + 1, then=Value('+1')),
- output_field=models.CharField(),
+ output_field=CharField(),
)).order_by('pk'),
[(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')],
transform=attrgetter('integer', 'f_test')
@@ -133,7 +135,7 @@ class CaseExpressionTests(TestCase):
When(integer2=F('o2o_rel__integer'), then=Value('equal')),
When(integer2=F('o2o_rel__integer') + 1, then=Value('+1')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
)).order_by('pk'),
[(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, 'other')],
transform=attrgetter('integer', 'join_test')
@@ -146,7 +148,7 @@ class CaseExpressionTests(TestCase):
When(o2o_rel__integer=2, then=Value('two')),
When(o2o_rel__integer=3, then=Value('three')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
)).order_by('pk'),
[(1, 'one'), (2, 'two'), (3, 'three'), (2, 'two'), (3, 'three'), (3, 'three'), (4, 'one')],
transform=attrgetter('integer', 'join_test')
@@ -176,7 +178,7 @@ class CaseExpressionTests(TestCase):
f_test=Case(
When(integer2=F('integer'), then=Value('equal')),
When(integer2=F('f_plus_1'), then=Value('+1')),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')],
@@ -193,7 +195,7 @@ class CaseExpressionTests(TestCase):
When(f_minus_2=0, then=Value('zero')),
When(f_minus_2=1, then=Value('one')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[(1, 'negative one'), (2, 'zero'), (3, 'one'), (2, 'zero'), (3, 'one'), (3, 'one'), (4, 'other')],
@@ -224,7 +226,7 @@ class CaseExpressionTests(TestCase):
test=Case(
When(integer2=F('min'), then=Value('min')),
When(integer2=F('max'), then=Value('max')),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[(1, 1, 'min'), (2, 3, 'max'), (3, 4, 'max'), (2, 2, 'min'), (3, 4, 'max'), (3, 3, 'min'), (4, 5, 'min')],
@@ -240,7 +242,7 @@ class CaseExpressionTests(TestCase):
When(max=3, then=Value('max = 3')),
When(max=4, then=Value('max = 4')),
default=Value(''),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[(1, 1, ''), (2, 3, 'max = 3'), (3, 4, 'max = 4'), (2, 3, 'max = 3'),
@@ -254,7 +256,7 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=Value('one')),
When(integer=2, then=Value('two')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
)).exclude(test='other').order_by('pk'),
[(1, 'one'), (2, 'two'), (2, 'two')],
transform=attrgetter('integer', 'test')
@@ -267,7 +269,7 @@ class CaseExpressionTests(TestCase):
When(integer=2, then=Value('two')),
When(integer=3, then=Value('three')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
)).order_by('test').values_list('integer', flat=True)),
[1, 4, 3, 3, 3, 2, 2]
)
@@ -276,7 +278,7 @@ class CaseExpressionTests(TestCase):
objects = CaseTestModel.objects.annotate(
selected=Case(
When(pk__in=[], then=Value('selected')),
- default=Value('not selected'), output_field=models.CharField()
+ default=Value('not selected'), output_field=CharField()
)
)
self.assertEqual(len(objects), CaseTestModel.objects.count())
@@ -289,7 +291,7 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=2),
When(integer=2, then=1),
default=3,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
) + 1,
).order_by('pk'),
[(1, 3), (2, 2), (3, 4), (2, 2), (3, 4), (3, 4), (4, 4)],
@@ -303,7 +305,7 @@ class CaseExpressionTests(TestCase):
test=Case(
When(integer=F('integer2'), then='pk'),
When(integer=4, then='pk'),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
),
).values('test')).order_by('pk'),
[(1, 1), (2, 2), (3, 3), (4, 5)],
@@ -314,7 +316,7 @@ class CaseExpressionTests(TestCase):
SOME_CASE = Case(
When(pk=0, then=Value('0')),
default=Value('1'),
- output_field=models.CharField(),
+ output_field=CharField(),
)
self.assertQuerysetEqual(
CaseTestModel.objects.annotate(somecase=SOME_CASE).order_by('pk'),
@@ -325,21 +327,21 @@ class CaseExpressionTests(TestCase):
def test_aggregate(self):
self.assertEqual(
CaseTestModel.objects.aggregate(
- one=models.Sum(Case(
+ one=Sum(Case(
When(integer=1, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
- two=models.Sum(Case(
+ two=Sum(Case(
When(integer=2, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
- three=models.Sum(Case(
+ three=Sum(Case(
When(integer=3, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
- four=models.Sum(Case(
+ four=Sum(Case(
When(integer=4, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
),
{'one': 1, 'two': 2, 'three': 3, 'four': 1}
@@ -348,9 +350,9 @@ class CaseExpressionTests(TestCase):
def test_aggregate_with_expression_as_value(self):
self.assertEqual(
CaseTestModel.objects.aggregate(
- one=models.Sum(Case(When(integer=1, then='integer'))),
- two=models.Sum(Case(When(integer=2, then=F('integer') - 1))),
- three=models.Sum(Case(When(integer=3, then=F('integer') + 1))),
+ one=Sum(Case(When(integer=1, then='integer'))),
+ two=Sum(Case(When(integer=2, then=F('integer') - 1))),
+ three=Sum(Case(When(integer=3, then=F('integer') + 1))),
),
{'one': 1, 'two': 2, 'three': 12}
)
@@ -358,13 +360,13 @@ class CaseExpressionTests(TestCase):
def test_aggregate_with_expression_as_condition(self):
self.assertEqual(
CaseTestModel.objects.aggregate(
- equal=models.Sum(Case(
+ equal=Sum(Case(
When(integer2=F('integer'), then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
- plus_one=models.Sum(Case(
+ plus_one=Sum(Case(
When(integer2=F('integer') + 1, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
),
{'equal': 3, 'plus_one': 4}
@@ -376,7 +378,7 @@ class CaseExpressionTests(TestCase):
When(integer=2, then=3),
When(integer=3, then=4),
default=1,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by('pk'),
[(1, 1), (2, 3), (3, 4), (3, 4)],
transform=attrgetter('integer', 'integer2')
@@ -387,7 +389,7 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.filter(integer2=Case(
When(integer=2, then=3),
When(integer=3, then=4),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by('pk'),
[(2, 3), (3, 4), (3, 4)],
transform=attrgetter('integer', 'integer2')
@@ -409,7 +411,7 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.filter(string=Case(
When(integer2=F('integer'), then=Value('2')),
When(integer2=F('integer') + 1, then=Value('3')),
- output_field=models.CharField(),
+ output_field=CharField(),
)).order_by('pk'),
[(3, 4, '3'), (2, 2, '2'), (3, 4, '3')],
transform=attrgetter('integer', 'integer2', 'string')
@@ -431,7 +433,7 @@ class CaseExpressionTests(TestCase):
CaseTestModel.objects.filter(integer=Case(
When(integer2=F('o2o_rel__integer') + 1, then=2),
When(integer2=F('o2o_rel__integer'), then=3),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by('pk'),
[(2, 3), (3, 3)],
transform=attrgetter('integer', 'integer2')
@@ -443,7 +445,7 @@ class CaseExpressionTests(TestCase):
When(o2o_rel__integer=1, then=1),
When(o2o_rel__integer=2, then=3),
When(o2o_rel__integer=3, then=4),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by('pk'),
[(1, 1), (2, 3), (3, 4), (3, 4)],
transform=attrgetter('integer', 'integer2')
@@ -472,7 +474,7 @@ class CaseExpressionTests(TestCase):
integer=Case(
When(integer2=F('integer'), then=2),
When(integer2=F('f_plus_1'), then=3),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
),
).order_by('pk'),
[(3, 4), (2, 2), (3, 4)],
@@ -488,7 +490,7 @@ class CaseExpressionTests(TestCase):
When(f_plus_1=3, then=3),
When(f_plus_1=4, then=4),
default=1,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
),
).order_by('pk'),
[(1, 1), (2, 3), (3, 4), (3, 4)],
@@ -599,7 +601,7 @@ class CaseExpressionTests(TestCase):
integer=Case(
When(integer2=F('o2o_rel__integer') + 1, then=2),
When(integer2=F('o2o_rel__integer'), then=3),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
),
)
@@ -611,7 +613,7 @@ class CaseExpressionTests(TestCase):
When(o2o_rel__integer=2, then=Value('two')),
When(o2o_rel__integer=3, then=Value('three')),
default=Value('other'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
)
@@ -631,9 +633,9 @@ class CaseExpressionTests(TestCase):
def test_update_binary(self):
CaseTestModel.objects.update(
binary=Case(
- When(integer=1, then=Value(b'one', output_field=models.BinaryField())),
- When(integer=2, then=Value(b'two', output_field=models.BinaryField())),
- default=Value(b'', output_field=models.BinaryField()),
+ When(integer=1, then=Value(b'one', output_field=BinaryField())),
+ When(integer=2, then=Value(b'two', output_field=BinaryField())),
+ default=Value(b'', output_field=BinaryField()),
),
)
self.assertQuerysetEqual(
@@ -714,8 +716,8 @@ class CaseExpressionTests(TestCase):
duration=Case(
# fails on sqlite if output_field is not set explicitly on all
# Values containing timedeltas
- When(integer=1, then=Value(timedelta(1), output_field=models.DurationField())),
- When(integer=2, then=Value(timedelta(2), output_field=models.DurationField())),
+ When(integer=1, then=Value(timedelta(1), output_field=DurationField())),
+ When(integer=2, then=Value(timedelta(2), output_field=DurationField())),
),
)
self.assertQuerysetEqual(
@@ -798,7 +800,7 @@ class CaseExpressionTests(TestCase):
# fails on postgresql if output_field is not set explicitly
When(integer=1, then=Value('1.1.1.1')),
When(integer=2, then=Value('2.2.2.2')),
- output_field=models.GenericIPAddressField(),
+ output_field=GenericIPAddressField(),
),
)
self.assertQuerysetEqual(
@@ -902,8 +904,8 @@ class CaseExpressionTests(TestCase):
def test_update_string(self):
CaseTestModel.objects.filter(string__in=['1', '2']).update(
string=Case(
- When(integer=1, then=Value('1', output_field=models.CharField())),
- When(integer=2, then=Value('2', output_field=models.CharField())),
+ When(integer=1, then=Value('1', output_field=CharField())),
+ When(integer=2, then=Value('2', output_field=CharField())),
),
)
self.assertQuerysetEqual(
@@ -931,8 +933,8 @@ class CaseExpressionTests(TestCase):
time=Case(
# fails on sqlite if output_field is not set explicitly on all
# Values containing times
- When(integer=1, then=Value(time(1), output_field=models.TimeField())),
- When(integer=2, then=Value(time(2), output_field=models.TimeField())),
+ When(integer=1, then=Value(time(1), output_field=TimeField())),
+ When(integer=2, then=Value(time(2), output_field=TimeField())),
),
)
self.assertQuerysetEqual(
@@ -965,11 +967,11 @@ class CaseExpressionTests(TestCase):
# Values containing UUIDs
When(integer=1, then=Value(
UUID('11111111111111111111111111111111'),
- output_field=models.UUIDField(),
+ output_field=UUIDField(),
)),
When(integer=2, then=Value(
UUID('22222222222222222222222222222222'),
- output_field=models.UUIDField(),
+ output_field=UUIDField(),
)),
),
)
@@ -1009,7 +1011,7 @@ class CaseExpressionTests(TestCase):
When(integer__lt=2, then=Value('less than 2')),
When(integer__gt=2, then=Value('greater than 2')),
default=Value('equal to 2'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[
@@ -1025,7 +1027,7 @@ class CaseExpressionTests(TestCase):
test=Case(
When(integer=2, integer2=3, then=Value('when')),
default=Value('default'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[
@@ -1041,7 +1043,7 @@ class CaseExpressionTests(TestCase):
test=Case(
When(Q(integer=2) | Q(integer2=3), then=Value('when')),
default=Value('default'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[
@@ -1057,7 +1059,7 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=2),
When(integer=2, then=1),
default=3,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by('test', 'pk'),
[(2, 1), (2, 1), (1, 2)],
transform=attrgetter('integer', 'test')
@@ -1069,7 +1071,7 @@ class CaseExpressionTests(TestCase):
When(integer=1, then=2),
When(integer=2, then=1),
default=3,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)).order_by(F('test').asc(), 'pk'),
[(2, 1), (2, 1), (1, 2)],
transform=attrgetter('integer', 'test')
@@ -1088,7 +1090,7 @@ class CaseExpressionTests(TestCase):
foo=Case(
When(fk_rel__pk=1, then=2),
default=3,
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
),
[(o, 3)],
@@ -1100,7 +1102,7 @@ class CaseExpressionTests(TestCase):
foo=Case(
When(fk_rel__isnull=True, then=2),
default=3,
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
),
[(o, 2)],
@@ -1120,12 +1122,12 @@ class CaseExpressionTests(TestCase):
foo=Case(
When(fk_rel__pk=1, then=2),
default=3,
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
bar=Case(
When(fk_rel__pk=1, then=4),
default=5,
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
),
[(o, 3, 5)],
@@ -1137,12 +1139,12 @@ class CaseExpressionTests(TestCase):
foo=Case(
When(fk_rel__isnull=True, then=2),
default=3,
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
bar=Case(
When(fk_rel__isnull=True, then=4),
default=5,
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
),
[(o, 2, 4)],
@@ -1152,9 +1154,9 @@ class CaseExpressionTests(TestCase):
def test_m2m_exclude(self):
CaseTestModel.objects.create(integer=10, integer2=1, string='1')
qs = CaseTestModel.objects.values_list('id', 'integer').annotate(
- cnt=models.Sum(
+ cnt=Sum(
Case(When(~Q(fk_rel__integer=1), then=1), default=2),
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
).order_by('integer')
# The first o has 2 as its fk_rel__integer=1, thus it hits the
@@ -1174,14 +1176,14 @@ class CaseExpressionTests(TestCase):
# Need to use values before annotate so that Oracle will not group
# by fields it isn't capable of grouping by.
qs = CaseTestModel.objects.values_list('id', 'integer').annotate(
- cnt=models.Sum(
+ cnt=Sum(
Case(When(~Q(fk_rel__integer=1), then=1), default=2),
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
).annotate(
- cnt2=models.Sum(
+ cnt2=Sum(
Case(When(~Q(fk_rel__integer=1), then=1), default=2),
- output_field=models.IntegerField()
+ output_field=IntegerField()
),
).order_by('integer')
self.assertEqual(str(qs.query).count(' JOIN '), 1)
@@ -1218,7 +1220,7 @@ class CaseDocumentationExamples(TestCase):
When(account_type=Client.GOLD, then=Value('5%')),
When(account_type=Client.PLATINUM, then=Value('10%')),
default=Value('0%'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')],
@@ -1234,7 +1236,7 @@ class CaseDocumentationExamples(TestCase):
When(registered_on__lte=a_year_ago, then=Value('10%')),
When(registered_on__lte=a_month_ago, then=Value('5%')),
default=Value('0%'),
- output_field=models.CharField(),
+ output_field=CharField(),
),
).order_by('pk'),
[('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')],
@@ -1275,26 +1277,26 @@ class CaseDocumentationExamples(TestCase):
)
self.assertEqual(
Client.objects.aggregate(
- regular=models.Count('pk', filter=Q(account_type=Client.REGULAR)),
- gold=models.Count('pk', filter=Q(account_type=Client.GOLD)),
- platinum=models.Count('pk', filter=Q(account_type=Client.PLATINUM)),
+ regular=Count('pk', filter=Q(account_type=Client.REGULAR)),
+ gold=Count('pk', filter=Q(account_type=Client.GOLD)),
+ platinum=Count('pk', filter=Q(account_type=Client.PLATINUM)),
),
{'regular': 2, 'gold': 1, 'platinum': 3}
)
# This was the example before the filter argument was added.
self.assertEqual(
Client.objects.aggregate(
- regular=models.Sum(Case(
+ regular=Sum(Case(
When(account_type=Client.REGULAR, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
- gold=models.Sum(Case(
+ gold=Sum(Case(
When(account_type=Client.GOLD, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
- platinum=models.Sum(Case(
+ platinum=Sum(Case(
When(account_type=Client.PLATINUM, then=1),
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)),
),
{'regular': 2, 'gold': 1, 'platinum': 3}
@@ -1318,12 +1320,12 @@ class CaseDocumentationExamples(TestCase):
expression_1 = Case(
When(account_type__in=[Client.REGULAR, Client.GOLD], then=1),
default=2,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)
expression_2 = Case(
When(account_type__in=(Client.REGULAR, Client.GOLD), then=1),
default=2,
- output_field=models.IntegerField(),
+ output_field=IntegerField(),
)
expression_3 = Case(When(account_type__in=[Client.REGULAR, Client.GOLD], then=1), default=2)
expression_4 = Case(When(account_type__in=[Client.PLATINUM, Client.GOLD], then=2), default=1)
@@ -1347,7 +1349,7 @@ class CaseWhenTests(SimpleTestCase):
with self.assertRaisesMessage(TypeError, msg):
When(condition=object())
with self.assertRaisesMessage(TypeError, msg):
- When(condition=Value(1, output_field=models.IntegerField()))
+ When(condition=Value(1, output_field=IntegerField()))
with self.assertRaisesMessage(TypeError, msg):
When()