-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmc_post_validate_test.py
150 lines (111 loc) · 4.53 KB
/
mc_post_validate_test.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (c) 2012 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.
# pylint: disable=E0611
from pytest import raises
from multiconf import mc_config, ConfigItem, ConfigApiException, ConfigExcludedAttributeError
from multiconf.decorators import nested_repeatables, named_as
from multiconf.envs import EnvFactory
from .utils.tstclasses import ItemWithAA, RepeatableItemWithAA
from .utils.utils import local_func
ef2_pp_prod = EnvFactory()
pp2 = ef2_pp_prod.Env('pp')
prod2 = ef2_pp_prod.Env('prod')
ef2_pp_prod.EnvGroup('g_prod_like', prod2, pp2)
def test_mc_post_validate_getattr_env():
class root(ItemWithAA):
def mc_init(self):
self.aa = 7
def mc_post_validate(self):
assert self.getattr('aa', prod2) == self.getattr('aa', pp2) == 7
@mc_config(ef2_pp_prod, load_now=True)
def config(_):
with root():
pass
rt = config(prod2).root
assert rt.aa == 7
def test_setattr_not_allowed_in_mc_post_validate():
class root(ConfigItem):
def mc_post_validate(self):
self.setattr('y', default=7, mc_set_unknown=True)
with raises(ConfigApiException) as exinfo:
@mc_config(ef2_pp_prod, load_now=True)
def config(_):
with root():
pass
exp = "Trying to set attribute 'y'. Setting attributes is not allowed after configuration is loaded or while doing json dump (print) "
exp += "(in order to enforce derived value validity)."
assert str(exinfo.value) == exp
def test_item_dot_attr_not_allowed_in_mc_post_validate():
class root(ItemWithAA):
def mc_post_validate(self):
print(self.aa)
with raises(ConfigApiException) as exinfo:
@mc_config(ef2_pp_prod, load_now=True)
def config(_):
with root() as rt:
rt.aa = 1
exp = "Trying to access attribute 'aa'. " \
"Item.attribute access is not allowed in 'mc_post_validate' as there is no current env. " \
"Use: item.attr_env_items('aa') or item.getattr('aa', <env>)"
assert str(exinfo.value) == exp
def test_mc_post_validate_exception():
class item(ConfigItem):
def mc_post_validate(self):
raise Exception("Error in item mc_post_validate")
with raises(Exception) as exinfo:
@mc_config(ef2_pp_prod, load_now=True)
def config(_):
with ConfigItem():
item()
assert str(exinfo.value) == "Error in item mc_post_validate"
def test_mc_post_validate_excluded_item():
class root(ItemWithAA):
def mc_init(self):
self.aa = 7
def mc_post_validate(self):
assert self.getattr('aa', prod2) == self.getattr('aa', pp2) == 7
with raises(ConfigExcludedAttributeError) as exinfo:
@mc_config(ef2_pp_prod, load_now=True)
def config(_):
with root() as rt:
rt.mc_select_envs(exclude=[pp2])
exp_class = "Excluded: <class 'test.mc_post_validate_test.%(local_func)sroot'>" % {"local_func": local_func()}
exp = "Accessing attribute 'aa' for Env('pp') on an excluded config item: " + exp_class
assert exp in str(exinfo.value)
def test_mc_post_validate_excluded_repeatable_item():
@nested_repeatables('reps')
class Root(ConfigItem):
pass
@named_as('reps')
class Rep(RepeatableItemWithAA):
def mc_init(self):
self.aa = 7
def mc_post_validate(self):
assert self.getattr('aa', prod2) == self.getattr('aa', pp2) == 7
with raises(ConfigExcludedAttributeError) as exinfo:
@mc_config(ef2_pp_prod, load_now=True)
def config(_):
with Root() as rt:
Rep(1)
with Rep(2) as r2:
r2.mc_select_envs(exclude=[pp2])
exp_class = "Excluded: <class 'test.mc_post_validate_test.%(local_func)sRep'>" % {"local_func": local_func()}
exp = "Accessing attribute 'aa' for Env('pp') on an excluded config item: " + exp_class
assert exp in str(exinfo.value)
def test_mc_post_validate_disabled():
@nested_repeatables('reps')
class Root(ConfigItem):
pass
@named_as('reps')
class Rep(RepeatableItemWithAA):
def mc_init(self):
self.aa = 7
def mc_post_validate(self):
raise Exception('Never called')
@mc_config(ef2_pp_prod)
def config(_):
with Root() as rt:
Rep(1)
with Rep(2) as r2:
r2.mc_select_envs(exclude=[pp2])
config.load(do_post_validate=False)