aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_stateobject.py
blob: b9ffe7ae682ddb623c50698dc82f11674946e167 (plain)
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
from typing import List

from mitmproxy.stateobject import StateObject


class Child(StateObject):
    def __init__(self, x):
        self.x = x

    _stateobject_attributes = dict(
        x=int
    )

    @classmethod
    def from_state(cls, state):
        obj = cls(None)
        obj.set_state(state)
        return obj


class Container(StateObject):
    def __init__(self):
        self.child = None
        self.children = None

    _stateobject_attributes = dict(
        child=Child,
        children=List[Child],
    )

    @classmethod
    def from_state(cls, state):
        obj = cls()
        obj.set_state(state)
        return obj


def test_simple():
    a = Child(42)
    b = a.copy()
    assert b.get_state() == {"x": 42}
    a.set_state({"x": 44})
    assert a.x == 44
    assert b.x == 42


def test_container():
    a = Container()
    a.child = Child(42)
    b = a.copy()
    assert a.child.x == b.child.x
    b.child.x = 44
    assert a.child.x != b.child.x


def test_container_list():
    a = Container()
    a.children = [Child(42), Child(44)]
    assert a.get_state() == {
        "child": None,
        "children": [{"x": 42}, {"x": 44}]
    }
    assert len(a.copy().children) == 2