- #13 (critical): Add CoveredTableCell elements to ODT for valid row spans (ODF 1.2) - #28: Move "Total issues" info message from stdout to stderr (clean pipe output) - #27: Wrap HTML export in full document with DOCTYPE and meta charset utf-8 - #26: Save CSV with utf-8-sig encoding (UTF-8 BOM for Excel compatibility) - #31: Document CSV uses full project/version values (not display_* like console/MD) - #30: Fix ODT header formatting when author is empty (no leading dot/space) - #36: Remove test_cli_smoke_empty testing unreachable code path (return []) - #37: Remove unused mock_path variable in ODT test fixture - #34: Remove unreachable len(parts) != 2 check in parse_date_range Closes #13, #28, #27, #26, #31, #30, #36, #37, #34
117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
import os
|
||
from unittest import mock
|
||
|
||
import pytest
|
||
|
||
from redmine_reporter.cli import main, parse_date_range
|
||
|
||
VALID_ENV = {
|
||
"REDMINE_URL": "https://red.eltex.loc",
|
||
"REDMINE_API_KEY": "token",
|
||
}
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"date_arg, expected",
|
||
[
|
||
("2026-01-01--2026-01-31", ("2026-01-01", "2026-01-31")),
|
||
(" 2026-01-01 -- 2026-01-31 ", ("2026-01-01", "2026-01-31")),
|
||
],
|
||
)
|
||
def test_parse_date_range_valid(date_arg, expected):
|
||
assert parse_date_range(date_arg) == expected
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"date_arg",
|
||
[
|
||
"20260101-20260131",
|
||
"2026-1-01--2026-01-31",
|
||
"2026-02-30--2026-03-01",
|
||
"2026-02-01--2026-01-31",
|
||
],
|
||
)
|
||
def test_parse_date_range_invalid(date_arg):
|
||
with pytest.raises(ValueError):
|
||
parse_date_range(date_arg)
|
||
|
||
|
||
@mock.patch.dict(os.environ, VALID_ENV, clear=True)
|
||
@mock.patch("redmine_reporter.cli.fetch_issues_with_spent_time")
|
||
def test_cli_returns_zero_on_no_entries(mock_fetch):
|
||
"""None от fetch (нет time entries) -- выход 0."""
|
||
mock_fetch.return_value = None
|
||
code = main(["--date", "2026-01-01--2026-01-31"])
|
||
assert code == 0
|
||
|
||
|
||
@mock.patch.dict(os.environ, {}, clear=True)
|
||
def test_cli_config_error():
|
||
"""Невалидный конфиг -- выход 1."""
|
||
code = main(["--date", "2026-01-01--2026-01-31"])
|
||
assert code == 1
|
||
|
||
|
||
@mock.patch.dict(os.environ, VALID_ENV, clear=True)
|
||
def test_cli_invalid_date_format():
|
||
"""Неверный формат даты -- выход 1."""
|
||
code = main(["--date", "20260101-20260131"])
|
||
assert code == 1
|
||
|
||
|
||
@mock.patch.dict(os.environ, VALID_ENV, clear=True)
|
||
@mock.patch("redmine_reporter.cli.fetch_issues_with_spent_time")
|
||
def test_cli_unknown_output_extension(mock_fetch, tmp_path):
|
||
"""Неизвестное расширение файла -- выход 1."""
|
||
mock_fetch.return_value = []
|
||
output = str(tmp_path / "report.xyz")
|
||
code = main(["--date", "2026-01-01--2026-01-31", "--output", output])
|
||
assert code == 1
|
||
|
||
|
||
@mock.patch.dict(os.environ, VALID_ENV, clear=True)
|
||
@mock.patch("redmine_reporter.cli.fetch_issues_with_spent_time")
|
||
def test_cli_output_without_extension(mock_fetch, tmp_path):
|
||
"""Файл без расширения -- выход 1 с подсказкой."""
|
||
mock_fetch.return_value = []
|
||
output = str(tmp_path / "report")
|
||
code = main(["--date", "2026-01-01--2026-01-31", "--output", output])
|
||
assert code == 1
|
||
|
||
|
||
@mock.patch.dict(os.environ, VALID_ENV, clear=True)
|
||
@mock.patch("redmine_reporter.cli.fetch_issues_with_spent_time")
|
||
@mock.patch("redmine_reporter.cli.get_formatter_by_extension")
|
||
def test_cli_odt_missing_odfpy_message(mock_gf, mock_fetch, tmp_path, capsys):
|
||
"""При запросе .odt без odfpy — выход 1, понятное сообщение про odfpy."""
|
||
mock_fetch.return_value = []
|
||
mock_gf.return_value = None
|
||
output = str(tmp_path / "report.odt")
|
||
code = main(["--date", "2026-01-01--2026-01-31", "--output", output])
|
||
assert code == 1
|
||
captured = capsys.readouterr()
|
||
assert "odfpy" in captured.err
|
||
|
||
|
||
class _MockIssue:
|
||
"""Простой mock Redmine Issue для CLI-тестов."""
|
||
|
||
def __init__(self, issue_id=1, subject="Task", project="Project", status="New"):
|
||
self.id = issue_id
|
||
self.subject = subject
|
||
self.project = project
|
||
self.status = status
|
||
self.fixed_version = None
|
||
|
||
|
||
@mock.patch.dict(os.environ, VALID_ENV, clear=True)
|
||
@mock.patch("redmine_reporter.cli.fetch_issues_with_spent_time")
|
||
def test_total_issues_message_goes_to_stderr(mock_fetch, capsys):
|
||
"""«Total issues» пишется в stderr, не загрязняя stdout при pipe (#28)."""
|
||
issue = _MockIssue()
|
||
mock_fetch.return_value = [(issue, 1.0)]
|
||
main(["--date", "2026-01-01--2026-01-31"])
|
||
captured = capsys.readouterr()
|
||
assert "Total issues" not in captured.out
|
||
assert "Total issues" in captured.err
|