From 0221cc6e459235a9b4cc76a77c7066ffa4cb403d Mon Sep 17 00:00:00 2001
From: Stefan Mauerberger <mauerber@uni-potsdam.de>
Date: Thu, 23 Jul 2020 20:58:16 +0200
Subject: [PATCH 1/4] Run tests before deploying the documentation

---
 .gitlab-ci.yml | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c2d6bc7..103b9a1 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:
-- 
GitLab


From acd9be165c4456012db5e01db1c7fe4dcc293b0d Mon Sep 17 00:00:00 2001
From: Stefan Mauerberger <mauerber@uni-potsdam.de>
Date: Thu, 23 Jul 2020 20:59:53 +0200
Subject: [PATCH 2/4] Test if usage examples are parsed without error

---
 pymagglobal/__main__.py                   | 36 ++++++++++++++--------
 pymagglobal/tests/test_argument_parser.py | 37 ++++++++++++++++++++---
 2 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/pymagglobal/__main__.py b/pymagglobal/__main__.py
index 3b774fd..5bea2f6 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_argument_parser.py b/pymagglobal/tests/test_argument_parser.py
index 83aaade..ae04254 100644
--- a/pymagglobal/tests/test_argument_parser.py
+++ b/pymagglobal/tests/test_argument_parser.py
@@ -60,12 +60,41 @@ 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:])
+
+
+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:])
-- 
GitLab


From b9c0d277a4921d4dbcd68814d613a80e70cf5310 Mon Sep 17 00:00:00 2001
From: Stefan Mauerberger <mauerber@uni-potsdam.de>
Date: Thu, 23 Jul 2020 21:23:37 +0200
Subject: [PATCH 3/4] Infrastructure for even more tests

---
 pymagglobal/tests/test__commands.py | 31 +++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 pymagglobal/tests/test__commands.py

diff --git a/pymagglobal/tests/test__commands.py b/pymagglobal/tests/test__commands.py
new file mode 100644
index 0000000..2882242
--- /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)
-- 
GitLab


From c9488c4dace03cc63cadc8edfd2d3c769363cb51 Mon Sep 17 00:00:00 2001
From: Stefan Mauerberger <mauerber@uni-potsdam.de>
Date: Fri, 24 Jul 2020 09:47:15 +0200
Subject: [PATCH 4/4] Two more tests; expected to fail

---
 pymagglobal/tests/test_argument_parser.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/pymagglobal/tests/test_argument_parser.py b/pymagglobal/tests/test_argument_parser.py
index ae04254..d7e94f3 100644
--- a/pymagglobal/tests/test_argument_parser.py
+++ b/pymagglobal/tests/test_argument_parser.py
@@ -90,6 +90,20 @@ class ParserTestDipole(_SetUpCls):
         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'''
-- 
GitLab