Пример #1
0
def test__resolve_configuration__multiple_configurations():
    """ Test resolution for with multiple configurations. """
    @configurable
    def configured(arg):
        pass

    class Configured:
        @configurable
        def __init__(self):
            pass

    parsed = _parse_configuration({
        'tests.test_hparams.test__resolve_configuration__multiple_configurations':
        {
            '<locals>.Configured.__init__': HParams(),
            '<locals>.configured': HParams()
        }
    })
    resolved = _resolve_configuration(parsed)

    assert isinstance(
        resolved[_get_function_signature(Configured.__init__.__wrapped__)],
        HParams)
    assert isinstance(
        resolved[_get_function_signature(configured.__wrapped__)], HParams)
    assert len(resolved) == 2
Пример #2
0
def test__get_function_signature__no_sys_path():
    """ Test to ensure that `_get_function_signature` can handle an absolute path. """
    original = sys.path
    sys.path = []
    assert 'tests.test_hparams.test__get_function_signature' in _get_function_signature(
        test__get_function_signature__no_sys_path)
    sys.path = original
Пример #3
0
def test__resolve_configuration__configured_hparam():
    """ Test resolution for a configured `HParam`. """
    parsed = _parse_configuration(
        {_test__resolve_configuration__configured_hparam: HParams(arg=None)})
    function_signature = _get_function_signature(
        _test__resolve_configuration__configured_hparam.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
Пример #4
0
def test__resolve_configuration__external_internal_library():
    """ Test resolution for an internal api of an external library. """
    _pytest.python_api.approx = configurable(_pytest.python_api.approx)
    parsed = _parse_configuration({'pytest.approx': HParams(expected='')})
    function_signature = _get_function_signature(pytest.approx.__wrapped__)
    assert isinstance(
        _resolve_configuration(parsed)[function_signature], HParams)
Пример #5
0
def test__resolve_configuration__external_library():
    """ Test resolution for an external library and with a none-empty `HParams`. """
    pytest.approx = configurable(pytest.approx)
    parsed = _parse_configuration({'pytest.approx': HParams(expected='')})
    function_signature = _get_function_signature(pytest.approx.__wrapped__)
    assert isinstance(
        _resolve_configuration(parsed)[function_signature], HParams)
Пример #6
0
def test__resolve_configuration__built_in_function():
    """ Test resolution for an built in function. """
    builtins.open = configurable(builtins.open)
    function_name = 'builtins.open'
    parsed = _parse_configuration({function_name: HParams()})
    function_signature = _get_function_signature(builtins.open.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
Пример #7
0
def test__resolve_configuration__lambda_function():
    """ Test resolution for an lambda function. """
    configured = configurable(lambda: None)
    function_name = ('tests.test_hparams.test__resolve_configuration__lambda_function'
                     '.<locals>.<lambda>')
    parsed = _parse_configuration({function_name: HParams()})
    function_signature = _get_function_signature(configured.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
Пример #8
0
def test__get_function_signature__relative_sys_path():
    """ Test to ensure that `_get_function_signature` can handle an relatie path. """
    original = sys.path
    sys.path = ['']
    assert 'tests.test_hparams.test__get_function_signature__relative_sys_path' == (
        _get_function_signature(
            test__get_function_signature__relative_sys_path))
    sys.path = original
Пример #9
0
def test__resolve_configuration__multiple_sys_path():
    """ Test resolution for multiple `sys` path. """
    sys.path = [os.path.dirname(__file__)] + sys.path
    function_name = 'test_hparams._test__resolve_configuration__multiple_sys_path'
    parsed = _parse_configuration({function_name: HParams()})
    function_signature = _get_function_signature(
        _test__resolve_configuration__multiple_sys_path.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
    sys.path.pop(0)
Пример #10
0
def test__resolve_configuration__no_sys_path():
    """ Test resolution with no `sys` path. """
    original = sys.path
    sys.path = []
    parsed = _parse_configuration(
        {_test__resolve_configuration__no_sys_path.__wrapped__: HParams()})
    function_signature = _get_function_signature(
        _test__resolve_configuration__no_sys_path.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
    sys.path = original
Пример #11
0
def test__resolve_configuration__internal_function():
    """ Test resolution for an internal function. """

    @configurable
    def configured(arg):
        pass

    function_name = ('tests.test_hparams.test__resolve_configuration__internal_function'
                     '.<locals>.configured')
    parsed = _parse_configuration({function_name: HParams()})
    function_signature = _get_function_signature(configured.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
Пример #12
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()
Пример #13
0
def test__resolve_configuration__internal_class():
    """ Test resolution for an internal class. """

    class Configured:

        @configurable
        def __init__(self):
            pass

    function_name = ('tests.test_hparams.test__resolve_configuration__internal_class.'
                     '<locals>.Configured.__init__')
    parsed = _parse_configuration({function_name: HParams()})
    function_signature = _get_function_signature(Configured.__init__.__wrapped__)
    assert isinstance(_resolve_configuration(parsed)[function_signature], HParams)
Пример #14
0
def test__get_function_signature():
    assert _get_function_signature(
        test__get_function_signature) == 'tests.test_hparams.test__get_function_signature'