Changeset 76c37a9 in flex_extract.git


Ignore:
Timestamp:
Dec 19, 2019, 8:11:12 PM (4 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
d720895
Parents:
d2b7217
Message:

updated unit tests to work with updated code

Location:
Source/Pythontest
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • Source/Pythontest/TestInput.py

    rba99230 r76c37a9  
    1 #!/usr/bin/env python
     1#!/usr/bin/env python3
    22# -*- coding: utf-8 -*-
    33
     
    66import pytest
    77
    8 sys.path.append('../python')
    9 from classes.ControlFile import ControlFile
    10 from mods.tools import get_cmdline_arguments
     8sys.path.append('../Python')
     9from Classes.ControlFile import ControlFile
     10from Mods.tools import get_cmdline_args
    1111
    1212
     
    2121    # 2. check of parameter
    2222
    23     def __init__(self):
     23    @classmethod
     24    def setup_class(self):
    2425        # Default values for ArgumentParser
    2526        self.args = {'start_date':None,
     
    3637                     'job_template':'job.temp',
    3738                     'queue':None,
    38                      'controlfile':'CONTROL.temp',
     39                     'controlfile':'CONTROL.test',
    3940                     'debug':0,
    4041                     }
     
    4344        sys.argv = ['dummy.py', '--start_date=20180101']
    4445
    45         self.args = tools.get_commandline_arguments()
     46        self.args = get_cmdline_args()
    4647
    47         self.c = ControlFile('TestData/CONTROL.temp')
     48        self.c = ControlFile('../../Testing/Regression/Unit/Testfiles/CONTROL.test')
    4849
    4950        self.c.assign_args_to_control(self.args)
     
    5758                    '--step=0/to/11/BY/3', '--area=20./20./0./90.']
    5859
    59         arguments = tools.get_cmdline_arguments()
     60        arguments = get_cmdline_args()
    6061
    6162        args_exp = {'start_date':'20180101',
     
    7273                    'job_template':'job.temp',
    7374                    'queue':None,
    74                     'controlfile':'CONTROL.temp',
     75                    'controlfile':'CONTROL.test',
    7576                    'debug':1,
    7677                    }
     
    8990                    'area': None,
    9091                    'basetime': None,
    91                     'controlfile': 'CONTROL.temp',
     92                    'controlfile': 'CONTROL.test',
    9293                    'cwc': 0,
    9394                    'date_chunk': 3,
     
    158159
    159160        return
     161
     162    @classmethod
     163    def teardown_class(self):
     164 
     165        return
  • Source/Pythontest/TestInstall.py

    rba99230 r76c37a9  
    1 #!/usr/bin/env python
     1#!/usr/bin/env python3
    22# -*- coding: utf-8 -*-
    33
     
    1111import shutil
    1212from genshi.template import TemplateLoader
    13 from genshi.template.eval import  UndefinedError
    14 from exceptions import OSError
     13from genshi.template.eval import UndefinedError
     14
     15try:
     16    import exceptions
     17except ImportError:
     18    import builtins
    1519import pytest
    1620from mock import patch
    1721
    18 sys.path.append('../python')
     22sys.path.append('../Python')
    1923import _config
    20 import _config_test
     24from . import _config_test
    2125from install import (mk_tarball, un_tarball, mk_env_vars, mk_compilejob,
    2226                     mk_job_template)
    2327
    24 from mods.tools import make_dir, silent_remove
     28from Mods.tools import make_dir, silent_remove
    2529
    2630#    - main
     
    4448
    4549        # un tar the test tarballs from shell script
    46         subprocess.check_output([os.path.join(self.testinstalldir,
    47                                               'un_install_tar.sh')])
     50#        subprocess.check_output([os.path.join(self.testinstalldir,
     51#                                            'un_install_tar.sh')])
    4852
    4953
     
    5660        tarballname = _config.FLEXEXTRACT_DIRNAME + '_localtest.tar'
    5761
    58         with pytest.raises(SystemExit):
    59             mk_tarball(ecd + tarballname, 'local')
    6062        with pytest.raises(SystemExit):
    6163            mk_tarball(ecd + tarballname, 'local')
     
    7880
    7981        # remove test tar file from flex_extract directory
    80         os.remove(ecd + tarballname)
     82        #os.remove(ecd + tarballname)
    8183
    8284        # test if comparison filelist is equal to the
     
    310312        test_dir = os.path.join(self.testinstalldir,
    311313                                _config.FLEXEXTRACT_DIRNAME + '_local')
    312         shutil.rmtree(test_dir)
     314#        shutil.rmtree(test_dir)
    313315        test_dir = os.path.join(self.testinstalldir,
    314316                                _config.FLEXEXTRACT_DIRNAME + '_ecgate')
    315         shutil.rmtree(test_dir)
     317#        shutil.rmtree(test_dir)
    316318
    317319        test_dir = os.path.join(self.testinstalldir,
    318320                                'test_local')
    319         shutil.rmtree(test_dir)
     321#        shutil.rmtree(test_dir)
    320322        test_dir = os.path.join(self.testinstalldir,
    321323                                'test_ecgate')
    322         shutil.rmtree(test_dir)
     324#        shutil.rmtree(test_dir)
    323325
    324326        tar_file = os.path.join(self.testinstalldir,
    325327                     _config.FLEXEXTRACT_DIRNAME + '_local.tar')
    326         os.remove(tar_file)
     328#        os.remove(tar_file)
    327329        tar_file = os.path.join(self.testinstalldir,
    328330                                _config.FLEXEXTRACT_DIRNAME + '_ecgate.tar')
    329         os.remove(tar_file)
     331#        os.remove(tar_file)
    330332        pass
  • Source/Pythontest/TestTools.py

    rba99230 r76c37a9  
    77import sys
    88import errno
    9 from exceptions import OSError
     9#from exceptions import OSError
    1010import subprocess
    1111import pipes
     12
     13try:
     14    import exceptions
     15except ImportError:
     16    import builtins
     17
    1218import pytest
    1319from mock import patch, call
     
    1622
    1723import _config
    18 import _config_test
    19 from classes.ControlFile import ControlFile
    20 from mods.tools import (none_or_str, none_or_int, get_cmdline_arguments,
     24from . import _config_test
     25from Classes.ControlFile import ControlFile
     26from Mods.tools import (none_or_str, none_or_int, get_cmdline_args,
    2127                        read_ecenv, clean_up, my_error, send_mail,
    2228                        normal_exit, product, silent_remove,
     
    2531
    2632class TestTools(object):
    27     '''
    28     '''
     33    """Test the tools module."""
    2934
    3035    def setup_method(self):
     
    4853        sys.argv = ['dummy.py', '--wrong=1']
    4954        with pytest.raises(SystemExit):
    50             results = get_cmdline_arguments()
     55            results = get_cmdline_args()
    5156
    5257    def test_default_get_cmdline_arguments(self):
    53         cmd_dict_control = {'start_date':None,
    54                             'end_date':None,
    55                             'date_chunk':None,
    56                             'basetime':None,
    57                             'step':None,
    58                             'levelist':None,
    59                             'area':None,
    60                             'inputdir':None,
    61                             'outputdir':None,
    62                             'flexpart_root_scripts':None,
    63                             'ppid':None,
    64                             'job_template':'job.temp',
    65                             'queue':None,
    66                             'controlfile':'CONTROL.temp',
    67                             'debug':None,
    68                             'public':None,
    69                             'request':None}
     58        cmd_dict_control = {'start_date': None,
     59                            'end_date': None,
     60                            'date_chunk': None,
     61                            'basetime': None,
     62                            'step': None,
     63                            'levelist': None,
     64                            'area': None,
     65                            'inputdir': None,
     66                            'outputdir': None,
     67                            'job_template': None,
     68                            'job_chunk': None,
     69                            'ppid': None,
     70                            'job_template': 'job.temp',
     71                            'queue': None,
     72                            'controlfile': 'CONTROL_EA5',
     73                            'debug': None,
     74                            'public': None,
     75                            'request': None,
     76                            'oper': None,
     77                            'rrint': None}
    7078
    7179        sys.argv = ['dummy.py']
    7280
    73         results = get_cmdline_arguments()
     81        results = get_cmdline_args()
    7482
    7583        assert cmd_dict_control == vars(results)
    7684
    7785    def test_input_get_cmdline_arguments(self):
    78         cmd_dict_control = {'start_date':'20180101',
    79                             'end_date':'20180101',
    80                             'date_chunk':3,
    81                             'basetime':12,
    82                             'step':'1',
    83                             'levelist':'1/to/10',
    84                             'area':'50/10/60/20',
    85                             'inputdir':'../work',
    86                             'outputdir':None,
    87                             'flexpart_root_scripts':'../',
    88                             'ppid':1234,
    89                             'job_template':'job.sh',
    90                             'queue':'ecgate',
    91                             'controlfile':'CONTROL.WORK',
    92                             'debug':1,
    93                             'public':None,
    94                             'request':0}
     86        cmd_dict_control = {'start_date': '20180101',
     87                            'end_date': '20180101',
     88                            'date_chunk': 3,
     89                            'basetime': 12,
     90                            'step': '1',
     91                            'levelist': '1/to/10',
     92                            'area': '50/10/60/20',
     93                            'inputdir': '../work',
     94                            'outputdir': None,
     95                            'ppid': '1234',
     96                            'job_template': 'job.sh',
     97                            'queue': 'ecgate',
     98                            'controlfile': 'CONTROL.WORK',
     99                            'debug': 1,
     100                            'public': None,
     101                            'request': 0,
     102                            'rrint': 0,
     103                            'job_chunk': None,
     104                            'oper': 0}
    95105
    96106        sys.argv = ['dummy.py',
     
    104114                    '--inputdir=../work',
    105115                    '--outputdir=None',
    106                     '--flexpart_root_scripts=../',
    107116                    '--ppid=1234',
    108117                    '--job_template=job.sh',
     
    111120                    '--debug=1',
    112121                    '--public=None',
    113                     '--request=0']
    114 
    115         results = get_cmdline_arguments()
     122                    '--request=0',
     123                    '--rrint=0',
     124                    '--job_chunk=None',
     125                    '--oper=0']
     126
     127        results = get_cmdline_args()
    116128
    117129        assert cmd_dict_control == vars(results)
     
    121133        expected_sample = {'078': 'TCLW', '130': 'T', '034': 'SST'}
    122134        # check a sample of parameters which must have been read in
    123         assert all((k in table128 and table128[k]==v)
    124                    for k,v in expected_sample.iteritems())
    125 
    126     @patch('__builtin__.open', side_effect=[OSError(errno.EEXIST)])
     135        assert all((k in table128 and table128[k] == v)
     136                   for k, v in expected_sample.items())
     137
     138    @patch('builtins.open', side_effect=[OSError(errno.EEXIST)])
    127139    def test_fail_open_init128(self, mock_openfile):
    128140        with pytest.raises(SystemExit):
     
    145157
    146158    @patch('traceback.format_stack', return_value='empty trace')
    147     @patch('mods.tools.send_mail', return_value=0)
     159    @patch('Mods.tools.send_mail', return_value=0)
    148160    def test_success_my_error(self, mock_mail, mock_trace, capfd):
    149161        with pytest.raises(SystemExit):
    150             my_error(['any_user'], 'Failed!')
     162            my_error('Failed!')
    151163            out, err = capfd.readouterr()
    152164            assert out == "Failed!\n\nempty_trace\n"
     
    160172        send_mail(['${USER}', 'any_user'], 'ERROR', message='error mail')
    161173        out, err = capfd.readouterr()
    162         assert out == b'Email sent to user\nEmail sent to user\n'
     174        assert out == 'Email sent to user\nEmail sent to user\n'
    163175
    164176    @patch('subprocess.Popen')
     
    169181        send_mail(['any-user'], 'ERROR', message='error mail')
    170182        out, err = capfd.readouterr()
    171         assert out == b'Email sent to any_user\n'
     183        assert out == 'Email sent to any_user\n'
    172184
    173185    @patch('subprocess.Popen', side_effect=[ValueError, OSError])
     
    189201        assert envs_ref == envs
    190202
    191     @patch('__builtin__.open', side_effect=[OSError(errno.EPERM)])
     203    @patch('builtins.open', side_effect=[OSError(errno.EPERM)])
    192204    def test_fail_read_ecenv(self, mock_open):
    193205        with pytest.raises(SystemExit):
     
    195207
    196208    @patch('glob.glob', return_value=[])
    197     @patch('mods.tools.silent_remove')
     209    @patch('Mods.tools.silent_remove')
    198210    def test_empty_clean_up(self, mock_rm, mock_clean):
    199211        clean_up(self.c)
     
    203215    @patch('os.remove', return_value=0)
    204216    def test_success_clean_up(self, mock_rm, mock_glob):
    205         # ectrans=0; ecstorage=0; ecapi=None; prefix not in filename
    206         clean_up(self.c)
    207         mock_rm.assert_has_calls([call('any_file'), call('EIfile')])
    208         mock_rm.reset_mock()
    209 
    210         # ectrans=0; ecstorage=0; ecapi=False; prefix in filename
     217
    211218        self.c.prefix = 'EI'
    212219        self.c.ecapi = False
     
    215222        mock_rm.reset_mock()
    216223
    217         # ectrans=0; ecstorage=0; ecapi=True; prefix in filename
    218         self.c.prefix = 'EI'
    219         self.c.ecapi = True
    220         clean_up(self.c)
    221         mock_rm.assert_has_calls([call('any_file')])
    222         mock_rm.reset_mock()
    223 
    224         # ectrans=1; ecstorage=0; ecapi=True; prefix in filename
    225         self.c.prefix = 'EI'
    226         self.c.ecapi = True
    227         self.c.ectrans = 1
    228         clean_up(self.c)
    229         mock_rm.assert_has_calls([call('any_file')])
    230         mock_rm.reset_mock()
    231 
    232         # ectrans=1; ecstorage=0; ecapi=False; prefix in filename
    233         self.c.prefix = 'EI'
    234         self.c.ecapi = False
    235         self.c.ectrans = 1
    236         clean_up(self.c)
    237         mock_rm.assert_has_calls([call('any_file'), call('EIfile')])
    238         mock_rm.reset_mock()
    239 
    240         # ectrans=1; ecstorage=1; ecapi=False; prefix in filename
    241         self.c.prefix = 'EI'
    242         self.c.ecapi = False
    243         self.c.ectrans = 1
    244         self.c.ecstorage = 1
    245         clean_up(self.c)
    246         mock_rm.assert_has_calls([call('any_file'), call('EIfile')])
    247         mock_rm.reset_mock()
    248224
    249225    def test_default_normal_exit(self, capfd):
  • Source/Pythontest/TestUIOFiles.py

    rba99230 r76c37a9  
    55import sys
    66import pytest
     7from mock import patch
    78
    8 sys.path.append('../python')
    9 from classes.UioFiles import UioFiles
     9from . import _config_test
     10sys.path.append('../Python')
     11
     12from Classes.UioFiles import UioFiles
    1013
    1114
    1215class TestUioFiles():
    13     '''
    14     Test class to test the UIOFiles methods.
    15     '''
     16    """Test class to test the UIOFiles methods."""
     17
     18    @classmethod
     19    def setup_class(self):
     20        """Setup status"""
     21        self.testpath = os.path.join(_config_test.PATH_TEST_DIR, 'Dir')
     22        # Initialise and collect filenames
     23        self.files = UioFiles(self.testpath, '*.grb')
    1624
    1725    def test_listFiles(self):
    18         '''
    19         @Description:
    20             Test the listFiles method from class UIOFiles.
     26        """Test the listFiles method from class UIOFiles."""
     27        # set comparison information
     28        self.expected = ['FCGG__SL.20160410.40429.16424.grb',
     29                         'FCOG__ML.20160410.40429.16424.grb',
     30                         'FCSH__ML.20160410.40429.16424.grb',
     31                         'OG_OROLSM__SL.20160410.40429.16424.grb',
     32                         'FCOG_acc_SL.20160409.40429.16424.grb',
     33                         'FCOG__SL.20160410.40429.16424.grb',
     34                         'FCSH__SL.20160410.40429.16424.grb']
    2135
    22         @Input:
    23             self: instance of TestClass
    24                 Class to test the UIOFiles methods.
    25 
    26         @Return:
    27             <nothing>
    28         '''
    29         # set comparison information
    30         self.testpath = os.path.join(os.path.dirname(__file__), 'TestDir')
    31         self.expected = ['FCGG__SL.20160410.40429.16424.grb',
    32                              'FCOG__ML.20160410.40429.16424.grb',
    33                              'FCSH__ML.20160410.40429.16424.grb',
    34                              'OG_OROLSM__SL.20160410.40429.16424.grb',
    35                              'FCOG_acc_SL.20160409.40429.16424.grb',
    36                              'FCOG__SL.20160410.40429.16424.grb',
    37                              'FCSH__SL.20160410.40429.16424.grb']
    38 
    39         # Initialise and collect filenames
    40         files = UioFiles(self.testpath, '*.grb')
    4136        # get the basename to just check for equality of filenames
    42         filelist = [os.path.basename(f) for f in files.files]
     37        filelist = [os.path.basename(f) for f in self.files.files]
    4338        # comparison of expected filenames against the collected ones
    4439        assert sorted(self.expected) == sorted(filelist)
    4540
    46         return
    4741
     42    def test_delete_files(self):
     43        """Test if a file is deleted."""
     44        testfile = os.path.join(self.testpath, 'test.test')
     45        open(testfile, 'w').close()
     46        iofile = UioFiles(testfile, 'test.test')
     47        iofile.delete_files()
     48        assert [] == UioFiles(testfile, 'test.test').files
     49
     50
     51    def test_str_(self):
     52        """Test if list of file is correctly converted to string."""
     53        self.expected = "FCSH__SL.20160410.40429.16424.grb, "\
     54                        "FCSH__ML.20160410.40429.16424.grb, "\
     55                        "FCOG__SL.20160410.40429.16424.grb, "\
     56                        "FCOG__ML.20160410.40429.16424.grb, "\
     57                        "OG_OROLSM__SL.20160410.40429.16424.grb, "\
     58                        "FCGG__SL.20160410.40429.16424.grb, "\
     59                        "FCOG_acc_SL.20160409.40429.16424.grb"
     60        assert self.expected == self.files.__str__()
  • Source/Pythontest/_config_test.py

    rba99230 r76c37a9  
    1 #!/usr/bin/env python
     1#!/usr/bin/env python3
    22# -*- coding: utf-8 -*-
    33'''
     
    2727import sys
    2828
    29 sys.path.append('../python')
     29sys.path.append('../Python')
    3030import _config
    3131
     
    3838# DIRECTORY NAMES
    3939# ------------------------------------------------------------------------------
    40 TEST_DIR = 'test/Unit'
     40TEST_DIR = 'Testing/Regression/Unit'
    4141TESTFILES_DIR = 'Testfiles'
    4242TESTINSTALL_DIR = 'InstallTar'
  • Source/Pythontest/conftest.py

    rba99230 r76c37a9  
    22import pytest
    33
    4 sys.path.append('../python')
     4sys.path.append('../Python')
    55import _config
    66
     
    88#def prep_test_env():
    99#    testdir = _config.PATH_TEST_DIR
    10 print ''
     10print('')
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG