forked from rio-labs/rio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_settings.py
65 lines (46 loc) · 1.6 KB
/
test_user_settings.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
import aiofiles
from pytest import MonkeyPatch
from uniserde import JsonDoc
import rio.testing
class FakeFile:
def __init__(self, content: str):
self._content = content
async def __aenter__(self):
return self
async def __aexit__(self, *args):
pass
async def read(self) -> str:
return self._content
class RootSettings(rio.UserSettings):
foo: str = "foo"
class FooSettings(rio.UserSettings):
section_name = "foo"
bar: str = "bar"
async def test_load_settings():
user_settings: JsonDoc = {
":foo": "bar",
"foo:bar": "baz",
}
async with rio.testing.TestClient(
running_in_window=False,
default_attachments=(RootSettings(), FooSettings()),
user_settings=user_settings,
) as test_client:
root_settings = test_client.session[RootSettings]
foo_settings = test_client.session[FooSettings]
assert root_settings.foo == "bar"
assert foo_settings.bar == "baz"
async def test_load_settings_file(monkeypatch: MonkeyPatch):
monkeypatch.setattr(
aiofiles,
"open",
lambda _: FakeFile('{"foo": "bar", "section:foo": {"bar": "baz"} }'),
)
async with rio.testing.TestClient(
running_in_window=True,
default_attachments=(RootSettings(), FooSettings()),
) as test_client:
root_settings = test_client.session[RootSettings]
foo_settings = test_client.session[FooSettings]
assert root_settings.foo == "bar"
assert foo_settings.bar == "baz"