test_main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import csv
  2. import io
  3. import subprocess
  4. import typing
  5. import unittest.mock
  6. import pytest
  7. from freesurfer_surface.__main__ import annotation_labels
  8. from conftest import ANNOTATION_FILE_PATH
  9. def check_rows(csv_rows: typing.List[str]):
  10. assert len(csv_rows) == 36 + 1
  11. assert csv_rows[0] == ['index', 'color', 'name']
  12. assert csv_rows[1] == ['0', '#190519', 'unknown']
  13. assert csv_rows[23] == ['22', '#dc1414', 'postcentral']
  14. assert csv_rows[25] == ['24', '#3c14dc', 'precentral']
  15. def test_annotation_labels_function(capsys):
  16. with unittest.mock.patch('sys.argv', ['', ANNOTATION_FILE_PATH]):
  17. annotation_labels()
  18. out, err = capsys.readouterr()
  19. assert not err
  20. check_rows(list(csv.reader(io.StringIO(out), delimiter='\t')))
  21. def test_annotation_labels_function_delimiter(capsys):
  22. with unittest.mock.patch('sys.argv', ['', '--delimiter', ',', ANNOTATION_FILE_PATH]):
  23. annotation_labels()
  24. out, err = capsys.readouterr()
  25. assert not err
  26. check_rows(list(csv.reader(io.StringIO(out), delimiter=',')))
  27. def test_annotation_labels_script():
  28. proc_info = subprocess.run(['freesurfer-annotation-labels', ANNOTATION_FILE_PATH],
  29. check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  30. assert not proc_info.stderr
  31. check_rows(list(csv.reader(io.StringIO(proc_info.stdout.decode()), delimiter='\t')))