odoo-timesheets/tests/test_config.py
Jef Roosens 8b6f0b24e2
Add date_format config key for csv command
- Add get_date_format() to config.py: reads [csv] date_format, returns
  None when absent (falls back to the default %d/%m/%y)
- _cmd_csv applies the format to both single-day and weekly date strings
- Add 3 tests for get_date_format in TestGetters
- Update timesheets.example.toml and README with [csv] date_format key
2026-06-02 09:31:15 +02:00

98 lines
3.1 KiB
Python

import sys
from pathlib import Path
import pytest
from timesheets.config import (
DEFAULT_CONFIG_FILENAME,
find_default_config,
get_date_format,
get_map_path,
get_token,
load_config,
)
# ---------------------------------------------------------------------------
# load_config
# ---------------------------------------------------------------------------
class TestLoadConfig:
def test_none_returns_empty(self):
assert load_config(None) == {}
def test_loads_valid_toml(self, tmp_path):
f = tmp_path / "timesheets.toml"
f.write_text('[joplin]\ntoken = "abc123"\n')
result = load_config(f)
assert result == {"joplin": {"token": "abc123"}}
def test_missing_file_exits(self, tmp_path, capsys):
with pytest.raises(SystemExit):
load_config(tmp_path / "nonexistent.toml")
assert "not found" in capsys.readouterr().err
def test_invalid_toml_exits(self, tmp_path, capsys):
f = tmp_path / "bad.toml"
f.write_text("not = valid = toml\n")
with pytest.raises(SystemExit):
load_config(f)
assert "could not parse" in capsys.readouterr().err
def test_accepts_str_path(self, tmp_path):
f = tmp_path / "timesheets.toml"
f.write_text('[projects]\nmap = "/some/path.json"\n')
result = load_config(str(f))
assert result["projects"]["map"] == "/some/path.json"
# ---------------------------------------------------------------------------
# find_default_config
# ---------------------------------------------------------------------------
class TestFindDefaultConfig:
def test_returns_none_when_absent(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
assert find_default_config() is None
def test_returns_path_when_present(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
cfg = tmp_path / DEFAULT_CONFIG_FILENAME
cfg.write_text("")
result = find_default_config()
assert result == cfg
# ---------------------------------------------------------------------------
# get_token / get_map_path
# ---------------------------------------------------------------------------
class TestGetters:
def test_get_token_present(self):
assert get_token({"joplin": {"token": "xyz"}}) == "xyz"
def test_get_token_missing_section(self):
assert get_token({}) is None
def test_get_token_missing_key(self):
assert get_token({"joplin": {}}) is None
def test_get_map_path_present(self):
assert get_map_path({"projects": {"map": "/a/b.json"}}) == "/a/b.json"
def test_get_map_path_missing_section(self):
assert get_map_path({}) is None
def test_get_map_path_missing_key(self):
assert get_map_path({"projects": {}}) is None
def test_get_date_format_present(self):
assert get_date_format({"csv": {"date_format": "%Y-%m-%d"}}) == "%Y-%m-%d"
def test_get_date_format_missing_section(self):
assert get_date_format({}) is None
def test_get_date_format_missing_key(self):
assert get_date_format({"csv": {}}) is None