diff options
| author | Raphael Michel <mail@raphaelmichel.de> | 2018-08-07 18:08:39 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-08-07 16:47:54 -0400 |
| commit | 155b31d4ec138664d62665eb2d8a442469045b78 (patch) | |
| tree | 46cce56f1cccee3ca2bdb0c7555a286d5cdf3162 /tests/db_functions | |
| parent | 53e85705221dcc5302393aac40f4fa606e2097d3 (diff) | |
Fixed #29648 -- Fixed crash when using subqueries inside datetime truncation functions.
Diffstat (limited to 'tests/db_functions')
| -rw-r--r-- | tests/db_functions/models.py | 1 | ||||
| -rw-r--r-- | tests/db_functions/test_datetime.py | 36 |
2 files changed, 35 insertions, 2 deletions
diff --git a/tests/db_functions/models.py b/tests/db_functions/models.py index c31de39b85..083655e80f 100644 --- a/tests/db_functions/models.py +++ b/tests/db_functions/models.py @@ -32,6 +32,7 @@ class Fan(models.Model): name = models.CharField(max_length=50) age = models.PositiveSmallIntegerField(default=30) author = models.ForeignKey(Author, models.CASCADE, related_name='fans') + fan_since = models.DateTimeField(null=True, blank=True) def __str__(self): return self.name diff --git a/tests/db_functions/test_datetime.py b/tests/db_functions/test_datetime.py index dc4c911ab9..07b6b0438a 100644 --- a/tests/db_functions/test_datetime.py +++ b/tests/db_functions/test_datetime.py @@ -3,7 +3,9 @@ from datetime import datetime, timedelta import pytz from django.conf import settings -from django.db.models import DateField, DateTimeField, IntegerField, TimeField +from django.db.models import ( + DateField, DateTimeField, IntegerField, Max, OuterRef, Subquery, TimeField, +) from django.db.models.functions import ( Extract, ExtractDay, ExtractHour, ExtractMinute, ExtractMonth, ExtractQuarter, ExtractSecond, ExtractWeek, ExtractWeekDay, ExtractYear, @@ -15,7 +17,7 @@ from django.test import ( ) from django.utils import timezone -from .models import DTModel +from .models import Author, DTModel, Fan def truncate_to(value, kind, tzinfo=None): @@ -854,6 +856,36 @@ class DateFunctionTests(TestCase): with self.assertRaisesMessage(ValueError, "Cannot truncate DateField 'start_date' to DateTimeField"): list(DTModel.objects.annotate(truncated=TruncSecond('start_date', output_field=DateField()))) + def test_trunc_subquery_with_parameters(self): + author_1 = Author.objects.create(name='J. R. R. Tolkien') + author_2 = Author.objects.create(name='G. R. R. Martin') + fan_since_1 = datetime(2016, 2, 3, 15, 0, 0) + fan_since_2 = datetime(2015, 2, 3, 15, 0, 0) + fan_since_3 = datetime(2017, 2, 3, 15, 0, 0) + if settings.USE_TZ: + fan_since_1 = timezone.make_aware(fan_since_1, is_dst=False) + fan_since_2 = timezone.make_aware(fan_since_2, is_dst=False) + fan_since_3 = timezone.make_aware(fan_since_3, is_dst=False) + Fan.objects.create(author=author_1, name='Tom', fan_since=fan_since_1) + Fan.objects.create(author=author_1, name='Emma', fan_since=fan_since_2) + Fan.objects.create(author=author_2, name='Isabella', fan_since=fan_since_3) + + inner = Fan.objects.filter( + author=OuterRef('pk'), + name__in=('Emma', 'Isabella', 'Tom') + ).values('author').annotate(newest_fan=Max('fan_since')).values('newest_fan') + outer = Author.objects.annotate( + newest_fan_year=TruncYear(Subquery(inner, output_field=DateTimeField())) + ) + tz = pytz.UTC if settings.USE_TZ else None + self.assertSequenceEqual( + outer.order_by('name').values('name', 'newest_fan_year'), + [ + {'name': 'G. R. R. Martin', 'newest_fan_year': datetime(2017, 1, 1, 0, 0, tzinfo=tz)}, + {'name': 'J. R. R. Tolkien', 'newest_fan_year': datetime(2016, 1, 1, 0, 0, tzinfo=tz)}, + ] + ) + @override_settings(USE_TZ=True, TIME_ZONE='UTC') class DateFunctionWithTimeZoneTests(DateFunctionTests): |
