forked from reflex-dev/reflex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_config.py
195 lines (170 loc) Β· 5.82 KB
/
test_config.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import multiprocessing
import os
import pytest
import reflex as rx
import reflex.config
from reflex.constants import Endpoint
def test_requires_app_name():
"""Test that a config requires an app_name."""
with pytest.raises(ValueError):
rx.Config() # type: ignore
def test_set_app_name(base_config_values):
"""Test that the app name is set to the value passed in.
Args:
base_config_values: Config values.
"""
config = rx.Config(**base_config_values)
assert config.app_name == base_config_values["app_name"]
@pytest.mark.parametrize(
"env_var, value",
[
("APP_NAME", "my_test_app"),
("FRONTEND_PORT", 3001),
("FRONTEND_PATH", "/test"),
("BACKEND_PORT", 8001),
("API_URL", "https://mybackend.com:8000"),
("DEPLOY_URL", "https://myfrontend.com"),
("BACKEND_HOST", "127.0.0.1"),
("DB_URL", "postgresql://user:pass@localhost:5432/db"),
("REDIS_URL", "redis://localhost:6379"),
("TIMEOUT", 600),
("TELEMETRY_ENABLED", False),
("TELEMETRY_ENABLED", True),
],
)
def test_update_from_env(base_config_values, monkeypatch, env_var, value):
"""Test that environment variables override config values.
Args:
base_config_values: Config values.
monkeypatch: The pytest monkeypatch object.
env_var: The environment variable name.
value: The environment variable value.
"""
monkeypatch.setenv(env_var, str(value))
assert os.environ.get(env_var) == str(value)
config = rx.Config(**base_config_values)
assert getattr(config, env_var.lower()) == value
@pytest.mark.parametrize(
"kwargs, expected",
[
(
{"app_name": "test_app", "api_url": "http://example.com"},
f"{Endpoint.EVENT}",
),
(
{"app_name": "test_app", "api_url": "http://example.com/api"},
f"/api{Endpoint.EVENT}",
),
],
)
def test_event_namespace(mocker, kwargs, expected):
"""Test the event namespace.
Args:
mocker: The pytest mock object.
kwargs: The Config kwargs.
expected: Expected namespace
"""
conf = rx.Config(**kwargs)
mocker.patch("reflex.config.get_config", return_value=conf)
config = reflex.config.get_config()
assert conf == config
assert config.get_event_namespace() == expected
DEFAULT_CONFIG = rx.Config(app_name="a")
@pytest.mark.parametrize(
("config_kwargs", "env_vars", "set_persistent_vars", "exp_config_values"),
[
(
{},
{},
{},
{
"api_url": DEFAULT_CONFIG.api_url,
"backend_port": DEFAULT_CONFIG.backend_port,
"deploy_url": DEFAULT_CONFIG.deploy_url,
"frontend_port": DEFAULT_CONFIG.frontend_port,
},
),
# Ports set in config kwargs
(
{"backend_port": 8001, "frontend_port": 3001},
{},
{},
{
"api_url": "http://localhost:8001",
"backend_port": 8001,
"deploy_url": "http://localhost:3001",
"frontend_port": 3001,
},
),
# Ports set in environment take precedence
(
{"backend_port": 8001, "frontend_port": 3001},
{"BACKEND_PORT": 8002},
{},
{
"api_url": "http://localhost:8002",
"backend_port": 8002,
"deploy_url": "http://localhost:3001",
"frontend_port": 3001,
},
),
# Ports set on the command line take precedence
(
{"backend_port": 8001, "frontend_port": 3001},
{"BACKEND_PORT": 8002},
{"frontend_port": "3005"},
{
"api_url": "http://localhost:8002",
"backend_port": 8002,
"deploy_url": "http://localhost:3005",
"frontend_port": 3005,
},
),
# api_url / deploy_url already set should not be overridden
(
{"api_url": "http://foo.bar:8900", "deploy_url": "http://foo.bar:3001"},
{"BACKEND_PORT": 8002},
{"frontend_port": "3005"},
{
"api_url": "http://foo.bar:8900",
"backend_port": 8002,
"deploy_url": "http://foo.bar:3001",
"frontend_port": 3005,
},
),
],
)
def test_replace_defaults(
monkeypatch,
config_kwargs,
env_vars,
set_persistent_vars,
exp_config_values,
):
"""Test that the config replaces defaults with values from the environment.
Args:
monkeypatch: The pytest monkeypatch object.
config_kwargs: The config kwargs.
env_vars: The environment variables.
set_persistent_vars: The values passed to config._set_persistent variables.
exp_config_values: The expected config values.
"""
mock_os_env = os.environ.copy()
monkeypatch.setattr(reflex.config.os, "environ", mock_os_env) # type: ignore
mock_os_env.update({k: str(v) for k, v in env_vars.items()})
c = rx.Config(app_name="a", **config_kwargs)
c._set_persistent(**set_persistent_vars)
for key, value in exp_config_values.items():
assert getattr(c, key) == value
def reflex_dir_constant():
return rx.constants.Reflex.DIR
def test_reflex_dir_env_var(monkeypatch, tmp_path):
"""Test that the REFLEX_DIR environment variable is used to set the Reflex.DIR constant.
Args:
monkeypatch: The pytest monkeypatch object.
tmp_path: The pytest tmp_path object.
"""
monkeypatch.setenv("REFLEX_DIR", str(tmp_path))
mp_ctx = multiprocessing.get_context(method="spawn")
with mp_ctx.Pool(processes=1) as pool:
assert pool.apply(reflex_dir_constant) == str(tmp_path)