-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_config.py
More file actions
110 lines (88 loc) · 3.55 KB
/
test_config.py
File metadata and controls
110 lines (88 loc) · 3.55 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
import pytest
from pydantic import ValidationError
from deepnote_core.config.loader import ConfigurationLoader
from deepnote_core.config.models import DeepnoteConfig
from deepnote_toolkit.config import clear_config_cache, get_config
def test_port_validation_range():
# Invalid port should raise
with pytest.raises(ValidationError):
DeepnoteConfig.model_validate({"server": {"jupyter_port": 80}})
def test_legacy_env_mapping(monkeypatch):
monkeypatch.setenv("DEEPNOTE_JUPYTER_PORT", "9999")
monkeypatch.setenv("DEEPNOTE_WEBAPP_URL", "https://app.example")
cfg = ConfigurationLoader().load_config()
assert cfg.server.jupyter_port == 9999
assert cfg.runtime.webapp_url == "https://app.example"
def test_runtime_coerce_float_inverted_flag(monkeypatch):
# When legacy flag is set, coerce_float becomes False
monkeypatch.setenv("DEEPNOTE_DO_NOT_COERCE_FLOAT", "1")
cfg = ConfigurationLoader().load_config()
assert cfg.runtime.coerce_float is False
def test_coerce_float_picks_up_late_env_after_cache_clear(monkeypatch):
# Setup: start from a clean cache and confirm the default value
clear_config_cache()
monkeypatch.delenv("DEEPNOTE_DO_NOT_COERCE_FLOAT", raising=False)
cfg_default = get_config()
assert cfg_default.runtime.coerce_float is True
# Inject the env var; the cached config should still return the default
monkeypatch.setenv("DEEPNOTE_DO_NOT_COERCE_FLOAT", "1")
cfg_cached = get_config()
assert cfg_cached.runtime.coerce_float is True
# After clearing the cache, the config should pick up the injected value
clear_config_cache()
cfg_refreshed = get_config()
assert cfg_refreshed.runtime.coerce_float is False
def test_loader_precedence_cli_over_env_over_file(tmp_path, monkeypatch):
# File value: 7777
config_file = tmp_path / "deepnote-toolkit.toml"
config_file.write_text(
"""
[server]
jupyter_port = 7777
""".strip()
)
# Env overlay: 8888
monkeypatch.setenv("DEEPNOTE_SERVER__JUPYTER_PORT", "8888")
# CLI override: 9999
class Args:
jupyter_port = 9999
ls_port = None
enable_terminals = True
python_kernel_only = False
start_jupyter = True
start_ls = True
start_streamlit_servers = False
start_extra_servers = False
start_servers = True
root_dir = None
home_dir = None
log_dir = None
work_mountpoint = "/datasets/_deepnote_work"
venv_path = "~/venv"
version = None
index_url = None
bundle_path = None
cache_path = None
run_in_detached_mode = False
venv_without_pip = False
loader = ConfigurationLoader(config_path=config_file)
cfg = loader.load_with_args(Args()) # type: ignore
assert cfg.server.jupyter_port == 9999
def test_runtime_loader_env_over_file(tmp_path, monkeypatch):
config_file = tmp_path / "conf.toml"
config_file.write_text(
"""
[server]
jupyter_port = 7777
""".strip()
)
monkeypatch.setenv("DEEPNOTE_CONFIG_FILE", str(config_file))
monkeypatch.setenv("DEEPNOTE_SERVER__JUPYTER_PORT", "8888")
cfg = ConfigurationLoader().load_config()
assert cfg.server.jupyter_port == 8888
def test_env_extra_servers_into_config(monkeypatch):
# Simulate env-defined extra servers
monkeypatch.setenv("DEEPNOTE_TOOLKIT_EXTRA_SERVER_1", "cmd-one")
monkeypatch.setenv("DEEPNOTE_TOOLKIT_EXTRA_SERVER_2", "cmd-two")
cfg = ConfigurationLoader().load_config()
assert cfg.server.extra_servers == ["cmd-one", "cmd-two"]