2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2015 CEA/DEN, EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # This file is a set of basic use case to test (from the python
23 # context) the functions developped in MED modules for the field
26 # (gboulant - 16/6/2011)
29 from xmed import properties
30 from xmed import fieldproxy
31 from xmed.fieldproxy import FieldProxy
32 #from xmed.fieldtools import dup, stat, get, save
33 #from xmed.fieldguide import doc
35 # Don't forget to set the globals dictionnary for the fields tools to
37 xmed.setConsoleGlobals(globals())
39 # Load some test data in the MedDataManager
40 filepath = properties.testFilePath
41 xmed.dataManager.loadDatasource(filepath)
42 fieldHandlerList = xmed.dataManager.getFieldHandlerList()
46 This function defines a set of field variable for quick tests in
47 the python console. You just have to execute the function to get
48 the variables defined in the global context.
50 fh1=fieldHandlerList[0]
51 fh2=fieldHandlerList[1]
54 return fh1, fh2, f1, f2
56 # Setup for quick tests in the python console
57 fh1, fh2, f1, f2 = setup()
60 fieldHandler0 = fieldHandlerList[0]
61 fieldHandler1 = fieldHandlerList[1]
63 # The addition can be done using field handler directly
64 addFieldHandler = xmed.calculator.add(fieldHandler0, fieldHandler1)
67 # Or with a field proxy that ease the writing of operations
68 fieldProxy0 = FieldProxy(fieldHandler0)
69 fieldProxy1 = FieldProxy(fieldHandler1)
71 res = fieldProxy0 + fieldProxy1
72 if res is None: return False
76 def TEST_arithmetics():
77 fieldProxy0 = FieldProxy(fieldHandlerList[0])
78 fieldProxy1 = FieldProxy(fieldHandlerList[1])
80 # Standard operations where operandes are fields
81 res = fieldProxy0 + fieldProxy1
82 if res is None: return False
83 res = fieldProxy0 - fieldProxy1
84 if res is None: return False
85 res = fieldProxy0 * fieldProxy1
86 if res is None: return False
87 res = fieldProxy0 / fieldProxy1
89 # Standard operations with scalar operandes
90 res = fieldProxy0 + 3.4
91 if res is None: return False
92 res = 3.4 + fieldProxy0
93 if res is None: return False
94 res = fieldProxy0 - 3.4
95 if res is None: return False
96 res = 3.4 - fieldProxy0
97 if res is None: return False
98 res = fieldProxy0 * 3.4
99 if res is None: return False
100 res = 3.4 * fieldProxy0
101 if res is None: return False
102 res = fieldProxy0 / 3.4
103 if res is None: return False
104 res = 3.4 / fieldProxy0
105 if res is None: return False
109 def TEST_unary_operations():
110 fieldProxy0 = FieldProxy(fieldHandlerList[0])
112 res = fieldProxy0.dup()
113 if res is None: return False
114 res = xmed.dup(fieldProxy0)
115 if res is None: return False
116 res = pow(fieldProxy0,2)
117 if res is None: return False
121 def TEST_composition():
122 # In this test, we combine operandes that are supposed
123 # to be compatible. We expect that no error occur.
124 fieldProxy0 = FieldProxy(fieldHandlerList[0])
125 fieldProxy1 = FieldProxy(fieldHandlerList[1])
127 res = pow(fieldProxy0,2) + fieldProxy1
128 if res is None: return False
132 def TEST_litteral_equation():
133 fieldProxy0 = FieldProxy(fieldHandlerList[0])
134 res = fieldProxy0.ope("abs(u)^2")
135 if res is None: return False
138 def TEST_use_restriction():
139 fieldProxy0 = FieldProxy(fieldHandlerList[0])
140 res = fieldProxy0("c=1;g='toto'")
141 if res is None: return False
144 def TEST_modification_of_attributes():
145 fieldProxy0 = FieldProxy(fieldHandlerList[0])
146 id_ref = fieldProxy0.id
147 fieldname_ref = fieldProxy0.fieldname
148 meshname_ref = fieldProxy0.meshname
151 # This operations are not allowed, or not that way
153 # This should print that it is not allowed:
154 fieldProxy0.id = id_ref+3
155 if fieldProxy0.id != id_ref:
156 print "ERR: the id should be %d (%d found)"%(id_ref,fieldProxy0.id)
158 # This should print that it must be done using the command update
159 fieldProxy0.fieldname = fieldname_ref+"toto"
160 if fieldProxy0.fieldname != fieldname_ref:
161 print "ERR: the fieldname should be %s (%s found)"%(fieldname_ref,fieldProxy0.fieldname)
163 # This should print that it is not allowed:
164 fieldProxy0.meshname = meshname_ref+"titi"
165 if fieldProxy0.meshname != meshname_ref:
166 print "ERR: the meshname should be %s (%s found)"%(meshname_ref,fieldProxy0.meshname)
171 def TEST_update_metadata():
172 fieldProxyRef = FieldProxy(fieldHandlerList[0])
173 id = fieldProxyRef.id
176 fieldProxyRef.update(name=name_ref)
178 fieldProxyRes = xmed.get(id)
179 name_res = fieldProxyRes.fieldname
180 if name_res != name_ref:
181 print "ERR: the fieldname should be %s (%s found)"%(name_ref,name_res)
186 # =============================================================
188 # =============================================================
191 from salome.kernel import pyunittester
192 class MyTestSuite(unittest.TestCase):
193 def test_addition(self):
194 result = pyunittester.execAndConvertExceptionToBoolean(TEST_addition)
195 self.assertTrue(result)
197 def test_arithmetics(self):
198 result = pyunittester.execAndConvertExceptionToBoolean(TEST_arithmetics)
199 self.assertTrue(result)
201 def test_unary_operations(self):
202 result = pyunittester.execAndConvertExceptionToBoolean(TEST_unary_operations)
203 self.assertTrue(result)
205 def test_composition(self):
206 result = pyunittester.execAndConvertExceptionToBoolean(TEST_composition)
207 self.assertTrue(result)
209 def test_litteral_equation(self):
210 result = pyunittester.execAndConvertExceptionToBoolean(TEST_litteral_equation)
211 self.assertTrue(result)
213 def test_modification_of_attributes(self):
214 self.assertTrue(TEST_modification_of_attributes())
216 def test_update_metadata(self):
217 self.assertTrue(TEST_update_metadata())
220 pyunittester.run(MyTestSuite)
225 #TEST_unary_operations()
226 #TEST_update_metadata()
229 if __name__ == "__main__":