Skip to content

Commit 5cc9716

Browse files
committed
refactor(toggl): remove extra helper functions
1 parent 9a1fc4c commit 5cc9716

File tree

2 files changed

+10
-46
lines changed

2 files changed

+10
-46
lines changed

compiler_admin/services/toggl.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ def detailed_time_entries(self, start_date: datetime, end_date: datetime, **kwar
128128
return response
129129

130130

131-
def _harvest_client_name():
132-
"""Gets the value of the HARVEST_CLIENT_NAME env var."""
133-
return os.environ.get("HARVEST_CLIENT_NAME")
134-
135-
136131
def _get_info(obj: dict, key: str, env_key: str):
137132
"""Read key from obj, populating obj once from a file path at env_key."""
138133
if obj == {}:
@@ -143,19 +138,6 @@ def _get_info(obj: dict, key: str, env_key: str):
143138
return obj.get(key)
144139

145140

146-
def _toggl_api_token():
147-
"""Gets the value of the TOGGL_API_TOKEN env var."""
148-
return os.environ.get("TOGGL_API_TOKEN")
149-
150-
151-
def _toggl_client_id():
152-
"""Gets the value of the TOGGL_CLIENT_ID env var."""
153-
client_id = os.environ.get("TOGGL_CLIENT_ID")
154-
if client_id:
155-
return int(client_id)
156-
return None
157-
158-
159141
def _toggl_project_info(project: str):
160142
"""Return the cached project for the given project key."""
161143
return _get_info(PROJECT_INFO, project, "TOGGL_PROJECT_INFO")
@@ -166,11 +148,6 @@ def _toggl_user_info(email: str):
166148
return _get_info(USER_INFO, email, "TOGGL_USER_INFO")
167149

168150

169-
def _toggl_workspace():
170-
"""Gets the value of the TOGGL_WORKSPACE_ID env var."""
171-
return os.environ.get("TOGGL_WORKSPACE_ID")
172-
173-
174151
def _get_first_name(email: str) -> str:
175152
"""Get cached first name or derive from email."""
176153
user = _toggl_user_info(email)
@@ -226,7 +203,7 @@ def convert_to_harvest(
226203
None. Either prints the resulting CSV data or writes to output_path.
227204
"""
228205
if client_name is None:
229-
client_name = _harvest_client_name()
206+
client_name = os.environ.get("HARVEST_CLIENT_NAME")
230207

231208
# read CSV file, parsing dates and times
232209
source = files.read_csv(source_path, usecols=INPUT_COLUMNS, parse_dates=["Start date"], cache_dates=True)
@@ -277,11 +254,14 @@ def download_time_entries(
277254
Returns:
278255
None. Either prints the resulting CSV data or writes to output_path.
279256
"""
280-
if ("client_ids" not in kwargs or not kwargs["client_ids"]) and isinstance(_toggl_client_id(), int):
281-
kwargs["client_ids"] = [_toggl_client_id()]
282-
283-
token = _toggl_api_token()
284-
workspace = _toggl_workspace()
257+
env_client_id = os.environ.get("TOGGL_CLIENT_ID")
258+
if env_client_id:
259+
env_client_id = int(env_client_id)
260+
if ("client_ids" not in kwargs or not kwargs["client_ids"]) and isinstance(env_client_id, int):
261+
kwargs["client_ids"] = [env_client_id]
262+
263+
token = os.environ.get("TOGGL_API_TOKEN")
264+
workspace = os.environ.get("TOGGL_WORKSPACE_ID")
285265
toggl = Toggl(token, workspace)
286266

287267
response = toggl.detailed_time_entries(start_date, end_date, **kwargs)

tests/services/test_toggl.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
PROJECT_INFO,
1818
USER_INFO,
1919
Toggl,
20-
_harvest_client_name,
2120
_get_info,
2221
_toggl_project_info,
2322
_toggl_user_info,
@@ -46,11 +45,6 @@ def spy_files(mocker):
4645
return mocker.patch.object(compiler_admin.services.toggl, "files", wraps=files)
4746

4847

49-
@pytest.fixture
50-
def mock_harvest_client_name(mocker):
51-
return mocker.patch(f"{MODULE}._harvest_client_name")
52-
53-
5448
@pytest.fixture
5549
def mock_get_info(mocker):
5650
return mocker.patch(f"{MODULE}._get_info")
@@ -169,14 +163,6 @@ def test_toggl_detailed_time_entries_dynamic_timeout(mock_requests, toggl):
169163
assert mock_requests.post.call_args.kwargs["timeout"] == 30
170164

171165

172-
def test_harvest_client_name(monkeypatch):
173-
assert _harvest_client_name() == "Test_Client"
174-
175-
monkeypatch.setenv("HARVEST_CLIENT_NAME", "New Test Client")
176-
177-
assert _harvest_client_name() == "New Test Client"
178-
179-
180166
def test_get_info(monkeypatch):
181167
with NamedTemporaryFile("w") as temp:
182168
monkeypatch.setenv("INFO_FILE", temp.name)
@@ -286,13 +272,11 @@ def test_str_timedelta():
286272
assert result.total_seconds() == (1 * 60 * 60) + (30 * 60) + 15
287273

288274

289-
def test_convert_to_harvest_mocked(toggl_file, spy_files, mock_harvest_client_name, mock_google_user_info):
275+
def test_convert_to_harvest_mocked(toggl_file, spy_files, mock_google_user_info):
290276
mock_google_user_info.return_value = {}
291277

292278
convert_to_harvest(toggl_file, client_name=None)
293279

294-
mock_harvest_client_name.assert_called_once()
295-
296280
spy_files.read_csv.assert_called_once()
297281
call_args = spy_files.read_csv.call_args
298282
assert (toggl_file,) in call_args

0 commit comments

Comments
 (0)