|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from io import BytesIO, StringIO |
1 | 3 | import sys |
2 | | -from datetime import timedelta |
3 | | -from io import StringIO |
| 4 | +from pathlib import Path |
4 | 5 | from tempfile import NamedTemporaryFile |
5 | 6 |
|
6 | 7 | import pandas as pd |
|
22 | 23 | _get_last_name, |
23 | 24 | _str_timedelta, |
24 | 25 | convert_to_harvest, |
| 26 | + download_time_entries, |
25 | 27 | ) |
26 | 28 |
|
27 | 29 |
|
@@ -57,6 +59,11 @@ def mock_google_user_info(mocker): |
57 | 59 | return mocker.patch(f"{MODULE}.google_user_info") |
58 | 60 |
|
59 | 61 |
|
| 62 | +@pytest.fixture |
| 63 | +def mock_requests(mocker): |
| 64 | + return mocker.patch(f"{MODULE}.requests") |
| 65 | + |
| 66 | + |
60 | 67 | def test_harvest_client_name(monkeypatch): |
61 | 68 | assert _harvest_client_name() == "Test_Client" |
62 | 69 |
|
@@ -212,3 +219,35 @@ def test_convert_to_harvest_sample(toggl_file, harvest_file, mock_google_user_in |
212 | 219 |
|
213 | 220 | assert set(output_df.columns.to_list()) <= set(sample_output_df.columns.to_list()) |
214 | 221 | 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