diff options
author | David-Wobrock <david.wobrock@gmail.com> | 2020-10-04 19:28:21 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-10-14 20:06:26 +0200 |
commit | ee005328c8eec5c013c6bf9d6fbb2ae9d540df14 (patch) | |
tree | 5d9641ee00639c05b815cbc26061744e039552d5 /tests/db_functions | |
parent | 8d018231ac64c044c9740b79c0acf6d0abd7c513 (diff) | |
download | django-ee005328c8eec5c013c6bf9d6fbb2ae9d540df14.tar.gz |
Fixed #31640 -- Made Trunc() truncate datetimes to Date/TimeField in a specific timezone.
Diffstat (limited to 'tests/db_functions')
-rw-r--r-- | tests/db_functions/datetime/test_extract_trunc.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/db_functions/datetime/test_extract_trunc.py b/tests/db_functions/datetime/test_extract_trunc.py index 58ca9b52b5..035900da93 100644 --- a/tests/db_functions/datetime/test_extract_trunc.py +++ b/tests/db_functions/datetime/test_extract_trunc.py @@ -672,6 +672,18 @@ class DateFunctionTests(TestCase): lambda m: (m.start_datetime, m.truncated) ) + def test_datetime_to_time_kind(kind): + self.assertQuerysetEqual( + DTModel.objects.annotate( + truncated=Trunc('start_datetime', kind, output_field=TimeField()), + ).order_by('start_datetime'), + [ + (start_datetime, truncate_to(start_datetime.time(), kind)), + (end_datetime, truncate_to(end_datetime.time(), kind)), + ], + lambda m: (m.start_datetime, m.truncated), + ) + test_date_kind('year') test_date_kind('quarter') test_date_kind('month') @@ -688,6 +700,9 @@ class DateFunctionTests(TestCase): test_datetime_kind('hour') test_datetime_kind('minute') test_datetime_kind('second') + test_datetime_to_time_kind('hour') + test_datetime_to_time_kind('minute') + test_datetime_to_time_kind('second') qs = DTModel.objects.filter(start_datetime__date=Trunc('start_datetime', 'day', output_field=DateField())) self.assertEqual(qs.count(), 2) @@ -1205,6 +1220,60 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests): lambda m: (m.start_datetime, m.truncated) ) + def test_datetime_to_date_kind(kind): + self.assertQuerysetEqual( + DTModel.objects.annotate( + truncated=Trunc( + 'start_datetime', + kind, + output_field=DateField(), + tzinfo=melb, + ), + ).order_by('start_datetime'), + [ + ( + start_datetime, + truncate_to(start_datetime.astimezone(melb).date(), kind), + ), + ( + end_datetime, + truncate_to(end_datetime.astimezone(melb).date(), kind), + ), + ], + lambda m: (m.start_datetime, m.truncated), + ) + + def test_datetime_to_time_kind(kind): + self.assertQuerysetEqual( + DTModel.objects.annotate( + truncated=Trunc( + 'start_datetime', + kind, + output_field=TimeField(), + tzinfo=melb, + ) + ).order_by('start_datetime'), + [ + ( + start_datetime, + truncate_to(start_datetime.astimezone(melb).time(), kind), + ), + ( + end_datetime, + truncate_to(end_datetime.astimezone(melb).time(), kind), + ), + ], + lambda m: (m.start_datetime, m.truncated), + ) + + test_datetime_to_date_kind('year') + test_datetime_to_date_kind('quarter') + test_datetime_to_date_kind('month') + test_datetime_to_date_kind('week') + test_datetime_to_date_kind('day') + test_datetime_to_time_kind('hour') + test_datetime_to_time_kind('minute') + test_datetime_to_time_kind('second') test_datetime_kind('year') test_datetime_kind('quarter') test_datetime_kind('month') @@ -1216,3 +1285,15 @@ class DateFunctionWithTimeZoneTests(DateFunctionTests): qs = DTModel.objects.filter(start_datetime__date=Trunc('start_datetime', 'day', output_field=DateField())) self.assertEqual(qs.count(), 2) + + def test_trunc_invalid_field_with_timezone(self): + melb = pytz.timezone('Australia/Melbourne') + msg = 'tzinfo can only be used with DateTimeField.' + with self.assertRaisesMessage(ValueError, msg): + DTModel.objects.annotate( + day_melb=Trunc('start_date', 'day', tzinfo=melb), + ).get() + with self.assertRaisesMessage(ValueError, msg): + DTModel.objects.annotate( + hour_melb=Trunc('start_time', 'hour', tzinfo=melb), + ).get() |