Пример #1
0
def test_repr():
    """
    repr(AttrDict)
    """
    from attrdict.dictionary import AttrDict

    assert_equals(repr(AttrDict()), "AttrDict({})")
    assert_equals(repr(AttrDict({'foo': 'bar'})), "AttrDict({'foo': 'bar'})")
    assert_equals(repr(AttrDict({1: {
        'foo': 'bar'
    }})), "AttrDict({1: {'foo': 'bar'}})")
    assert_equals(repr(AttrDict({1: AttrDict({'foo': 'bar'})})),
                  "AttrDict({1: AttrDict({'foo': 'bar'})})")
Пример #2
0
def test_copy():
    """
    Make a dict copy of an AttrDict.
    """
    from attrdict.dictionary import AttrDict

    mapping_a = AttrDict({'foo': {'bar': 'baz'}})
    mapping_b = mapping_a.copy()
    mapping_c = mapping_b

    mapping_b['foo']['lorem'] = 'ipsum'

    assert_equals(mapping_a, mapping_b)
    assert_equals(mapping_b, mapping_c)
Пример #3
0
def test_copy():
    """
    Make a dict copy of an AttrDict.
    """
    from attrdict.dictionary import AttrDict

    mapping_a = AttrDict({'foo': {'bar': 'baz'}})
    mapping_b = mapping_a.copy()
    mapping_c = mapping_b

    mapping_b['foo']['lorem'] = 'ipsum'

    assert_equals(mapping_a, mapping_b)
    assert_equals(mapping_b, mapping_c)
Пример #4
0
    def test_has_key():
        """
        The now-depricated has_keys method
        """
        from attrdict.dictionary import AttrDict

        assert_false(hasattr(AttrDict(), 'has_key'))
Пример #5
0
    def constructor(items=None, sequence_type=tuple):
        """
        Build a new AttrDict.
        """
        if items is None:
            items = {}

        return AttrDict._constructor(items, sequence_type)
Пример #6
0
    def constructor(items=None, sequence_type=tuple):
        """
        Build a new AttrDict.
        """
        if items is None:
            items = {}

        return AttrDict._constructor(items, sequence_type)
Пример #7
0
    def test_has_key():
        """
        The now-depricated has_keys method
        """
        from attrdict.dictionary import AttrDict

        mapping = AttrDict({"foo": "bar", frozenset((1, 2, 3)): "abc", 1: 2})
        empty = AttrDict()

        assert_true(mapping.has_key("foo"))
        assert_false(empty.has_key("foo"))

        assert_true(mapping.has_key(frozenset((1, 2, 3))))
        assert_false(empty.has_key(frozenset((1, 2, 3))))

        assert_true(mapping.has_key(1))
        assert_false(empty.has_key(1))

        assert_false(mapping.has_key("banana"))
        assert_false(empty.has_key("banana"))
Пример #8
0
def test_fromkeys():
    """
    make a new sequence from a set of keys.
    """
    from attrdict.dictionary import AttrDict

    # default value
    assert_equals(AttrDict.fromkeys(()), {})
    assert_equals(AttrDict.fromkeys({
        'foo': 'bar',
        'baz': 'qux'
    }), {
        'foo': None,
        'baz': None
    })
    assert_equals(AttrDict.fromkeys(('foo', 'baz')), {
        'foo': None,
        'baz': None
    })

    # custom value
    assert_equals(AttrDict.fromkeys((), 0), {})
    assert_equals(AttrDict.fromkeys({
        'foo': 'bar',
        'baz': 'qux'
    }, 0), {
        'foo': 0,
        'baz': 0
    })
    assert_equals(AttrDict.fromkeys(('foo', 'baz'), 0), {'foo': 0, 'baz': 0})
Пример #9
0
def test_fromkeys():
    """
    make a new sequence from a set of keys.
    """
    from attrdict.dictionary import AttrDict

    # default value
    assert_equals(AttrDict.fromkeys(()), {})
    assert_equals(
        AttrDict.fromkeys({'foo': 'bar', 'baz': 'qux'}),
        {'foo': None, 'baz': None}
    )
    assert_equals(
        AttrDict.fromkeys(('foo', 'baz')),
        {'foo': None, 'baz': None}
    )

    # custom value
    assert_equals(AttrDict.fromkeys((), 0), {})
    assert_equals(
        AttrDict.fromkeys({'foo': 'bar', 'baz': 'qux'}, 0),
        {'foo': 0, 'baz': 0}
    )
    assert_equals(
        AttrDict.fromkeys(('foo', 'baz'), 0),
        {'foo': 0, 'baz': 0}
    )
Пример #10
0
def test_init():
    """
    Create a new AttrDict.
    """
    from attrdict.dictionary import AttrDict

    # empty
    assert_equals(AttrDict(), {})
    assert_equals(AttrDict(()), {})
    assert_equals(AttrDict({}), {})

    # with items
    assert_equals(AttrDict({'foo': 'bar'}), {'foo': 'bar'})
    assert_equals(AttrDict((('foo', 'bar'), )), {'foo': 'bar'})
    assert_equals(AttrDict(foo='bar'), {'foo': 'bar'})

    # non-overlapping
    assert_equals(AttrDict({}, foo='bar'), {'foo': 'bar'})
    assert_equals(AttrDict((), foo='bar'), {'foo': 'bar'})

    assert_equals(AttrDict({'alpha': 'bravo'}, foo='bar'), {
        'foo': 'bar',
        'alpha': 'bravo'
    })

    assert_equals(AttrDict((('alpha', 'bravo'), ), foo='bar'), {
        'foo': 'bar',
        'alpha': 'bravo'
    })

    # updating
    assert_equals(AttrDict({'alpha': 'bravo'}, foo='bar', alpha='beta'), {
        'foo': 'bar',
        'alpha': 'beta'
    })

    assert_equals(AttrDict((('alpha', 'bravo'), ('alpha', 'beta')), foo='bar'),
                  {
                      'foo': 'bar',
                      'alpha': 'beta'
                  })

    assert_equals(
        AttrDict((('alpha', 'bravo'), ('alpha', 'beta')), alpha='bravo'),
        {'alpha': 'bravo'})
Пример #11
0
    def test_has_key():
        """
        The now-depricated has_keys method
        """
        from attrdict.dictionary import AttrDict

        mapping = AttrDict(
            {'foo': 'bar', frozenset((1, 2, 3)): 'abc', 1: 2}
        )
        empty = AttrDict()

        assert_true(mapping.has_key('foo'))
        assert_false(empty.has_key('foo'))

        assert_true(mapping.has_key(frozenset((1, 2, 3))))
        assert_false(empty.has_key(frozenset((1, 2, 3))))

        assert_true(mapping.has_key(1))
        assert_false(empty.has_key(1))

        assert_false(mapping.has_key('banana'))
        assert_false(empty.has_key('banana'))