예제 #1
0
def test_dunder_get():
    assert dunder_get({'a': {'b': 5}}, 'a__b') == 5
    assert dunder_get({'a': {'b': 8, 'c': {'d': 8}}}, 'a__c__d') == 8
    assert dunder_get([1, 2, 3, [4, 5, [6]]], '3__1') == 5

    class B:
        c = 5

    class A:
        b = B

    assert dunder_get(A, 'b__c') == 5

    with Document() as d:
        d.tags['a'] = 'hello'
        assert dunder_get(d, 'tags__a') == 'hello'

    # Error on invalid key

    assert dunder_get({'a': {'b': 5}}, 'a__c') is None
    # Error if key is too nested
    with pytest.raises(Exception):
        dunder_get({'a': {'b': 5}, 'c': 8}, 'a__b__c')
    # Error using str keys on list
    with pytest.raises(Exception):
        dunder_get([[1, 2], [3, 4]], 'a')
예제 #2
0
def test_dunder_get():
    a = SimpleNamespace()
    a.b = {'c': 1}
    assert dunder_get(a, 'b__c') == 1