File: test_state.py

package info (click to toggle)
sbws 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,084 kB
  • sloc: python: 10,432; sh: 146; makefile: 38
file content (103 lines) | stat: -rw-r--r-- 2,522 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os

try:
    from datetime import UTC
except ImportError:
    from datetime import timezone

    UTC = timezone.utc

from sbws.util.state import State

# from tempfile import NamedTemporaryFile as NTF


def test_state_set_allowed_key_types(tmpdir):
    state = State(os.path.join(str(tmpdir), "statefoo"))
    attempt_keys = "k"
    for key in attempt_keys:
        state[key] = 4
        assert state[key] == 4


def test_state_set_allowed_value_types(tmpdir):
    state = State(os.path.join(str(tmpdir), "statefoo"))
    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):
    state = State(os.path.join(str(tmpdir), "statefoo"))
    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):
    state = State(os.path.join(str(tmpdir), "statefoo"))
    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):
    state = State(os.path.join(str(tmpdir), "statefoo"))
    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):
    state = State(os.path.join(str(tmpdir), "statefoo"))
    for key in state:
        pass
    d = {"a": 1, "b": 2, "c": 3, "d": 4}
    for key in d:
        state[key] = d[key]
    assert set([key for key in state]) == set(d)


def test_two_instances(tmpdir):
    """Test that 2 different instances 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"


def test_datetime_values(tmpdir):
    import datetime

    state = State(os.path.join(str(tmpdir), "state.dat"))
    now = datetime.datetime.now(UTC).replace(microsecond=0)
    state["datetimes"] = now
    assert now.replace(tzinfo=None) == state["datetimes"]