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 # FIXME: stdout remains redirected and suppresses potential output 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_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, mst_ex_ofn as ofn nsp = self.parser.parse_args(cmd.split()[1:]) fig = nsp.func(nsp) # TODO: use --no-show together with --savefig fig.savefig(f'./docs/{ofn}', bbox_inches='tight') class ParserTestMap(_SetUpCls): '''Test-cases from the `map` sub-command''' def test_custom_model(self): '''Check whether a file-name passed is treated as a custom model''' from pymagglobal import models cmd = 'map 1500 ' + models['arhimag1k'] nsp = self.parser.parse_args(cmd.split()) self.assertEqual(nsp.model.name, 'custom model') def test_custom_model_raises(self): '''Check if a wrong file-name throws FileNotFoundError''' cmd = 'map 1500 ./non_existent_file' self.assertRaises(FileNotFoundError, self.parser.parse_args, cmd.split()) 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:]) def test_command_run(self): ''' Check if the example runs flawlessly ''' from pymagglobal.__main__ import map_ex_cmd as cmd, map_ex_ofn as ofn nsp = self.parser.parse_args(cmd.split()[1:]) fig = nsp.func(nsp) # TODO: use --no-show together with --savefig fig.savefig(f'./docs/{ofn}', bbox_inches='tight') 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_command_run(self): ''' Check if the example runs flawlessly ''' from pymagglobal.__main__ import dip_ex_cmd as cmd, dip_ex_ofn as ofn nsp = self.parser.parse_args(cmd.split()[1:]) fig = nsp.func(nsp) # TODO: use --no-show together with --savefig fig.savefig(f'./docs/{ofn}', bbox_inches='tight') def test_longterm(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 ParserTestCoefficientsEpoch(_SetUpCls): '''Test-cases from the `coeffs-epoch` sub-command''' def test_usage_valid(self): '''Check if the example of use is parsed without error''' from pymagglobal.__main__ import cfe_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 cfe_ex_cmd as cmd, cfe_ex_ofn as ofn nsp = self.parser.parse_args(cmd.split()[1:]) fig = nsp.func(nsp) # TODO: use --no-show together with --savefig fig.savefig(f'./docs/{ofn}', bbox_inches='tight') class ParserTestCoefficientSeries(_SetUpCls): '''Test-cases from the `coeff-series` sub-command''' def test_usage_valid(self): '''Check if the example of use is parsed without error''' from pymagglobal.__main__ import cfs_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 cfs_ex_cmd as cmd, cfs_ex_ofn as ofn nsp = self.parser.parse_args(cmd.split()[1:]) fig = nsp.func(nsp) # TODO: use --no-show together with --savefig fig.savefig(f'./docs/{ofn}', bbox_inches='tight')