Fixed #32699 -- Fixed comparing to TruncTime() with 0 microseconds on MySQL.

This commit is contained in:
Alex Hill 2021-04-30 22:38:42 +08:00 committed by Mariusz Felisiak
parent c240ceea7d
commit c4ee3b208a
3 changed files with 21 additions and 1 deletions

View File

@ -262,7 +262,7 @@ class DatabaseOperations(BaseDatabaseOperations):
if timezone.is_aware(value): if timezone.is_aware(value):
raise ValueError("MySQL backend does not support timezone-aware times.") raise ValueError("MySQL backend does not support timezone-aware times.")
return str(value) return value.isoformat(timespec='microseconds')
def max_name_length(self): def max_name_length(self):
return 64 return 64

View File

@ -87,6 +87,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
'Raises ORA-00600: internal error code.': { 'Raises ORA-00600: internal error code.': {
'model_fields.test_jsonfield.TestQuerying.test_usage_in_subquery', 'model_fields.test_jsonfield.TestQuerying.test_usage_in_subquery',
}, },
"Comparing to TruncTime() doesn't work on Oracle (#32722).": {
'db_functions.datetime.test_extract_trunc.DateFunctionWithTimeZoneTests.test_trunc_time_no_microseconds',
'db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_time_no_microseconds',
},
} }
django_test_expected_failures = { django_test_expected_failures = {
# A bug in Django/cx_Oracle with respect to string handling (#23843). # A bug in Django/cx_Oracle with respect to string handling (#23843).

View File

@ -923,6 +923,22 @@ class DateFunctionTests(TestCase):
self.create_model(None, None) self.create_model(None, None)
self.assertIsNone(DTModel.objects.annotate(truncated=TruncTime('start_datetime')).first().truncated) self.assertIsNone(DTModel.objects.annotate(truncated=TruncTime('start_datetime')).first().truncated)
def test_trunc_time_no_microseconds(self):
start_datetime = datetime(2015, 6, 15, 14, 30, 26)
if settings.USE_TZ:
start_datetime = timezone.make_aware(start_datetime, is_dst=False)
self.create_model(start_datetime, None)
self.assertIs(
DTModel.objects.filter(start_datetime__time=start_datetime.time()).exists(),
True,
)
self.assertIs(
DTModel.objects.annotate(
extracted=TruncTime('start_datetime'),
).filter(extracted=start_datetime.time()).exists(),
True,
)
def test_trunc_day_func(self): def test_trunc_day_func(self):
start_datetime = datetime(2015, 6, 15, 14, 30, 50, 321) start_datetime = datetime(2015, 6, 15, 14, 30, 50, 321)
end_datetime = truncate_to(datetime(2016, 6, 15, 14, 10, 50, 123), 'day') end_datetime = truncate_to(datetime(2016, 6, 15, 14, 10, 50, 123), 'day')