Salome HOME
Copyright update 2022
[modules/yacs.git] / src / yacsloader_swig / Test / testLoader.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2022  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import pilot
22 import SALOMERuntime
23 import loader
24 import unittest
25
26 class TestLoader(unittest.TestCase):
27
28   def setUp(self):
29     SALOMERuntime.RuntimeSALOME_setRuntime()
30     self.r = pilot.getRuntime()
31     self.l = loader.YACSLoader()
32     self.e = pilot.ExecutorSwig()
33     pass
34
35   def tearDown(self):
36     del self.r
37     del self.l
38     del self.e
39
40   def test1_FileNotExist(self):
41     # --- File does not exist
42     retex=None
43     try:
44       p = self.l.load("nonexisting")
45     except IOError as ex:
46       print("IO Error: ", ex)
47       retex=ex
48     # except pilot.invalid_argument as ex:
49     #  print("invalid_argument:",str(ex))
50     #  retex=ex.what()
51     self.assertTrue(retex is not None, "exception not raised, or wrong type")
52     pass
53
54   def test2_parseError(self):
55     # --- File exists but parse error
56     retex=None
57     try:
58       p = self.l.load("samples/bid.xml")
59     except ValueError as ex:
60       print("Caught ValueError Exception:",ex)
61       retex = ex
62     expected="LogRecord: parser:ERROR:from node node5 does not exist in control link: node5->b2 context: b1. (samples/bid.xml:53)\n"
63     self.assertTrue(p.getLogger("parser").getStr() == expected, "error not found: "+p.getLogger("parser").getStr())
64     pass
65
66   def test3_normal(self):
67     # --- File exists and no parsing problem
68     try:
69       p = self.l.load("samples/aschema.xml")
70       print(p.getLogger("parser").getStr())
71       print(p)
72       print(p.getName())
73       for k in p.typeMap: print(k)
74       for k in p.nodeMap: print(k)
75       for k in p.inlineMap: print(k)
76       for k in p.serviceMap: print(k)
77       print(self.e.getTasksToLoad())
78       self.e.RunW(p,0)
79       self.assertEqual(106, p.getChildByName('node48').getEffectiveState())
80     except pilot.Exception as ex:
81       print("YACS exception:",ex)
82       self.fail(ex)
83       pass
84     pass
85
86 if __name__ == '__main__':
87   import tempfile
88   import os
89   dir_test = tempfile.mkdtemp(suffix=".yacstest")
90   file_test = os.path.join(dir_test,"UnitTestsResult")
91   with open(file_test, 'a') as f:
92       f.write("  --- TEST src/yacsloader: testLoader.py\n")
93       suite = unittest.makeSuite(TestLoader)
94       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
95   sys.exit(not result.wasSuccessful())