|
2 | 2 | from decart import DecartClient, InvalidAPIKeyError, InvalidBaseURLError |
3 | 3 |
|
4 | 4 |
|
5 | | -def test_create_client_success() -> None: |
6 | | - client = DecartClient(api_key="test-key") |
7 | | - assert client is not None |
8 | | - assert client.process is not None |
9 | | - |
10 | | - |
11 | | -def test_create_client_invalid_api_key() -> None: |
12 | | - with pytest.raises(InvalidAPIKeyError): |
13 | | - DecartClient(api_key="") |
14 | | - |
15 | | - |
16 | | -def test_create_client_invalid_base_url() -> None: |
17 | | - with pytest.raises(InvalidBaseURLError): |
18 | | - DecartClient(api_key="test-key", base_url="invalid-url") |
19 | | - |
20 | | - |
21 | | -def test_create_client_custom_base_url() -> None: |
22 | | - client = DecartClient(api_key="test-key", base_url="https://custom.decart.ai") |
23 | | - assert client is not None |
24 | | - assert client.base_url == "https://custom.decart.ai" |
| 5 | +class TestDecartClient: |
| 6 | + """Tests for DecartClient initialization.""" |
| 7 | + |
| 8 | + def test_create_client_with_explicit_api_key(self) -> None: |
| 9 | + """Creates a client with explicit api_key.""" |
| 10 | + client = DecartClient(api_key="test-key") |
| 11 | + assert client is not None |
| 12 | + assert client.process is not None |
| 13 | + assert client.api_key == "test-key" |
| 14 | + |
| 15 | + def test_create_client_from_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 16 | + """Creates a client using DECART_API_KEY env var.""" |
| 17 | + monkeypatch.setenv("DECART_API_KEY", "env-api-key") |
| 18 | + client = DecartClient() |
| 19 | + assert client is not None |
| 20 | + assert client.api_key == "env-api-key" |
| 21 | + |
| 22 | + def test_explicit_api_key_takes_precedence(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 23 | + """Explicit api_key takes precedence over env var.""" |
| 24 | + monkeypatch.setenv("DECART_API_KEY", "env-api-key") |
| 25 | + client = DecartClient(api_key="explicit-key") |
| 26 | + assert client.api_key == "explicit-key" |
| 27 | + |
| 28 | + def test_create_client_no_api_key_no_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 29 | + """Throws an error if api key is not provided and env var is not set.""" |
| 30 | + monkeypatch.delenv("DECART_API_KEY", raising=False) |
| 31 | + with pytest.raises(InvalidAPIKeyError, match="Missing API key"): |
| 32 | + DecartClient() |
| 33 | + |
| 34 | + def test_create_client_empty_api_key(self) -> None: |
| 35 | + """Throws an error if api key is empty string.""" |
| 36 | + with pytest.raises(InvalidAPIKeyError, match="Missing API key"): |
| 37 | + DecartClient(api_key="") |
| 38 | + |
| 39 | + def test_create_client_empty_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 40 | + """Throws an error if env var is empty string.""" |
| 41 | + monkeypatch.setenv("DECART_API_KEY", "") |
| 42 | + with pytest.raises(InvalidAPIKeyError, match="Missing API key"): |
| 43 | + DecartClient() |
| 44 | + |
| 45 | + def test_create_client_whitespace_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 46 | + """Throws an error if env var is only whitespace.""" |
| 47 | + monkeypatch.setenv("DECART_API_KEY", " ") |
| 48 | + with pytest.raises(InvalidAPIKeyError, match="Missing API key"): |
| 49 | + DecartClient() |
| 50 | + |
| 51 | + def test_create_client_invalid_base_url(self) -> None: |
| 52 | + """Throws an error if invalid base url is provided.""" |
| 53 | + with pytest.raises(InvalidBaseURLError): |
| 54 | + DecartClient(api_key="test-key", base_url="invalid-url") |
| 55 | + |
| 56 | + def test_create_client_custom_base_url(self) -> None: |
| 57 | + """Creates a client with custom base url.""" |
| 58 | + client = DecartClient(api_key="test-key", base_url="https://custom.decart.ai") |
| 59 | + assert client is not None |
| 60 | + assert client.base_url == "https://custom.decart.ai" |
0 commit comments