diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c2d6bc7c7f018797cfe77fcdfce7c3097900572c..103b9a1d891596561e3bd96cdcb4da7243655913 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,3 +1,7 @@ +tests: + stage: test + script: python setup.py test + pages: stage: deploy script: diff --git a/pymagglobal/__main__.py b/pymagglobal/__main__.py index 3b774fd468bb05dc87cd0f75a289ca04d17dd1d4..5bea2f6b4ad7d32b2baf133d9d31a93b2b7028bc 100644 --- a/pymagglobal/__main__.py +++ b/pymagglobal/__main__.py @@ -35,6 +35,26 @@ from pymagglobal import __version__ from matplotlib import pyplot as plt +mst_ex_ofn = 'pic_mst.png' +'''Output file name for the mater curve example :py:attr:`~mst_ex_cmd`''' +mst_ex_cmd = 'pymagglobal master 12 12 ARCH10k.1' +'''CLI usage example for plotting a master curve.''' + +dip_ex_ofn = 'pic_dip.png' +'''Output file name for the dipole moment example :py:attr:`~dip_ex_cmd`''' +dip_ex_cmd = 'pymagglobal dipole CALS10k.2' +'''CLI usage example for plotting the dipole moment.''' + +map_ex_ofn = 'pic_map.png' +'''Output file name for the field map example :py:attr:`~map_ex_cmd`''' +map_ex_cmd = 'pymagglobal map 1700 gufm1' +'''CLI usage example for plotting a field map.''' + +cff_ex_cmd = 'pymagglobal coefficients 1700 arhimag1k' +'''CLI usage example for coefficients +.. todo: Show a nice and shiny figure?''' + + class ListModelsAction(argparse.Action): ''' the action class that will produce a list of available models ''' def __init__(self, option_strings, dest=argparse.SUPPRESS, @@ -108,11 +128,9 @@ def argument_parser(): subparsers = parser.add_subparsers(dest='command', required=True) # Epilogue for the master curve handling - mst_ex_ofn = './pic_mst.png ' # Output file name - mst_ex_cmd = 'pymagglobal master 12 12 gufm1' # Example command mst_epilog = f"""Example of use: :command:`{mst_ex_cmd}`" -.. image:: {mst_ex_ofn}""" # sphinx-argparse requires a multi line string +.. image:: ./{mst_ex_ofn}""" # sphinx-argparse requires a multi line string # Parser for the master curve handling mst_parser = subparsers.add_parser('master', help=f'create a master curve ' @@ -136,11 +154,9 @@ def argument_parser(): f'and down components', default='dif') # parser for the dipole moment series - dip_ex_ofn = './pic_dip.png' - dip_ex_cmd = 'pymagglobal dipole gufm1' dip_epilog = f"""Example of use: :command:`{dip_ex_cmd}` -.. image:: {dip_ex_ofn}""" # sphinx-argparse requires a multi line string +.. image:: ./{dip_ex_ofn}""" # sphinx-argparse requires a multi line string dip_parser = subparsers.add_parser('dipole', help=f'create a time series ' f'of the dipole moment', parents=[base_parser, @@ -149,8 +165,6 @@ def argument_parser(): dip_parser.set_defaults(func=_commands.dipole_series) # parser for coefficients - cff_ex_ofn = '../dat_cff.txt' - cff_ex_cmd = 'pymagglobal coefficients 1700 gufm1' cff_epilog = f'''Example of use: :command:`{cff_ex_cmd}` .. code-block:: none @@ -166,7 +180,7 @@ def argument_parser(): 2 2 -1.9291e+03 2 -2 -8.7141e+02 3 0 6.8948e+02 - ... ''' + ... ''' # XXX Shouldn't this be a figure cff_parser = subparsers.add_parser('coefficients', help=f'output ' f'coefficients for a specific epoch', parents=[base_parser], @@ -177,11 +191,9 @@ def argument_parser(): 'the coeffients are returned') # parser for maps - map_ex_ofn = './pic_map.png' - map_ex_cmd = 'pymagglobal map 1700 gufm1' map_epilog = f'''Example of use: :command:`{map_ex_cmd}` -.. image:: {map_ex_ofn}''' +.. image:: ./{map_ex_ofn}''' map_parser = subparsers.add_parser('map', help=f'output a grid of lat-lon-' f'pairs and field components at the ' f'locations for a specific epoch. note' diff --git a/pymagglobal/tests/test__commands.py b/pymagglobal/tests/test__commands.py new file mode 100644 index 0000000000000000000000000000000000000000..2882242f334f2286036754bacb63192a19d7a0c6 --- /dev/null +++ b/pymagglobal/tests/test__commands.py @@ -0,0 +1,31 @@ +import unittest +import io +import sys + +from matplotlib import pyplot as plt +from pymagglobal import models +from test_argument_parser import _SetUpCls + + +class Commands(_SetUpCls): + + @unittest.skip('not yet implemented') + def test_usage_ex_master(self): + from pymagglobal.__main__ import mst_ex_ofn, mst_ex_cmd + nsp = self.parser.parse_args(mst_ex_cmd.split()[1:]) + #nsp.func(nsp) + #plt.savefig('./docs/' + mst_ex_ofn) + + @unittest.skip('not yet implemented') + def test_usage_ex_dipole(self): + from pymagglobal.__main__ import dip_ex_ofn, dip_ex_cmd + nsp = self.parser.parse_args(dip_ex_cmd.split()[1:]) + #nsp.func(nsp) + #plt.savefig('./docs/' + mst_ex_ofn) + + @unittest.skip('not yet implemented') + def test_usage_ex_map(self): + from pymagglobal.__main__ import map_ex_ofn, map_ex_cmd + nsp = self.parser.parse_args(map_ex_cmd.split()[1:]) + #nsp.func(nsp) + #plt.savefig('./docs/' + mst_ex_ofn) diff --git a/pymagglobal/tests/test_argument_parser.py b/pymagglobal/tests/test_argument_parser.py index 83aaadea8a723f560d9402964bf86a98c2f9fab7..d7e94f37689b4fe11747fcabb19b90213745a45a 100644 --- a/pymagglobal/tests/test_argument_parser.py +++ b/pymagglobal/tests/test_argument_parser.py @@ -60,12 +60,55 @@ class ParserTestMaster(_SetUpCls): 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:]) + class ParserTestMap(_SetUpCls): '''Test-cases from the `map` 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 map 1700 gufm1' + @unittest.expectedFailure + def test_custom_model(self): + cmd = 'map 1500 ../my_custom_model' + nsp = self.parser.parse_args(cmd.split()) + self.assertEqual(nsp.model, 'custom model') + self.assertEqual(nsp.input, '../my_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:]) + + @unittest.expectedFailure + 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]') + + @unittest.expectedFailure + 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:])