Package test_suite :: Package unit_tests :: Module state_testing_base
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests.state_testing_base

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007-2011 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports. 
 24  from os import sep 
 25  from tempfile import mktemp 
 26   
 27  # relax module imports. 
 28  from data import Relax_data_store; ds = Relax_data_store() 
 29  from generic_fns import pipes 
 30  from generic_fns.reset import reset 
 31  from status import Status; status = Status() 
 32  from test_suite.unit_tests.base_classes import UnitTestCase 
 33   
 34   
35 -class State_base_class(UnitTestCase):
36 """Base class for the tests of both the 'prompt.state' and 'generic_fns.state' modules. 37 38 This base class also contains shared unit tests. 39 """ 40
41 - def test_load(self):
42 """The unpickling and restoration of the relax data storage singleton. 43 44 This tests the normal operation of the generic_fns.state.load() function. 45 """ 46 47 # Test the contents of the empty singleton. 48 self.assertEqual(list(ds.keys()), []) 49 self.assertEqual(pipes.cdp_name(), None) 50 self.assert_(not hasattr(ds, 'y')) 51 52 # Load the state. 53 self.state.load_state(state='basic_single_pipe', dir=status.install_path+sep+'test_suite'+sep+'shared_data'+sep+'saved_states') 54 55 # Get the data pipe. 56 dp = pipes.get_pipe('orig') 57 58 # Test the contents of the restored singleton. 59 self.assertEqual(list(ds.keys()), ['orig']) 60 self.assertEqual(pipes.cdp_name(), 'orig') 61 self.assertEqual(dp.x, 1) 62 self.assertEqual(ds.y, 'Hello')
63 64
65 - def test_load_and_modify(self):
66 """The modification of an unpickled and restored relax data storage singleton. 67 68 This tests the normal operation of the generic_fns.state.load() function. 69 """ 70 71 # Test the contents of the empty singleton. 72 self.assertEqual(list(ds.keys()), []) 73 self.assertEqual(pipes.cdp_name(), None) 74 self.assert_(not hasattr(ds, 'y')) 75 76 # Load the state. 77 self.state.load_state(state='basic_single_pipe', dir=status.install_path+sep+'test_suite'+sep+'shared_data'+sep+'saved_states') 78 79 # Add a new data pipe and some data to it. 80 ds.add('new', 'jw_mapping') 81 cdp.z = [None, None] 82 83 # Get the data pipes. 84 dp_orig = pipes.get_pipe('orig') 85 dp_new = pipes.get_pipe('new') 86 87 # Test the contents of the restored singleton (with subsequent data added). 88 self.assertEqual(list(ds.keys()).sort(), ['orig', 'new'].sort()) 89 self.assertEqual(pipes.cdp_name(), 'new') 90 self.assertEqual(dp_orig.x, 1) 91 self.assertEqual(ds.y, 'Hello') 92 self.assertEqual(dp_new.z, [None, None])
93 94
95 - def test_load_and_reset(self):
96 """The resetting of an unpickled and restored relax data storage singleton. 97 98 This tests the normal operation of the generic_fns.state.load() function. 99 """ 100 101 # Test the contents of the empty singleton. 102 self.assertEqual(list(ds.keys()), []) 103 self.assertEqual(pipes.cdp_name(), None) 104 self.assert_(not hasattr(ds, 'y')) 105 106 # Load the state. 107 self.state.load_state(state='basic_single_pipe', dir=status.install_path+sep+'test_suite'+sep+'shared_data'+sep+'saved_states') 108 109 # Reset relax. 110 reset() 111 112 # Test that there are no contents in the reset singleton. 113 self.assertEqual(list(ds.keys()), []) 114 self.assertEqual(pipes.cdp_name(), None) 115 self.assert_(not hasattr(ds, 'y'))
116 117
118 - def test_save(self):
119 """The pickling and saving of the relax data storage singleton. 120 121 This tests the normal operation of the generic_fns.state.save() function. 122 """ 123 124 # Create a temporary file descriptor. 125 ds.tmpfile = mktemp() 126 127 # Add a data pipe to the data store. 128 ds.add(pipe_name='orig', pipe_type='mf') 129 130 # Get the data pipe. 131 dp = pipes.get_pipe('orig') 132 133 # Add a single object to the 'orig' data pipe. 134 dp.x = 1 135 136 # Add a single object to the storage object. 137 ds.y = 'Hello' 138 139 # Save the state. 140 self.state.save_state(state=ds.tmpfile)
141