Ejemplo n.º 1
0
def test_set_lazy_resolution(logger_mock):
    """ Test if `@configurable` can resolve it's configuration lazily.
    Also, test if `get_config` only lazily returns the configuration.

    This test combines multiple tests because Python does not support unloading a module:
    https://stackoverflow.com/questions/8781257/remove-an-imported-python-module

    TODO: This doesn't set `set_lazy_resolution(False)` which forces a resolution for any unresolved
    modules. In order to add more tests like so, we'd need to create more modules.
    """
    set_lazy_resolution(True)

    expected = ''
    add_config({'_tests.other_module.configured': HParams(arg=expected)})

    assert {} == get_config()
    assert logger_mock.warning.call_count == 1
    assert len(_get_skip_resolution()) == 1
    logger_mock.reset_mock()

    # NOTE: Test multiple similar config calls
    add_config({'_tests.other_module.configured': HParams(arg=expected)})
    assert len(_get_skip_resolution()) == 1

    from _tests.other_module import configured
    assert configured() == expected
    assert len(get_config()) == 1
    assert logger_mock.warning.call_count == 0
    assert len(_get_skip_resolution()) == 0
Ejemplo n.º 2
0
def test_config_operators():
    """ Test the `log_config`, `clear_config`, `add_config` and `get_config` together. It's
    difficult to test them alone.
    """
    @configurable
    def configured(arg):
        pass

    assert len(get_config()) == 0
    add_config({configured: HParams()})
    log_config()  # Smoke test
    assert len(get_config()) == 1
    clear_config()
    assert len(get_config()) == 0
Ejemplo n.º 3
0
def test_merge_configs():
    """ Test the merging of two configurations via `add_config`.
    """

    @configurable
    def configured(arg, arg_two):
        pass

    @configurable
    def other_configured(arg):
        pass

    clear_config()
    add_config({configured: HParams(arg='arg', arg_two='arg_two')})
    add_config({other_configured: HParams()})
    assert len(get_config()) == 2
    assert get_config()[_get_function_signature(configured.__wrapped__)]['arg'] == 'arg'
    add_config({configured: HParams(arg='gra')})
    assert len(get_config()) == 2
    assert get_config()[_get_function_signature(configured.__wrapped__)]['arg'] == 'gra'
    assert get_config()[_get_function_signature(configured.__wrapped__)]['arg_two'] == 'arg_two'
    clear_config()
Ejemplo n.º 4
0
def test_add_config__empty():
    """ Test if `add_config` works with an empty config. """
    clear_config()
    add_config({})
    assert {} == get_config()