ocado-grocy/tests/test_paths.py

60 lines
2.6 KiB
Python

from pathlib import Path
from click.testing import CliRunner
from ocado_grocy import paths
from ocado_grocy.history import main
def isolate(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
for name, directory in [('XDG_CONFIG_HOME', 'config'), ('XDG_STATE_HOME', 'state'), ('XDG_DATA_HOME', 'data')]:
monkeypatch.setenv(name, str(tmp_path / directory))
def test_xdg_paths_and_shared_journal(tmp_path, monkeypatch):
isolate(tmp_path, monkeypatch)
assert paths.config_file() == tmp_path / 'config/ocado-grocy/config.toml'
assert paths.browser_directory() == tmp_path / 'state/ocado-grocy/browser'
assert paths.archive_directory() == tmp_path / 'data/ocado-grocy/history'
assert paths.journal_file({}, paths.config_file()) == tmp_path / 'state/ocado-grocy/imports.sqlite3'
def test_legacy_installation_keeps_its_journal_and_archive(tmp_path, monkeypatch):
isolate(tmp_path, monkeypatch)
(tmp_path / 'config.toml').write_text('')
assert paths.config_file() == Path('config.toml')
assert paths.browser_directory() == Path('.ocado-browser')
assert paths.archive_directory() == Path('history')
assert paths.journal_file({}, paths.config_file()) == tmp_path / 'imports.sqlite3'
def test_custom_journal_paths(tmp_path, monkeypatch):
isolate(tmp_path, monkeypatch)
config = tmp_path / 'custom/config.toml'
assert paths.journal_file({}, config) == config.parent / 'imports.sqlite3'
assert paths.journal_file({'import': {'state_file': 'journal.db'}}, config) == config.parent / 'journal.db'
target = tmp_path / 'journal.db'
assert paths.journal_file({'import': {'state_file': str(target)}}, config) == target
def test_invalid_xdg_values_use_home(tmp_path, monkeypatch):
isolate(tmp_path, monkeypatch)
monkeypatch.setattr(Path, 'home', classmethod(lambda cls: tmp_path))
monkeypatch.setenv('XDG_CONFIG_HOME', 'relative')
monkeypatch.setenv('XDG_STATE_HOME', '')
assert paths.config_file() == tmp_path / '.config/ocado-grocy/config.toml'
assert paths.state_directory() == tmp_path / '.local/state/ocado-grocy'
def test_cli_defaults_are_resolved_at_invocation(tmp_path, monkeypatch):
isolate(tmp_path, monkeypatch)
calls = []
def download(profile, archive, **kwargs):
calls.append((profile, archive))
return [], []
monkeypatch.setattr('ocado_grocy.history.download_orders', download)
result = CliRunner().invoke(main, ['--no-images', '--no-openfoodfacts'])
assert result.exit_code == 0, result.output
assert calls == [(paths.browser_directory(), paths.archive_directory())]
assert not (tmp_path / 'imports.sqlite3').exists()