-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage_test.py
98 lines (76 loc) · 2.9 KB
/
storage_test.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
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
from dataclasses import dataclass
import pytest
# test only LocalStorage for now, figure out DiscordStorage later
from snakecore.storage import LocalStorage
from snakecore.exceptions import StorageException
@dataclass
class StorageRecords:
TEST1 = ("test1", list[str])
TEST2 = ("test2", dict[int, tuple[str, int]])
def test_local_storage_args():
with pytest.raises(TypeError):
# no args is type error
LocalStorage()
with pytest.raises(TypeError):
# too many args is type error
LocalStorage("abc", dict, None)
@pytest.mark.asyncio
async def test_local_storage():
# access without lock should error
with pytest.raises(StorageException):
LocalStorage(*StorageRecords.TEST1).obj
with pytest.raises(StorageException):
LocalStorage(*StorageRecords.TEST1).obj = ["c"]
with pytest.raises(StorageException):
del LocalStorage(*StorageRecords.TEST1).obj
async with LocalStorage(*StorageRecords.TEST1) as storage:
# always true for LocalStorage
assert storage.is_init
# default value
assert storage.obj == []
storage.obj = ["a", "b"]
assert storage.obj == ["a", "b"]
storage.obj.append("c")
# always true for LocalStorage even without lock
assert storage.is_init
# access after released lock should error
with pytest.raises(StorageException):
storage.obj
with pytest.raises(StorageException):
storage.obj = ["c"]
with pytest.raises(StorageException):
del storage.obj
async with LocalStorage(*StorageRecords.TEST1) as storage:
# retain old value
assert storage.obj == ["a", "b", "c"]
del storage.obj
# should be already deleted
with pytest.raises(AttributeError):
storage.obj
# should be already deleted
with pytest.raises(AttributeError):
del storage.obj
async with LocalStorage(*StorageRecords.TEST1) as storage:
# back to default value after delete
assert storage.obj == []
storage.obj = ["c", "d"]
del storage.obj
# test assign after del, should work
storage.obj = ["e", "f"]
assert storage.obj == ["e", "f"]
@pytest.mark.asyncio
async def test_local_storage_nested_use():
async with LocalStorage(*StorageRecords.TEST1) as storage:
# retained old value
assert storage.obj == ["e", "f"]
# test holding new lock for different Storage within lock to another Storage
async with LocalStorage(*StorageRecords.TEST2) as storage2:
# default val
assert storage2.obj == {}
storage2.obj = {1: ("test", 100)}
assert storage2.obj == {1: ("test", 100)}
# access after released lock should error
with pytest.raises(StorageException):
storage2.obj
# remain unchanged
assert storage.obj == ["e", "f"]