2015-01-11 00:14:20 +08:00
|
|
|
import datetime
|
|
|
|
import json
|
2018-10-03 07:17:23 +08:00
|
|
|
from decimal import Decimal
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.core import exceptions, serializers
|
2017-02-08 01:46:18 +08:00
|
|
|
from django.db.models import DateField, DateTimeField, F, Func, Value
|
2019-09-05 22:10:21 +08:00
|
|
|
from django.test import override_settings
|
2015-01-11 00:14:20 +08:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
|
2015-05-21 19:25:50 +08:00
|
|
|
from .models import RangeLookupsModel, RangesModel
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-04-05 00:10:26 +08:00
|
|
|
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
|
|
|
|
from django.contrib.postgres import fields as pg_fields, forms as pg_forms
|
|
|
|
from django.contrib.postgres.validators import (
|
|
|
|
RangeMaxValueValidator, RangeMinValueValidator,
|
|
|
|
)
|
2017-09-07 20:16:21 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2015-04-05 00:10:26 +08:00
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2016-02-12 22:59:13 +08:00
|
|
|
class TestSaveLoad(PostgreSQLTestCase):
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_all_fields(self):
|
|
|
|
now = timezone.now()
|
|
|
|
instance = RangesModel(
|
|
|
|
ints=NumericRange(0, 10),
|
|
|
|
bigints=NumericRange(10, 20),
|
2018-10-03 07:17:23 +08:00
|
|
|
decimals=NumericRange(20, 30),
|
2015-01-11 00:14:20 +08:00
|
|
|
timestamps=DateTimeTZRange(now - datetime.timedelta(hours=1), now),
|
|
|
|
dates=DateRange(now.date() - datetime.timedelta(days=1), now.date()),
|
|
|
|
)
|
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
|
|
|
self.assertEqual(instance.ints, loaded.ints)
|
|
|
|
self.assertEqual(instance.bigints, loaded.bigints)
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertEqual(instance.decimals, loaded.decimals)
|
2015-01-11 00:14:20 +08:00
|
|
|
self.assertEqual(instance.timestamps, loaded.timestamps)
|
|
|
|
self.assertEqual(instance.dates, loaded.dates)
|
|
|
|
|
|
|
|
def test_range_object(self):
|
|
|
|
r = NumericRange(0, 10)
|
|
|
|
instance = RangesModel(ints=r)
|
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
|
|
|
self.assertEqual(r, loaded.ints)
|
|
|
|
|
|
|
|
def test_tuple(self):
|
|
|
|
instance = RangesModel(ints=(0, 10))
|
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
|
|
|
self.assertEqual(NumericRange(0, 10), loaded.ints)
|
|
|
|
|
|
|
|
def test_range_object_boundaries(self):
|
|
|
|
r = NumericRange(0, 10, '[]')
|
2018-10-03 07:17:23 +08:00
|
|
|
instance = RangesModel(decimals=r)
|
2015-01-11 00:14:20 +08:00
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertEqual(r, loaded.decimals)
|
|
|
|
self.assertIn(10, loaded.decimals)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_unbounded(self):
|
|
|
|
r = NumericRange(None, None, '()')
|
2018-10-03 07:17:23 +08:00
|
|
|
instance = RangesModel(decimals=r)
|
2015-01-11 00:14:20 +08:00
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertEqual(r, loaded.decimals)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
r = NumericRange(empty=True)
|
|
|
|
instance = RangesModel(ints=r)
|
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
|
|
|
self.assertEqual(r, loaded.ints)
|
|
|
|
|
|
|
|
def test_null(self):
|
|
|
|
instance = RangesModel(ints=None)
|
|
|
|
instance.save()
|
|
|
|
loaded = RangesModel.objects.get()
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertIsNone(loaded.ints)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2016-04-03 18:39:18 +08:00
|
|
|
def test_model_set_on_base_field(self):
|
|
|
|
instance = RangesModel()
|
|
|
|
field = instance._meta.get_field('ints')
|
|
|
|
self.assertEqual(field.model, RangesModel)
|
|
|
|
self.assertEqual(field.base_field.model, RangesModel)
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2017-02-08 01:46:18 +08:00
|
|
|
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 = [
|
2018-01-19 00:21:12 +08:00
|
|
|
timezone.make_aware(timestamp)
|
2017-02-08 01:46:18 +08:00
|
|
|
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]),
|
2019-07-10 16:33:36 +08:00
|
|
|
dates_inner=(cls.dates[1], cls.dates[2]),
|
2017-02-08 01:46:18 +08:00
|
|
|
timestamps=(cls.timestamps[0], cls.timestamps[3]),
|
2019-07-10 16:33:36 +08:00
|
|
|
timestamps_inner=(cls.timestamps[1], cls.timestamps[2]),
|
2017-02-08 01:46:18 +08:00
|
|
|
)
|
|
|
|
cls.aware_obj = RangesModel.objects.create(
|
|
|
|
dates=(cls.dates[0], cls.dates[3]),
|
2019-07-10 16:33:36 +08:00
|
|
|
dates_inner=(cls.dates[1], cls.dates[2]),
|
2017-02-08 01:46:18 +08:00
|
|
|
timestamps=(cls.aware_timestamps[0], cls.aware_timestamps[3]),
|
2019-07-10 16:33:36 +08:00
|
|
|
timestamps_inner=(cls.timestamps[1], cls.timestamps[2]),
|
2017-02-08 01:46:18 +08:00
|
|
|
)
|
|
|
|
# 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()),
|
2019-07-10 16:33:36 +08:00
|
|
|
F('timestamps_inner'),
|
2017-02-08 01:46:18 +08:00
|
|
|
)
|
|
|
|
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()),
|
2019-07-10 16:33:36 +08:00
|
|
|
F('dates_inner'),
|
2017-02-08 01:46:18 +08:00
|
|
|
)
|
|
|
|
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],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-02-12 22:59:13 +08:00
|
|
|
class TestQuerying(PostgreSQLTestCase):
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2019-03-22 06:05:29 +08:00
|
|
|
cls.objs = RangesModel.objects.bulk_create([
|
|
|
|
RangesModel(ints=NumericRange(0, 10)),
|
|
|
|
RangesModel(ints=NumericRange(5, 15)),
|
|
|
|
RangesModel(ints=NumericRange(None, 0)),
|
|
|
|
RangesModel(ints=NumericRange(empty=True)),
|
|
|
|
RangesModel(ints=None),
|
|
|
|
])
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_exact(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__exact=NumericRange(0, 10)),
|
|
|
|
[self.objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_isnull(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__isnull=True),
|
|
|
|
[self.objs[4]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_isempty(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__isempty=True),
|
|
|
|
[self.objs[3]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_contains(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__contains=8),
|
|
|
|
[self.objs[0], self.objs[1]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_contains_range(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__contains=NumericRange(3, 8)),
|
|
|
|
[self.objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_contained_by(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__contained_by=NumericRange(0, 20)),
|
|
|
|
[self.objs[0], self.objs[1], self.objs[3]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_overlap(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__overlap=NumericRange(3, 8)),
|
|
|
|
[self.objs[0], self.objs[1]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_fully_lt(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__fully_lt=NumericRange(5, 10)),
|
|
|
|
[self.objs[2]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_fully_gt(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__fully_gt=NumericRange(5, 10)),
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_not_lt(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__not_lt=NumericRange(5, 10)),
|
|
|
|
[self.objs[1]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_not_gt(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__not_gt=NumericRange(5, 10)),
|
|
|
|
[self.objs[0], self.objs[2]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_adjacent_to(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__adjacent_to=NumericRange(0, 5)),
|
|
|
|
[self.objs[1], self.objs[2]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_startswith(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__startswith=0),
|
|
|
|
[self.objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_endswith(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__endswith=0),
|
|
|
|
[self.objs[2]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_startswith_chaining(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangesModel.objects.filter(ints__startswith__gte=0),
|
|
|
|
[self.objs[0], self.objs[1]],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-02-12 22:59:13 +08:00
|
|
|
class TestQueryingWithRanges(PostgreSQLTestCase):
|
2015-05-21 19:25:50 +08:00
|
|
|
def test_date_range(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(date='2015-01-01'),
|
|
|
|
RangeLookupsModel.objects.create(date='2015-05-05'),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.filter(date__contained_by=DateRange('2015-01-01', '2015-05-04')),
|
|
|
|
[objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_date_range_datetime_field(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(timestamp='2015-01-01'),
|
|
|
|
RangeLookupsModel.objects.create(timestamp='2015-05-05'),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.filter(timestamp__date__contained_by=DateRange('2015-01-01', '2015-05-04')),
|
|
|
|
[objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_datetime_range(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(timestamp='2015-01-01T09:00:00'),
|
|
|
|
RangeLookupsModel.objects.create(timestamp='2015-05-05T17:00:00'),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.filter(
|
|
|
|
timestamp__contained_by=DateTimeTZRange('2015-01-01T09:00', '2015-05-04T23:55')
|
|
|
|
),
|
|
|
|
[objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_integer_range(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(integer=5),
|
|
|
|
RangeLookupsModel.objects.create(integer=99),
|
|
|
|
RangeLookupsModel.objects.create(integer=-1),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.filter(integer__contained_by=NumericRange(1, 98)),
|
|
|
|
[objs[0]]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_biginteger_range(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(big_integer=5),
|
|
|
|
RangeLookupsModel.objects.create(big_integer=99),
|
|
|
|
RangeLookupsModel.objects.create(big_integer=-1),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.filter(big_integer__contained_by=NumericRange(1, 98)),
|
|
|
|
[objs[0]]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_float_range(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(float=5),
|
|
|
|
RangeLookupsModel.objects.create(float=99),
|
|
|
|
RangeLookupsModel.objects.create(float=-1),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.filter(float__contained_by=NumericRange(1, 98)),
|
|
|
|
[objs[0]]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_f_ranges(self):
|
2018-10-03 07:17:23 +08:00
|
|
|
parent = RangesModel.objects.create(decimals=NumericRange(0, 10))
|
2015-05-21 19:25:50 +08:00
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(float=5, parent=parent),
|
|
|
|
RangeLookupsModel.objects.create(float=99, parent=parent),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
2018-10-03 07:17:23 +08:00
|
|
|
RangeLookupsModel.objects.filter(float__contained_by=F('parent__decimals')),
|
2015-05-21 19:25:50 +08:00
|
|
|
[objs[0]]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_exclude(self):
|
|
|
|
objs = [
|
|
|
|
RangeLookupsModel.objects.create(float=5),
|
|
|
|
RangeLookupsModel.objects.create(float=99),
|
|
|
|
RangeLookupsModel.objects.create(float=-1),
|
|
|
|
]
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
RangeLookupsModel.objects.exclude(float__contained_by=NumericRange(0, 100)),
|
|
|
|
[objs[2]]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestSerialization(PostgreSQLSimpleTestCase):
|
2015-01-11 00:14:20 +08:00
|
|
|
test_data = (
|
2015-06-06 19:55:04 +08:00
|
|
|
'[{"fields": {"ints": "{\\"upper\\": \\"10\\", \\"lower\\": \\"0\\", '
|
2018-10-03 07:17:23 +08:00
|
|
|
'\\"bounds\\": \\"[)\\"}", "decimals": "{\\"empty\\": true}", '
|
2015-06-06 20:05:29 +08:00
|
|
|
'"bigints": null, "timestamps": "{\\"upper\\": \\"2014-02-02T12:12:12+00:00\\", '
|
|
|
|
'\\"lower\\": \\"2014-01-01T00:00:00+00:00\\", \\"bounds\\": \\"[)\\"}", '
|
2019-07-10 16:33:36 +08:00
|
|
|
'"timestamps_inner": null, '
|
|
|
|
'"dates": "{\\"upper\\": \\"2014-02-02\\", \\"lower\\": \\"2014-01-01\\", \\"bounds\\": \\"[)\\"}", '
|
|
|
|
'"dates_inner": null }, '
|
2015-01-11 00:14:20 +08:00
|
|
|
'"model": "postgres_tests.rangesmodel", "pk": null}]'
|
|
|
|
)
|
|
|
|
|
2015-06-06 04:56:00 +08:00
|
|
|
lower_date = datetime.date(2014, 1, 1)
|
|
|
|
upper_date = datetime.date(2014, 2, 2)
|
2015-06-06 20:05:29 +08:00
|
|
|
lower_dt = datetime.datetime(2014, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
|
|
upper_dt = datetime.datetime(2014, 2, 2, 12, 12, 12, tzinfo=timezone.utc)
|
2015-06-06 04:56:00 +08:00
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
def test_dumping(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
instance = RangesModel(
|
2018-10-03 07:17:23 +08:00
|
|
|
ints=NumericRange(0, 10), decimals=NumericRange(empty=True),
|
2015-06-06 04:56:00 +08:00
|
|
|
timestamps=DateTimeTZRange(self.lower_dt, self.upper_dt),
|
2016-04-08 10:04:45 +08:00
|
|
|
dates=DateRange(self.lower_date, self.upper_date),
|
|
|
|
)
|
2015-01-11 00:14:20 +08:00
|
|
|
data = serializers.serialize('json', [instance])
|
|
|
|
dumped = json.loads(data)
|
2015-06-06 04:56:00 +08:00
|
|
|
for field in ('ints', 'dates', 'timestamps'):
|
|
|
|
dumped[0]['fields'][field] = json.loads(dumped[0]['fields'][field])
|
2015-01-11 00:14:20 +08:00
|
|
|
check = json.loads(self.test_data)
|
2015-06-06 04:56:00 +08:00
|
|
|
for field in ('ints', 'dates', 'timestamps'):
|
|
|
|
check[0]['fields'][field] = json.loads(check[0]['fields'][field])
|
2015-01-11 00:14:20 +08:00
|
|
|
self.assertEqual(dumped, check)
|
|
|
|
|
|
|
|
def test_loading(self):
|
|
|
|
instance = list(serializers.deserialize('json', self.test_data))[0].object
|
|
|
|
self.assertEqual(instance.ints, NumericRange(0, 10))
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertEqual(instance.decimals, NumericRange(empty=True))
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(instance.bigints)
|
2015-11-13 17:20:09 +08:00
|
|
|
self.assertEqual(instance.dates, DateRange(self.lower_date, self.upper_date))
|
|
|
|
self.assertEqual(instance.timestamps, DateTimeTZRange(self.lower_dt, self.upper_dt))
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2016-02-16 02:28:49 +08:00
|
|
|
def test_serialize_range_with_null(self):
|
|
|
|
instance = RangesModel(ints=NumericRange(None, 10))
|
|
|
|
data = serializers.serialize('json', [instance])
|
|
|
|
new_instance = list(serializers.deserialize('json', data))[0].object
|
|
|
|
self.assertEqual(new_instance.ints, NumericRange(None, 10))
|
|
|
|
|
|
|
|
instance = RangesModel(ints=NumericRange(10, None))
|
|
|
|
data = serializers.serialize('json', [instance])
|
|
|
|
new_instance = list(serializers.deserialize('json', data))[0].object
|
|
|
|
self.assertEqual(new_instance.ints, NumericRange(10, None))
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestValidators(PostgreSQLSimpleTestCase):
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_max(self):
|
|
|
|
validator = RangeMaxValueValidator(5)
|
|
|
|
validator(NumericRange(0, 5))
|
2017-10-31 08:03:52 +08:00
|
|
|
msg = 'Ensure that this range is completely less than or equal to 5.'
|
2015-01-11 00:14:20 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
validator(NumericRange(0, 10))
|
2017-10-31 08:03:52 +08:00
|
|
|
self.assertEqual(cm.exception.messages[0], msg)
|
2015-01-11 00:14:20 +08:00
|
|
|
self.assertEqual(cm.exception.code, 'max_value')
|
2017-10-31 08:03:52 +08:00
|
|
|
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
|
|
|
validator(NumericRange(0, None)) # an unbound range
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_min(self):
|
|
|
|
validator = RangeMinValueValidator(5)
|
|
|
|
validator(NumericRange(10, 15))
|
2017-10-31 08:03:52 +08:00
|
|
|
msg = 'Ensure that this range is completely greater than or equal to 5.'
|
2015-01-11 00:14:20 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
validator(NumericRange(0, 10))
|
2017-10-31 08:03:52 +08:00
|
|
|
self.assertEqual(cm.exception.messages[0], msg)
|
2015-01-11 00:14:20 +08:00
|
|
|
self.assertEqual(cm.exception.code, 'min_value')
|
2017-10-31 08:03:52 +08:00
|
|
|
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
|
|
|
validator(NumericRange(None, 10)) # an unbound range
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestFormField(PostgreSQLSimpleTestCase):
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_valid_integer(self):
|
|
|
|
field = pg_forms.IntegerRangeField()
|
|
|
|
value = field.clean(['1', '2'])
|
|
|
|
self.assertEqual(value, NumericRange(1, 2))
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_valid_decimal(self):
|
|
|
|
field = pg_forms.DecimalRangeField()
|
|
|
|
value = field.clean(['1.12345', '2.001'])
|
|
|
|
self.assertEqual(value, NumericRange(Decimal('1.12345'), Decimal('2.001')))
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
def test_valid_timestamps(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
value = field.clean(['01/01/2014 00:00:00', '02/02/2014 12:12:12'])
|
|
|
|
lower = datetime.datetime(2014, 1, 1, 0, 0, 0)
|
|
|
|
upper = datetime.datetime(2014, 2, 2, 12, 12, 12)
|
|
|
|
self.assertEqual(value, DateTimeTZRange(lower, upper))
|
|
|
|
|
|
|
|
def test_valid_dates(self):
|
|
|
|
field = pg_forms.DateRangeField()
|
|
|
|
value = field.clean(['01/01/2014', '02/02/2014'])
|
|
|
|
lower = datetime.date(2014, 1, 1)
|
|
|
|
upper = datetime.date(2014, 2, 2)
|
|
|
|
self.assertEqual(value, DateRange(lower, upper))
|
|
|
|
|
|
|
|
def test_using_split_datetime_widget(self):
|
|
|
|
class SplitDateTimeRangeField(pg_forms.DateTimeRangeField):
|
|
|
|
base_field = forms.SplitDateTimeField
|
|
|
|
|
|
|
|
class SplitForm(forms.Form):
|
|
|
|
field = SplitDateTimeRangeField()
|
|
|
|
|
|
|
|
form = SplitForm()
|
|
|
|
self.assertHTMLEqual(str(form), '''
|
|
|
|
<tr>
|
|
|
|
<th>
|
|
|
|
<label for="id_field_0">Field:</label>
|
|
|
|
</th>
|
|
|
|
<td>
|
2018-01-21 15:09:10 +08:00
|
|
|
<input id="id_field_0_0" name="field_0_0" type="text">
|
|
|
|
<input id="id_field_0_1" name="field_0_1" type="text">
|
|
|
|
<input id="id_field_1_0" name="field_1_0" type="text">
|
|
|
|
<input id="id_field_1_1" name="field_1_1" type="text">
|
2015-01-11 00:14:20 +08:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
''')
|
|
|
|
form = SplitForm({
|
|
|
|
'field_0_0': '01/01/2014',
|
|
|
|
'field_0_1': '00:00:00',
|
|
|
|
'field_1_0': '02/02/2014',
|
|
|
|
'field_1_1': '12:12:12',
|
|
|
|
})
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
lower = datetime.datetime(2014, 1, 1, 0, 0, 0)
|
|
|
|
upper = datetime.datetime(2014, 2, 2, 12, 12, 12)
|
|
|
|
self.assertEqual(form.cleaned_data['field'], DateTimeTZRange(lower, upper))
|
|
|
|
|
|
|
|
def test_none(self):
|
|
|
|
field = pg_forms.IntegerRangeField(required=False)
|
|
|
|
value = field.clean(['', ''])
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(value)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_rendering(self):
|
|
|
|
class RangeForm(forms.Form):
|
|
|
|
ints = pg_forms.IntegerRangeField()
|
|
|
|
|
|
|
|
self.assertHTMLEqual(str(RangeForm()), '''
|
|
|
|
<tr>
|
|
|
|
<th><label for="id_ints_0">Ints:</label></th>
|
|
|
|
<td>
|
2018-01-21 15:09:10 +08:00
|
|
|
<input id="id_ints_0" name="ints_0" type="number">
|
|
|
|
<input id="id_ints_1" name="ints_1" type="number">
|
2015-01-11 00:14:20 +08:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
''')
|
|
|
|
|
2015-02-20 05:19:33 +08:00
|
|
|
def test_integer_lower_bound_higher(self):
|
2015-01-11 00:14:20 +08:00
|
|
|
field = pg_forms.IntegerRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['10', '2'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'The start of the range must not exceed the end of the range.')
|
|
|
|
self.assertEqual(cm.exception.code, 'bound_ordering')
|
|
|
|
|
2015-02-20 05:19:33 +08:00
|
|
|
def test_integer_open(self):
|
2015-01-11 00:14:20 +08:00
|
|
|
field = pg_forms.IntegerRangeField()
|
|
|
|
value = field.clean(['', '0'])
|
|
|
|
self.assertEqual(value, NumericRange(None, 0))
|
|
|
|
|
2015-02-20 05:19:33 +08:00
|
|
|
def test_integer_incorrect_data_type(self):
|
2015-01-11 00:14:20 +08:00
|
|
|
field = pg_forms.IntegerRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean('1')
|
2015-02-20 05:19:33 +08:00
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter two whole numbers.')
|
2015-01-11 00:14:20 +08:00
|
|
|
self.assertEqual(cm.exception.code, 'invalid')
|
|
|
|
|
2015-02-20 05:19:33 +08:00
|
|
|
def test_integer_invalid_lower(self):
|
2015-01-11 00:14:20 +08:00
|
|
|
field = pg_forms.IntegerRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['a', '2'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a whole number.')
|
|
|
|
|
2015-02-20 05:19:33 +08:00
|
|
|
def test_integer_invalid_upper(self):
|
2015-01-11 00:14:20 +08:00
|
|
|
field = pg_forms.IntegerRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['1', 'b'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a whole number.')
|
|
|
|
|
2015-02-20 05:19:33 +08:00
|
|
|
def test_integer_required(self):
|
2015-01-11 00:14:20 +08:00
|
|
|
field = pg_forms.IntegerRangeField(required=True)
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['', ''])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'This field is required.')
|
|
|
|
value = field.clean([1, ''])
|
|
|
|
self.assertEqual(value, NumericRange(1, None))
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_decimal_lower_bound_higher(self):
|
|
|
|
field = pg_forms.DecimalRangeField()
|
2015-02-20 05:19:33 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['1.8', '1.6'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'The start of the range must not exceed the end of the range.')
|
|
|
|
self.assertEqual(cm.exception.code, 'bound_ordering')
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_decimal_open(self):
|
|
|
|
field = pg_forms.DecimalRangeField()
|
2015-02-20 05:19:33 +08:00
|
|
|
value = field.clean(['', '3.1415926'])
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertEqual(value, NumericRange(None, Decimal('3.1415926')))
|
2015-02-20 05:19:33 +08:00
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_decimal_incorrect_data_type(self):
|
|
|
|
field = pg_forms.DecimalRangeField()
|
2015-02-20 05:19:33 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean('1.6')
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter two numbers.')
|
|
|
|
self.assertEqual(cm.exception.code, 'invalid')
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_decimal_invalid_lower(self):
|
|
|
|
field = pg_forms.DecimalRangeField()
|
2015-02-20 05:19:33 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['a', '3.1415926'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a number.')
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_decimal_invalid_upper(self):
|
|
|
|
field = pg_forms.DecimalRangeField()
|
2015-02-20 05:19:33 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['1.61803399', 'b'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a number.')
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
def test_decimal_required(self):
|
|
|
|
field = pg_forms.DecimalRangeField(required=True)
|
2015-02-20 05:19:33 +08:00
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['', ''])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'This field is required.')
|
|
|
|
value = field.clean(['1.61803399', ''])
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertEqual(value, NumericRange(Decimal('1.61803399'), None))
|
2015-02-20 05:19:33 +08:00
|
|
|
|
|
|
|
def test_date_lower_bound_higher(self):
|
|
|
|
field = pg_forms.DateRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['2013-04-09', '1976-04-16'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'The start of the range must not exceed the end of the range.')
|
|
|
|
self.assertEqual(cm.exception.code, 'bound_ordering')
|
|
|
|
|
|
|
|
def test_date_open(self):
|
|
|
|
field = pg_forms.DateRangeField()
|
|
|
|
value = field.clean(['', '2013-04-09'])
|
|
|
|
self.assertEqual(value, DateRange(None, datetime.date(2013, 4, 9)))
|
|
|
|
|
|
|
|
def test_date_incorrect_data_type(self):
|
|
|
|
field = pg_forms.DateRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean('1')
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter two valid dates.')
|
|
|
|
self.assertEqual(cm.exception.code, 'invalid')
|
|
|
|
|
|
|
|
def test_date_invalid_lower(self):
|
|
|
|
field = pg_forms.DateRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['a', '2013-04-09'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a valid date.')
|
|
|
|
|
|
|
|
def test_date_invalid_upper(self):
|
|
|
|
field = pg_forms.DateRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['2013-04-09', 'b'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a valid date.')
|
|
|
|
|
|
|
|
def test_date_required(self):
|
|
|
|
field = pg_forms.DateRangeField(required=True)
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['', ''])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'This field is required.')
|
|
|
|
value = field.clean(['1976-04-16', ''])
|
|
|
|
self.assertEqual(value, DateRange(datetime.date(1976, 4, 16), None))
|
|
|
|
|
|
|
|
def test_datetime_lower_bound_higher(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['2006-10-25 14:59', '2006-10-25 14:58'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'The start of the range must not exceed the end of the range.')
|
|
|
|
self.assertEqual(cm.exception.code, 'bound_ordering')
|
|
|
|
|
|
|
|
def test_datetime_open(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
value = field.clean(['', '2013-04-09 11:45'])
|
|
|
|
self.assertEqual(value, DateTimeTZRange(None, datetime.datetime(2013, 4, 9, 11, 45)))
|
|
|
|
|
|
|
|
def test_datetime_incorrect_data_type(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean('2013-04-09 11:45')
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter two valid date/times.')
|
|
|
|
self.assertEqual(cm.exception.code, 'invalid')
|
|
|
|
|
|
|
|
def test_datetime_invalid_lower(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['45', '2013-04-09 11:45'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a valid date/time.')
|
|
|
|
|
|
|
|
def test_datetime_invalid_upper(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['2013-04-09 11:45', 'sweet pickles'])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'Enter a valid date/time.')
|
|
|
|
|
|
|
|
def test_datetime_required(self):
|
|
|
|
field = pg_forms.DateTimeRangeField(required=True)
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean(['', ''])
|
|
|
|
self.assertEqual(cm.exception.messages[0], 'This field is required.')
|
|
|
|
value = field.clean(['2013-04-09 11:45', ''])
|
|
|
|
self.assertEqual(value, DateTimeTZRange(datetime.datetime(2013, 4, 9, 11, 45), None))
|
|
|
|
|
2015-05-22 21:54:31 +08:00
|
|
|
@override_settings(USE_TZ=True, TIME_ZONE='Africa/Johannesburg')
|
|
|
|
def test_datetime_prepare_value(self):
|
|
|
|
field = pg_forms.DateTimeRangeField()
|
|
|
|
value = field.prepare_value(
|
|
|
|
DateTimeTZRange(datetime.datetime(2015, 5, 22, 16, 6, 33, tzinfo=timezone.utc), None)
|
|
|
|
)
|
|
|
|
self.assertEqual(value, [datetime.datetime(2015, 5, 22, 18, 6, 33), None])
|
|
|
|
|
2015-01-11 00:14:20 +08:00
|
|
|
def test_model_field_formfield_integer(self):
|
|
|
|
model_field = pg_fields.IntegerRangeField()
|
|
|
|
form_field = model_field.formfield()
|
|
|
|
self.assertIsInstance(form_field, pg_forms.IntegerRangeField)
|
|
|
|
|
|
|
|
def test_model_field_formfield_biginteger(self):
|
|
|
|
model_field = pg_fields.BigIntegerRangeField()
|
|
|
|
form_field = model_field.formfield()
|
|
|
|
self.assertIsInstance(form_field, pg_forms.IntegerRangeField)
|
|
|
|
|
|
|
|
def test_model_field_formfield_float(self):
|
2018-10-03 07:17:23 +08:00
|
|
|
model_field = pg_fields.DecimalRangeField()
|
2015-01-11 00:14:20 +08:00
|
|
|
form_field = model_field.formfield()
|
2018-10-03 07:17:23 +08:00
|
|
|
self.assertIsInstance(form_field, pg_forms.DecimalRangeField)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
def test_model_field_formfield_date(self):
|
|
|
|
model_field = pg_fields.DateRangeField()
|
|
|
|
form_field = model_field.formfield()
|
|
|
|
self.assertIsInstance(form_field, pg_forms.DateRangeField)
|
|
|
|
|
|
|
|
def test_model_field_formfield_datetime(self):
|
|
|
|
model_field = pg_fields.DateTimeRangeField()
|
|
|
|
form_field = model_field.formfield()
|
|
|
|
self.assertIsInstance(form_field, pg_forms.DateTimeRangeField)
|
2015-01-18 10:20:42 +08:00
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestWidget(PostgreSQLSimpleTestCase):
|
2015-01-18 10:20:42 +08:00
|
|
|
def test_range_widget(self):
|
|
|
|
f = pg_forms.ranges.DateTimeRangeField()
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
f.widget.render('datetimerange', ''),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="datetimerange_0"><input type="text" name="datetimerange_1">'
|
2015-01-18 10:20:42 +08:00
|
|
|
)
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
f.widget.render('datetimerange', None),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="datetimerange_0"><input type="text" name="datetimerange_1">'
|
2015-01-18 10:20:42 +08:00
|
|
|
)
|
|
|
|
dt_range = DateTimeTZRange(
|
|
|
|
datetime.datetime(2006, 1, 10, 7, 30),
|
|
|
|
datetime.datetime(2006, 2, 12, 9, 50)
|
|
|
|
)
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
f.widget.render('datetimerange', dt_range),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="datetimerange_0" value="2006-01-10 07:30:00">'
|
|
|
|
'<input type="text" name="datetimerange_1" value="2006-02-12 09:50:00">'
|
2015-01-18 10:20:42 +08:00
|
|
|
)
|