test_config.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:fleaker 作者: croscon 项目源码 文件源码
def test_config_post_configure_callbacks():
    """Ensure post configure callbacks work."""
    app = MultiStageConfigurableApp.create_app('tests')
    app.config['CANARY'] = True

    configure_args = {
        'FOO': 'bar',
    }

    def evil_cb(cfg, cfg_args):
        """This fella tries to change the config so we can make sure we pass
        around a frozen config.
        """
        # this is the only way you can change the config from here
        app.config['BAD_VAL'] = True

    def small_cb(cfg, cfg_args):
        """Ensure that we get the right arguments to our callbacks."""
        assert cfg['CANARY']
        assert cfg['FOO'] == 'bar'
        assert cfg_args == (configure_args,)
        assert 'BAD_VAL' not in cfg

    # we need the proper arguments to the call assertions below, so construct
    # them.
    expected_config = app.config.copy()
    expected_config['FOO'] = 'bar'
    expected_config = ImmutableDict(expected_config)

    # make sure we can count calls and call order
    small_cb = mock.MagicMock(wraps=small_cb)
    evil_cb = mock.MagicMock(wraps=evil_cb)
    parent_mock = mock.Mock()
    parent_mock.m1, parent_mock.m2 = small_cb, evil_cb

    assert not app._post_configure_callbacks['single']
    assert not app._post_configure_callbacks['multiple']

    app.add_post_configure_callback(evil_cb)
    app.add_post_configure_callback(small_cb)

    assert len(app._post_configure_callbacks) == 2

    app.configure(configure_args)

    # ensure we called the right number of times in the right order
    assert small_cb.call_count == 1
    assert evil_cb.call_count == 1
    parent_mock.assert_has_calls([
        mock.call.m2(expected_config, (configure_args,)),
        mock.call.m1(expected_config, (configure_args,)),
    ])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号