-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_storage.py
More file actions
121 lines (103 loc) · 3.83 KB
/
Copy pathtest_storage.py
File metadata and controls
121 lines (103 loc) · 3.83 KB
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
"""
LuckPermsAPI 存储层单元测试。
"""
import json
import os
import tempfile
from pathlib import Path
import pytest
import yaml
from luckperms.storage import JSONBackend, LuckPermsStorage, YAMLBackend
class TestYAMLBackend:
def test_save_and_load(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "test.yml"
backend = YAMLBackend()
data = {"users": {"u1": {"id": "u1", "nodes": []}}}
backend.save(path, data)
assert path.exists()
loaded = backend.load(path)
assert loaded == data
def test_load_missing_file(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "missing.yml"
backend = YAMLBackend()
assert backend.load(path) == {}
def test_load_empty_file(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "empty.yml"
path.write_text("")
backend = YAMLBackend()
assert backend.load(path) == {}
def test_save_creates_parent_dirs(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "sub" / "dir" / "test.yml"
backend = YAMLBackend()
backend.save(path, {"key": "value"})
assert path.exists()
class TestJSONBackend:
def test_save_and_load(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "test.json"
backend = JSONBackend()
data = {"groups": {"g1": {"id": "g1", "nodes": []}}}
backend.save(path, data)
assert path.exists()
loaded = backend.load(path)
assert loaded == data
def test_load_missing_file(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "missing.json"
backend = JSONBackend()
assert backend.load(path) == {}
def test_pretty_print(self):
tmpdir = tempfile.mkdtemp()
path = Path(tmpdir) / "test.json"
backend = JSONBackend()
backend.save(path, {"a": 1})
content = path.read_text()
assert "\"a\": 1" in content
class TestLuckPermsStorage:
def test_users_roundtrip(self):
tmpdir = tempfile.mkdtemp()
storage = LuckPermsStorage(tmpdir)
users = {"u1": {"id": "u1", "nodes": []}}
storage.save_users(users)
loaded = storage.load_users()
assert loaded == users
def test_groups_roundtrip(self):
tmpdir = tempfile.mkdtemp()
storage = LuckPermsStorage(tmpdir)
groups = {"admin": {"id": "admin", "nodes": []}}
storage.save_groups(groups)
loaded = storage.load_groups()
assert loaded == groups
def test_tracks_roundtrip(self):
tmpdir = tempfile.mkdtemp()
storage = LuckPermsStorage(tmpdir)
tracks = {"staff": {"name": "staff", "groups": ["a", "b"]}}
storage.save_tracks(tracks)
loaded = storage.load_tracks()
assert loaded == tracks
def test_load_all(self):
tmpdir = tempfile.mkdtemp()
storage = LuckPermsStorage(tmpdir)
storage.save_users({"u1": {"id": "u1"}})
storage.save_groups({"g1": {"id": "g1"}})
storage.save_tracks({"t1": {"name": "t1"}})
users, groups, tracks = storage.load_all()
assert users == {"u1": {"id": "u1"}}
assert groups == {"g1": {"id": "g1"}}
assert tracks == {"t1": {"name": "t1"}}
def test_file_names(self):
tmpdir = tempfile.mkdtemp()
storage = LuckPermsStorage(tmpdir)
storage.save_all({}, {}, {})
assert os.path.exists(os.path.join(tmpdir, "users.yml"))
assert os.path.exists(os.path.join(tmpdir, "groups.yml"))
assert os.path.exists(os.path.join(tmpdir, "tracks.yml"))
def test_json_extension(self):
tmpdir = tempfile.mkdtemp()
storage = LuckPermsStorage(tmpdir, backend=JSONBackend())
storage.save_users({})
assert os.path.exists(os.path.join(tmpdir, "users.json"))