Browse Source

refactor integration tests: use common assert function

Fabian Peter Hammerle 5 years ago
parent
commit
026599dc3a
1 changed files with 34 additions and 31 deletions
  1. 34 31
      tests/hippocampus_test.py

+ 34 - 31
tests/hippocampus_test.py

@@ -1,6 +1,7 @@
 import io
 import os
 import re
+import typing
 import unittest.mock
 
 import pandas
@@ -146,6 +147,20 @@ def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
         check_like=True,
     )
 
+def assert_main_volume_frame_equals(capsys, argv: list, expected_frame: pandas.DataFrame,
+                                    subjects_dir: typing.Optional[str] = None):
+    if subjects_dir:
+        os.environ['SUBJECTS_DIR'] = subjects_dir
+    elif 'SUBJECTS_DIR' in os.environ:
+        del os.environ['SUBJECTS_DIR']
+    with unittest.mock.patch('sys.argv', [''] + argv):
+        freesurfer_volume_reader.main()
+    out, _ = capsys.readouterr()
+    assert_volume_frames_equal(
+        left=expected_frame,
+        right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
+    )
+
 
 @pytest.mark.parametrize(('root_dir_paths', 'expected_csv_path'), [
     ([os.path.join(SUBJECTS_DIR, 'alice')],
@@ -157,14 +172,10 @@ def assert_volume_frames_equal(left: pandas.DataFrame, right: pandas.DataFrame):
      os.path.join(SUBJECTS_DIR, 'all-hippocampal-volumes.csv')),
 ])
 def test_main_root_dir_param(capsys, root_dir_paths: list, expected_csv_path):
-    if 'SUBJECTS_DIR' in os.environ:
-        del os.environ['SUBJECTS_DIR']
-    with unittest.mock.patch('sys.argv', [''] + root_dir_paths):
-        freesurfer_volume_reader.main()
-    out, _ = capsys.readouterr()
-    assert_volume_frames_equal(
-        left=pandas.read_csv(expected_csv_path),
-        right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
+    assert_main_volume_frame_equals(
+        argv=root_dir_paths,
+        expected_frame=pandas.read_csv(expected_csv_path),
+        capsys=capsys,
     )
 
 
@@ -173,13 +184,11 @@ def test_main_root_dir_param(capsys, root_dir_paths: list, expected_csv_path):
      os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
 ])
 def test_main_root_dir_env(capsys, root_dir_path, expected_csv_path):
-    os.environ['SUBJECTS_DIR'] = root_dir_path
-    with unittest.mock.patch('sys.argv', ['']):
-        freesurfer_volume_reader.main()
-    out, _ = capsys.readouterr()
-    assert_volume_frames_equal(
-        left=pandas.read_csv(expected_csv_path),
-        right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
+    assert_main_volume_frame_equals(
+        argv=[],
+        subjects_dir=root_dir_path,
+        expected_frame=pandas.read_csv(expected_csv_path),
+        capsys=capsys,
     )
 
 
@@ -193,26 +202,20 @@ def test_main_root_dir_env(capsys, root_dir_path, expected_csv_path):
      os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv')),
 ])
 def test_main_root_dir_overwrite_env(capsys, root_dir_path, subjects_dir, expected_csv_path):
-    os.environ['SUBJECTS_DIR'] = subjects_dir
-    with unittest.mock.patch('sys.argv', ['', root_dir_path]):
-        freesurfer_volume_reader.main()
-    out, _ = capsys.readouterr()
-    assert_volume_frames_equal(
-        left=pandas.read_csv(expected_csv_path),
-        right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
+    assert_main_volume_frame_equals(
+        argv=[root_dir_path],
+        subjects_dir=subjects_dir,
+        expected_frame=pandas.read_csv(expected_csv_path),
+        capsys=capsys,
     )
 
 
 def test_main_root_dir_filename_regex(capsys):
-    if 'SUBJECTS_DIR' in os.environ:
-        del os.environ['SUBJECTS_DIR']
-    with unittest.mock.patch('sys.argv', ['', '--filename-regex', r'^.*-T1-T2\.v10\.txt$',
-                                          os.path.join(SUBJECTS_DIR, 'bert')]):
-        freesurfer_volume_reader.main()
-    out, _ = capsys.readouterr()
     expected_volume_frame = pandas.read_csv(
         os.path.join(SUBJECTS_DIR, 'bert', 'hippocampal-volumes.csv'))
-    assert_volume_frames_equal(
-        left=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
-        right=pandas.read_csv(io.StringIO(out)).drop(columns=['source_path']),
+    assert_main_volume_frame_equals(
+        argv=['--filename-regex', r'^.*-T1-T2\.v10\.txt$',
+              os.path.join(SUBJECTS_DIR, 'bert')],
+        expected_frame=expected_volume_frame[expected_volume_frame['analysis_id'] == 'T2'].copy(),
+        capsys=capsys,
     )