Beispiel #1
0
def test_traverse_traverses():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    assert [n for n in tr.traverse()] == [
        (['user'], {
            'information': {
                'attribute': 'infonugget',
                'another_attribute': 'secondnugget'
            },
            'moreinformation': 'extranugget'
        }),
        (['user', 'information'], {
            'attribute': 'infonugget',
            'another_attribute': 'secondnugget'
        }), (['user', 'information', 'attribute'], 'infonugget'),
        (['user', 'information', 'another_attribute'], 'secondnugget'),
        (['user', 'moreinformation'], 'extranugget')
    ]
    assert tr.data == old_data
    assert [k for k in tr.traverse(keys_only=True)] == [
        ['user'],
        ['user', 'information'],
        ['user', 'information', 'attribute'],
        ['user', 'information', 'another_attribute'],
        ['user', 'moreinformation'],
    ]
    assert tr.data == old_data
Beispiel #2
0
def test_nonstrict_map_with_dict_not_throws():
    tr = Trict(base_dict())
    mapper = {
        'newkey': [['none', 'of', 'these'], ['are', 'in', 'there']],
        'othernewkey': ['and.these.are', 'not.either']
    }
    mapped = tr.map_with_dict(mapper)
    assert mapped == {'newkey': None, 'othernewkey': None}
Beispiel #3
0
def test_leaves():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    assert [l for l in tr.leaves()] == [
        (['user', 'information', 'attribute'], 'infonugget'),
        (['user', 'information', 'another_attribute'], 'secondnugget'),
        (['user', 'moreinformation'], 'extranugget')
    ]
    assert tr.data == old_data
Beispiel #4
0
def test_flatten_flats():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    assert tr.flatten() == {
        'user.information.attribute': 'infonugget',
        'user.information.another_attribute': 'secondnugget',
        'user.moreinformation': 'extranugget'
    }
    assert tr.data == old_data
Beispiel #5
0
def test_map_leaves_maps():
    tr = Trict(base_dict())
    tr.map_leaves(lambda x: x + ' and something more')
    assert tr.data == {
        'user': {
            'information': {
                'attribute': 'infonugget and something more',
                'another_attribute': 'secondnugget and something more'
            },
            'moreinformation': 'extranugget and something more'
        }
    }
Beispiel #6
0
def test_get_by_seq_finds():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    val = tr.get_by_seq([
        'user.misinformation.attribute', 'nonuser.noninformation.nonattribute',
        ['user', 'information', 'attribute']
    ])
    assert val == 'infonugget'
    assert tr.data == old_data
    val = tr.get_by_seq([['user', 'information', 'whatattribute'],
                         ['user', 'information', 'nonattribute'],
                         'user.information.another_attribute'])
    assert val == 'secondnugget'
    assert tr.data == old_data
Beispiel #7
0
def test_map_with_dict_maps():
    tr = Trict(base_dict())
    mapper = {
        'newkey': [
            'user.noninformation.nonattribute',
            'user.information.another_attribute'
        ],
        'othernewkey': [['user', 'noninformation'], ['user', 'information']]
    }
    assert tr.map_with_dict(mapper) == {
        'newkey': 'secondnugget',
        'othernewkey': {
            'attribute': 'infonugget',
            'another_attribute': 'secondnugget'
        },
    }
Beispiel #8
0
def test_strict_trict_getter_throws():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    with pytest.raises(KeyError):
        tr['user.information.notanattribute']
    with pytest.raises(KeyError):
        tr[['user', 'information', 'notanattribute']]
    assert tr.data == old_data
Beispiel #9
0
def test_from_flat_dict_with_nested_dict_inits():
    d = {'info.attribute': {'nugget'}, 'otherinfo': 'secondnugget'}
    tr = Trict.from_flat_dict(d)
    assert tr.data == {
        'info': {
            'attribute': {'nugget'}
        },
        'otherinfo': 'secondnugget'
    }
Beispiel #10
0
def test_trict_contains():
    tr = Trict(base_dict())
    for key in [
            'user.information', 'user.information.another_attribute',
        ['user', 'moreinformation']
    ]:
        assert key in tr

    for key in ['information.attribute', 'doesntexist', ['nothing', 'here']]:
        assert key not in tr
Beispiel #11
0
def test_trict_setter_with_tuple_key_sets():
    tr = Trict(base_dict())
    tr[('user', 'hyperinformation', 'hyperattribute')] = 'hyper'
    assert tr.data == {
        'user': {
            'information': {
                'attribute': 'infonugget',
                'another_attribute': 'secondnugget'
            },
            'moreinformation': 'extranugget',
            'hyperinformation': {
                'hyperattribute': 'hyper',
            },
        }
    }
Beispiel #12
0
def test_trict_setter_sets():
    tr = Trict(base_dict())
    tr['user.superinformation.superattribute'] = 'super'
    tr[['user', 'hyperinformation', 'hyperattribute']] = 'hyper'
    assert tr.data == {
        'user': {
            'information': {
                'attribute': 'infonugget',
                'another_attribute': 'secondnugget'
            },
            'moreinformation': 'extranugget',
            'superinformation': {
                'superattribute': 'super',
            },
            'hyperinformation': {
                'hyperattribute': 'hyper',
            },
        }
    }
    tr['user.information.attribute'] = 'differentnugget'
    tr[['user', 'information', 'another_attribute']] = 'thirdnugget'
    assert tr.data == {
        'user': {
            'information': {
                'attribute': 'differentnugget',
                'another_attribute': 'thirdnugget'
            },
            'moreinformation': 'extranugget',
            'superinformation': {
                'superattribute': 'super',
            },
            'hyperinformation': {
                'hyperattribute': 'hyper',
            },
        }
    }
Beispiel #13
0
def test_trict_deleter_deletes():
    tr = Trict(base_dict())
    del tr['user.information']
    assert tr.data == {'user': {'moreinformation': 'extranugget'}}
    del tr[['user', 'moreinformation']]
    assert tr.data == {'user': {}}
Beispiel #14
0
def test_trict_init_inits():
    tr = Trict(base_dict())  # pylint: disable=unused-variable
Beispiel #15
0
def test_trict_init_throws():
    with pytest.raises(ValueError, match='key_sep found in key attr.ibute'):
        tr = Trict(invalid_base_dict())  # pylint: disable=unused-variable
Beispiel #16
0
def test_nonstrict_trict_getter_not_throws():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    assert tr.get('user.information.notanattribute') == None
    assert tr.get(['user', 'information', 'notanattribute']) == None
    assert tr.data == old_data
Beispiel #17
0
def test_key_to_seq():
    tr = Trict({})
    assert tr.key_to_seq('i.j.k') == ['i', 'j', 'k']
    assert tr.key_to_seq(['i', 'j']) == ['i', 'j']
    assert tr.key_to_seq(('i', 'j')) == ('i', 'j')
Beispiel #18
0
def test_repr():
    assert Trict({}).__repr__() == 'Trict({})'
Beispiel #19
0
def test_strict_get_by_seq_throws():
    tr = Trict(base_dict())
    with pytest.raises(KeyError):
        tr.get_by_seq(['none.of.these', 'i.mean.none', ['keys', 'exist']],
                      strict=True)
Beispiel #20
0
def test_from_flat_dict_inits():
    tr = Trict(base_dict())
    flat = tr.flatten()
    new_tr = Trict.from_flat_dict(flat)
    assert new_tr.data == tr.data
Beispiel #21
0
def test_map_with_dict_throws():
    tr = Trict(base_dict())
    mapper = {'newkey': [['none', 'of', 'these'], ['are', 'in', 'there']]}
    with pytest.raises(KeyError):
        tr.map_with_dict(mapper, strict=True)
Beispiel #22
0
def test_trict_getter_with_tuple_key_gets():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    assert tr[('user', 'information', 'attribute')] == 'infonugget'
    assert tr.data == old_data
Beispiel #23
0
def test_nonstrict_get_by_seq_not_throws():
    tr = Trict(base_dict())
    val = tr.get_by_seq(['none.of.these', 'i.mean.none', ['keys', 'exist']])
    assert val == None
Beispiel #24
0
def test_trict_deleter_throws():
    tr = Trict(base_dict())
    with pytest.raises(KeyError):
        del tr['user.information.nonexistantattribute']
    with pytest.raises(KeyError):
        del tr['user.information.nonexistantattribute']
Beispiel #25
0
def test_trict_getter_gets():
    tr = Trict(base_dict())
    old_data = copy.deepcopy(tr.data)
    assert tr['user.information.attribute'] == 'infonugget'
    assert tr[['user', 'information', 'attribute']] == 'infonugget'
    assert tr.data == old_data