Skip to content

Commit 6f44010

Browse files
authored
Merge pull request #744 from minrk/rm-mock-config
Remove MockConfigurer
2 parents e194a1a + afa77c4 commit 6f44010

File tree

1 file changed

+3
-53
lines changed

1 file changed

+3
-53
lines changed

tests/test_configurer.py

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,14 @@
33
"""
44

55
from traitlets import Dict
6+
from traitlets.config import Config
67
import os
78
import sys
89

910
from tljh import configurer
1011

1112

12-
class MockConfigurer:
13-
"""
14-
Mock a Traitlets Config class object.
15-
16-
Equivalent to the `c` in `c.JupyterHub.some_property` method of setting
17-
traitlet properties. If an accessed attribute doesn't exist, a new instance
18-
of EmtpyObject is returned. This lets us set arbitrary attributes two
19-
levels deep.
20-
21-
>>> c = MockConfigurer()
22-
>>> c.FirstLevel.second_level = 'hi'
23-
>>> c.FirstLevel.second_level == 'hi'
24-
True
25-
>>> hasattr(c.FirstLevel, 'does_not_exist')
26-
False
27-
28-
The actual Config class implementation can be found at
29-
https://github.com/ipython/traitlets/blob/34f596dd03b98434900a7d31c912fc168342bb80/traitlets/config/loader.py#L220
30-
"""
31-
32-
class _EmptyObject:
33-
"""
34-
Empty class for putting attributes in.
35-
"""
36-
pass
37-
38-
def __getattr__(self, k):
39-
if k not in self.__dict__:
40-
self.__dict__[k] = MockConfigurer._EmptyObject()
41-
return self.__dict__[k]
42-
43-
def __getitem__(self, key):
44-
"""
45-
To mimic the traitlets Config class instance we often access as "c", we
46-
need to provide a subscript functionality that can be used as
47-
c["Something"]. To do this, we provide a __getitem__ function.
48-
"""
49-
return self.__getattr__(key)
50-
51-
def test_mock_configurer():
52-
"""
53-
Test the MockConfigurer's mocking ability
54-
"""
55-
m = MockConfigurer()
56-
m.SomethingSomething = 'hi'
57-
m.FirstLevel.second_level = 'boo'
58-
59-
assert m.SomethingSomething == 'hi'
60-
assert m.FirstLevel.second_level == 'boo'
61-
assert m["FirstLevel"].second_level == 'boo'
6213

63-
assert not hasattr(m.FirstLevel, 'non_existent')
6414

6515

6616
def apply_mock_config(overrides):
@@ -69,7 +19,7 @@ def apply_mock_config(overrides):
6919
7020
overrides should be a dict that matches what you parse from a config.yaml
7121
"""
72-
c = MockConfigurer()
22+
c = Config()
7323
configurer.apply_config(overrides, c)
7424
return c
7525

@@ -112,7 +62,7 @@ def test_app_default():
11262
"""
11363
c = apply_mock_config({})
11464
# default_url is not set, so JupyterHub will pick default.
115-
assert not hasattr(c.Spawner, 'default_url')
65+
assert 'default_url' not in c.Spawner
11666

11767

11868
def test_app_jupyterlab():

0 commit comments

Comments
 (0)