forked from nonebot/nonebot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
133 lines (99 loc) · 3.68 KB
/
test_config.py
File metadata and controls
133 lines (99 loc) · 3.68 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
122
123
124
125
126
127
128
129
130
131
132
133
from typing import TYPE_CHECKING, Optional, Union
from pydantic import BaseModel, Field
import pytest
from nonebot.compat import PYDANTIC_V2
from nonebot.config import DOTENV_TYPE, BaseSettings, SettingsConfig, SettingsError
class Simple(BaseModel):
a: int = 0
b: int = 0
c: dict = {}
complex: list = []
class Example(BaseSettings):
if TYPE_CHECKING:
_env_file: Optional[DOTENV_TYPE] = ".env", ".env.example"
_env_nested_delimiter: Optional[str] = "__"
if PYDANTIC_V2:
model_config = SettingsConfig(
env_file=(".env", ".env.example"), env_nested_delimiter="__"
)
else:
class Config( # pyright: ignore[reportIncompatibleVariableOverride]
SettingsConfig
):
env_file = ".env", ".env.example"
env_nested_delimiter = "__"
simple: str = ""
complex: list[int] = Field(default=[1])
complex_none: Optional[list[int]] = None
complex_union: Union[int, list[int]] = 1
nested: Simple = Simple()
nested_inner: Simple = Simple()
aliased_simple: str = Field(default="", alias="alias_simple")
class ExampleWithoutDelimiter(Example):
if PYDANTIC_V2:
model_config = SettingsConfig(env_nested_delimiter=None)
else:
class Config( # pyright: ignore[reportIncompatibleVariableOverride]
SettingsConfig
):
env_nested_delimiter = None
def test_config_no_env():
config = Example(_env_file=None)
assert config.simple == ""
with pytest.raises(AttributeError):
config.common_config
def test_config_with_env():
config = Example(_env_file=(".env", ".env.example"))
assert config.simple == "simple"
assert config.complex == [1, 2, 3]
assert config.complex_none is None
assert config.complex_union == [1, 2, 3]
assert config.nested.a == 1
assert config.nested.b == 2
assert config.nested.c == {"c": "3"}
assert config.nested.complex == [1, 2, 3]
with pytest.raises(AttributeError):
config.nested__b
with pytest.raises(AttributeError):
config.nested__c__c
with pytest.raises(AttributeError):
config.nested__complex
assert config.nested_inner.a == 1
assert config.nested_inner.b == 2
with pytest.raises(AttributeError):
config.nested_inner__a
with pytest.raises(AttributeError):
config.nested_inner__b
assert config.aliased_simple == "aliased_simple"
assert config.common_config == "common"
assert config.other_simple == "simple"
assert config.other_nested == {"a": 1, "b": 2}
with pytest.raises(AttributeError):
config.other_nested__b
assert config.other_nested_inner == {"a": 1, "b": 2}
with pytest.raises(AttributeError):
config.other_nested_inner__a
with pytest.raises(AttributeError):
config.other_nested_inner__b
def test_config_error_env():
with pytest.MonkeyPatch().context() as m:
m.setenv("COMPLEX", "not json")
with pytest.raises(SettingsError):
Example(_env_file=(".env", ".env.example"))
def test_config_without_delimiter():
config = ExampleWithoutDelimiter()
assert config.nested.a == 1
assert config.nested.b == 0
assert config.nested__b == 2
assert config.nested.c == {}
assert config.nested__c__c == 3
assert config.nested.complex == []
assert config.nested__complex == [1, 2, 3]
assert config.nested_inner.a == 0
assert config.nested_inner.b == 0
assert config.other_nested == {"a": 1}
assert config.other_nested__b == 2
with pytest.raises(AttributeError):
config.other_nested_inner
assert config.other_nested_inner__a == 1
assert config.other_nested_inner__b == 2