TestPreferences.py 3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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
34
import shutil
35
36
import unittest

37
from MDANSE.Core.Preferences import Preferences
38
39
40
41
42
43
44
45
46
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):
47
48
49
50

    def setUp(self):
                
        PREFERENCES = Preferences()
51
52
53
    
    def test_get_item(self):
        
54
        self.assertRaises(PreferencesError,PREFERENCES.__getitem__,'xxxxx')
55
56
57

    def test_set_item(self):
        
58
59
60
61
        val = PREFERENCES["working_directory"].get_value()
        PREFERENCES["working_directory"].set_value("test")
        self.assertEqual(PREFERENCES["working_directory"].value,os.path.join(os.getcwd(),"test"))
        PREFERENCES["working_directory"].set_value(val)
62
63
        
        shutil.rmtree("test")
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
                
    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)