Skip to content

Commit 1166cab

Browse files
committed
test(toggl): download_time_entries with a mock report
1 parent 7896750 commit 1166cab

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

tests/services/test_toggl.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from datetime import datetime, timedelta
2+
from io import BytesIO, StringIO
13
import sys
2-
from datetime import timedelta
3-
from io import StringIO
4+
from pathlib import Path
45
from tempfile import NamedTemporaryFile
56

67
import pandas as pd
@@ -22,6 +23,7 @@
2223
_get_last_name,
2324
_str_timedelta,
2425
convert_to_harvest,
26+
download_time_entries,
2527
)
2628

2729

@@ -57,6 +59,11 @@ def mock_google_user_info(mocker):
5759
return mocker.patch(f"{MODULE}.google_user_info")
5860

5961

62+
@pytest.fixture
63+
def mock_requests(mocker):
64+
return mocker.patch(f"{MODULE}.requests")
65+
66+
6067
def test_harvest_client_name(monkeypatch):
6168
assert _harvest_client_name() == "Test_Client"
6269

@@ -212,3 +219,35 @@ def test_convert_to_harvest_sample(toggl_file, harvest_file, mock_google_user_in
212219

213220
assert set(output_df.columns.to_list()) <= set(sample_output_df.columns.to_list())
214221
assert output_df["Client"].eq("Test Client 123").all()
222+
223+
224+
def test_download_time_entries(monkeypatch, toggl_file, mock_requests, mocker):
225+
monkeypatch.setenv("TOGGL_API_TOKEN", "token")
226+
monkeypatch.setenv("TOGGL_CLIENT_ID", "1234")
227+
monkeypatch.setenv("TOGGL_WORKSPACE_ID", "workspace")
228+
229+
# setup a mock response to a requests.post call
230+
mock_csv_bytes = Path(toggl_file).read_bytes()
231+
mock_post_response = mocker.Mock()
232+
mock_post_response.raise_for_status.return_value = None
233+
# prepend the BOM to the mock content
234+
mock_post_response.content = b"\xef\xbb\xbf" + mock_csv_bytes
235+
# override the requests.post call to return the mock response
236+
mock_requests.post.return_value = mock_post_response
237+
238+
with NamedTemporaryFile("w") as temp:
239+
download_time_entries(datetime.now(), datetime.now(), temp.name)
240+
temp.flush()
241+
response_csv_bytes = Path(temp.name).read_bytes()
242+
243+
# load each CSV into a DataFrame
244+
mock_df = pd.read_csv(BytesIO(mock_csv_bytes))
245+
response_df = pd.read_csv(BytesIO(response_csv_bytes))
246+
247+
# check that the response DataFrame has all columns from the mock DataFrame
248+
assert set(response_df.columns.to_list()).issubset(mock_df.columns.to_list())
249+
250+
# check that all column values from response DataFrame are the same
251+
# as corresponding column values from the mock DataFrame
252+
for col in response_df.columns:
253+
assert response_df[col].equals(mock_df[col])

0 commit comments

Comments
 (0)