mirror of
https://github.com/django/django.git
synced 2025-08-02 10:02:41 +00:00
Fixed #26903 -- Fixed __contains lookup for Date/DateTimeRangeField.
Thanks Mariusz Felisiak and Tim Graham for polishing the patch.
This commit is contained in:
parent
0034e9af18
commit
6b048b364c
2 changed files with 107 additions and 1 deletions
|
@ -3,7 +3,7 @@ import json
|
|||
|
||||
from django import forms
|
||||
from django.core import exceptions, serializers
|
||||
from django.db.models import F
|
||||
from django.db.models import DateField, DateTimeField, F, Func, Value
|
||||
from django.test import override_settings
|
||||
from django.utils import timezone
|
||||
|
||||
|
@ -87,6 +87,80 @@ class TestSaveLoad(PostgreSQLTestCase):
|
|||
self.assertEqual(field.base_field.model, RangesModel)
|
||||
|
||||
|
||||
class TestRangeContainsLookup(PostgreSQLTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.timestamps = [
|
||||
datetime.datetime(year=2016, month=1, day=1),
|
||||
datetime.datetime(year=2016, month=1, day=2, hour=1),
|
||||
datetime.datetime(year=2016, month=1, day=2, hour=12),
|
||||
datetime.datetime(year=2016, month=1, day=3),
|
||||
datetime.datetime(year=2016, month=1, day=3, hour=1),
|
||||
datetime.datetime(year=2016, month=2, day=2),
|
||||
]
|
||||
cls.aware_timestamps = [
|
||||
timezone.make_aware(timestamp, timezone.get_current_timezone())
|
||||
for timestamp in cls.timestamps
|
||||
]
|
||||
cls.dates = [
|
||||
datetime.date(year=2016, month=1, day=1),
|
||||
datetime.date(year=2016, month=1, day=2),
|
||||
datetime.date(year=2016, month=1, day=3),
|
||||
datetime.date(year=2016, month=1, day=4),
|
||||
datetime.date(year=2016, month=2, day=2),
|
||||
datetime.date(year=2016, month=2, day=3),
|
||||
]
|
||||
cls.obj = RangesModel.objects.create(
|
||||
dates=(cls.dates[0], cls.dates[3]),
|
||||
timestamps=(cls.timestamps[0], cls.timestamps[3]),
|
||||
)
|
||||
cls.aware_obj = RangesModel.objects.create(
|
||||
dates=(cls.dates[0], cls.dates[3]),
|
||||
timestamps=(cls.aware_timestamps[0], cls.aware_timestamps[3]),
|
||||
)
|
||||
# Objects that don't match any queries.
|
||||
for i in range(3, 4):
|
||||
RangesModel.objects.create(
|
||||
dates=(cls.dates[i], cls.dates[i + 1]),
|
||||
timestamps=(cls.timestamps[i], cls.timestamps[i + 1]),
|
||||
)
|
||||
RangesModel.objects.create(
|
||||
dates=(cls.dates[i], cls.dates[i + 1]),
|
||||
timestamps=(cls.aware_timestamps[i], cls.aware_timestamps[i + 1]),
|
||||
)
|
||||
|
||||
def test_datetime_range_contains(self):
|
||||
filter_args = (
|
||||
self.timestamps[1],
|
||||
self.aware_timestamps[1],
|
||||
(self.timestamps[1], self.timestamps[2]),
|
||||
(self.aware_timestamps[1], self.aware_timestamps[2]),
|
||||
Value(self.dates[0], output_field=DateTimeField()),
|
||||
Func(F('dates'), function='lower', output_field=DateTimeField()),
|
||||
)
|
||||
for filter_arg in filter_args:
|
||||
with self.subTest(filter_arg=filter_arg):
|
||||
self.assertCountEqual(
|
||||
RangesModel.objects.filter(**{'timestamps__contains': filter_arg}),
|
||||
[self.obj, self.aware_obj],
|
||||
)
|
||||
|
||||
def test_date_range_contains(self):
|
||||
filter_args = (
|
||||
self.timestamps[1],
|
||||
(self.dates[1], self.dates[2]),
|
||||
Value(self.dates[0], output_field=DateField()),
|
||||
Func(F('timestamps'), function='lower', output_field=DateField()),
|
||||
)
|
||||
for filter_arg in filter_args:
|
||||
with self.subTest(filter_arg=filter_arg):
|
||||
self.assertCountEqual(
|
||||
RangesModel.objects.filter(**{'dates__contains': filter_arg}),
|
||||
[self.obj, self.aware_obj],
|
||||
)
|
||||
|
||||
|
||||
class TestQuerying(PostgreSQLTestCase):
|
||||
|
||||
@classmethod
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue