Skip to content
Snippets Groups Projects
test_argument_parser.py 4.24 KiB
Newer Older
import unittest
import io
import sys

from pymagglobal.__main__ import argument_parser


class _SetUpCls(unittest.TestCase):
    '''Dummy just to instantiate the parser'''

    @classmethod
    def setUpClass(cls):
        cls.parser = argument_parser()


class ParserTestGlobal(_SetUpCls):
    '''Test-cases from global options'''

    def test_help(self):
        '''Check if the help flag terminates successfully with exit code 0'''
        with self.assertRaises(SystemExit) as cm:
            # Redirect stdout not to pollute output the terminal
            sys.stdout = io.StringIO()
            # Pass list since sys.argv splits at white space (filename
            # stripped)
            nsp = self.parser.parse_args(['-h'])
        # Assert if exit-code differs from 0
        self.assertEqual(cm.exception.code, 0)
        # Again with --help
        with self.assertRaises(SystemExit) as cm:
            nsp = self.parser.parse_args(['--help'])
        self.assertEqual(cm.exception.code, 0)

    def test_list_models(self):
        '''Check if `--list-models` terminates successfully'''
        with self.assertRaises(SystemExit) as cm:
            # Redirect stdout not to pollute output the terminal
            sys.stdout = io.StringIO()
            # Pass list since sys.argv splits at white space (filename
            # stripped)
            nsp = self.parser.parse_args(['--list-models'])
        # Assert if exit-code is different from 0
        self.assertEqual(cm.exception.code, 0)


class ParserTestMaster(_SetUpCls):
    '''Test-cases from the `master` sub-command'''

    def test_master_valid(self):
        '''Check if a syntactically proper string is parsed without error'''
        # TODO: Grab that command from 'example of use'
        cmd = 'pymagglobal master --no-show 12 12 gufm1'
        nsp = self.parser.parse_args(cmd.split()[1:])

    def test_master_exclusive_res_every(self):
        '''Raise if both, --res and --every are specified'''
        cmd = 'pymagglobal master --no-show --res=20 --every=12 12 12 gufm1'
        with self.assertRaises(SystemExit) as cm:
            # Redirect stderr not to pollute output the terminal
            sys.stderr = io.StringIO()
            nsp = self.parser.parse_args(cmd.split()[1:])
        self.assertNotEqual(cm.exception.code, 0,
                            msg='A non-zero exit code is expected.')
    def test_usage_valid(self):
        '''Check if the example of us is parsed without error'''
        from pymagglobal.__main__ import mst_ex_cmd as cmd
        nsp = self.parser.parse_args(cmd.split()[1:])

    def test_command_run(self):
        ''' Check if the example runs flawlessly '''
        from pymagglobal.__main__ import mst_ex_cmd as cmd
        nsp = self.parser.parse_args(cmd.split()[1:])
        nsp.func(nsp)

class ParserTestMap(_SetUpCls):
    '''Test-cases from the `map` sub-command'''

    def test_custom_model(self):
        cmd = 'map 1500 ./pymagglobal/dat/arhimag1k'
        nsp = self.parser.parse_args(cmd.split())
        self.assertEqual(nsp.model.name, 'custom model')

    def test_usage_valid(self):
        '''Check if the example of us is parsed without error'''
        from pymagglobal.__main__ import map_ex_cmd as cmd
        nsp = self.parser.parse_args(cmd.split()[1:])


class ParserTestDipole(_SetUpCls):
    '''Test-cases from the `dipole` sub-command'''

    def test_usage_valid(self):
        '''Check if the example of use is parsed without error'''
        from pymagglobal.__main__ import dip_ex_cmd as cmd
        nsp = self.parser.parse_args(cmd.split()[1:])

    def test_longerm(self):
        cmd = 'dipole --longterm LSMOD.2'
        nsp = self.parser.parse_args(cmd.split())
        self.assertEqual(nsp.t_unit, 'ka')
        self.assertEqual(nsp.t_label, 'age [ka]')

    def test_namspece_time_attributs(self):
        cmd = 'dipole --longterm LSMOD.2'
        nsp = self.parser.parse_args(cmd.split())
        assert hasattr(nsp, 't_unit')
        assert hasattr(nsp, 't_label')


class ParserTestCoefficients(_SetUpCls):
    '''Test-cases from the `map` sub-command'''

    def test_usage_valid(self):
        '''Check if the example of use is parsed without error'''
        from pymagglobal.__main__ import cff_ex_cmd as cmd
        nsp = self.parser.parse_args(cmd.split()[1:])