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

Source Code for Module test_suite.unit_tests._specific_analyses.test_api

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program 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 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Python module imports. 
 23  from inspect import getargspec 
 24  from re import search 
 25  import types 
 26  from unittest import TestCase 
 27   
 28  # relax module imports. 
 29  from specific_analyses.api_base import API_base 
 30  from specific_analyses.consistency_tests import Consistency_tests 
 31  from specific_analyses.frame_order import Frame_order 
 32  from specific_analyses.hybrid import Hybrid 
 33  from specific_analyses.jw_mapping import Jw_mapping 
 34  from specific_analyses.model_free import Model_free 
 35  from specific_analyses.n_state_model import N_state_model 
 36  from specific_analyses.noe import Noe 
 37  from specific_analyses.relax_fit import Relax_fit 
 38   
 39   
40 -class Test_api(TestCase):
41 """Unit tests for the specific analyses API.""" 42
43 - def __format_method(self, name, args, varargs, varkw, defaults):
44 """Method for formatting the method.""" 45 46 # Method start. 47 text = name + '(' 48 49 # No keywords. 50 if defaults == None: 51 defaults = () 52 53 # Counts. 54 num_args = len(args) - len(defaults) 55 num_kw = len(defaults) 56 57 # Args. 58 for i in range(num_args): 59 # Separator. 60 if i != 0: 61 text = text + ', ' 62 63 # The arg. 64 text = text + args[i] 65 66 # Keyword args. 67 for j in range(num_kw): 68 # Separator. 69 if num_args or j != 0: 70 text = text + ', ' 71 72 # The keyword. 73 text = text + args[num_args+j] + '=' + repr(defaults[j]) 74 75 # End. 76 text = text + ')' 77 return text
78 79
80 - def __check_method_args(self, analysis_obj):
81 """Check the args of all API methods. 82 83 @param analysis_obj: The specific analysis object. 84 @type analysis_obj: instance 85 """ 86 87 # The base object. 88 base = API_base() 89 90 # Loop over the objects of the specific analysis. 91 for name in dir(analysis_obj): 92 # Skip anything starting with '_'. 93 if search('^_', name): 94 continue 95 96 # Get the object in the two classes. 97 obj_base = getattr(base, name) 98 obj = getattr(analysis_obj, name) 99 100 # Skip non-method objects. 101 if not isinstance(obj_base, types.MethodType): 102 continue 103 104 # Get the args and their default values. 105 args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) 106 args, varargs, varkw, defaults = getargspec(obj) 107 108 # Check the args. 109 if args_base != args or varargs_base != varargs or varkw_base != varkw: 110 # Get string representations of the methods. 111 doc_base = self.__format_method(name, args_base, varargs_base, varkw_base, defaults_base) 112 doc = self.__format_method(name, args, varargs, varkw, defaults) 113 print(doc_base) 114 115 # Fail. 116 self.fail('The args of the method:\n\t' + doc + '\ndo not match those of the API method:\n\t' + doc_base)
117 118
119 - def __check_objects(self, analysis_obj):
120 """Check the args of all API methods. 121 122 @param analysis_obj: The specific analysis object. 123 @type analysis_obj: instance 124 """ 125 126 # The base object. 127 base = API_base() 128 129 # The objects in the base class. 130 base_names = dir(base) 131 132 # Loop over the objects of the specific analysis. 133 for name in dir(analysis_obj): 134 # Skip anything starting with '_'. 135 if search('^_', name): 136 continue 137 138 # Get the object in the derived class. 139 obj = getattr(analysis_obj, name) 140 141 # Not present. 142 if name not in base_names: 143 self.fail('The object ' + repr(name) + ' ' + repr(type(obj)) + ' cannot be found in the API base class.')
144 145
147 """The consistency tests object public method args check.""" 148 149 # Check. 150 self.__check_method_args(Consistency_tests())
151 152
154 """The consistency tests object public objects check.""" 155 156 # Check. 157 self.__check_objects(Consistency_tests())
158 159
161 """The frame order object public method args check.""" 162 163 # Check. 164 self.__check_method_args(Frame_order())
165 166
167 - def test_frame_order_objects(self):
168 """The frame order object public objects check.""" 169 170 # Check. 171 self.__check_objects(Frame_order())
172 173
174 - def test_hybrid_method_args(self):
175 """The hybrid object public method args check.""" 176 177 # Check. 178 self.__check_method_args(Hybrid())
179 180
181 - def test_hybrid_objects(self):
182 """The hybrid object public objects check.""" 183 184 # Check. 185 self.__check_objects(Hybrid())
186 187
189 """The reduced spectral density mapping object public method args check.""" 190 191 # Check. 192 self.__check_method_args(Jw_mapping())
193 194
195 - def test_jw_mapping_objects(self):
196 """The reduced spectral density mapping object public objects check.""" 197 198 # Check. 199 self.__check_objects(Jw_mapping())
200 201
203 """The model-free object public method args check.""" 204 205 # Check. 206 self.__check_method_args(Model_free())
207 208
209 - def test_model_free_objects(self):
210 """The model-free object public objects check.""" 211 212 # Check. 213 self.__check_objects(Model_free())
214 215
217 """The N-state model object public method args check.""" 218 219 # Check. 220 self.__check_method_args(N_state_model())
221 222
224 """The N-state model object public objects check.""" 225 226 # Check. 227 self.__check_objects(N_state_model())
228 229
230 - def test_noe_method_args(self):
231 """The NOE object public method args check.""" 232 233 # Check. 234 self.__check_method_args(Noe())
235 236
237 - def test_noe_objects(self):
238 """The NOE object public objects check.""" 239 240 # Check. 241 self.__check_objects(Noe())
242 243
245 """The relaxation curve fitting object public method args check.""" 246 247 # Check. 248 self.__check_method_args(Relax_fit())
249 250
251 - def test_relax_fit_objects(self):
252 """The relaxation curve fitting object public objects check.""" 253 254 # Check. 255 self.__check_objects(Relax_fit())
256