-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtest_stash.py
69 lines (59 loc) · 1.83 KB
/
test_stash.py
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
from __future__ import annotations
from _pytest.stash import Stash
from _pytest.stash import StashKey
import pytest
def test_stash() -> None:
stash = Stash()
assert len(stash) == 0
assert not stash
key1 = StashKey[str]()
key2 = StashKey[int]()
# Basic functionality - single key.
assert key1 not in stash
stash[key1] = "hello"
assert key1 in stash
assert stash[key1] == "hello"
assert stash.get(key1, None) == "hello"
stash[key1] = "world"
assert stash[key1] == "world"
# Has correct type (no mypy error).
stash[key1] + "string"
assert len(stash) == 1
assert stash
# No interaction with another key.
assert key2 not in stash
assert stash.get(key2, None) is None
with pytest.raises(KeyError):
stash[key2]
with pytest.raises(KeyError):
del stash[key2]
stash[key2] = 1
assert stash[key2] == 1
# Has correct type (no mypy error).
stash[key2] + 20
del stash[key1]
with pytest.raises(KeyError):
del stash[key1]
with pytest.raises(KeyError):
stash[key1]
# setdefault
stash[key1] = "existing"
assert stash.setdefault(key1, "default") == "existing"
assert stash[key1] == "existing"
key_setdefault = StashKey[bytes]()
assert stash.setdefault(key_setdefault, b"default") == b"default"
assert stash[key_setdefault] == b"default"
assert len(stash) == 3
assert stash
# Can't accidentally add attributes to stash object itself.
with pytest.raises(AttributeError):
stash.foo = "nope" # type: ignore[attr-defined]
# No interaction with another stash.
stash2 = Stash()
key3 = StashKey[int]()
assert key2 not in stash2
stash2[key2] = 100
stash2[key3] = 200
assert stash2[key2] + stash2[key3] == 300
assert stash[key2] == 1
assert key3 not in stash