|
| 1 | +import sys |
| 2 | +from datetime import timedelta |
| 3 | +from io import StringIO |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +import pytest |
| 8 | + |
| 9 | +import compiler_admin.services.harvest |
| 10 | +from compiler_admin.services.harvest import ( |
| 11 | + __name__ as MODULE, |
| 12 | + files, |
| 13 | + INPUT_COLUMNS, |
| 14 | + OUTPUT_COLUMNS, |
| 15 | + _calc_start_time, |
| 16 | + _duration_str, |
| 17 | + _toggl_client_name, |
| 18 | + convert_to_toggl, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def mock_environment(monkeypatch): |
| 24 | + monkeypatch.setenv("TOGGL_CLIENT_NAME", "Test_Client") |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def spy_files(mocker): |
| 29 | + return mocker.patch.object(compiler_admin.services.harvest, "files", wraps=files) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def mock_toggl_client_name(mocker): |
| 34 | + return mocker.patch(f"{MODULE}._toggl_client_name") |
| 35 | + |
| 36 | + |
| 37 | +def test_calc_start_time(): |
| 38 | + durations = pd.to_timedelta(np.arange(1, 6), unit="m") |
| 39 | + df = pd.DataFrame(data={"Duration": durations, "Start time": [pd.to_timedelta("09:00:00") for d in durations]}) |
| 40 | + |
| 41 | + calc_df = _calc_start_time(df) |
| 42 | + |
| 43 | + assert calc_df.columns.equals(df.columns) |
| 44 | + assert calc_df["Duration"].equals(df["Duration"]) |
| 45 | + assert calc_df["Start time"].to_list() == [ |
| 46 | + # offset = 0, cumsum = 0 |
| 47 | + pd.to_timedelta("09:00:00"), |
| 48 | + # offset = 1, cumsum = 1 |
| 49 | + pd.to_timedelta("09:01:00"), |
| 50 | + # offset = 2, cumsum = 3 |
| 51 | + pd.to_timedelta("09:03:00"), |
| 52 | + # offset = 3, cumsum = 6 |
| 53 | + pd.to_timedelta("09:06:00"), |
| 54 | + # offset = 4, cumsum = 10 |
| 55 | + pd.to_timedelta("09:10:00"), |
| 56 | + ] |
| 57 | + |
| 58 | + |
| 59 | +def test_duration_str(): |
| 60 | + td = timedelta(hours=1, minutes=30, seconds=15) |
| 61 | + |
| 62 | + result = _duration_str(td) |
| 63 | + |
| 64 | + assert isinstance(result, str) |
| 65 | + assert result == "01:30" |
| 66 | + |
| 67 | + |
| 68 | +def test_toggl_client_name(monkeypatch): |
| 69 | + assert _toggl_client_name() == "Test_Client" |
| 70 | + |
| 71 | + monkeypatch.setenv("TOGGL_CLIENT_NAME", "New Test Client") |
| 72 | + |
| 73 | + assert _toggl_client_name() == "New Test Client" |
| 74 | + |
| 75 | + |
| 76 | +def test_convert_to_toggl_mocked(harvest_file, spy_files, mock_toggl_client_name): |
| 77 | + convert_to_toggl(harvest_file, client_name=None) |
| 78 | + |
| 79 | + mock_toggl_client_name.assert_called_once() |
| 80 | + |
| 81 | + spy_files.read_csv.assert_called_once() |
| 82 | + call_args = spy_files.read_csv.call_args |
| 83 | + assert (harvest_file,) in call_args |
| 84 | + assert call_args.kwargs["usecols"] == INPUT_COLUMNS |
| 85 | + assert call_args.kwargs["parse_dates"] == ["Date"] |
| 86 | + assert call_args.kwargs["cache_dates"] is True |
| 87 | + |
| 88 | + spy_files.write_csv.assert_called_once() |
| 89 | + call_args = spy_files.write_csv.call_args |
| 90 | + assert call_args[0][0] == sys.stdout |
| 91 | + assert call_args[0][2] == OUTPUT_COLUMNS |
| 92 | + |
| 93 | + |
| 94 | +def test_convert_to_toggl_sample(harvest_file, toggl_file): |
| 95 | + output = None |
| 96 | + |
| 97 | + with StringIO() as output_data: |
| 98 | + convert_to_toggl(harvest_file, output_data, "Test Client 123") |
| 99 | + output = output_data.getvalue() |
| 100 | + |
| 101 | + assert output |
| 102 | + assert isinstance(output, str) |
| 103 | + assert ",".join(OUTPUT_COLUMNS) in output |
| 104 | + |
| 105 | + order = ["Start date", "Start time", "Email"] |
| 106 | + sample_output_df = pd.read_csv(toggl_file).sort_values(order) |
| 107 | + output_df = pd.read_csv(StringIO(output)).sort_values(order) |
| 108 | + |
| 109 | + assert set(output_df.columns.to_list()) <= set(sample_output_df.columns.to_list()) |
| 110 | + assert output_df["Client"].eq("Test Client 123").all() |
| 111 | + assert output_df["Project"].eq("Test Client 123").all() |
0 commit comments