-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_param_scope.py
114 lines (92 loc) · 3.68 KB
/
test_param_scope.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from unittest import TestCase
from hyperparameter import param_scope
class TestParamScopeCreate(TestCase):
def test_param_scope_create_from_empty(self):
ps = param_scope()
def test_param_scope_create_from_kwargs(self):
ps = param_scope(a=1, b=2)
assert ps.a | 0 == 1
assert ps.b | 0 == 2
def test_param_scope_create_from_args(self):
ps = param_scope("a=1", "b=2")
assert ps.a | 0 == 1
assert ps.b | 0 == 2
def test_param_scope_create_with_long_name(self):
ps = param_scope("a.b.c=1")
assert ps.a.b.c | 0 == 1
def test_param_scope_create_from_dict(self):
ps = param_scope(**{"a.b.c": 1, "A.B.C": 2})
assert ps.a.b.c | 0 == 1
assert ps.A.B.C | 0 == 2
class TestParamScopeDirectAccess(TestCase):
def test_param_scope_undefined_short_name(self):
assert param_scope.a | 0 == 0
assert param_scope.a(1) == 1
assert param_scope().a(1) == 1
def test_param_scope_undefined_with_long_name(self):
assert param_scope.a.b.c | 0 == 0
assert param_scope.a.b.c(1) == 1
assert param_scope().a.b.c(1) == 1
def test_param_scope_direct_write(self):
with param_scope():
param_scope.a = 1
param_scope().b = 2
assert param_scope.a() == 1
assert param_scope.b() == None
ps = param_scope()
ps.b = 2
assert ps.b() == 2
with ps:
param_scope.b() == 2
# check for param leak
assert param_scope.a() == None
assert param_scope.b() == None
class TestParamScopeWith(TestCase):
def test_with_param_scope(self):
with param_scope() as ps:
assert ps.a | 1 == 1
with param_scope(a=1) as ps:
assert ps.a | 0 == 1
with param_scope("a=1") as ps:
assert ps.a | 0 == 1
with param_scope(**{"a": 1}) as ps:
assert ps.a | 0 == 1
def test_nested_param_scope(self):
with param_scope() as ps1:
assert ps1.a | "empty" == "empty"
with param_scope(a="non-empty") as ps2:
assert ps2.a | "empty" == "non-empty"
with param_scope() as ps3:
with param_scope() as ps4:
assert ps2.a | "empty" == "non-empty"
assert ps1.a | "empty" == "empty"
assert param_scope.a | "empty" == "empty"
class TestParamScopeGetOrElse(TestCase):
def test_param_scope_default_int(self):
with param_scope(a=1, b="1", c="1.12", d="not int", e=False) as ps:
assert ps.a | 0 == 1
assert ps.b | 1 == 1
assert ps.c | 1 == 1.12
assert ps.d | 1 == "not int"
assert ps.e | 1 == 0
def test_param_scope_default_float(self):
with param_scope(a=1, b="1", c="1.12", d="not int", e=False) as ps:
assert ps.a | 0.0 == 1
assert ps.b | 1.0 == 1
assert ps.c | 1.0 == 1.12
assert ps.d | 1.0 == "not int"
assert ps.e | 1.0 == 0
def test_param_scope_default_str(self):
with param_scope(a=1, b="1", c="1.12", d="not int", e=False) as ps:
assert ps.a | "0" == "1"
assert ps.b | "1" == "1"
assert ps.c | "1" == "1.12"
assert ps.d | "1" == "not int"
assert ps.e | "1" == "False"
def test_param_scope_default_bool(self):
with param_scope(a=1, b="1", c="1.12", d="not int", e=False) as ps:
assert ps.a | False == True
assert ps.b | False == True
assert ps.c | False == False
assert ps.d | False == False
assert ps.e | False == False