mirror of
https://hub.psychoinformatics.de/actions/xlsx-to-tsv.git
synced 2026-09-10 03:46:04 +00:00
147 lines
3.3 KiB
Python
147 lines
3.3 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
from openpyxl import Workbook
|
|
|
|
from xlsx_to_tsv import (
|
|
main,
|
|
xlsx_to_tsv,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def make_xlsx(tmp_path: Path):
|
|
def _make_xlsx(filename: str = 'input.xlsx') -> Path:
|
|
path = tmp_path / filename
|
|
wb = Workbook()
|
|
|
|
ws1 = wb.active
|
|
ws1.title = 'First'
|
|
ws1.append(['a', 'b', None])
|
|
ws1.append([1, 2, 3])
|
|
|
|
ws2 = wb.create_sheet('Second')
|
|
ws2.append(['x', 'y'])
|
|
|
|
wb.save(path)
|
|
wb.close()
|
|
return path
|
|
|
|
return _make_xlsx
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
('sheet', 'expected'),
|
|
[
|
|
(None, 'a\tb\t\n1\t2\t3\n'),
|
|
('Second', 'x\ty\n'),
|
|
(1, 'x\ty\n'),
|
|
],
|
|
)
|
|
def test_xlsx_to_tsv_sheet_selection(
|
|
tmp_path: Path,
|
|
make_xlsx,
|
|
sheet,
|
|
expected,
|
|
) -> None:
|
|
input_path = make_xlsx()
|
|
output_path = tmp_path / 'output.tsv'
|
|
|
|
xlsx_to_tsv(input_path, output_path, sheet=sheet)
|
|
|
|
assert output_path.read_text(encoding='utf-8') == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
('rows', 'expected'),
|
|
[
|
|
(
|
|
[[None, 'x', None]],
|
|
'\tx\t\n',
|
|
),
|
|
(
|
|
[['only', None, 3]],
|
|
'only\t\t3\n',
|
|
),
|
|
],
|
|
)
|
|
def test_xlsx_to_tsv_none_and_values(
|
|
tmp_path: Path,
|
|
rows,
|
|
expected,
|
|
) -> None:
|
|
input_path = tmp_path / 'input.xlsx'
|
|
output_path = tmp_path / 'output.tsv'
|
|
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
for row in rows:
|
|
ws.append(row)
|
|
wb.save(input_path)
|
|
wb.close()
|
|
|
|
xlsx_to_tsv(input_path, output_path)
|
|
|
|
assert output_path.read_text(encoding='utf-8') == expected
|
|
|
|
|
|
def test_xlsx_to_tsv_accepts_path_objects(tmp_path: Path, make_xlsx) -> None:
|
|
input_path = make_xlsx()
|
|
output_path = Path(tmp_path / 'output.tsv')
|
|
|
|
xlsx_to_tsv(Path(input_path), Path(output_path), sheet='First')
|
|
|
|
assert output_path.exists()
|
|
assert output_path.read_text(encoding='utf-8').startswith('a\tb\t\n')
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
('argv', 'expected'),
|
|
[
|
|
(['input.xlsx', 'output.tsv'], 'a\tb\t\n1\t2\t3\n'),
|
|
(['input.xlsx', 'output.tsv', 'Second'], 'x\ty\n'),
|
|
(['input.xlsx', 'output.tsv', '1'], 'x\ty\n'),
|
|
],
|
|
)
|
|
def test_main_converts_file(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
make_xlsx,
|
|
argv,
|
|
expected,
|
|
) -> None:
|
|
input_path = make_xlsx()
|
|
output_path = tmp_path / 'output.tsv'
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
args = [str(input_path), str(output_path), *argv[2:]]
|
|
|
|
assert main(args) == 0
|
|
assert output_path.read_text(encoding='utf-8') == expected
|
|
|
|
|
|
def test_main_help(capsys: pytest.CaptureFixture[str]) -> None:
|
|
assert main(['--help']) == 0
|
|
captured = capsys.readouterr()
|
|
assert 'Usage:' in captured.out
|
|
assert 'INPUT' in captured.out
|
|
assert 'SHEET' in captured.out
|
|
|
|
|
|
def test_main_invalid_args_prints_usage_to_stderr(
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
assert main(['only-one-arg']) == 2 # noqa: PLR2004
|
|
captured = capsys.readouterr()
|
|
assert 'Usage:' in captured.err
|
|
|
|
|
|
def test_main_accepts_no_sheet_argument(
|
|
tmp_path: Path,
|
|
make_xlsx,
|
|
) -> None:
|
|
input_path = make_xlsx()
|
|
output_path = tmp_path / 'output.tsv'
|
|
|
|
assert main([str(input_path), str(output_path)]) == 0
|
|
assert output_path.read_text(encoding='utf-8') == 'a\tb\t\n1\t2\t3\n'
|