Esempio n. 1
0
def test_lazy_accessor_deepupdate_nested(context):
    log_context.remove('rid')

    from pylogctx import LazyAccessor

    class MyObject(object):
        value = 'foo'

        def __repr__(self):
            return 'foo'

    instance = MyObject()
    lazy_instance = LazyAccessor(instance, 'value')

    log_context.update(parent={"lazy_instance": lazy_instance,
                               "foo": "bar"})

    fields = log_context.as_dict()
    assert str(fields['parent']['lazy_instance']) == 'foo'

    # Check value change for LazyAccessor
    instance.value = 'bar'

    fields = log_context.as_dict()
    assert str(fields['parent']['lazy_instance']) == 'bar'
Esempio n. 2
0
def test_deep_update_dict(context):
    log_context.remove('rid')

    # Value to dict update override
    log_context.update(myField={'toto': {'tata1': {'titi1': 'tutu'}}})
    fields = log_context.as_dict()
    assert fields == {'myField': {'toto': {'tata1': {'titi1': 'tutu'}}}}

    # Update tata1 to add titi2
    log_context.update(myField={'toto': {'tata1': {'titi2': 'tutu'}}})
    fields = log_context.as_dict()
    assert fields == {
        'myField': {
            'toto': {
                'tata1': {
                    'titi1': 'tutu',
                    'titi2': 'tutu'
                }
            }
        }
    }

    # Override value `tata1/titi1`
    log_context.update(myField={'toto': {'tata1': {'titi1': 'val'}}})
    fields = log_context.as_dict()
    assert fields == {
        'myField': {
            'toto': {
                'tata1': {
                    'titi1': 'val',
                    'titi2': 'tutu'
                }
            }
        }
    }
Esempio n. 3
0
def test_update_clear_remove(context):
    log_context.update(myField='toto', myOtherField='titi')
    fields = log_context.as_dict()
    assert 'myField' in fields
    assert 'myOtherField' in fields

    log_context.remove('myOtherField')
    fields = log_context.as_dict()
    assert 'myField' in fields
    assert 'myOtherField' not in fields

    log_context.clear()
    fields = log_context.as_dict()
    assert 'myField' not in fields
Esempio n. 4
0
def test_update_clear_remove(context):
    log_context.update(myField='toto', myOtherField='titi')
    fields = log_context.as_dict()
    assert 'myField' in fields
    assert 'myOtherField' in fields

    log_context.remove('myOtherField')
    fields = log_context.as_dict()
    assert 'myField' in fields
    assert 'myOtherField' not in fields

    log_context.clear()
    fields = log_context.as_dict()
    assert 'myField' not in fields
Esempio n. 5
0
def test_deep_update_dict(context):
    log_context.remove('rid')

    # Value to dict update override
    log_context.update(myField={'toto': {'tata1': {'titi1': 'tutu'}}})
    fields = log_context.as_dict()
    assert fields == {'myField': {'toto': {'tata1': {'titi1': 'tutu'}}}}

    # Update tata1 to add titi2
    log_context.update(myField={'toto': {'tata1': {'titi2': 'tutu'}}})
    fields = log_context.as_dict()
    assert fields == {
        'myField': {'toto': {'tata1': {'titi1': 'tutu', 'titi2': 'tutu'}}}}

    # Override value `tata1/titi1`
    log_context.update(myField={'toto': {'tata1': {'titi1': 'val'}}})
    fields = log_context.as_dict()

    assert fields == {
        'myField': {'toto': {'tata1': {'titi1': 'val',
                                       'titi2': 'tutu'}}}}
Esempio n. 6
0
def test_deep_update_dict_context_manger(context):
    log_context.remove('rid')
    with log_context(myField={'toto': {'tata1': {'titi1': 'tutu'}}}):
        fields = log_context.as_dict()
        assert fields == {'myField': {'toto': {'tata1': {'titi1': 'tutu'}}}}

        # Update tata1 to add titi2
        with log_context(myField={'toto': {'tata1': {'titi2': 'tutu'}}}):
            fields = log_context.as_dict()
            assert fields == {
                'myField': {
                    'toto': {'tata1': {'titi1': 'tutu', 'titi2': 'tutu'}}}}

            # Override value `tata1/titi1`
            with log_context(myField={'toto': {'tata1': {'titi1': 'val'}}}):
                fields = log_context.as_dict()
                assert fields == {
                    'myField': {'toto': {'tata1': {'titi1': 'val',
                                                   'titi2': 'tutu'}}}}

    fields = log_context.as_dict()
    assert 'myField' not in fields
Esempio n. 7
0
def test_lazy_accessor_deepupdate_nested(context):
    log_context.remove('rid')

    from pylogctx import LazyAccessor

    class MyObject(object):
        value = 'foo'

        def __repr__(self):
            return 'foo'

    instance = MyObject()
    lazy_instance = LazyAccessor(instance, 'value')

    log_context.update(parent={"lazy_instance": lazy_instance, "foo": "bar"})

    fields = log_context.as_dict()
    assert str(fields['parent']['lazy_instance']) == 'foo'

    # Check value change for LazyAccessor
    instance.value = 'bar'

    fields = log_context.as_dict()
    assert str(fields['parent']['lazy_instance']) == 'bar'
Esempio n. 8
0
def test_deep_update_dict_context_manger(context):
    log_context.remove('rid')
    with log_context(myField={'toto': {'tata1': {'titi1': 'tutu'}}}):
        fields = log_context.as_dict()
        assert fields == {'myField': {'toto': {'tata1': {'titi1': 'tutu'}}}}

        # Update tata1 to add titi2
        with log_context(myField={'toto': {'tata1': {'titi2': 'tutu'}}}):
            fields = log_context.as_dict()
            assert fields == {
                'myField': {
                    'toto': {
                        'tata1': {
                            'titi1': 'tutu',
                            'titi2': 'tutu'
                        }
                    }
                }
            }

            # Override value `tata1/titi1`
            with log_context(myField={'toto': {'tata1': {'titi1': 'val'}}}):
                fields = log_context.as_dict()
                assert fields == {
                    'myField': {
                        'toto': {
                            'tata1': {
                                'titi1': 'val',
                                'titi2': 'tutu'
                            }
                        }
                    }
                }

    fields = log_context.as_dict()
    assert 'myField' not in fields