From 9d4fb225722f62ae5dcc096da077a09c5dfa5c17 Mon Sep 17 00:00:00 2001 From: "Oleg E. Peil" Date: Mon, 16 Feb 2015 17:14:24 +0100 Subject: [PATCH] Rearranged test cases for 'inpconf.py'. A single-file test suite for 'inpconf.py' is split into several files, each containing a separate TestCase class. In addition, all test cases are derived from class ArrayTestCase (in turn derived from TestCase) which contains a numpy-array equality method. --- python/converters/vasp/python/inpconf.py | 6 +- .../converters/vasp/test/inpconf/arraytest.py | 27 ++ .../test/inpconf/{test7.cfg => example.cfg} | 0 .../inpconf/{test6.cfg => input_test_1.cfg} | 0 .../inpconf/{test8.cfg => input_test_2.cfg} | 0 .../inpconf/{test9.cfg => input_test_3.cfg} | 0 .../inpconf/{test10.cfg => input_test_4.cfg} | 0 .../vasp/test/inpconf/parse_groups_1.cfg | 13 + .../inpconf/{test2.cfg => parse_shells_1.cfg} | 0 .../inpconf/{test3.cfg => parse_shells_2.cfg} | 0 .../inpconf/{test4.cfg => parse_shells_3.cfg} | 0 .../inpconf/{test5.cfg => parse_shells_4.cfg} | 0 .../vasp/test/inpconf/test_groups.py | 42 +++ .../vasp/test/inpconf/test_inpconf.py | 350 +----------------- .../vasp/test/inpconf/test_input.py | 85 +++++ .../vasp/test/inpconf/test_parameter_set.py | 48 +++ .../vasp/test/inpconf/test_shells.py | 76 ++++ .../vasp/test/inpconf/test_special_parsers.py | 149 ++++++++ 18 files changed, 445 insertions(+), 351 deletions(-) create mode 100644 python/converters/vasp/test/inpconf/arraytest.py rename python/converters/vasp/test/inpconf/{test7.cfg => example.cfg} (100%) rename python/converters/vasp/test/inpconf/{test6.cfg => input_test_1.cfg} (100%) rename python/converters/vasp/test/inpconf/{test8.cfg => input_test_2.cfg} (100%) rename python/converters/vasp/test/inpconf/{test9.cfg => input_test_3.cfg} (100%) rename python/converters/vasp/test/inpconf/{test10.cfg => input_test_4.cfg} (100%) create mode 100644 python/converters/vasp/test/inpconf/parse_groups_1.cfg rename python/converters/vasp/test/inpconf/{test2.cfg => parse_shells_1.cfg} (100%) rename python/converters/vasp/test/inpconf/{test3.cfg => parse_shells_2.cfg} (100%) rename python/converters/vasp/test/inpconf/{test4.cfg => parse_shells_3.cfg} (100%) rename python/converters/vasp/test/inpconf/{test5.cfg => parse_shells_4.cfg} (100%) create mode 100644 python/converters/vasp/test/inpconf/test_groups.py create mode 100644 python/converters/vasp/test/inpconf/test_input.py create mode 100644 python/converters/vasp/test/inpconf/test_parameter_set.py create mode 100644 python/converters/vasp/test/inpconf/test_shells.py create mode 100644 python/converters/vasp/test/inpconf/test_special_parsers.py diff --git a/python/converters/vasp/python/inpconf.py b/python/converters/vasp/python/inpconf.py index 234a3388..c783261b 100644 --- a/python/converters/vasp/python/inpconf.py +++ b/python/converters/vasp/python/inpconf.py @@ -433,7 +433,7 @@ class ConfigParameters: """ Parses [General] section. """ -# TODO: +# TODO: write the parser pass ################################################################################ @@ -451,7 +451,9 @@ class ConfigParameters: self.groups_shells_consistency() - +# +# Obsolete part +# if __name__ == '__main__': narg = len(sys.argv) if narg < 2: diff --git a/python/converters/vasp/test/inpconf/arraytest.py b/python/converters/vasp/test/inpconf/arraytest.py new file mode 100644 index 00000000..77f04d29 --- /dev/null +++ b/python/converters/vasp/test/inpconf/arraytest.py @@ -0,0 +1,27 @@ +r""" +Module defining a custom TestCase with extra functionality. +""" + +import unittest +import numpy as np + +class ArrayTestCase(unittest.TestCase): + """ + Custom TestCase class supporting array equality. + """ + def __init__(self, *args, **kwargs): + """ + Initializes a custom equality function for comparing numpy arrays. + """ + super(ArrayTestCase, self).__init__(*args, **kwargs) + self.addTypeEqualityFunc(np.ndarray, self.is_arrays_equal) + + def is_arrays_equal(self, arr1, arr2, msg=None): + """ + Raises self.failureException is arrays arr1 and arr2 + are not equal. + """ + if not np.allclose(arr1, arr2): + raise self.failureException(msg) + + diff --git a/python/converters/vasp/test/inpconf/test7.cfg b/python/converters/vasp/test/inpconf/example.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test7.cfg rename to python/converters/vasp/test/inpconf/example.cfg diff --git a/python/converters/vasp/test/inpconf/test6.cfg b/python/converters/vasp/test/inpconf/input_test_1.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test6.cfg rename to python/converters/vasp/test/inpconf/input_test_1.cfg diff --git a/python/converters/vasp/test/inpconf/test8.cfg b/python/converters/vasp/test/inpconf/input_test_2.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test8.cfg rename to python/converters/vasp/test/inpconf/input_test_2.cfg diff --git a/python/converters/vasp/test/inpconf/test9.cfg b/python/converters/vasp/test/inpconf/input_test_3.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test9.cfg rename to python/converters/vasp/test/inpconf/input_test_3.cfg diff --git a/python/converters/vasp/test/inpconf/test10.cfg b/python/converters/vasp/test/inpconf/input_test_4.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test10.cfg rename to python/converters/vasp/test/inpconf/input_test_4.cfg diff --git a/python/converters/vasp/test/inpconf/parse_groups_1.cfg b/python/converters/vasp/test/inpconf/parse_groups_1.cfg new file mode 100644 index 00000000..91aa306a --- /dev/null +++ b/python/converters/vasp/test/inpconf/parse_groups_1.cfg @@ -0,0 +1,13 @@ +[General] + +[Group 1] +SHELLS = 1 2 + +[Shell 1] +LSHELL = 2 +IONS = 5..8 + +[Shell 2] +LSHELL = 1 +IONS = 1..4 + diff --git a/python/converters/vasp/test/inpconf/test2.cfg b/python/converters/vasp/test/inpconf/parse_shells_1.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test2.cfg rename to python/converters/vasp/test/inpconf/parse_shells_1.cfg diff --git a/python/converters/vasp/test/inpconf/test3.cfg b/python/converters/vasp/test/inpconf/parse_shells_2.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test3.cfg rename to python/converters/vasp/test/inpconf/parse_shells_2.cfg diff --git a/python/converters/vasp/test/inpconf/test4.cfg b/python/converters/vasp/test/inpconf/parse_shells_3.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test4.cfg rename to python/converters/vasp/test/inpconf/parse_shells_3.cfg diff --git a/python/converters/vasp/test/inpconf/test5.cfg b/python/converters/vasp/test/inpconf/parse_shells_4.cfg similarity index 100% rename from python/converters/vasp/test/inpconf/test5.cfg rename to python/converters/vasp/test/inpconf/parse_shells_4.cfg diff --git a/python/converters/vasp/test/inpconf/test_groups.py b/python/converters/vasp/test/inpconf/test_groups.py new file mode 100644 index 00000000..6c36e420 --- /dev/null +++ b/python/converters/vasp/test/inpconf/test_groups.py @@ -0,0 +1,42 @@ +r""" +Tests of 'parse_groups()' defined in ConfigParameters class +""" +import arraytest +import numpy as np +from inpconf import ConfigParameters + +################################################################################ +# +# TestParseGroups +# +################################################################################ +class TestParseGroups(arraytest.ArrayTestCase): + """ + Function: + + def parse_groups(self) + + Scenarios: + + - **if** a [Group] section does not contain all required parameters + **raise** Exception + - **if** a correct group section is defined **return** a list of dictionaries + """ +# Scenario 1 + def test_gr_required(self): + conf_pars = ConfigParameters('parse_groups_1.cfg') + err_mess = "Required parameter" + with self.assertRaisesRegexp(Exception, err_mess): + conf_pars.parse_groups() + +# Scenario 2 + def test_example(self): + conf_pars = ConfigParameters('example.cfg') + conf_pars.parse_groups() + res = conf_pars.groups + expected = [{'index': 1, 'shells': [1, 2], 'emin': -7.6, 'emax': 3.0}, + {'index': 2, 'shells': [3], 'emin': -1.6, 'emax': 2.0}] + self.assertListEqual(res, expected) + + + diff --git a/python/converters/vasp/test/inpconf/test_inpconf.py b/python/converters/vasp/test/inpconf/test_inpconf.py index 7a893cb1..9700a765 100644 --- a/python/converters/vasp/test/inpconf/test_inpconf.py +++ b/python/converters/vasp/test/inpconf/test_inpconf.py @@ -1,357 +1,9 @@ r""" Test suite for module `inpconf.py`. """ - import unittest -import numpy as np -from inpconf import ConfigParameters -import ConfigParser - -class TestSpecialParsers(unittest.TestCase): - """ - Tests of special parsers. - """ - def __init__(self, *args, **kwargs): - """ - Initializes a custom equality function for comparing numpy arrays. - """ - super(TestSpecialParsers, self).__init__(*args, **kwargs) - self.addTypeEqualityFunc(np.ndarray, self.is_arrays_equal) - - def is_arrays_equal(self, arr1, arr2, msg=None): - """ - Raises self.failureException is arrays arr1 and arr2 - are not equal. - """ - if not np.allclose(arr1, arr2): - raise self.failureException(msg) - - def setUp(self): - """ - """ - pass - -################################################################################ -# -# test_parse_string_logical() -# -################################################################################ - def test_parse_string_logical(self): - """ - Function: - - def parse_string_logical(self, par_str) - - Scenarios: - - - **if** par_str == 'True' **return** True - - **if** par_str == 'False' **return** False - - **if** par_str == '0' **raise** assertion - """ - conf_pars = ConfigParameters('test1.cfg') - -# Scenario 1 - res = conf_pars.parse_string_logical('True') - self.assertEqual(res, True) - -# Scenario 2 - res = conf_pars.parse_string_logical('False') - self.assertEqual(res, False) - -# Scenario 3 - with self.assertRaises(AssertionError): - conf_pars.parse_string_logical('0') - -################################################################################ -# -# test_parse_string_ion_list() -# -################################################################################ - def test_parse_string_ion_list(self): - """ - Function: - - def parse_string_ion_list(self, par_str) - - Scenarios: - - - **if** par_str == '5 6 7 8' **return** array([4, 5, 6, 7]) - - **if** par_str == 'Ni' **raise** NotImplementedError - - **if** par_str == '0 1' **raise** AssertionError - - **if** par_str == '5..8' **return** array([4, 5, 6, 7]) - - **if** par_str == '8..5' **raise** AssertionError - """ - conf_pars = ConfigParameters('test1.cfg') - -# Scenario 1 - expected = np.array([4, 5, 6, 7]) - res = conf_pars.parse_string_ion_list('5 6 7 8') - self.assertEqual(res, expected) - -# Scenario 2 - with self.assertRaises(NotImplementedError): - conf_pars.parse_string_ion_list('Ni') - -# Scenario 3 - with self.assertRaises(AssertionError): - conf_pars.parse_string_ion_list('0 1') - -# Scenario 4 - res = conf_pars.parse_string_ion_list('5..8') - self.assertEqual(res, expected) - -# Scenario 5 - err_mess = "First index of the range" - with self.assertRaisesRegexp(AssertionError, err_mess): - conf_pars.parse_string_ion_list('8..5') - - -################################################################################ -# -# test_parse_string_tmatrix() -# -################################################################################ - def test_parse_string_tmatrix(self): - """ - Function: - - def parse_string_tmatrix(self, par_str) - - Parses a matrix defined as a set of rows in the conf-file. - - Scenarios: - - - **if** number of columns is not the same **raise** AssertionError - - **if** complex matrix is read and the number of columns is odd - **raise** AssertionError - - **if** a correct matrix is given **return** an array - - """ - conf_pars = ConfigParameters('test1.cfg') -# Scenario 1 - par_str = "1.0 0.0\n1.0" - err_mess = "Number of columns" - with self.assertRaisesRegexp(AssertionError, err_mess): - conf_pars.parse_string_tmatrix(par_str, real=True) - -# Scenario 2 - par_str = "1.0 0.0 2.0 1.0 0.0\n0.0 1.0 2.0 3.0 -1.0" - err_mess = "Complex matrix must" - with self.assertRaisesRegexp(AssertionError, err_mess): - conf_pars.parse_string_tmatrix(par_str, real=False) - -# Scenario 3 - par_str = "1.0 0.0 2.0 -3.0\n0.0 1.0 -1.0 1.0" - res = conf_pars.parse_string_tmatrix(par_str, real=False) - expected = np.array([[1.0, 2.0 - 3.0j], [1.0j, -1.0 + 1.0j]]) - self.assertEqual(res, expected) - - -################################################################################ -# -# test_parse_parameter_set() -# -################################################################################ - def test_parse_parameter_set(self): - """ - Function: - - def parse_parameter_set(self, section, param_set, excpetion=False) - - Scenarios: - - - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** - 'lshell' and 'ions' are in `param_set` **return** a dictionary {'lshell': 2} - - - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** - 'lshell' and 'ions' are in `param_set` and - exception=True **raise** Exception - """ - conf_pars = ConfigParameters('test1.cfg') - param_set = conf_pars.sh_required # contains 'lshell' and 'ions' - -# Scenario 1 - res = conf_pars.parse_parameter_set('Shell 1', param_set) - expected = {'lshell': 2} - self.assertDictEqual(res, expected) - -# Scenario 2 - section = 'Shell 1' - err_mess = "Required parameter" # .* in section [%s]"%(section) - with self.assertRaisesRegexp(Exception, err_mess): - conf_pars.parse_parameter_set(section, param_set, exception=True) - -################################################################################ -# -# test_parse_shells() -# -################################################################################ - def test_parse_shells(self): - """ - Function: - - def parse_shells(self) - - Scenarios: - - - **if** config-file does not contain a valid [Shell] section - **raise** AssertionError - - **if** a [Shell] section does not contain a valid index - **raise** ValueError - - **if** a [Shell] section does not contain all required parameters - **raise** Exception - - **if** two correct [Shell] sections are defined - **return** a dictionary of shell parameters - """ -# Scenario 1 - conf_pars = ConfigParameters('test2.cfg') - err_mess = "No projected shells" - with self.assertRaisesRegexp(AssertionError, err_mess): - conf_pars.parse_shells() - -# Scenario 2 - conf_pars = ConfigParameters('test3.cfg') - err_mess = "Failed to extract shell indices" - with self.assertRaisesRegexp(ValueError, err_mess): - conf_pars.parse_shells() - -# Scenario 3 - conf_pars = ConfigParameters('test4.cfg') - err_mess = "Required parameter" - with self.assertRaisesRegexp(Exception, err_mess): - conf_pars.parse_shells() - -# Scenario 4 - conf_pars = ConfigParameters('test5.cfg') - conf_pars.parse_shells() - res = conf_pars.shells - expected = [{'user_index': 1, 'lshell': 2, 'ion_list': np.array([4, 5, 6, 7])}, - {'user_index': 2, 'lshell': 1, 'ion_list': np.array([0, 1, 2, 3]), - 'tmatrix': np.array([[ 0., 1., 0.], [ 1., 0., 0.], [ 0., 0., 1.]])}] -# ...lousy way to test equality of two dictionaries containing numpy arrays - self.assertEqual(len(res), len(expected)) - - arr = res[0].pop('ion_list') - arr_exp = expected[0].pop('ion_list') - self.assertEqual(arr, arr_exp) - - arr = res[1].pop('ion_list') - arr_exp = expected[1].pop('ion_list') - self.assertEqual(arr, arr_exp) - - arr = res[1].pop('tmatrix') - arr_exp = expected[1].pop('tmatrix') - self.assertEqual(arr, arr_exp) - - self.assertListEqual(res, expected) - -################################################################################ -# -# test_parse_groups() -# -################################################################################ - def test_parse_groups(self): - """ - Function: - - def parse_groups(self) - - Scenarios: - - - **if** a [Group] section does not contain all required parameters - **raise** Exception - - **if** a correct group section is defined **return** a list of dictionaries - """ -# Scenario 1 - conf_pars = ConfigParameters('test5.cfg') - err_mess = "Required parameter" - with self.assertRaisesRegexp(Exception, err_mess): - conf_pars.parse_groups() - -# Scenario 2 - conf_pars = ConfigParameters('test7.cfg') - conf_pars.parse_groups() - res = conf_pars.groups - expected = [{'index': 1, 'shells': [1, 2], 'emin': -7.6, 'emax': 3.0}, - {'index': 2, 'shells': [3], 'emin': -1.6, 'emax': 2.0}] - self.assertListEqual(res, expected) - - -################################################################################ -# -# test_parse_input() -# -################################################################################ - def test_parse_input(self): - """ - Function: - - def parse_input(self) - - Scenarios: - - - **if** no [Group] section exists and more than one [Shell] section - is given **raise** AssertionError - - **if** no [Group] section exists but the single [Shell] section - does not contain required group information **raise** KeyError - - **if** a shell referenced in a group does not exist - **raise** Exception - - **if** not all defined shells are referenced in the groups - **raise** Exception - - **if** all sections are parsed error-free check that the output - is correct - """ -# Scenario 1 - conf_pars = ConfigParameters('test6.cfg') - err_mess = "At least one group" - with self.assertRaisesRegexp(AssertionError, err_mess): - conf_pars.parse_input() - -# Scenario 2 - conf_pars = ConfigParameters('test8.cfg') - err_mess = "One \[Shell\] section is" - with self.assertRaisesRegexp(KeyError, err_mess): - conf_pars.parse_input() - -# Scenario 3 - conf_pars = ConfigParameters('test9.cfg') - err_mess = "Shell 3 referenced in" - with self.assertRaisesRegexp(Exception, err_mess): - conf_pars.parse_input() - -# Scenario 4 - conf_pars = ConfigParameters('test10.cfg') - err_mess = "Some shells are not inside" - with self.assertRaisesRegexp(AssertionError, err_mess): - conf_pars.parse_input() - -# Scenario 5 - conf_pars = ConfigParameters('test7.cfg') - conf_pars.parse_input() -# with open('parse_input.output.test', 'wt') as f: -# f.write("Shells:\n") -# f.write(conf_pars.shells.__repr__() + '\n\n') -# f.write("Groups:\n") -# f.write(conf_pars.groups.__repr__() + '\n') - res = "Shells:\n" - res += conf_pars.shells.__repr__() + '\n\n' - res += "Groups:\n" - res += conf_pars.groups.__repr__() - - expected = r"""Shells: -[{'ion_list': array([4, 5, 6, 7]), 'user_index': 1, 'lshell': 2}, {'tmatrix': array([[ 0., 1., 0.], - [ 1., 0., 0.], - [ 0., 0., 1.]]), 'ion_list': array([0, 1, 2, 3]), 'user_index': 2, 'lshell': 1}, {'ion_list': array([0, 1, 2, 3]), 'user_index': 3, 'lshell': 3}] - -Groups: -[{'index': 1, 'shells': [0, 1], 'emin': -7.6, 'emax': 3.0}, {'index': 2, 'shells': [2], 'emin': -1.6, 'emax': 2.0}]""" - - self.assertEqual(res, expected) - - if __name__ == '__main__': - suite = unittest.TestLoader().loadTestsFromTestCase(TestSpecialParsers) -# unittest.TextTestRunner(verbosity=2, buffer=False).run(suite) + suite = unittest.TestLoader().discover('./') unittest.TextTestRunner(verbosity=2, buffer=True).run(suite) diff --git a/python/converters/vasp/test/inpconf/test_input.py b/python/converters/vasp/test/inpconf/test_input.py new file mode 100644 index 00000000..f1311aa1 --- /dev/null +++ b/python/converters/vasp/test/inpconf/test_input.py @@ -0,0 +1,85 @@ +r""" +Tests of 'parse_input()' defined in ConfigParameters class +""" +import arraytest +import numpy as np +from inpconf import ConfigParameters + +################################################################################ +# +# TestParseInput +# +################################################################################ +class TestParseInput(arraytest.ArrayTestCase): + """ + Function: + + def parse_input(self) + + Scenarios: + + - **if** no [Group] section exists and more than one [Shell] section + is given **raise** AssertionError + - **if** no [Group] section exists but the single [Shell] section + does not contain required group information **raise** KeyError + - **if** a shell referenced in a group does not exist + **raise** Exception + - **if** not all defined shells are referenced in the groups + **raise** Exception + - **if** all sections are parsed error-free check that the output + is correct + """ +# Scenario 1 + def test_no_group(self): + conf_pars = ConfigParameters('input_test_1.cfg') + err_mess = "At least one group" + with self.assertRaisesRegexp(AssertionError, err_mess): + conf_pars.parse_input() + +# Scenario 2 + def test_gr_required(self): + conf_pars = ConfigParameters('input_test_2.cfg') + err_mess = "One \[Shell\] section is" + with self.assertRaisesRegexp(KeyError, err_mess): + conf_pars.parse_input() + +# Scenario 3 + def test_no_shell(self): + conf_pars = ConfigParameters('input_test_3.cfg') + err_mess = "Shell 3 referenced in" + with self.assertRaisesRegexp(Exception, err_mess): + conf_pars.parse_input() + +# Scenario 4 + def test_shell_outside_groups(self): + conf_pars = ConfigParameters('input_test_4.cfg') + err_mess = "Some shells are not inside" + with self.assertRaisesRegexp(AssertionError, err_mess): + conf_pars.parse_input() + +# Scenario 5 + def test_example(self): + conf_pars = ConfigParameters('example.cfg') + conf_pars.parse_input() +# with open('parse_input.output.test', 'wt') as f: +# f.write("Shells:\n") +# f.write(conf_pars.shells.__repr__() + '\n\n') +# f.write("Groups:\n") +# f.write(conf_pars.groups.__repr__() + '\n') + res = "Shells:\n" + res += conf_pars.shells.__repr__() + '\n\n' + res += "Groups:\n" + res += conf_pars.groups.__repr__() + + expected = r"""Shells: +[{'ion_list': array([4, 5, 6, 7]), 'user_index': 1, 'lshell': 2}, {'tmatrix': array([[ 0., 1., 0.], + [ 1., 0., 0.], + [ 0., 0., 1.]]), 'ion_list': array([0, 1, 2, 3]), 'user_index': 2, 'lshell': 1}, {'ion_list': array([0, 1, 2, 3]), 'user_index': 3, 'lshell': 3}] + +Groups: +[{'index': 1, 'shells': [0, 1], 'emin': -7.6, 'emax': 3.0}, {'index': 2, 'shells': [2], 'emin': -1.6, 'emax': 2.0}]""" + + self.assertEqual(res, expected) + + + diff --git a/python/converters/vasp/test/inpconf/test_parameter_set.py b/python/converters/vasp/test/inpconf/test_parameter_set.py new file mode 100644 index 00000000..fb5bc988 --- /dev/null +++ b/python/converters/vasp/test/inpconf/test_parameter_set.py @@ -0,0 +1,48 @@ +r""" +Tests of 'parse_parameter_set()' defined in ConfigParameters class +""" +import arraytest +import numpy as np +from inpconf import ConfigParameters + +################################################################################ +# +# TestParseParameterSet +# +################################################################################ +class TestParseParameterSet(arraytest.ArrayTestCase): + """ + Function: + + def parse_parameter_set(self, section, param_set, excpetion=False) + + Scenarios: + + - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** + 'lshell' and 'ions' are in `param_set` **return** a dictionary {'lshell': 2} + + - **if** config-file section [Shell 1] contains 'LSHELL = 2' **and** + 'lshell' and 'ions' are in `param_set` and + exception=True **raise** Exception + """ + def setUp(self): + """ + """ +# Dummy ConfigParameters object + self.cpars = ConfigParameters('test1.cfg') + +# Scenario 1 + def test_sh_required(self): + param_set = self.cpars.sh_required # contains 'lshell' and 'ions' + res = self.cpars.parse_parameter_set('Shell 1', param_set) + expected = {'lshell': 2} + self.assertDictEqual(res, expected) + +# Scenario 2 + def test_sh_required_exception(self): + section = 'Shell 1' + param_set = self.cpars.sh_required # contains 'lshell' and 'ions' + err_mess = "Required parameter" # .* in section [%s]"%(section) + with self.assertRaisesRegexp(Exception, err_mess): + self.cpars.parse_parameter_set(section, param_set, exception=True) + diff --git a/python/converters/vasp/test/inpconf/test_shells.py b/python/converters/vasp/test/inpconf/test_shells.py new file mode 100644 index 00000000..24f73981 --- /dev/null +++ b/python/converters/vasp/test/inpconf/test_shells.py @@ -0,0 +1,76 @@ +r""" +Tests of 'parse_shells()' defined in ConfigParameters class +""" +import arraytest +import numpy as np +from inpconf import ConfigParameters + +################################################################################ +# +# TestParseShells +# +################################################################################ +class TestParseShells(arraytest.ArrayTestCase): + """ + Function: + + def parse_shells(self) + + Scenarios: + + - **if** config-file does not contain a valid [Shell] section + **raise** AssertionError + - **if** a [Shell] section does not contain a valid index + **raise** ValueError + - **if** a [Shell] section does not contain all required parameters + **raise** Exception + - **if** two correct [Shell] sections are defined + **return** a dictionary of shell parameters + """ +# Scenario 1 + def test_no_shell(self): + conf_pars = ConfigParameters('parse_shells_1.cfg') + err_mess = "No projected shells" + with self.assertRaisesRegexp(AssertionError, err_mess): + conf_pars.parse_shells() + +# Scenario 2 + def test_bad_indices(self): + conf_pars = ConfigParameters('parse_shells_2.cfg') + err_mess = "Failed to extract shell indices" + with self.assertRaisesRegexp(ValueError, err_mess): + conf_pars.parse_shells() + +# Scenario 3 + def test_sh_required(self): + conf_pars = ConfigParameters('parse_shells_3.cfg') + err_mess = "Required parameter" + with self.assertRaisesRegexp(Exception, err_mess): + conf_pars.parse_shells() + +# Scenario 4 + def test_two_shells(self): + conf_pars = ConfigParameters('parse_shells_4.cfg') + conf_pars.parse_shells() + res = conf_pars.shells + expected = [{'user_index': 1, 'lshell': 2, 'ion_list': np.array([4, 5, 6, 7])}, + {'user_index': 2, 'lshell': 1, 'ion_list': np.array([0, 1, 2, 3]), + 'tmatrix': np.array([[ 0., 1., 0.], [ 1., 0., 0.], [ 0., 0., 1.]])}] +# ...lousy way to test equality of two dictionaries containing numpy arrays + self.assertEqual(len(res), len(expected)) + + arr = res[0].pop('ion_list') + arr_exp = expected[0].pop('ion_list') + self.assertEqual(arr, arr_exp) + + arr = res[1].pop('ion_list') + arr_exp = expected[1].pop('ion_list') + self.assertEqual(arr, arr_exp) + + arr = res[1].pop('tmatrix') + arr_exp = expected[1].pop('tmatrix') + self.assertEqual(arr, arr_exp) + + self.assertListEqual(res, expected) + + diff --git a/python/converters/vasp/test/inpconf/test_special_parsers.py b/python/converters/vasp/test/inpconf/test_special_parsers.py new file mode 100644 index 00000000..233c44a0 --- /dev/null +++ b/python/converters/vasp/test/inpconf/test_special_parsers.py @@ -0,0 +1,149 @@ +r""" +Tests of special parseres defined in ConfigParameters class +""" +import arraytest +import numpy as np +from inpconf import ConfigParameters + +################################################################################ +# +# TestParseStringLogical +# +################################################################################ +class TestParseStringLogical(arraytest.ArrayTestCase): + """ + Function: + + def parse_string_logical(self, par_str) + + Scenarios: + + - **if** par_str == 'True' **return** True + - **if** par_str == 'False' **return** False + - **if** par_str == '0' **raise** assertion + """ + def setUp(self): + """ + """ +# Dummy ConfigParameters object + self.cpars = ConfigParameters('test1.cfg') + +# Scenario 1 + def test_true(self): + res = self.cpars.parse_string_logical('True') + self.assertEqual(res, True) + +# Scenario 2 + def test_false(self): + res = self.cpars.parse_string_logical('False') + self.assertEqual(res, False) + +# Scenario 3 + def test_incorrect(self): + with self.assertRaises(AssertionError): + self.cpars.parse_string_logical('0') + +################################################################################ +# +# TestParseStringIonList +# +################################################################################ +class TestParseStringIonList(arraytest.ArrayTestCase): + """ + Function: + + def parse_string_ion_list(self, par_str) + + Scenarios: + + - **if** par_str == '5 6 7 8' **return** array([4, 5, 6, 7]) + - **if** par_str == 'Ni' **raise** NotImplementedError + - **if** par_str == '0 1' **raise** AssertionError + - **if** par_str == '5..8' **return** array([4, 5, 6, 7]) + - **if** par_str == '8..5' **raise** AssertionError + """ + def setUp(self): + """ + """ +# Dummy ConfigParameters object + self.cpars = ConfigParameters('test1.cfg') + +# Scenario 1 + def test_simple_list(self): + expected = np.array([4, 5, 6, 7]) + res = self.cpars.parse_string_ion_list('5 6 7 8') + self.assertEqual(res, expected) + +# Scenario 2 + def test_atomic_symbol(self): + with self.assertRaises(NotImplementedError): + self.cpars.parse_string_ion_list('Ni') + +# Scenario 3 + def test_out_of_bounds(self): + err_mess = "Lowest ion index is" + with self.assertRaisesRegexp(AssertionError, err_mess): + self.cpars.parse_string_ion_list('0 1') + +# Scenario 4 + def test_list_range(self): + expected = np.array([4, 5, 6, 7]) + res = self.cpars.parse_string_ion_list('5..8') + self.assertEqual(res, expected) + +# Scenario 5 + def test_range_wrong_order(self): + err_mess = "First index of the range" + with self.assertRaisesRegexp(AssertionError, err_mess): + self.cpars.parse_string_ion_list('8..5') + + +################################################################################ +# +# TestParseStringTmatrix +# +################################################################################ +class TestParseStringTmatrix(arraytest.ArrayTestCase): + """ + Function: + + def parse_string_tmatrix(self, par_str) + + Parses a matrix defined as a set of rows in the conf-file. + + Scenarios: + + - **if** number of columns is not the same **raise** AssertionError + - **if** complex matrix is read and the number of columns is odd + **raise** AssertionError + - **if** a correct matrix is given **return** an array + + """ + def setUp(self): + """ + """ +# Dummy ConfigParameters object + self.cpars = ConfigParameters('test1.cfg') + +# Scenario 1 + def test_number_of_columns(self): + par_str = "1.0 0.0\n1.0" + err_mess = "Number of columns" + with self.assertRaisesRegexp(AssertionError, err_mess): + self.cpars.parse_string_tmatrix(par_str, real=True) + +# Scenario 2 + def test_complex_matrix_odd(self): + par_str = "1.0 0.0 2.0 1.0 0.0\n0.0 1.0 2.0 3.0 -1.0" + err_mess = "Complex matrix must" + with self.assertRaisesRegexp(AssertionError, err_mess): + self.cpars.parse_string_tmatrix(par_str, real=False) + +# Scenario 3 + def test_complex_matrix(self): + par_str = "1.0 0.0 2.0 -3.0\n0.0 1.0 -1.0 1.0" + res = self.cpars.parse_string_tmatrix(par_str, real=False) + expected = np.array([[1.0, 2.0 - 3.0j], [1.0j, -1.0 + 1.0j]]) + self.assertEqual(res, expected) + +