コード例 #1
0
def test_context_update(spf):
    context = ContextDict(spf, None)
    child_context = context.create_child_context()
    context['t1'] = "hello world"
    child_context['t2'] = "hello2"
    assert child_context['t1'] == "hello world"
    child_context.update({'t1': "test1", 't2': "test2"})
    assert context['t1'] == "test1"
    assert child_context['t2'] == "test2"
コード例 #2
0
def test_context_replace(spf):
    context = ContextDict(spf, None)
    child_context = context.create_child_context()
    context['t1'] = "hello world"
    assert child_context['t1'] == "hello world"
    child_context['t1'] = "goodbye world"
    assert context['t1'] != "goodbye world"
    del(child_context['t1'])
    child_context.replace('t1', 'goodbye world')
    assert context['t1'] == "goodbye world"
コード例 #3
0
def test_context_pickle(spf):
    context = ContextDict(spf, None)
    child_context = context.create_child_context()
    child_context['t1'] = "hello world"
    p_bytes = pickle.dumps(child_context)
    un_p = pickle.loads(p_bytes)
    # the spf and the parent context are not the same as before, because
    # their state got pickled and unpicked too
    assert un_p._spf != spf
    assert un_p._parent_context != context
    assert un_p['t1'] == "hello world"
コード例 #4
0
def test_recursive_dict(spf):
    context = ContextDict(spf, None)
    context['t1'] = "hello world"
    c2 = context.create_child_context()
    c2['t2'] = "hello 2"
    c3 = c2.create_child_context()
    c3['t3'] = "hello 3"
    context._parent_context = c3  # This is dodgy, why would anyone do this?
    exceptions = []
    try:
        _ = c2['t4']
    except RuntimeError as e:
        assert len(e.args) > 0
        assert "recursive" in str(e.args[0]).lower()
        exceptions.append(e)
    finally:
        assert len(exceptions) == 1