From 76c37a99c13dbe456f0aa5adcf7439778e84a4c3 Mon Sep 17 00:00:00 2001
From: Anne Philipp <anne.philipp@univie.ac.at>
Date: Thu, 19 Dec 2019 21:11:12 +0100
Subject: [PATCH] updated unit tests to work with updated code

---
 Source/Pythontest/TestInput.py    |  28 +++---
 Source/Pythontest/TestInstall.py  |  36 +++----
 Source/Pythontest/TestTools.py    | 156 +++++++++++++-----------------
 Source/Pythontest/TestUIOFiles.py |  67 +++++++------
 Source/Pythontest/_config_test.py |   6 +-
 Source/Pythontest/conftest.py     |   4 +-
 6 files changed, 147 insertions(+), 150 deletions(-)

diff --git a/Source/Pythontest/TestInput.py b/Source/Pythontest/TestInput.py
index ff19076..596d9c7 100644
--- a/Source/Pythontest/TestInput.py
+++ b/Source/Pythontest/TestInput.py
@@ -1,13 +1,13 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 import os
 import sys
 import pytest
 
-sys.path.append('../python')
-from classes.ControlFile import ControlFile
-from mods.tools import get_cmdline_arguments
+sys.path.append('../Python')
+from Classes.ControlFile import ControlFile
+from Mods.tools import get_cmdline_args
 
 
 class TestInput():
@@ -20,7 +20,8 @@ class TestInput():
     # 1. nur controlfile reading
     # 2. check of parameter
 
-    def __init__(self):
+    @classmethod
+    def setup_class(self):
         # Default values for ArgumentParser
         self.args = {'start_date':None,
                      'end_date':None,
@@ -35,16 +36,16 @@ class TestInput():
                      'ppid':None,
                      'job_template':'job.temp',
                      'queue':None,
-                     'controlfile':'CONTROL.temp',
+                     'controlfile':'CONTROL.test',
                      'debug':0,
                      }
         #sys.argv = ['dummy.py', '--start_date=20180101', '--debug=1',
         #            '--step=0/to/11/BY/3', '--area=20./20./0./90.']
         sys.argv = ['dummy.py', '--start_date=20180101']
 
-        self.args = tools.get_commandline_arguments()
+        self.args = get_cmdline_args()
 
-        self.c = ControlFile('TestData/CONTROL.temp')
+        self.c = ControlFile('../../Testing/Regression/Unit/Testfiles/CONTROL.test')
 
         self.c.assign_args_to_control(self.args)
 
@@ -56,7 +57,7 @@ class TestInput():
         sys.argv = ['dummy.py', '--start_date=20180101', '--debug=1',
                     '--step=0/to/11/BY/3', '--area=20./20./0./90.']
 
-        arguments = tools.get_cmdline_arguments()
+        arguments = get_cmdline_args()
 
         args_exp = {'start_date':'20180101',
                     'end_date':None,
@@ -71,7 +72,7 @@ class TestInput():
                     'ppid':None,
                     'job_template':'job.temp',
                     'queue':None,
-                    'controlfile':'CONTROL.temp',
+                    'controlfile':'CONTROL.test',
                     'debug':1,
                     }
 
@@ -88,7 +89,7 @@ class TestInput():
                     'addpar': ['186', '187', '188', '235', '139', '39'],
                     'area': None,
                     'basetime': None,
-                    'controlfile': 'CONTROL.temp',
+                    'controlfile': 'CONTROL.test',
                     'cwc': 0,
                     'date_chunk': 3,
                     'debug': 0,
@@ -157,3 +158,8 @@ class TestInput():
         assert cdict == exp_dict
 
         return
+
+    @classmethod
+    def teardown_class(self):
+ 
+        return
diff --git a/Source/Pythontest/TestInstall.py b/Source/Pythontest/TestInstall.py
index fb3a643..0e9c086 100644
--- a/Source/Pythontest/TestInstall.py
+++ b/Source/Pythontest/TestInstall.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 
@@ -10,18 +10,22 @@ import tarfile
 import errno
 import shutil
 from genshi.template import TemplateLoader
-from genshi.template.eval import  UndefinedError
-from exceptions import OSError
+from genshi.template.eval import UndefinedError
+
+try:
+    import exceptions
+except ImportError:
+    import builtins
 import pytest
 from mock import patch
 
-sys.path.append('../python')
+sys.path.append('../Python')
 import _config
-import _config_test
+from . import _config_test
 from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob,
                      mk_job_template)
 
-from mods.tools import make_dir, silent_remove
+from Mods.tools import make_dir, silent_remove
 
 #    - main
 #    - get_install_cmdline_arguments
@@ -43,8 +47,8 @@ class TestInstall():
                                               'mk_install_tar.sh')])
 
         # un tar the test tarballs from shell script
-        subprocess.check_output([os.path.join(self.testinstalldir,
-                                              'un_install_tar.sh')])
+#        subprocess.check_output([os.path.join(self.testinstalldir,
+#                                            'un_install_tar.sh')])
 
 
 
@@ -57,8 +61,6 @@ class TestInstall():
 
         with pytest.raises(SystemExit):
             mk_tarball(ecd + tarballname, 'local')
-        with pytest.raises(SystemExit):
-            mk_tarball(ecd + tarballname, 'local')
 
     def test_success_mk_tarball_local(self):
         ecd = _config.PATH_FLEXEXTRACT_DIR + os.path.sep
@@ -77,7 +79,7 @@ class TestInstall():
             tar_content_list = tar_handle.getnames()
 
         # remove test tar file from flex_extract directory
-        os.remove(ecd + tarballname)
+        #os.remove(ecd + tarballname)
 
         # test if comparison filelist is equal to the
         # filelist of tarball content
@@ -309,22 +311,22 @@ class TestInstall():
 
         test_dir = os.path.join(self.testinstalldir,
                                 _config.FLEXEXTRACT_DIRNAME + '_local')
-        shutil.rmtree(test_dir)
+#        shutil.rmtree(test_dir)
         test_dir = os.path.join(self.testinstalldir,
                                 _config.FLEXEXTRACT_DIRNAME + '_ecgate')
-        shutil.rmtree(test_dir)
+#        shutil.rmtree(test_dir)
 
         test_dir = os.path.join(self.testinstalldir,
                                 'test_local')
-        shutil.rmtree(test_dir)
+#        shutil.rmtree(test_dir)
         test_dir = os.path.join(self.testinstalldir,
                                 'test_ecgate')
-        shutil.rmtree(test_dir)
+#        shutil.rmtree(test_dir)
 
         tar_file = os.path.join(self.testinstalldir,
                      _config.FLEXEXTRACT_DIRNAME + '_local.tar')
-        os.remove(tar_file)
+#        os.remove(tar_file)
         tar_file = os.path.join(self.testinstalldir,
                                 _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar')
-        os.remove(tar_file)
+#        os.remove(tar_file)
         pass
diff --git a/Source/Pythontest/TestTools.py b/Source/Pythontest/TestTools.py
index 4653711..0b88578 100644
--- a/Source/Pythontest/TestTools.py
+++ b/Source/Pythontest/TestTools.py
@@ -6,26 +6,31 @@
 import os
 import sys
 import errno
-from exceptions import OSError
+#from exceptions import OSError
 import subprocess
 import pipes
+
+try:
+    import exceptions
+except ImportError:
+    import builtins
+
 import pytest
 from mock import patch, call
 #from mock import PropertyMock
 
 
 import _config
-import _config_test
-from classes.ControlFile import ControlFile
-from mods.tools import (none_or_str, none_or_int, get_cmdline_arguments,
+from . import _config_test
+from Classes.ControlFile import ControlFile
+from Mods.tools import (none_or_str, none_or_int, get_cmdline_args,
                         read_ecenv, clean_up, my_error, send_mail,
                         normal_exit, product, silent_remove,
                         init128, to_param_id, get_list_as_string, make_dir,
                         put_file_to_ecserver, submit_job_to_ecserver)
 
 class TestTools(object):
-    '''
-    '''
+    """Test the tools module."""
 
     def setup_method(self):
         self.testdir = _config_test.PATH_TEST_DIR
@@ -47,51 +52,56 @@ class TestTools(object):
     def test_fail_get_cmdline_arguments(self):
         sys.argv = ['dummy.py', '--wrong=1']
         with pytest.raises(SystemExit):
-            results = get_cmdline_arguments()
+            results = get_cmdline_args()
 
     def test_default_get_cmdline_arguments(self):
-        cmd_dict_control = {'start_date':None,
-                            'end_date':None,
-                            'date_chunk':None,
-                            'basetime':None,
-                            'step':None,
-                            'levelist':None,
-                            'area':None,
-                            'inputdir':None,
-                            'outputdir':None,
-                            'flexpart_root_scripts':None,
-                            'ppid':None,
-                            'job_template':'job.temp',
-                            'queue':None,
-                            'controlfile':'CONTROL.temp',
-                            'debug':None,
-                            'public':None,
-                            'request':None}
+        cmd_dict_control = {'start_date': None,
+                            'end_date': None,
+                            'date_chunk': None,
+                            'basetime': None,
+                            'step': None,
+                            'levelist': None,
+                            'area': None,
+                            'inputdir': None,
+                            'outputdir': None,
+                            'job_template': None,
+                            'job_chunk': None,
+                            'ppid': None,
+                            'job_template': 'job.temp',
+                            'queue': None,
+                            'controlfile': 'CONTROL_EA5',
+                            'debug': None,
+                            'public': None,
+                            'request': None,
+                            'oper': None,
+                            'rrint': None}
 
         sys.argv = ['dummy.py']
 
-        results = get_cmdline_arguments()
+        results = get_cmdline_args()
 
         assert cmd_dict_control == vars(results)
 
     def test_input_get_cmdline_arguments(self):
-        cmd_dict_control = {'start_date':'20180101',
-                            'end_date':'20180101',
-                            'date_chunk':3,
-                            'basetime':12,
-                            'step':'1',
-                            'levelist':'1/to/10',
-                            'area':'50/10/60/20',
-                            'inputdir':'../work',
-                            'outputdir':None,
-                            'flexpart_root_scripts':'../',
-                            'ppid':1234,
-                            'job_template':'job.sh',
-                            'queue':'ecgate',
-                            'controlfile':'CONTROL.WORK',
-                            'debug':1,
-                            'public':None,
-                            'request':0}
+        cmd_dict_control = {'start_date': '20180101',
+                            'end_date': '20180101',
+                            'date_chunk': 3,
+                            'basetime': 12,
+                            'step': '1',
+                            'levelist': '1/to/10',
+                            'area': '50/10/60/20',
+                            'inputdir': '../work',
+                            'outputdir': None,
+                            'ppid': '1234',
+                            'job_template': 'job.sh',
+                            'queue': 'ecgate',
+                            'controlfile': 'CONTROL.WORK',
+                            'debug': 1,
+                            'public': None,
+                            'request': 0,
+                            'rrint': 0,
+                            'job_chunk': None,
+                            'oper': 0}
 
         sys.argv = ['dummy.py',
                     '--start_date=20180101',
@@ -103,16 +113,18 @@ class TestTools(object):
                     '--area=50/10/60/20',
                     '--inputdir=../work',
                     '--outputdir=None',
-                    '--flexpart_root_scripts=../',
                     '--ppid=1234',
                     '--job_template=job.sh',
                     '--queue=ecgate',
                     '--controlfile=CONTROL.WORK',
                     '--debug=1',
                     '--public=None',
-                    '--request=0']
+                    '--request=0',
+                    '--rrint=0',
+                    '--job_chunk=None',
+                    '--oper=0']
 
-        results = get_cmdline_arguments()
+        results = get_cmdline_args()
 
         assert cmd_dict_control == vars(results)
 
@@ -120,10 +132,10 @@ class TestTools(object):
         table128 = init128(_config.PATH_GRIBTABLE)
         expected_sample = {'078': 'TCLW', '130': 'T', '034': 'SST'}
         # check a sample of parameters which must have been read in
-        assert all((k in table128 and table128[k]==v)
-                   for k,v in expected_sample.iteritems())
+        assert all((k in table128 and table128[k] == v)
+                   for k, v in expected_sample.items())
 
-    @patch('__builtin__.open', side_effect=[OSError(errno.EEXIST)])
+    @patch('builtins.open', side_effect=[OSError(errno.EEXIST)])
     def test_fail_open_init128(self, mock_openfile):
         with pytest.raises(SystemExit):
             table128 = init128(_config.PATH_GRIBTABLE)
@@ -144,10 +156,10 @@ class TestTools(object):
         assert sorted(ipars) == sorted(opar_listint)
 
     @patch('traceback.format_stack', return_value='empty trace')
-    @patch('mods.tools.send_mail', return_value=0)
+    @patch('Mods.tools.send_mail', return_value=0)
     def test_success_my_error(self, mock_mail, mock_trace, capfd):
         with pytest.raises(SystemExit):
-            my_error(['any_user'], 'Failed!')
+            my_error('Failed!')
             out, err = capfd.readouterr()
             assert out == "Failed!\n\nempty_trace\n"
 
@@ -159,7 +171,7 @@ class TestTools(object):
                                                    stdout=subprocess.PIPE)
         send_mail(['${USER}', 'any_user'], 'ERROR', message='error mail')
         out, err = capfd.readouterr()
-        assert out == b'Email sent to user\nEmail sent to user\n'
+        assert out == 'Email sent to user\nEmail sent to user\n'
 
     @patch('subprocess.Popen')
     @patch('os.path.expandvars', return_value='any_user')
@@ -168,7 +180,7 @@ class TestTools(object):
                                                    stdout=subprocess.PIPE)
         send_mail(['any-user'], 'ERROR', message='error mail')
         out, err = capfd.readouterr()
-        assert out == b'Email sent to any_user\n'
+        assert out == 'Email sent to any_user\n'
 
     @patch('subprocess.Popen', side_effect=[ValueError, OSError])
     @patch('os.path.expandvars', return_value='any_user')
@@ -188,13 +200,13 @@ class TestTools(object):
 
         assert envs_ref == envs
 
-    @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
+    @patch('builtins.open', side_effect=[OSError(errno.EPERM)])
     def test_fail_read_ecenv(self, mock_open):
         with pytest.raises(SystemExit):
             read_ecenv('any_file')
 
     @patch('glob.glob', return_value=[])
-    @patch('mods.tools.silent_remove')
+    @patch('Mods.tools.silent_remove')
     def test_empty_clean_up(self, mock_rm, mock_clean):
         clean_up(self.c)
         mock_rm.assert_not_called()
@@ -202,49 +214,13 @@ class TestTools(object):
     @patch('glob.glob', return_value=['any_file','EIfile'])
     @patch('os.remove', return_value=0)
     def test_success_clean_up(self, mock_rm, mock_glob):
-        # ectrans=0; ecstorage=0; ecapi=None; prefix not in filename
-        clean_up(self.c)
-        mock_rm.assert_has_calls([call('any_file'), call('EIfile')])
-        mock_rm.reset_mock()
 
-        # ectrans=0; ecstorage=0; ecapi=False; prefix in filename
         self.c.prefix = 'EI'
         self.c.ecapi = False
         clean_up(self.c)
         mock_rm.assert_has_calls([call('any_file')])
         mock_rm.reset_mock()
 
-        # ectrans=0; ecstorage=0; ecapi=True; prefix in filename
-        self.c.prefix = 'EI'
-        self.c.ecapi = True
-        clean_up(self.c)
-        mock_rm.assert_has_calls([call('any_file')])
-        mock_rm.reset_mock()
-
-        # ectrans=1; ecstorage=0; ecapi=True; prefix in filename
-        self.c.prefix = 'EI'
-        self.c.ecapi = True
-        self.c.ectrans = 1
-        clean_up(self.c)
-        mock_rm.assert_has_calls([call('any_file')])
-        mock_rm.reset_mock()
-
-        # ectrans=1; ecstorage=0; ecapi=False; prefix in filename
-        self.c.prefix = 'EI'
-        self.c.ecapi = False
-        self.c.ectrans = 1
-        clean_up(self.c)
-        mock_rm.assert_has_calls([call('any_file'), call('EIfile')])
-        mock_rm.reset_mock()
-
-        # ectrans=1; ecstorage=1; ecapi=False; prefix in filename
-        self.c.prefix = 'EI'
-        self.c.ecapi = False
-        self.c.ectrans = 1
-        self.c.ecstorage = 1
-        clean_up(self.c)
-        mock_rm.assert_has_calls([call('any_file'), call('EIfile')])
-        mock_rm.reset_mock()
 
     def test_default_normal_exit(self, capfd):
         normal_exit()
diff --git a/Source/Pythontest/TestUIOFiles.py b/Source/Pythontest/TestUIOFiles.py
index 239d1f6..c5018b7 100644
--- a/Source/Pythontest/TestUIOFiles.py
+++ b/Source/Pythontest/TestUIOFiles.py
@@ -4,44 +4,57 @@
 import os
 import sys
 import pytest
+from mock import patch
 
-sys.path.append('../python')
-from classes.UioFiles import UioFiles
+from . import _config_test
+sys.path.append('../Python')
 
+from Classes.UioFiles import UioFiles
 
-class TestUioFiles():
-    '''
-    Test class to test the UIOFiles methods.
-    '''
 
-    def test_listFiles(self):
-        '''
-        @Description:
-            Test the listFiles method from class UIOFiles.
+class TestUioFiles():
+    """Test class to test the UIOFiles methods."""
 
-        @Input:
-            self: instance of TestClass
-                Class to test the UIOFiles methods.
+    @classmethod
+    def setup_class(self):
+        """Setup status"""
+        self.testpath = os.path.join(_config_test.PATH_TEST_DIR, 'Dir')
+        # Initialise and collect filenames
+        self.files = UioFiles(self.testpath, '*.grb')
 
-        @Return:
-            <nothing>
-        '''
+    def test_listFiles(self):
+        """Test the listFiles method from class UIOFiles."""
         # set comparison information
-        self.testpath = os.path.join(os.path.dirname(__file__), 'TestDir')
         self.expected = ['FCGG__SL.20160410.40429.16424.grb',
-                             'FCOG__ML.20160410.40429.16424.grb',
-                             'FCSH__ML.20160410.40429.16424.grb',
-                             'OG_OROLSM__SL.20160410.40429.16424.grb',
-                             'FCOG_acc_SL.20160409.40429.16424.grb',
-                             'FCOG__SL.20160410.40429.16424.grb',
-                             'FCSH__SL.20160410.40429.16424.grb']
+                         'FCOG__ML.20160410.40429.16424.grb',
+                         'FCSH__ML.20160410.40429.16424.grb',
+                         'OG_OROLSM__SL.20160410.40429.16424.grb',
+                         'FCOG_acc_SL.20160409.40429.16424.grb',
+                         'FCOG__SL.20160410.40429.16424.grb',
+                         'FCSH__SL.20160410.40429.16424.grb']
 
-        # Initialise and collect filenames
-        files = UioFiles(self.testpath, '*.grb')
         # get the basename to just check for equality of filenames
-        filelist = [os.path.basename(f) for f in files.files]
+        filelist = [os.path.basename(f) for f in self.files.files]
         # comparison of expected filenames against the collected ones
         assert sorted(self.expected) == sorted(filelist)
 
-        return
 
+    def test_delete_files(self):
+        """Test if a file is deleted."""
+        testfile = os.path.join(self.testpath, 'test.test')
+        open(testfile, 'w').close()
+        iofile = UioFiles(testfile, 'test.test')
+        iofile.delete_files()
+        assert [] == UioFiles(testfile, 'test.test').files
+
+
+    def test_str_(self):
+        """Test if list of file is correctly converted to string."""
+        self.expected = "FCSH__SL.20160410.40429.16424.grb, "\
+                        "FCSH__ML.20160410.40429.16424.grb, "\
+                        "FCOG__SL.20160410.40429.16424.grb, "\
+                        "FCOG__ML.20160410.40429.16424.grb, "\
+                        "OG_OROLSM__SL.20160410.40429.16424.grb, "\
+                        "FCGG__SL.20160410.40429.16424.grb, "\
+                        "FCOG_acc_SL.20160409.40429.16424.grb"
+        assert self.expected == self.files.__str__()
diff --git a/Source/Pythontest/_config_test.py b/Source/Pythontest/_config_test.py
index 630608f..e9fcca4 100644
--- a/Source/Pythontest/_config_test.py
+++ b/Source/Pythontest/_config_test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 '''
 *******************************************************************************
@@ -26,7 +26,7 @@
 import os
 import sys
 
-sys.path.append('../python')
+sys.path.append('../Python')
 import _config
 
 # ------------------------------------------------------------------------------
@@ -37,7 +37,7 @@ import _config
 # ------------------------------------------------------------------------------
 # DIRECTORY NAMES
 # ------------------------------------------------------------------------------
-TEST_DIR = 'test/Unit'
+TEST_DIR = 'Testing/Regression/Unit'
 TESTFILES_DIR = 'Testfiles'
 TESTINSTALL_DIR = 'InstallTar'
 # ------------------------------------------------------------------------------
diff --git a/Source/Pythontest/conftest.py b/Source/Pythontest/conftest.py
index 27469f2..644c77b 100644
--- a/Source/Pythontest/conftest.py
+++ b/Source/Pythontest/conftest.py
@@ -1,10 +1,10 @@
 import sys
 import pytest
 
-sys.path.append('../python')
+sys.path.append('../Python')
 import _config
 
 #@pytest.fixture
 #def prep_test_env():
 #    testdir = _config.PATH_TEST_DIR
-print ''
+print('')
-- 
GitLab