Salome HOME
datasource load from gui relies on tui
[modules/med.git] / src / MEDOP / tui / xmedpy / tests / test_xmed_fieldOperations.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2015  CEA/DEN, EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
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
24 # operations.
25 #
26 # (gboulant - 16/6/2011)
27 #
28 import xmed
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
34
35 # Don't forget to set the globals dictionnary for the fields tools to
36 # work properly
37 xmed.setConsoleGlobals(globals())
38
39 # Load some test data in the MedDataManager
40 filepath  = properties.testFilePath
41 xmed.dataManager.loadDatasource(filepath)
42 fieldHandlerList = xmed.dataManager.getFieldHandlerList()
43
44 def setup():
45     """
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.
49     """
50     fh1=fieldHandlerList[0]
51     fh2=fieldHandlerList[1]
52     f1 = FieldProxy(fh1)
53     f2 = FieldProxy(fh2)
54     return fh1, fh2, f1, f2
55
56 # Setup for quick tests in the python console
57 fh1, fh2, f1, f2 = setup()
58
59 def TEST_addition():
60     fieldHandler0 = fieldHandlerList[0]
61     fieldHandler1 = fieldHandlerList[1]
62
63     # The addition can be done using field handler directly
64     addFieldHandler = xmed.calculator.add(fieldHandler0, fieldHandler1)
65     print addFieldHandler
66
67     # Or with a field proxy that ease the writing of operations
68     fieldProxy0 = FieldProxy(fieldHandler0)
69     fieldProxy1 = FieldProxy(fieldHandler1)
70
71     res = fieldProxy0 + fieldProxy1
72     if res is None: return False
73
74     return True
75
76 def TEST_arithmetics():
77     fieldProxy0 = FieldProxy(fieldHandlerList[0])
78     fieldProxy1 = FieldProxy(fieldHandlerList[1])
79
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
88
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
106
107     return True
108
109 def TEST_unary_operations():
110     fieldProxy0 = FieldProxy(fieldHandlerList[0])
111
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
118
119     return True
120
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])
126
127     res = pow(fieldProxy0,2) + fieldProxy1
128     if res is None: return False
129
130     return True
131
132 def TEST_litteral_equation():
133     fieldProxy0 = FieldProxy(fieldHandlerList[0])
134     res = fieldProxy0.ope("abs(u)^2")
135     if res is None: return False
136     return True
137
138 def TEST_use_restriction():
139     fieldProxy0 = FieldProxy(fieldHandlerList[0])
140     res = fieldProxy0("c=1;g='toto'")
141     if res is None: return False
142     return True
143
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
149
150     #
151     # This operations are not allowed, or not that way
152     #
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)
157         return False
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)
162         return False
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)
167         return False
168
169     return True
170
171 def TEST_update_metadata():
172     fieldProxyRef = FieldProxy(fieldHandlerList[0])
173     id = fieldProxyRef.id
174
175     name_ref = "toto"
176     fieldProxyRef.update(name=name_ref)
177
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)
182         return False
183     return True
184
185 #
186 # =============================================================
187 # Unit tests
188 # =============================================================
189 #
190 import unittest
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)
196
197     def test_arithmetics(self):
198         result = pyunittester.execAndConvertExceptionToBoolean(TEST_arithmetics)
199         self.assertTrue(result)
200
201     def test_unary_operations(self):
202         result = pyunittester.execAndConvertExceptionToBoolean(TEST_unary_operations)
203         self.assertTrue(result)
204
205     def test_composition(self):
206         result = pyunittester.execAndConvertExceptionToBoolean(TEST_composition)
207         self.assertTrue(result)
208
209     def test_litteral_equation(self):
210         result = pyunittester.execAndConvertExceptionToBoolean(TEST_litteral_equation)
211         self.assertTrue(result)
212
213     def test_modification_of_attributes(self):
214         self.assertTrue(TEST_modification_of_attributes())
215
216     def test_update_metadata(self):
217         self.assertTrue(TEST_update_metadata())
218
219 def myunittests():
220     pyunittester.run(MyTestSuite)
221
222 def myusecases():
223     TEST_addition()
224     #TEST_arithmetics()
225     #TEST_unary_operations()
226     #TEST_update_metadata()
227     #TEST_composition()
228
229 if __name__ == "__main__":
230     #myusecases()
231     myunittests()
232     pass
233