|
1 | 1 | from argparse import Namespace |
2 | | -from io import StringIO |
3 | 2 | import pytest |
4 | 3 |
|
5 | 4 | from compiler_admin import RESULT_SUCCESS |
6 | 5 | from compiler_admin.commands.time.convert import ( |
7 | 6 | __name__ as MODULE, |
| 7 | + CONVERTERS, |
8 | 8 | _get_source_converter, |
9 | | - convert_to_harvest, |
10 | | - convert_to_toggl, |
11 | 9 | convert, |
12 | 10 | ) |
| 11 | +from compiler_admin.services.harvest import CONVERTERS as HARVEST_CONVERTERS |
| 12 | +from compiler_admin.services.toggl import CONVERTERS as TOGGL_CONVERTERS |
13 | 13 |
|
14 | 14 |
|
15 | 15 | @pytest.fixture |
16 | 16 | def mock_get_source_converter(mocker): |
17 | 17 | return mocker.patch(f"{MODULE}._get_source_converter") |
18 | 18 |
|
19 | 19 |
|
20 | | -def test_get_source_converter_match_toggl(toggl_file): |
21 | | - result = _get_source_converter(toggl_file) |
22 | | - |
23 | | - assert result == convert_to_harvest |
| 20 | +@pytest.fixture |
| 21 | +def mock_converters(mocker): |
| 22 | + return mocker.patch(f"{MODULE}.CONVERTERS", new={}) |
24 | 23 |
|
25 | 24 |
|
26 | | -def test_get_source_converter_match_harvest(harvest_file): |
27 | | - result = _get_source_converter(harvest_file) |
| 25 | +def test_get_source_converter_match(mock_converters): |
| 26 | + mock_converters["toggl"] = {"test_fmt": "converter"} |
| 27 | + result = _get_source_converter("toggl", "test_fmt") |
28 | 28 |
|
29 | | - assert result == convert_to_toggl |
| 29 | + assert result == "converter" |
30 | 30 |
|
31 | 31 |
|
32 | 32 | def test_get_source_converter_mismatch(): |
33 | | - data = StringIO("one,two,three\n1,2,3") |
34 | | - |
35 | | - with pytest.raises(NotImplementedError, match="A converter for the given source data does not exist."): |
36 | | - _get_source_converter(data) |
| 33 | + with pytest.raises( |
| 34 | + NotImplementedError, match="A converter for the given source and target formats does not exist: nope to toggl" |
| 35 | + ): |
| 36 | + _get_source_converter("nope", "toggl") |
| 37 | + with pytest.raises( |
| 38 | + NotImplementedError, match="A converter for the given source and target formats does not exist: toggl to nope" |
| 39 | + ): |
| 40 | + _get_source_converter("toggl", "nope") |
37 | 41 |
|
38 | 42 |
|
39 | 43 | def test_convert(mock_get_source_converter): |
40 | | - args = Namespace(input="input", output="output", client="client") |
| 44 | + args = Namespace(input="input", output="output", client="client", from_fmt="from", to_fmt="to") |
41 | 45 | res = convert(args) |
42 | 46 |
|
43 | 47 | assert res == RESULT_SUCCESS |
44 | | - mock_get_source_converter.assert_called_once_with(args.input) |
| 48 | + mock_get_source_converter.assert_called_once_with(args.from_fmt, args.to_fmt) |
45 | 49 | mock_get_source_converter.return_value.assert_called_once_with( |
46 | 50 | source_path=args.input, output_path=args.output, client_name=args.client |
47 | 51 | ) |
| 52 | + |
| 53 | + |
| 54 | +def test_converters(): |
| 55 | + assert CONVERTERS.get("harvest") == HARVEST_CONVERTERS |
| 56 | + assert CONVERTERS.get("toggl") == TOGGL_CONVERTERS |
0 commit comments