File: test_session_dict.py

package info (click to toggle)
python-aiohttp-session 2.12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 496 kB
  • sloc: python: 2,534; makefile: 197
file content (160 lines) | stat: -rw-r--r-- 4,628 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import time
from typing import Any, MutableMapping, cast

import pytest

from aiohttp_session import Session, SessionData


def test_create() -> None:
    s = Session("test_identity", data=None, new=True)
    assert s == cast(MutableMapping[str, Any], {})
    assert s.new
    assert "test_identity" == s.identity
    assert not s._changed
    assert s.created is not None


def test_create2() -> None:
    s = Session("test_identity", data={"session": {"some": "data"}}, new=False)
    assert s == cast(MutableMapping[str, Any], {"some": "data"})
    assert not s.new
    assert "test_identity" == s.identity
    assert not s._changed
    assert s.created is not None


def test_create3() -> None:
    s = Session(identity=1, data=None, new=True)
    assert s == cast(MutableMapping[str, Any], {})
    assert s.new
    assert s.identity == 1
    assert not s._changed
    assert s.created is not None


def test_set_new_identity_ok() -> None:
    s = Session(identity=1, data=None, new=True)
    assert s.new
    assert s.identity == 1

    s.set_new_identity(2)
    assert s.new
    assert s.identity == 2


def test_set_new_identity_for_not_new_session() -> None:
    s = Session(identity=1, data=None, new=False)
    with pytest.raises(RuntimeError):
        s.set_new_identity(2)


def test__repr__() -> None:
    s = Session("test_identity", data=None, new=True)
    assert str(s) == "<Session [new:True, changed:False, created:{0}] {{}}>".format(
        s.created
    )
    s["foo"] = "bar"
    assert str(
        s
    ) == "<Session [new:True, changed:True, created:{0}]" " {{'foo': 'bar'}}>".format(
        s.created
    )


def test__repr__2() -> None:
    created = int(time.time()) - 1000
    session_data: SessionData = {"session": {"key": 123}, "created": created}
    s = Session("test_identity", data=session_data, new=False)
    assert str(
        s
    ) == "<Session [new:False, changed:False, created:{0}]" " {{'key': 123}}>".format(
        created
    )
    s.invalidate()
    assert str(s) == "<Session [new:False, changed:True, created:{0}] {{}}>".format(
        created
    )


def test_invalidate() -> None:
    s = Session("test_identity", data={"session": {"foo": "bar"}}, new=False)
    assert s == cast(MutableMapping[str, Any], {"foo": "bar"})
    assert not s._changed

    s.invalidate()
    assert s == cast(MutableMapping[str, Any], {})
    assert s._changed
    # Mypy bug: https://github.com/python/mypy/issues/11853
    assert s.created is not None  # type: ignore[unreachable]


def test_invalidate2() -> None:
    s = Session("test_identity", data={"session": {"foo": "bar"}}, new=False)
    assert s == cast(MutableMapping[str, Any], {"foo": "bar"})
    assert not s._changed

    s.invalidate()
    assert s == cast(MutableMapping[str, Any], {})
    assert s._changed
    # Mypy bug: https://github.com/python/mypy/issues/11853
    assert s.created is not None  # type: ignore[unreachable]


def test_operations() -> None:
    s = Session("test_identity", data=None, new=False)
    assert s == cast(MutableMapping[str, Any], {})
    assert len(s) == 0
    assert list(s) == []
    assert "foo" not in s
    assert "key" not in s

    s = Session("test_identity", data={"session": {"foo": "bar"}}, new=False)
    assert len(s) == 1
    assert s == cast(MutableMapping[str, Any], {"foo": "bar"})
    assert list(s) == ["foo"]
    assert "foo" in s
    assert "key" not in s

    s["key"] = "value"
    assert len(s) == 2
    assert s == cast(MutableMapping[str, Any], {"foo": "bar", "key": "value"})
    assert sorted(s) == ["foo", "key"]
    assert "foo" in s
    assert "key" in s

    del s["key"]
    assert len(s) == 1
    assert s == cast(MutableMapping[str, Any], {"foo": "bar"})
    assert list(s) == ["foo"]
    assert "foo" in s
    assert "key" not in s

    s.pop("foo")
    assert len(s) == 0
    assert s == cast(MutableMapping[str, Any], {})
    assert list(s) == []
    assert "foo" not in s
    assert "key" not in s


def test_change() -> None:
    created = int(time.time())
    s = Session(
        "test_identity",
        new=False,
        data={"session": {"a": {"key": "value"}}, "created": created},
    )
    assert not s._changed

    s["a"]["key2"] = "val2"
    assert not s._changed
    assert cast(MutableMapping[str, Any], {"a": {"key": "value", "key2": "val2"}}) == s

    assert s.created == created

    s.changed()
    assert s._changed
    # Mypy bug: https://github.com/python/mypy/issues/11853
    assert s.created == created  # type: ignore[unreachable]
    assert cast(MutableMapping[str, Any], {"a": {"key": "value", "key2": "val2"}}) == s