Skip to content

Commit

Permalink
refactor(toggl): remove extra helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman committed Nov 14, 2024
1 parent 467e38f commit a297ad0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 46 deletions.
38 changes: 9 additions & 29 deletions compiler_admin/services/toggl.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ def detailed_time_entries(self, start_date: datetime, end_date: datetime, **kwar
return response


def _harvest_client_name():
"""Gets the value of the HARVEST_CLIENT_NAME env var."""
return os.environ.get("HARVEST_CLIENT_NAME")


def _get_info(obj: dict, key: str, env_key: str):
"""Read key from obj, populating obj once from a file path at env_key."""
if obj == {}:
Expand All @@ -143,19 +138,6 @@ def _get_info(obj: dict, key: str, env_key: str):
return obj.get(key)


def _toggl_api_token():
"""Gets the value of the TOGGL_API_TOKEN env var."""
return os.environ.get("TOGGL_API_TOKEN")


def _toggl_client_id():
"""Gets the value of the TOGGL_CLIENT_ID env var."""
client_id = os.environ.get("TOGGL_CLIENT_ID")
if client_id:
return int(client_id)
return None


def _toggl_project_info(project: str):
"""Return the cached project for the given project key."""
return _get_info(PROJECT_INFO, project, "TOGGL_PROJECT_INFO")
Expand All @@ -166,11 +148,6 @@ def _toggl_user_info(email: str):
return _get_info(USER_INFO, email, "TOGGL_USER_INFO")


def _toggl_workspace():
"""Gets the value of the TOGGL_WORKSPACE_ID env var."""
return os.environ.get("TOGGL_WORKSPACE_ID")


def _get_first_name(email: str) -> str:
"""Get cached first name or derive from email."""
user = _toggl_user_info(email)
Expand Down Expand Up @@ -226,7 +203,7 @@ def convert_to_harvest(
None. Either prints the resulting CSV data or writes to output_path.
"""
if client_name is None:
client_name = _harvest_client_name()
client_name = os.environ.get("HARVEST_CLIENT_NAME")

# read CSV file, parsing dates and times
source = files.read_csv(source_path, usecols=INPUT_COLUMNS, parse_dates=["Start date"], cache_dates=True)
Expand Down Expand Up @@ -277,11 +254,14 @@ def download_time_entries(
Returns:
None. Either prints the resulting CSV data or writes to output_path.
"""
if ("client_ids" not in kwargs or not kwargs["client_ids"]) and isinstance(_toggl_client_id(), int):
kwargs["client_ids"] = [_toggl_client_id()]

token = _toggl_api_token()
workspace = _toggl_workspace()
env_client_id = os.environ.get("TOGGL_CLIENT_ID")
if env_client_id:
env_client_id = int(env_client_id)
if ("client_ids" not in kwargs or not kwargs["client_ids"]) and isinstance(env_client_id, int):
kwargs["client_ids"] = [env_client_id]

token = os.environ.get("TOGGL_API_TOKEN")
workspace = os.environ.get("TOGGL_WORKSPACE_ID")
toggl = Toggl(token, workspace)

response = toggl.detailed_time_entries(start_date, end_date, **kwargs)
Expand Down
18 changes: 1 addition & 17 deletions tests/services/test_toggl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
PROJECT_INFO,
USER_INFO,
Toggl,
_harvest_client_name,
_get_info,
_toggl_project_info,
_toggl_user_info,
Expand Down Expand Up @@ -46,11 +45,6 @@ def spy_files(mocker):
return mocker.patch.object(compiler_admin.services.toggl, "files", wraps=files)


@pytest.fixture
def mock_harvest_client_name(mocker):
return mocker.patch(f"{MODULE}._harvest_client_name")


@pytest.fixture
def mock_get_info(mocker):
return mocker.patch(f"{MODULE}._get_info")
Expand Down Expand Up @@ -169,14 +163,6 @@ def test_toggl_detailed_time_entries_dynamic_timeout(mock_requests, toggl):
assert mock_requests.post.call_args.kwargs["timeout"] == 30


def test_harvest_client_name(monkeypatch):
assert _harvest_client_name() == "Test_Client"

monkeypatch.setenv("HARVEST_CLIENT_NAME", "New Test Client")

assert _harvest_client_name() == "New Test Client"


def test_get_info(monkeypatch):
with NamedTemporaryFile("w") as temp:
monkeypatch.setenv("INFO_FILE", temp.name)
Expand Down Expand Up @@ -286,13 +272,11 @@ def test_str_timedelta():
assert result.total_seconds() == (1 * 60 * 60) + (30 * 60) + 15


def test_convert_to_harvest_mocked(toggl_file, spy_files, mock_harvest_client_name, mock_google_user_info):
def test_convert_to_harvest_mocked(toggl_file, spy_files, mock_google_user_info):
mock_google_user_info.return_value = {}

convert_to_harvest(toggl_file, client_name=None)

mock_harvest_client_name.assert_called_once()

spy_files.read_csv.assert_called_once()
call_args = spy_files.read_csv.call_args
assert (toggl_file,) in call_args
Expand Down

0 comments on commit a297ad0

Please sign in to comment.