1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
"""
Unit test for CalendarDate parameters.
"""
import datetime as dt
import re
import unittest
import pytest
import param
from .utils import check_defaults
class TestDateTimeParameters(unittest.TestCase):
def _check_defaults(self, p):
assert p.default is None
assert p.allow_None is True
assert p.bounds is None
assert p.softbounds is None
assert p.inclusive_bounds == (True, True)
assert p.step is None
def test_defaults_class(self):
class A(param.Parameterized):
d = param.CalendarDate()
check_defaults(A.param.d, label='D')
self._check_defaults(A.param.d)
def test_defaults_inst(self):
class A(param.Parameterized):
d = param.CalendarDate()
a = A()
check_defaults(a.param.d, label='D')
self._check_defaults(a.param.d)
def test_defaults_unbound(self):
d = param.CalendarDate()
check_defaults(d, label=None)
self._check_defaults(d)
def test_initialization_out_of_bounds(self):
with pytest.raises(ValueError):
class Q(param.Parameterized):
q = param.CalendarDate(dt.date(2017,2,27),
bounds=(dt.date(2017,2,1),
dt.date(2017,2,26)))
def test_set_out_of_bounds(self):
class Q(param.Parameterized):
q = param.CalendarDate(bounds=(dt.date(2017,2,1),
dt.date(2017,2,26)))
with pytest.raises(ValueError):
Q.q = dt.date(2017,2,27)
def test_set_exclusive_out_of_bounds(self):
class Q(param.Parameterized):
q = param.CalendarDate(bounds=(dt.date(2017,2,1),
dt.date(2017,2,26)),
inclusive_bounds=(True, False))
with pytest.raises(ValueError):
Q.q = dt.date(2017,2,26)
def test_get_soft_bounds(self):
q = param.CalendarDate(dt.date(2017,2,25),
bounds=(dt.date(2017,2,1),
dt.date(2017,2,26)),
softbounds=(dt.date(2017,2,1),
dt.date(2017,2,25)))
self.assertEqual(q.get_soft_bounds(), (dt.date(2017,2,1),
dt.date(2017,2,25)))
def test_datetime_not_accepted(self):
with pytest.raises(ValueError, match=re.escape('CalendarDate parameter only takes date types.')):
param.CalendarDate(dt.datetime(2021, 8, 16, 10))
def test_step_invalid_type_parameter(self):
with pytest.raises(
ValueError,
match=re.escape("Attribute 'step' of CalendarDate parameter can only be None or a date type, not <class 'float'>.")
):
param.CalendarDate(dt.date(2017,2,27), step=3.2)
|