Exemple #1
0
def test_update():
    a = Diot(**test_dict)
    a.grand = 1000
    a.update({'key1': {'new': 5}, 'Key 2': {"add_key": 6}, 'lister': ['a']})
    a.update([('asdf', 'fdsa')])
    a.update(testkey=66)
    a.update({'items': {'test': 'pme'}})
    a.update({'key1': {'gg': 4}})
    b = Diot(diot_nest=[list, dict])
    b.update(item=1)

    assert a.grand == 1000
    assert a['grand'] == 1000
    assert isinstance(a['items'], Diot)
    assert a['items'].test == 'pme'
    assert a['Key 2'].add_key == 6
    assert isinstance(a.key1, Diot)
    assert isinstance(a.lister, list)
    assert a.asdf == 'fdsa'
    assert a.testkey == 66
    assert a.key1.new == 5  # On regular dict update this shouldn't happen
    assert a.key1.gg == 4

    c = Diot(diot_nest=[dict])
    c.a = [1, 2]
    c.update({'b': [3, 4]})

    assert c.a == [1, 2]
    assert isinstance(c.b, list)
Exemple #2
0
def test_frozen_modify():
    d = Diot(a=1, b=2, diot_frozen=True)
    with pytest.raises(DiotFrozenError):
        d.a = 2

    with pytest.raises(DiotFrozenError):
        d["a"] = 2

    with pytest.raises(DiotFrozenError):
        d.pop("a")

    with pytest.raises(DiotFrozenError):
        d.popitem()

    with pytest.raises(DiotFrozenError):
        d.update({})

    with pytest.raises(DiotFrozenError):
        d |= {}

    with pytest.raises(DiotFrozenError):
        del d["item"]

    with pytest.raises(DiotFrozenError):
        d.setdefault("c", 3)

    with pytest.raises(DiotFrozenError):
        d.clear()

    d2 = d.copy()
    with pytest.raises(DiotFrozenError):
        d2.clear()

    d2.unfreeze()
    d2.c = 3
    assert d2.c == 3

    d2.freeze()
    with pytest.raises(DiotFrozenError):
        d2.d = 4