GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

test_state.py 2.1 KB
Newer Older
1 2 3 4 5 6
from sbws.util.state import State
import os
# from tempfile import NamedTemporaryFile as NTF


def test_state_set_allowed_key_types(tmpdir):
7
    state = State(os.path.join(str(tmpdir), 'statefoo'))
8 9 10 11 12 13 14
    attempt_keys = ('k')
    for key in attempt_keys:
        state[key] = 4
        assert state[key] == 4


def test_state_set_allowed_value_types(tmpdir):
15
    state = State(os.path.join(str(tmpdir), 'statefoo'))
16 17 18 19 20 21 22
    attempt_vals = (15983, None, True, -1.2, 'loooooool')
    for val in attempt_vals:
        state['foo'] = val
        assert state['foo'] == val


def test_state_del(tmpdir):
23
    state = State(os.path.join(str(tmpdir), 'statefoo'))
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    for key in d:
        state[key] = d[key]
    assert len(state) == len(d)

    del d['a']
    del state['a']
    assert len(state) == len(d)
    for key in d:
        assert d[key] == state[key]

    d['e'] = 5
    state['e'] = 5
    d['e'] = 5.5
    state['e'] = 5.5
    assert len(state) == len(d)


def test_state_get_len(tmpdir):
43
    state = State(os.path.join(str(tmpdir), 'statefoo'))
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    for key in d:
        state[key] = d[key]
    assert len(state) == len(d)

    del d['a']
    del state['a']
    assert len(state) == len(d)

    d['e'] = 5
    state['e'] = 5
    d['e'] = 5.5
    state['e'] = 5.5
    assert len(state) == len(d)


def test_state_contains(tmpdir):
61
    state = State(os.path.join(str(tmpdir), 'statefoo'))
62 63 64 65 66 67 68 69
    d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    for key in d:
        state[key] = d[key]
    assert 'a' in state
    assert 'e' not in state


def test_state_iter(tmpdir):
70
    state = State(os.path.join(str(tmpdir), 'statefoo'))
71 72 73 74 75
    for key in state:
        pass
    d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    for key in d:
        state[key] = d[key]
76
    assert set([key for key in state]) == set(d)
77 78 79 80 81 82 83 84 85


def test_two_instances(tmpdir):
    """Test that 2 different intances don't overwrite each other"""
    s1 = State(os.path.join(str(tmpdir), 'state.dat'))
    s2 = State(os.path.join(str(tmpdir), 'state.dat'))
    s1["x"] = "foo"
    s2["y"] = "bar"
    assert s2["x"] == "foo"