Skip to content

Fix USE_TZ RemovedInDjango50Warning #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
}
]

USE_TZ = True

WSGI_APPLICATION = "example.wsgi.application"


Expand Down
39 changes: 27 additions & 12 deletions tests/panels/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,41 @@ def test_param_conversion(self):
.filter(group_count__lt=10)
.filter(group_count__gt=1)
)
list(User.objects.filter(date_joined=datetime.datetime(2017, 12, 22, 16, 7, 1)))
list(
User.objects.filter(
date_joined=datetime.datetime(
2017, 12, 22, 16, 7, 1, tzinfo=datetime.timezone.utc
)
)
)
Comment on lines -124 to +130
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change from naive datetime, which django will interpret with the default timezone, to aware


response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)

# ensure query was logged
self.assertEqual(len(self.panel._queries), 3)

# Django 4.1 started passing true/false back for boolean
# comparisons in MySQL.
if not (django.VERSION >= (4, 1) and connection.vendor == "mysql"):
self.assertEqual(
tuple(q[1]["params"] for q in self.panel._queries),
('["Foo"]', "[10, 1]", '["2017-12-22 16:07:01"]'),
)
if connection.vendor == "mysql" and django.VERSION >= (4, 1):
# Django 4.1 started passing true/false back for boolean
# comparisons in MySQL.
expected_bools = '["Foo", true, false]'
else:
self.assertEqual(
tuple(q[1]["params"] for q in self.panel._queries),
('["Foo", true, false]', "[10, 1]", '["2017-12-22 16:07:01"]'),
)
expected_bools = '["Foo"]'

if connection.vendor == "postgresql":
# PostgreSQL always includes timezone
expected_datetime = '["2017-12-22 16:07:01+00:00"]'
else:
expected_datetime = '["2017-12-22 16:07:01"]'
Comment on lines +145 to +149
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new formatting difference being triggered on PostgreSQL


self.assertEqual(
tuple(q[1]["params"] for q in self.panel._queries),
(
expected_bools,
"[10, 1]",
expected_datetime,
),
)

@unittest.skipUnless(
connection.vendor == "postgresql", "Test valid only on PostgreSQL"
Expand Down
2 changes: 2 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
},
]

USE_TZ = True

STATIC_ROOT = os.path.join(BASE_DIR, "tests", "static")

STATIC_URL = "/static/"
Expand Down
10 changes: 5 additions & 5 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from datetime import datetime
from datetime import datetime, timezone

from django import forms
from django.test import TestCase

from debug_toolbar.forms import SignedDataForm

SIGNATURE = "v02QBcJplEET6QXHNWejnRcmSENWlw6_RjxLTR7QG9g"
SIGNATURE = "-WiogJKyy4E8Om00CrFSy0T6XHObwBa6Zb46u-vmeYE"

DATA = {"value": "foo", "date": datetime(2020, 1, 1)}
SIGNED_DATA = f'{{"date": "2020-01-01 00:00:00", "value": "foo"}}:{SIGNATURE}'
DATA = {"value": "foo", "date": datetime(2020, 1, 1, tzinfo=timezone.utc)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this changes the naive datetime to aware, so that it isn't interpreted with the default timezone when converting to str

SIGNED_DATA = f'{{"date": "2020-01-01 00:00:00+00:00", "value": "foo"}}:{SIGNATURE}'


class FooForm(forms.Form):
Expand All @@ -32,7 +32,7 @@ def test_verified_data(self):
form.verified_data(),
{
"value": "foo",
"date": "2020-01-01 00:00:00",
"date": "2020-01-01 00:00:00+00:00",
},
)
# Take it back to the foo form to validate the datetime is serialized
Expand Down