forked from dremio/dremio-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_settings.py
More file actions
168 lines (150 loc) · 5.47 KB
/
test_settings.py
File metadata and controls
168 lines (150 loc) · 5.47 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
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
#
# Copyright (C) 2017-2025 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import uuid
import pydantic
import pytest
import yaml
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from pydantic_core import ValidationError
from dremioai.config import settings
from dremioai.config.tools import ToolType
def test_configure_with_no_file_works(mock_config_dir):
s = settings.instance()
assert settings.instance() is not None
settings.configure(force=True)
assert settings.instance() is not None
assert settings.instance() is not s
def test_configure_creates_default_config(mock_config_dir):
"""Test that configure creates the default config file if it doesn't exist"""
default_path = mock_config_dir / "dremioai" / "config.yaml"
assert default_path == settings.default_config()
assert not default_path.exists()
# Call configure with no arguments (should use default path)
settings.configure()
# Check that the default config file was created
assert default_path.exists()
assert settings.instance() is not None and settings.instance().dremio is None
def test_create_default_config(mock_config_dir):
uri = settings.DremioCloudUri.PRODEMEA.value
pat = "test-pat"
project_id = uuid.uuid4()
mode = ToolType.FOR_DATA_PATTERNS
settings.configure(force=True)
settings._settings.set(
settings.instance().model_validate(
{
"dremio": {
"uri": uri,
"pat": pat,
"project_id": project_id,
},
"tools": {"server_mode": mode.name},
}
)
)
settings.write_settings()
assert settings.default_config().exists()
settings.configure(force=True)
dremio = settings.instance().dremio
assert (
dremio.uri == "https://api.eu.dremio.cloud"
and dremio.pat == pat
and dremio.project_id == str(project_id)
)
tools = settings.instance().tools
assert tools.server_mode == mode
@pytest.mark.parametrize(
"name,value",
[
(name, value)
for name in ("enable_search", "enable_experimental")
for value in (True, False)
],
)
def test_experimental_rename(name: str, value: bool):
d = settings.Dremio.model_validate(
{name: value, "uri": "https://foo", "pat": "bar"}
)
assert d.enable_search == value
@pytest.mark.parametrize(
"project_id,error",
[
pytest.param(str(uuid.uuid4()), False, id="valid project id"),
pytest.param(None, False, id="no project id"),
pytest.param("asdfsa safsa", True, id="invalid project id"),
pytest.param(str(uuid.uuid4())[:-1] + "a", True, id="invalid project id"),
pytest.param("DREMIO_DYNAMIC", False, id="dynamic project id"),
],
)
def test_projects(project_id: str | None, error: bool):
val = {"uri": "https://foo", "project_id": project_id}
if error:
try:
settings.Dremio.model_validate(val)
assert False
except:
pass
else:
d = settings.Dremio.model_validate(val)
assert d.project_id == project_id or d.project_id is None and project_id is None
def test_env_file(mock_config_dir):
try:
os.environ["DREMIOAI_DREMIO__URI"] = "https://foo"
os.environ["DREMIOAI_DREMIO__PAT"] = "bar"
os.environ["DREMIOAI_TOOLS__SERVER_MODE"] = "FOR_DATA_PATTERNS"
settings.configure(force=True)
assert settings.instance().dremio.uri == "https://foo"
assert settings.instance().dremio.pat == "bar"
assert settings.instance().tools.server_mode == ToolType.FOR_DATA_PATTERNS
finally:
os.environ.pop("DREMIOAI_DREMIO_URI", None)
os.environ.pop("DREMIOAI_DREMIO_PAT", None)
@pytest.mark.parametrize(
"uri,project_id,issuer,error",
[
pytest.param(
uri,
project_id,
iss,
project_id is None,
id=f"{label} with {plabel}",
)
for uri, iss, label in (
("https://foo", "https://foo", "custom-uri"),
("https://api.dremio.cloud", "https://login.dremio.cloud", "prod"),
(
"https://api.eu.dremio.cloud",
"https://login.eu.dremio.cloud",
"prodemea",
),
("https://api.dev.dremio.site", "https://login.dev.dremio.site", "dev"),
)
for project_id, plabel in (
(None, "no-project-id"),
("DREMIO_DYNAMIC", "dynamic-project-id"),
(str(uuid.uuid4()), "project-id"),
)
],
)
def test_auth_urls(uri: str, project_id: str | None, issuer: str, error: bool):
d = settings.Dremio.model_validate({"uri": uri, "project_id": project_id})
auth = (f"{issuer}/oauth/authorize", f"{issuer}/oauth/token") if not error else None
issuer = issuer if not error else None
assert d.auth_issuer_uri == issuer
assert d.auth_endpoints == auth