Back to index

salome-kernel  6.5.0
testdata.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 __author__="gboulant"
00025 __date__ ="$17 avr. 2010 19:44:36$"
00026 
00027 from enumerate import Enumerate
00028 from datamodeler import DataModeler, TypeString, TypeInteger
00029 from salome.kernel import Callable
00030 
00031 class TestData(DataModeler):
00032     """
00033     This class is the placeholder for meta data associated to a study case.
00034     The meta-data are used for organisation and indexing purposes
00035     """
00036     TYPES_LIST=Enumerate([
00037         'SEP',
00038         'OTHER'
00039     ])
00040 
00041     def __init__(self):
00042         DataModeler.__init__(self)
00043         self.addAttribute(
00044             name  = "NAME",
00045             type  = TypeString,
00046             range = None
00047             )
00048         self.addAttribute(
00049             name  = "TYPE",
00050             type  = TypeInteger,
00051             range = self.TYPES_LIST.listvalues()
00052             )
00053 
00054     def setName(self, value):
00055         self.NAME = value
00056 
00057     def getName(self):
00058         return self.NAME
00059 
00060     def setType(self, value):
00061         self.TYPE = value
00062 
00063     def getType(self):
00064         return self.TYPE
00065 
00066     def getDefault():
00067         """
00068         Create a default instance of TestData
00069         @class-method
00070         """
00071         testdata = TestData()
00072         testdata.NAME = "my case"
00073         testdata.TYPE = TestData.TYPES_LIST.SEP
00074         return testdata
00075 
00076     getDefault = Callable(getDefault)
00077 #
00078 # ==============================================================================
00079 # Basic use cases and unit tests
00080 # ==============================================================================
00081 #
00082 from uiexception import UiException
00083 
00084 def TEST_getName():
00085     testdata = TestData()
00086     testdata.setName("Sous-epaisseur")
00087     testdata.setType(TestData.TYPES_LIST.SEP)
00088     if ( testdata.NAME != "Sous-epaisseur" ):
00089         return False
00090     return True
00091 
00092 
00093 def TEST_useBadKey():
00094     testdata = TestData()
00095     try:
00096         testdata.unknown = "unknown"
00097         # This should not arrive here
00098         return False
00099     except UiException, err:
00100         print err
00101         return True
00102 
00103 def TEST_useBadType():
00104     testdata = TestData()
00105     try:
00106         testdata.TYPE = "unknown"
00107         # This should not arrive here
00108         return False
00109     except UiException, err:
00110         print err
00111         return True
00112 
00113 def TEST_useBadRange():
00114     testdata = TestData()
00115 
00116     try:
00117         testdata.TYPE = TestData.TYPES_LIST.SEP
00118         testdata.setType(TestData.TYPES_LIST.SEP)
00119         # This should arrive here
00120     except UiException, err:
00121         # And not here
00122         print err
00123         return False
00124 
00125     try:
00126         testdata.TYPE = 9999 # a type that does not exist in the range
00127         # This should not arrive here
00128         return False
00129     except UiException, err:
00130         print err
00131         return True
00132 
00133 def TEST_serialize():
00134     import salome.kernel
00135     ref_testdata = TestData()
00136     ref_testdata.setName("The firts name")
00137     res_testdata = salome.kernel.unserialize(salome.kernel.serialize(ref_testdata))
00138 
00139     print res_testdata.getName()
00140 
00141     if res_testdata.getName() != ref_testdata.getName():
00142         return False
00143 
00144     # Is the unserialized data still functional?
00145     try:
00146         res_testdata.setName("An other name")
00147         print res_testdata.getName()
00148     except:
00149         print e
00150         return False
00151     return True
00152 
00153 if __name__ == "__main__":
00154     from unittester import run
00155     run("salome/kernel/testdata","TEST_getName")
00156     run("salome/kernel/testdata","TEST_useBadKey")
00157     run("salome/kernel/testdata","TEST_useBadType")
00158     run("salome/kernel/testdata","TEST_useBadRange")
00159     run("salome/kernel/testdata","TEST_serialize")