Skip to content

Commit 0f28d64

Browse files
committed
refactor: 💡 retire dotenv on esaclient
1 parent 0095c1d commit 0f28d64

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

esa_client.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import logging
2-
import os
32

43
import requests
5-
from dotenv import load_dotenv
64

7-
load_dotenv()
5+
# from dotenv import load_dotenv
6+
7+
# load_dotenv()
88
logger = logging.getLogger(__name__)
99

1010

1111
class EsaClient:
12-
def __init__(self):
13-
self.token = os.getenv("ESA_TOKEN")
14-
self.team_name = os.getenv("ESA_TEAM_NAME")
12+
def __init__(self, token: str, team_name: str):
13+
self.token = token
14+
self.team_name = team_name
1515

1616
if not self.token:
17-
logger.error("Environment variable ESA_TOKEN is not set.")
17+
logger.error("Token is required but was not provided.")
1818
raise ValueError("ESA_TOKEN is required")
1919
if not self.team_name:
20-
logger.error("Environment variable ESA_TEAM_NAME is not set.")
20+
logger.error("Team name is required but was not provided.")
2121
raise ValueError("ESA_TEAM_NAME is required")
2222

2323
self.base_url = f"https://api.esa.io/v1/teams/{self.team_name}"
@@ -118,6 +118,9 @@ def delete_post(self, post_number: int):
118118

119119
# Example usage (optional, for testing during development)
120120
if __name__ == "__main__":
121+
from dotenv import load_dotenv
122+
123+
load_dotenv()
121124
logging.basicConfig(level=logging.INFO)
122125
try:
123126
client = EsaClient()

tests/test_esa_client.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77

88

99
@pytest.fixture
10-
def mock_env(monkeypatch):
11-
"Set up environment variables for testing."
12-
monkeypatch.setenv("ESA_TOKEN", "test_token")
13-
monkeypatch.setenv("ESA_TEAM_NAME", "test_team")
14-
15-
16-
@pytest.fixture
17-
def client(mock_env):
18-
"Return an instance of EsaClient with mocked env vars."
19-
return EsaClient()
10+
def client():
11+
"Return an instance of EsaClient with specified token and team_name."
12+
return EsaClient(token="test_token", team_name="test_team")
2013

2114

2215
def test_esa_client_initialization(client):
@@ -27,20 +20,20 @@ def test_esa_client_initialization(client):
2720
assert client.session.headers["Authorization"] == f"Bearer {client.token}"
2821

2922

30-
def test_esa_client_initialization_missing_token(monkeypatch):
31-
"Test ValueError is raised if ESA_TOKEN is missing."
32-
monkeypatch.delenv("ESA_TOKEN", raising=False) # Ensure it's removed if exists
33-
monkeypatch.setenv("ESA_TEAM_NAME", "test_team")
23+
def test_esa_client_initialization_missing_token():
24+
"Test ValueError is raised if token is missing or invalid."
3425
with pytest.raises(ValueError, match="ESA_TOKEN is required"):
35-
EsaClient()
26+
EsaClient(token=None, team_name="test_team")
27+
with pytest.raises(ValueError, match="ESA_TOKEN is required"):
28+
EsaClient(token="", team_name="test_team")
3629

3730

38-
def test_esa_client_initialization_missing_team_name(monkeypatch):
39-
"Test ValueError is raised if ESA_TEAM_NAME is missing."
40-
monkeypatch.setenv("ESA_TOKEN", "test_token")
41-
monkeypatch.delenv("ESA_TEAM_NAME", raising=False) # Ensure it's removed if exists
31+
def test_esa_client_initialization_missing_team_name():
32+
"Test ValueError is raised if team_name is missing or invalid."
33+
with pytest.raises(ValueError, match="ESA_TEAM_NAME is required"):
34+
EsaClient(token="test_token", team_name=None)
4235
with pytest.raises(ValueError, match="ESA_TEAM_NAME is required"):
43-
EsaClient()
36+
EsaClient(token="test_token", team_name="")
4437

4538

4639
@patch("esa_client.requests.Session.request")

0 commit comments

Comments
 (0)