From eaaef016e3339a42a3be1ec075116d7644fe27f3 Mon Sep 17 00:00:00 2001
From: garion0000 <michele.garioni@unibas.ch>
Date: Wed, 8 Dec 2021 13:53:52 +0100
Subject: [PATCH] fix: add testing for cli

---
 tests/test_sampleinput.py | 42 +++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/tests/test_sampleinput.py b/tests/test_sampleinput.py
index c03630f..70d8eea 100644
--- a/tests/test_sampleinput.py
+++ b/tests/test_sampleinput.py
@@ -2,11 +2,20 @@
 
 import pytest
 import pandas as pd
+import os
+import importlib.util
+from pathlib import Path
+import sys
 
-from src.sample_from_input import sample_from_input
+from src.sample_from_input import (
+    sample_from_input,
+    main
+    )
+
+PACKAGE_DIR = Path(__file__).resolve().parents[1] / 'src'
 
 
-def test_sampleinput(tmpdir):
+def test_sample_from_input(tmpdir):
     """Tests the output, input file name and separator."""
     sample_from_input(
         input_file='./tests/resources/Transcript2.tsv',
@@ -14,9 +23,34 @@ def test_sampleinput(tmpdir):
         sep='\t',
         n=142958
     )
-    t1=pd.read_table(tmpdir / 'test1.csv', header=None, sep=',')
-    assert t1[1].sum()==142958
+    t1 = pd.read_table(tmpdir / 'test1.csv', header=None, sep=',')
+    assert t1[1].sum() == 142958
     with pytest.raises(IndexError):
         sample_from_input(input_file='./tests/resources/Transcript2.tsv')
     with pytest.raises(IOError):
         sample_from_input(input_file='file_not_existing.txt')
+
+
+def test_main_with_args(monkeypatch, tmpdir):
+    """Call with args."""
+    monkeypatch.setattr(
+        sys, 'argv', [
+            'sample_from_input',
+            './tests/resources/Transcript1.csv',
+            '-o',
+            str(tmpdir / 'test1.csv'),
+        ]
+    )
+    ret_val = main()
+    assert ret_val is None
+
+
+def test_main_as_script():
+    """Run as script."""
+    main_file = PACKAGE_DIR / "sample_from_input.py"
+    fl = os.path.join(os.path.dirname(__file__), main_file)
+    spec = importlib.util.spec_from_file_location('__main__', fl)
+    module = importlib.util.module_from_spec(spec)
+    with pytest.raises(SystemExit) as exc:
+        spec.loader.exec_module(module)
+    assert exc.value.code != 0
\ No newline at end of file
-- 
GitLab