#34699: Filtering on annotated TruncSecond expression gives unexpected result.
-------------------------------------+-------------------------------------
     Reporter:  Stefan               |                    Owner:  Wes P.
         Type:                       |                   Status:  assigned
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Wes P.):

 Thanks for responding to the PR. I look forward to the feedback.

 I tried running the test you describe in your comment and I also had
 errors.

 I was wondering about the line:
 > {{{ with (self.settings(USE_TZ=True, TIME_ZONE="UTC"),
 self.subTest(kind=kind, tz=tz)) }}}
 It seems like it would always assume that we are using the UTC timezone
 instead of switching to the value in tz. I modified your test running it
 twice, first in the UTC timezone only but using tzinfo and the second time
 adjusting the `TIME_ZONE` value in the settings without using tzinfo.

 I still encountered errors and I realized that the original documentation
 does have two errors for the Australia timezone:
  * "2015-04-01T00:00:00+10:00" should be "2015-04-01T00:00:00+11:00",
 daylight savings time starts after April 1 at 3 am (DST is the first
 Sunday in April)
  * "“week”: 2015-06-16 00:00:00+10:00" should be "“week”: 2015-06-15
 00:00:00+10:00" since the 15th is the start of the week
 I made the above changes and all the tests pass.

 {{{#!python
 class Ticket34699Tests(TestCase):
     def test_docs_example(self):
         self.assertSequenceEqual(DTModel.objects.all(), [])

         dt = datetime(2015, 6, 15, 14, 30, 50, 321,
 zoneinfo.ZoneInfo("UTC"))
         # From the docs: Given the datetime 2015-06-15
 14:30:50.000321+00:00...
         docs_dt = "2015-06-15T14:30:50.000321+00:00"
         self.assertEqual(dt.isoformat(), docs_dt)

         utc = {
             "year": "2015-01-01T00:00:00+00:00",
             "quarter": "2015-04-01T00:00:00+00:00",
             "month": "2015-06-01T00:00:00+00:00",
             "week": "2015-06-15T00:00:00+00:00",
             "day": "2015-06-15T00:00:00+00:00",
             "hour": "2015-06-15T14:00:00+00:00",
             "minute": "2015-06-15T14:30:00+00:00",
             "second": "2015-06-15T14:30:50+00:00",
         }
         melbourne = {
             "year": "2015-01-01T00:00:00+11:00",
             "quarter": "2015-04-01T00:00:00+11:00",
             "month": "2015-06-01T00:00:00+10:00",
             "week": "2015-06-15T00:00:00+10:00",
             "day": "2015-06-16T00:00:00+10:00",
             "hour": "2015-06-16T00:00:00+10:00",
             "minute": "2015-06-16T00:30:00+10:00",
             "second": "2015-06-16T00:30:50+10:00",
         }
         for tz, cases in [("UTC", utc), ("Australia/Melbourne",
 melbourne)]:
             for kind, expected in cases.items():
                 with (
                     self.settings(USE_TZ=True, TIME_ZONE="UTC"),
                     self.subTest(kind=kind, tz=tz, with_tzinfo=True)
                 ):
                     test_zone = zoneinfo.ZoneInfo(tz)
                     instance = DTModel.objects.create(start_datetime=dt)
                     self.assertEqual(instance.start_datetime.isoformat(),
 docs_dt)

                     result = DTModel.objects.annotate(
                         truncated=Trunc(
                             "start_datetime",
                             kind,
                             output_field=DateTimeField(),
                             tzinfo=test_zone,
                         )
                     ).get()
                     self.assertEqual(result.truncated.isoformat(),
 expected)

                 DTModel.objects.all().delete()
         for tz, cases in [("UTC", utc), ("Australia/Melbourne",
 melbourne)]:
             for kind, expected in cases.items():
                 with (self.settings(USE_TZ=True, TIME_ZONE=tz),
                       self.subTest(kind=kind, tz=tz, with_tzinfo=False)):
                     instance = DTModel.objects.create(start_datetime=dt)
                     self.assertEqual(instance.start_datetime.isoformat(),
 docs_dt)

                     result = DTModel.objects.annotate(
                         truncated=Trunc(
                             "start_datetime",
                             kind,
                             output_field=DateTimeField()
                         )
                     ).get()
                     self.assertEqual(result.truncated.isoformat(),
 expected)

                 DTModel.objects.all().delete()
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/34699#comment:19>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070196173834fb-8e69fd87-529e-4865-91bc-355e090efc70-000000%40eu-central-1.amazonses.com.

Reply via email to