/ sdks / sandbox / python / tests / test_connection_config_env_and_timeout.py
test_connection_config_env_and_timeout.py
 1  #
 2  # Copyright 2025 Alibaba Group Holding Ltd.
 3  #
 4  # Licensed under the Apache License, Version 2.0 (the "License");
 5  # you may not use this file except in compliance with the License.
 6  # You may obtain a copy of the License at
 7  #
 8  #     http://www.apache.org/licenses/LICENSE-2.0
 9  #
10  # Unless required by applicable law or agreed to in writing, software
11  # distributed under the License is distributed on an "AS IS" BASIS,
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  # See the License for the specific language governing permissions and
14  # limitations under the License.
15  #
16  from datetime import timedelta
17  
18  import pytest
19  
20  from opensandbox.config import ConnectionConfig
21  
22  
23  def test_get_api_key_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
24      monkeypatch.setenv("OPEN_SANDBOX_API_KEY", "k1")
25      cfg = ConnectionConfig(api_key=None)
26      assert cfg.get_api_key() == "k1"
27  
28  
29  def test_get_domain_from_env_and_default(monkeypatch: pytest.MonkeyPatch) -> None:
30      monkeypatch.delenv("OPEN_SANDBOX_DOMAIN", raising=False)
31      cfg = ConnectionConfig(domain=None)
32      assert cfg.get_domain() == "localhost:8080"
33  
34      monkeypatch.setenv("OPEN_SANDBOX_DOMAIN", "example.com:8081")
35      cfg2 = ConnectionConfig(domain=None)
36      assert cfg2.get_domain() == "example.com:8081"
37  
38  
39  def test_timeout_must_be_positive() -> None:
40      ConnectionConfig(request_timeout=timedelta(seconds=1))
41      with pytest.raises(ValueError):
42          ConnectionConfig(request_timeout=timedelta(seconds=0))