Salome HOME
Synchronize adm files
[modules/kernel.git] / src / KERNEL_PY / kernel / testdata.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library 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 GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 __author__="gboulant"
25 __date__ ="$17 avr. 2010 19:44:36$"
26
27 from enumerate import Enumerate
28 from datamodeler import DataModeler, TypeString, TypeInteger
29 from salome.kernel import Callable
30
31 class TestData(DataModeler):
32     """
33     This class is the placeholder for meta data associated to a study case.
34     The meta-data are used for organisation and indexing purposes
35     """
36     TYPES_LIST=Enumerate([
37         'SEP',
38         'OTHER'
39     ])
40
41     def __init__(self):
42         DataModeler.__init__(self)
43         self.addAttribute(
44             name  = "NAME",
45             type  = TypeString,
46             range = None
47             )
48         self.addAttribute(
49             name  = "TYPE",
50             type  = TypeInteger,
51             range = self.TYPES_LIST.listvalues()
52             )
53
54     def setName(self, value):
55         self.NAME = value
56
57     def getName(self):
58         return self.NAME
59
60     def setType(self, value):
61         self.TYPE = value
62
63     def getType(self):
64         return self.TYPE
65
66     def getDefault():
67         """
68         Create a default instance of TestData
69         @class-method
70         """
71         testdata = TestData()
72         testdata.NAME = "my case"
73         testdata.TYPE = TestData.TYPES_LIST.SEP
74         return testdata
75
76     getDefault = Callable(getDefault)
77 #
78 # ==============================================================================
79 # Basic use cases and unit tests
80 # ==============================================================================
81 #
82 from uiexception import UiException
83
84 def TEST_getName():
85     testdata = TestData()
86     testdata.setName("Sous-epaisseur")
87     testdata.setType(TestData.TYPES_LIST.SEP)
88     if ( testdata.NAME != "Sous-epaisseur" ):
89         return False
90     return True
91
92
93 def TEST_useBadKey():
94     testdata = TestData()
95     try:
96         testdata.unknown = "unknown"
97         # This should not arrive here
98         return False
99     except UiException, err:
100         print err
101         return True
102
103 def TEST_useBadType():
104     testdata = TestData()
105     try:
106         testdata.TYPE = "unknown"
107         # This should not arrive here
108         return False
109     except UiException, err:
110         print err
111         return True
112
113 def TEST_useBadRange():
114     testdata = TestData()
115
116     try:
117         testdata.TYPE = TestData.TYPES_LIST.SEP
118         testdata.setType(TestData.TYPES_LIST.SEP)
119         # This should arrive here
120     except UiException, err:
121         # And not here
122         print err
123         return False
124
125     try:
126         testdata.TYPE = 9999 # a type that does not exist in the range
127         # This should not arrive here
128         return False
129     except UiException, err:
130         print err
131         return True
132
133 def TEST_serialize():
134     import salome.kernel
135     ref_testdata = TestData()
136     ref_testdata.setName("The firts name")
137     res_testdata = salome.kernel.unserialize(salome.kernel.serialize(ref_testdata))
138
139     print res_testdata.getName()
140
141     if res_testdata.getName() != ref_testdata.getName():
142         return False
143
144     # Is the unserialized data still functional?
145     try:
146         res_testdata.setName("An other name")
147         print res_testdata.getName()
148     except:
149         print e
150         return False
151     return True
152
153 if __name__ == "__main__":
154     from unittester import run
155     run("salome/kernel/testdata","TEST_getName")
156     run("salome/kernel/testdata","TEST_useBadKey")
157     run("salome/kernel/testdata","TEST_useBadType")
158     run("salome/kernel/testdata","TEST_useBadRange")
159     run("salome/kernel/testdata","TEST_serialize")