Ejemplo n.º 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
Ejemplo n.º 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}
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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'
        }
    }
Ejemplo n.º 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
Ejemplo n.º 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'
        },
    }
Ejemplo n.º 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
Ejemplo n.º 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'
    }
Ejemplo n.º 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
Ejemplo n.º 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',
            },
        }
    }
Ejemplo n.º 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',
            },
        }
    }
Ejemplo n.º 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': {}}
Ejemplo n.º 14
0
def test_trict_init_inits():
    tr = Trict(base_dict())  # pylint: disable=unused-variable
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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')
Ejemplo n.º 18
0
def test_repr():
    assert Trict({}).__repr__() == 'Trict({})'
Ejemplo n.º 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)
Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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']
Ejemplo n.º 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