Skip to content

Commit

Permalink
Add more tests (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Mar 12, 2023
1 parent 6c995be commit 612aaee
Show file tree
Hide file tree
Showing 25 changed files with 2,702 additions and 25 deletions.
155 changes: 155 additions & 0 deletions tests/API1/testbooleanparam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""
Unit test for Boolean parameters.
"""
import datetime as dt

import param

from . import API1TestCase
from .utils import check_defaults


class TestBooleanParameters(API1TestCase):

def setUp(self):
super(TestBooleanParameters, self).setUp()
class P(param.Parameterized):
e = param.Boolean()
f = param.Boolean(default=None)

self.P = P

def _check_defaults(self, p):
assert p.default is False
assert p.allow_None is False
assert p.bounds == (0, 1)

def test_defaults_class(self):
class A(param.Parameterized):
b = param.Boolean()

check_defaults(A.param.b, label='B')
self._check_defaults(A.param.b)

def test_defaults_inst(self):
class A(param.Parameterized):
b = param.Boolean()

a = A()

check_defaults(a.param.b, label='B')
self._check_defaults(a.param.b)

def test_defaults_unbound(self):
b = param.Boolean()

check_defaults(b, label=None)
self._check_defaults(b)

def test_default_is_None(self):
p = self.P()
assert p.f is None
assert p.param.f.allow_None is True

p.f = True
p.f = None
assert p.f is None

def test_raise_None_when_not_allowed(self):
p = self.P()

msg = r"Boolean parameter 'e' must be True or False, not None"
with self.assertRaisesRegex(ValueError, msg):
p.e = None

with self.assertRaisesRegex(ValueError, msg):
self.P.e = None

def test_bad_type(self):
msg = r"Boolean parameter 'e' must be True or False, not test"

with self.assertRaisesRegex(ValueError, msg):
self.P.e = 'test'

with self.assertRaisesRegex(ValueError, msg):
self.P(e='test')

p = self.P()

with self.assertRaisesRegex(ValueError, msg):
p.e = 'test'


class TestEventParameters(API1TestCase):

def setUp(self):
super(TestEventParameters, self).setUp()
class P(param.Parameterized):
e = param.Event()
f = param.Event(default=None)

self.P = P

def _check_defaults(self, p):
assert p.default is False
assert p.allow_None is False
assert p.bounds == (0, 1)

def test_defaults_class(self):
class A(param.Parameterized):
b = param.Event()

check_defaults(A.param.b, label='B')
self._check_defaults(A.param.b)

def test_defaults_inst(self):
class A(param.Parameterized):
b = param.Event()

a = A()

check_defaults(a.param.b, label='B')
self._check_defaults(a.param.b)

def test_defaults_unbound(self):
b = param.Event()

check_defaults(b, label=None)
self._check_defaults(b)

def test_resets_to_false(self):
p = self.P()
p.e = True
assert p.e is False

def test_default_is_None(self):
p = self.P()
assert p.f is None
assert p.param.f.allow_None is True

p.f = None
assert p.f is False

def test_raise_None_when_not_allowed(self):
p = self.P()

msg = r"Boolean parameter 'e' must be True or False, not None"
with self.assertRaisesRegex(ValueError, msg):
p.e = None

with self.assertRaisesRegex(ValueError, msg):
self.P.e = None

def test_bad_type(self):
msg = r"Boolean parameter 'e' must be True or False, not test"

with self.assertRaisesRegex(ValueError, msg):
self.P.e = 'test'

with self.assertRaisesRegex(ValueError, msg):
self.P(e='test')

p = self.P()

with self.assertRaisesRegex(ValueError, msg):
p.e = 'test'
28 changes: 28 additions & 0 deletions tests/API1/testbytesparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from . import API1TestCase
from .utils import check_defaults

import param

Expand All @@ -14,6 +15,33 @@

class TestBytesParameters(API1TestCase):

def _check_defaults(self, p):
assert p.default == b''
assert p.allow_None is False
assert p.regex is None

def test_defaults_class(self):
class A(param.Parameterized):
b = param.Bytes()

check_defaults(A.param.b, label='B')
self._check_defaults(A.param.b)

def test_defaults_inst(self):
class A(param.Parameterized):
b = param.Bytes()

a = A()

check_defaults(a.param.b, label='B')
self._check_defaults(a.param.b)

def test_defaults_unbound(self):
b = param.Bytes()

check_defaults(b, label=None)
self._check_defaults(b)

def test_bytes_default_type(self):
if sys.version_info.major < 3:
pytest.skip()
Expand Down
36 changes: 36 additions & 0 deletions tests/API1/testcalendardateparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,41 @@
import pytest
import param
from . import API1TestCase
from .utils import check_defaults


class TestDateTimeParameters(API1TestCase):

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):
try:
class Q(param.Parameterized):
Expand Down Expand Up @@ -57,3 +88,8 @@ def test_get_soft_bounds(self):
def test_datetime_not_accepted(self):
with pytest.raises(ValueError):
param.CalendarDate(dt.datetime(2021, 8, 16, 10))

def test_step_invalid_type_parameter(self):
exception = "Step can only be None or a date type"
with self.assertRaisesRegex(ValueError, exception):
param.CalendarDate(dt.date(2017,2,27), step=3.2)
28 changes: 28 additions & 0 deletions tests/API1/testcalendardaterangeparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@

class TestDateTimeRange(API1TestCase):

def _check_defaults(self, p):
assert p.default is None
assert p.allow_None is True
assert p.length == 2
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 P(param.Parameterized):
r = param.CalendarDateRange()

self._check_defaults(P.param.r)

def test_defaults_inst(self):
class P(param.Parameterized):
r = param.CalendarDateRange()

p = P()

self._check_defaults(p.param.r)

def test_defaults_unbound(self):
r = param.CalendarDateRange()

self._check_defaults(r)

bad_range = (dt.date(2017,2,27),dt.date(2017,2,26))

def test_wrong_type_default(self):
Expand Down
61 changes: 61 additions & 0 deletions tests/API1/testclassselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import param
from . import API1TestCase
from .utils import check_defaults

class TestClassSelectorParameters(API1TestCase):

Expand All @@ -20,6 +21,37 @@ class P(param.Parameterized):

self.P = P

def _check_defaults(self, p):
assert p.default is None
assert p.allow_None is True
assert p.instantiate is True
assert p.is_instance is True

def test_defaults_class(self):
class P(param.Parameterized):
s = param.ClassSelector(int)

check_defaults(P.param.s, label='S', skip=['instantiate'])
self._check_defaults(P.param.s)
assert P.param.s.class_ is int

def test_defaults_inst(self):
class P(param.Parameterized):
s = param.ClassSelector(int)

p = P()

check_defaults(p.param.s, label='S', skip=['instantiate'])
self._check_defaults(p.param.s)
assert p.param.s.class_ is int

def test_defaults_unbound(self):
s = param.ClassSelector(int)

check_defaults(s, label=None, skip=['instantiate'])
self._check_defaults(s)
assert s.class_ is int

def test_single_class_instance_constructor(self):
p = self.P(e=6)
self.assertEqual(p.e, 6)
Expand Down Expand Up @@ -73,6 +105,35 @@ def test_multiple_class_type_error(self):

class TestDictParameters(API1TestCase):

def _check_defaults(self, p):
assert p.default is None
assert p.allow_None is True
assert p.instantiate is True
assert p.is_instance is True
assert p.class_ == dict

def test_defaults_class(self):
class P(param.Parameterized):
s = param.Dict()

check_defaults(P.param.s, label='S', skip=['instantiate'])
self._check_defaults(P.param.s)

def test_defaults_inst(self):
class P(param.Parameterized):
s = param.Dict()

p = P()

check_defaults(p.param.s, label='S', skip=['instantiate'])
self._check_defaults(p.param.s)

def test_defaults_unbound(self):
s = param.Dict()

check_defaults(s, label=None, skip=['instantiate'])
self._check_defaults(s)

def test_valid_dict_parameter(self):
valid_dict = {1:2, 3:3}

Expand Down
Loading

0 comments on commit 612aaee

Please sign in to comment.