Salome HOME
Correct processing of errors while HDF reading
[modules/shaper.git] / test.hdfs / test_hdf.py
1 # Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import salome, os, sys
21 import SalomePyQt
22
23 import unittest
24 import subprocess
25
26 import ModelHighAPI, ModelAPI, PartSetAPI
27 from GeomAPI import GeomAPI_Shape
28 from salome.shaper import model
29
30 class TestHDF(unittest.TestCase):
31   testfile = ""
32   reffile = ""
33
34   def setUp(self):
35     # ===========================================
36     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
37     print("  UnitTest setUp() started", file=dbgFile)
38     dbgFile.close()
39     # ===========================================
40     salome.salome_close()
41     # ===========================================
42     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
43     print("  UnitTest previous session closed", file=dbgFile)
44     dbgFile.close()
45     # ===========================================
46
47     # leave file name only (trim path and extension)
48     fileName = os.path.basename(self.testfile)
49     self.reffile = self.reffile + "/" + os.path.splitext(fileName)[0] + ".py"
50     # ===========================================
51     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
52     print("  UnitTest reffile: {}".format(self.reffile), file=dbgFile)
53     dbgFile.close()
54     # ===========================================
55
56     salome.salome_init(self.testfile, embedded=1)
57     myStudyName = salome.myStudy._get_Name()
58     self.session = salome.naming_service.Resolve('/Kernel/Session')
59     self.session.emitMessage("connect_to_study")
60     # ===========================================
61     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
62     print("  UnitTest Salome started", file=dbgFile)
63     dbgFile.close()
64     # ===========================================
65
66     self.sg = SalomePyQt.SalomePyQt()
67     self.sg.activateModule("Shaper")
68     self.session = ModelAPI.ModelAPI_Session.get()
69     self.partSet = self.session.moduleDocument()
70     # ===========================================
71     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
72     print("  UnitTest Shaper started", file=dbgFile)
73     dbgFile.close()
74     # ===========================================
75
76   def tearDown(self):
77     # ===========================================
78     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
79     print("  UnitTest tearDown() started", file=dbgFile)
80     dbgFile.close()
81     # ===========================================
82     salome.sg.UpdateView()
83     self.sg.processEvents()
84     salome.sg.FitAll()
85     # ===========================================
86     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
87     print("  UnitTest tearDown() finished", file=dbgFile)
88     dbgFile.close()
89     # ===========================================
90
91   def test_hdf_file(self):
92     # ===========================================
93     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
94     print("  UnitTest start HDF file testing", file=dbgFile)
95     dbgFile.close()
96     # ===========================================
97     self.assertTrue(self.partSet.size("Parts") > 0)
98     # ===========================================
99     dbgFile = open(os.getcwd() + "/dbgfile", 'a')
100     print("  UnitTest number of parts: {}".format(self.partSet.size("Parts")), file=dbgFile)
101     dbgFile.close()
102     # ===========================================
103     aPartsList = []
104     for aPartIndex in range(self.partSet.size("Parts")):
105       self.session.startOperation()
106       aPart = ModelAPI.modelAPI_ResultPart(ModelAPI.objectToResult(self.partSet.object("Parts", aPartIndex)))
107       aPart.activate()
108       self.session.finishOperation()
109
110       # ===========================================
111       dbgFile = open(os.getcwd() + "/dbgfile", 'a')
112       print("  UnitTest test part: {}".format(aPartIndex), file=dbgFile)
113       dbgFile.close()
114       # ===========================================
115       aPartFeature = PartSetAPI.PartSetAPI_Part(self.partSet.currentFeature(True))
116       # check reference data
117       exec(open(self.reffile, "rb").read())
118       # ===========================================
119       dbgFile = open(os.getcwd() + "/dbgfile", 'a')
120       print("  UnitTest test part finished", file=dbgFile)
121       dbgFile.close()
122       # ===========================================
123
124
125 if __name__ == "__main__":
126   if len(sys.argv) > 1:
127     TestHDF.testfile = sys.argv[1]
128   if len(sys.argv) > 2:
129     salomePortFile = sys.argv[2]
130   if len(sys.argv) > 3:
131     errFile = open(sys.argv[3], 'w')
132   if len(sys.argv) > 4:
133     salomeKernelDir = sys.argv[4]
134   if len(sys.argv) > 5:
135     TestHDF.reffile = sys.argv[5]
136
137   # ===========================================
138   dbgFile = open(os.getcwd() + "/dbgfile", 'a')
139   print("  UnitTest started with parameters:", file=dbgFile)
140   print("      testfile: {}".format(TestHDF.testfile), file=dbgFile)
141   print("      salomePortFile: {}".format(salomePortFile), file=dbgFile)
142   print("      salomeKernelDir: {}".format(salomeKernelDir), file=dbgFile)
143   print("      reffile: {}".format(TestHDF.reffile), file=dbgFile)
144   dbgFile.close()
145   # ===========================================
146   aTest = unittest.TestLoader().loadTestsFromTestCase(TestHDF)
147   unittest.TextTestRunner(stream=errFile).run(aTest)
148   errFile.close()
149
150 #  test_program = unittest.main(argv=[sys.argv[0]], exit=False)
151   proc = subprocess.Popen(salomeKernelDir + "/bin/salome/killSalome.py")
152
153   try:
154     os.remove(salomePortFile)
155   except:
156     print("Cannot remove file", file=f)
157
158 #  assert test_program.result.wasSuccessful(), "Test failed"