diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2017-02-07 18:46:18 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-02-07 12:46:18 -0500 |
| commit | 6b048b364ca1e0e56a0d3815bf2be33ac9998355 (patch) | |
| tree | 3c1f9c766c2a3161ec1dccf988684ed7b25c4808 /tests/postgres_tests | |
| parent | 0034e9af18f3d393a6dd2389ffbba4c919b1d7d7 (diff) | |
Fixed #26903 -- Fixed __contains lookup for Date/DateTimeRangeField.
Thanks Mariusz Felisiak and Tim Graham for polishing the patch.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_ranges.py | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py index 3dd60dea0d..d87ad36438 100644 --- a/tests/postgres_tests/test_ranges.py +++ b/tests/postgres_tests/test_ranges.py @@ -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 |
