Skip to content

Commit

Permalink
use Config object when testing config
Browse files Browse the repository at this point in the history
I don't see a reason to mock a simple object
  • Loading branch information
minrk committed Oct 27, 2021
1 parent e50b60f commit 0110877
Showing 1 changed file with 3 additions and 42 deletions.
45 changes: 3 additions & 42 deletions tests/test_configurer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,14 @@
"""

from traitlets import Dict
from traitlets.config import Config
import os
import sys

from tljh import configurer


class MockConfigurer:
"""
Mock a Traitlet Configurable object.
Equivalent to the `c` in `c.JupyterHub.some_property` method of setting
traitlet properties. If an accessed attribute doesn't exist, a new instance
of EmtpyObject is returned. This lets us set arbitrary attributes two
levels deep.
>>> c = MockConfigurer()
>>> c.FirstLevel.second_level = 'hi'
>>> c.FirstLevel.second_level == 'hi'
True
>>> hasattr(c.FirstLevel, 'does_not_exist')
False
"""

class _EmptyObject:
"""
Empty class for putting attributes in.
"""
pass

def __getattr__(self, k):
if k not in self.__dict__:
self.__dict__[k] = MockConfigurer._EmptyObject()
return self.__dict__[k]


def test_mock_configurer():
"""
Test the MockConfigurer's mocking ability
"""
m = MockConfigurer()
m.SomethingSomething = 'hi'
m.FirstLevel.second_level = 'boo'

assert m.SomethingSomething == 'hi'
assert m.FirstLevel.second_level == 'boo'

assert not hasattr(m.FirstLevel, 'non_existent')


def apply_mock_config(overrides):
Expand All @@ -58,7 +19,7 @@ def apply_mock_config(overrides):
overrides should be a dict that matches what you parse from a config.yaml
"""
c = MockConfigurer()
c = Config()
configurer.apply_config(overrides, c)
return c

Expand Down Expand Up @@ -101,7 +62,7 @@ def test_app_default():
"""
c = apply_mock_config({})
# default_url is not set, so JupyterHub will pick default.
assert not hasattr(c.Spawner, 'default_url')
assert 'default_url' not in c.Spawner


def test_app_jupyterlab():
Expand Down

0 comments on commit 0110877

Please sign in to comment.