-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathtest_traittypes.py
69 lines (55 loc) · 1.73 KB
/
test_traittypes.py
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
import pytest
from traitlets import HasTraits, TraitError
from traitlets.utils.importstring import import_item
from jupyter_server.services.contents.largefilemanager import LargeFileManager
from jupyter_server.traittypes import InstanceFromClasses, TypeFromClasses
class DummyClass:
"""Dummy class for testing Instance"""
class DummyInt(int):
"""Dummy class for testing types."""
class Thing(HasTraits):
a = InstanceFromClasses(
default_value=2,
klasses=[
int,
str,
DummyClass,
],
)
b = TypeFromClasses(
default_value=None,
allow_none=True,
klasses=[
DummyClass,
int,
"jupyter_server.services.contents.manager.ContentsManager",
],
)
class TestInstanceFromClasses:
@pytest.mark.parametrize("value", [1, "test", DummyClass()])
def test_good_values(self, value):
thing = Thing(a=value)
assert thing.a == value
@pytest.mark.parametrize("value", [2.4, object()])
def test_bad_values(self, value):
with pytest.raises(TraitError) as e:
thing = Thing(a=value)
class TestTypeFromClasses:
@pytest.mark.parametrize(
"value",
[
DummyClass,
DummyInt,
LargeFileManager,
"jupyter_server.services.contents.manager.ContentsManager",
],
)
def test_good_values(self, value):
thing = Thing(b=value)
if isinstance(value, str):
value = import_item(value)
assert thing.b == value
@pytest.mark.parametrize("value", [float, object])
def test_bad_values(self, value):
with pytest.raises(TraitError) as e:
thing = Thing(b=value)