Commit d2f3271c authored by eric pellegrini's avatar eric pellegrini

Reintroduced and refactored some unit tests

parent 56ee68be
......@@ -305,12 +305,18 @@ class Preferences(object):
except KeyError:
raise PreferencesError("Unknown preferences item")
def load(self):
def load(self, path=None):
'''
Load the preferences.
Load the preferences from an existing Preferences file.
The default value is the default location for loading Preferences file.
:param path: the path for the preferences file
:type path: str
'''
path = PLATFORM.preferences_file()
if path is None:
path = PLATFORM.preferences_file()
if not isinstance(path,basestring):
raise PreferencesError("Invalid type for preferences filename: %s" % path)
......@@ -328,12 +334,18 @@ class Preferences(object):
for k, v in self._parser.items(s):
self.set_preferences_item(k,v)
def save(self):
def save(self,path=None):
'''
Save the preferences.
Save the preferences to a file.
The default value is the default location for loading Preferences file.
:param path: the path for the preferences file
:type path: str
'''
path = PLATFORM.preferences_file()
if path is None:
path = PLATFORM.preferences_file()
try:
f = open(path, "w")
......
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on May 29, 2015
@author: Eric C. Pellegrini
'''
import unittest
import os
import glob
def suite():
files = glob.glob('Test*.py')
modules = [__import__(os.path.splitext(f)[0],globals(),locals(),[],-1) for f in files]
test_suite = unittest.TestSuite()
for m in modules:
test_suite.addTests(m.suite())
return test_suite
def run_test():
unittest.TextTestRunner(verbosity=2).run(suite())
if __name__ == '__main__':
run_test()
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on May 29, 2015
@author: Eric C. Pellegrini
'''
import unittest
from UnitTest import UnitTest
from MDANSE import ELEMENTS
from MDANSE.Data.ElementsDatabase import ElementsDatabaseError
class TestElementsDatabase(UnitTest):
'''
Unittest for the configurators used to setup an analysis in nMolDyn
'''
def setUp(self):
self._types = {float:1.0,int:1,str:"toto"}
def test___contains__(self):
self.assertFalse("fhsdjfsd" in ELEMENTS)
self.assertFalse("h" in ELEMENTS)
self.assertTrue("H" in ELEMENTS)
def test___getitem__(self):
for e in ELEMENTS.elements:
for p in ELEMENTS.properties:
self.assertNotRaises(ELEMENTS.__getitem__,(e,p))
def test_getelement(self):
for e in ELEMENTS.elements:
self.assertNotRaises(ELEMENTS.get_element,e)
def test_get_property(self):
for p in ELEMENTS.properties:
self.assertNotRaises(ELEMENTS.get_property,p)
def test___setitem__(self):
self.assertNotRaises(ELEMENTS.__setitem__,('C','atomic_weight'),20.0)
def test_add_element(self):
# Adding an already existing element does not trigger an error anymore
self.assertNotRaises(ELEMENTS.add_element, "H")
# Otherwise, everything should be OK
self.assertNotRaises(ELEMENTS.add_element, "element1")
def test_add_property(self):
# Adding an already existing property must trigger an error
self.assertRaises(ElementsDatabaseError, ELEMENTS.add_property, "atomic_weight",0.0)
# Otherwise, everything should be OK
self.assertNotRaises(ELEMENTS.add_property, "prop1",'float')
self.assertNotRaises(ELEMENTS.add_property, "prop2",'int')
self.assertNotRaises(ELEMENTS.add_property, "prop3",'str')
def test_has_property(self):
for p in ELEMENTS.properties:
self.assertTrue(ELEMENTS.has_property(p))
self.assertFalse(ELEMENTS.has_property("gfkljfklsj"))
def test_has_element(self):
for e in ELEMENTS.elements:
self.assertTrue(ELEMENTS.has_element(e))
self.assertFalse(ELEMENTS.has_element("gfkljfklsj"))
def suite():
loader = unittest.TestLoader()
s = unittest.TestSuite()
s.addTest(loader.loadTestsFromTestCase(TestElementsDatabase))
return s
if __name__ == '__main__':
unittest.main(verbosity=2)
\ No newline at end of file
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on May 29, 2015
@author: Eric C. Pellegrini
'''
import unittest
import numpy
from UnitTest import UnitTest
from MDANSE.Mathematics.Geometry import center_of_mass
class TestGeometry(UnitTest):
'''
Unittest for the geometry-related functions
'''
def test_center_of_mass(self):
coords = numpy.array([[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]],dtype=numpy.float64)
self.assertTrue(numpy.array_equal(center_of_mass(coords),numpy.array([0.5,0.5,0.5],dtype=numpy.float64)))
masses = numpy.array([1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0],dtype=numpy.float64)
self.assertTrue(numpy.array_equal(center_of_mass(coords,masses=masses),numpy.array([0.5,0.5,0.0],dtype=numpy.float64)))
def suite():
loader = unittest.TestLoader()
s = unittest.TestSuite()
s.addTest(loader.loadTestsFromTestCase(TestGeometry))
return s
if __name__ == '__main__':
unittest.main(verbosity=2)
\ No newline at end of file
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on May 29, 2015
@author: Eric C. Pellegrini
'''
import unittest
import numpy
from UnitTest import UnitTest
from MDANSE.MolecularDynamics.Analysis import radius_of_gyration, mean_square_deviation, mean_square_fluctuation
class TestMolecularDynamics(UnitTest):
'''
Unittest for the geometry-related functions
'''
def test_radius_of_gyration(self):
coords = numpy.array([[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]],dtype=numpy.float64)
self.assertEqual(radius_of_gyration(coords,root=True),numpy.sqrt(0.75))
masses = numpy.array([1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0],dtype=numpy.float64)
self.assertEqual(radius_of_gyration(coords,masses=masses,root=True),numpy.sqrt(0.5))
def test_mean_square_deviation(self):
coords1 = numpy.array([[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]],dtype=numpy.float64)
coords2 = numpy.array([[1,1,1],[2,1,1],[2,2,1],[1,2,1],[1,1,2],[2,1,2],[2,2,2],[1,2,2]],dtype=numpy.float64)
self.assertEqual(mean_square_deviation(coords1,coords2,root=True),numpy.sqrt(3))
self.assertEqual(mean_square_deviation(coords1,coords2,root=False),3)
def test_mean_square_fluctuation(self):
coords = numpy.array([[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]],dtype=numpy.float64)
self.assertEqual(mean_square_fluctuation(coords,root=False),0.75)
def suite():
loader = unittest.TestLoader()
s = unittest.TestSuite()
s.addTest(loader.loadTestsFromTestCase(TestMolecularDynamics))
return s
if __name__ == '__main__':
unittest.main(verbosity=2)
\ No newline at end of file
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on May 29, 2015
@author: Eric C. Pellegrini
'''
import os
import unittest
# The nmoldyn imports
from MDANSE import PREFERENCES
from MDANSE.Core.Preferences import PreferencesError
from UnitTest import UnitTest
class UnStringable:
def __str__(self):
raise TypeError("Object not castable to a string.")
class TestPreferences(UnitTest):
def test_get_item(self):
self.assertRaises(PreferencesError,PREFERENCES.get_preferences_item,'xxxxx')
def test_set_item(self):
val = PREFERENCES.get_preferences_item("working_directory")
PREFERENCES.set_preferences_item("working_directory","test")
self.assertEqual(PREFERENCES.get_preferences_item("working_directory").value,os.path.join(os.getcwd(),"test"))
PREFERENCES.set_preferences_item("working_directory",val)
def test_load_preferences(self):
'''
Test the method that loads the preferences
'''
# Test that loading a preferences file whose type is not a basestring throw a PreferencesError
self.assertRaises(PreferencesError,PREFERENCES.load,10)
def test_save_preferences(self):
'''
Test the method that saves the preferences
'''
# Test that saving a preferences file whose type is not a basestring throw a PreferencesError
self.assertRaises(PreferencesError,PREFERENCES.save,10)
# Test that saving a preferences whose path does not exists throw a PreferencesError
self.assertRaises(PreferencesError,PREFERENCES.save,os.path.join('xxxx','yyyy'))
def suite():
loader = unittest.TestLoader()
s = unittest.TestSuite()
s.addTest(loader.loadTestsFromTestCase(TestPreferences))
return s
if __name__ == '__main__':
unittest.main(verbosity=2)
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on May 29, 2015
@author: Eric C. Pellegrini
'''
import sys
import unittest
class UnitTest(unittest.TestCase):
def assertNotRaises(self, callableObj, *args, **kwargs):
try:
callableObj(*args, **kwargs)
except:
self.fail(sys.exc_info())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment