Ticket #8913: history_test.py

File history_test.py, 2.5 KB (added by Samuel Jackson, 6 years ago)

Script for testing

Line 
1from mantid.simpleapi import _set_properties
2
3class SubAlg(PythonAlgorithm):
4
5    def PyInit(self):
6        pass
7
8    def PyExec(self):
9        #this algorithm should never show up in history!
10        pass
11
12AlgorithmFactory.subscribe(SubAlg)
13
14
15class BasicAlg(PythonAlgorithm):
16
17    def PyInit(self):
18        pass
19
20    def PyExec(self):
21        alg = self.createChildAlgorithm('SubAlg')
22        alg.initialize()
23        args = {}
24        kwargs = {}
25        _set_properties(alg, *args, **kwargs)
26        alg.execute()
27
28
29AlgorithmFactory.subscribe(BasicAlg)
30
31class ChildAlg(DataProcessorAlgorithm):
32
33    def PyInit(self):
34        pass
35
36    def PyExec(self):
37        alg = self.createChildAlgorithm('BasicAlg')
38        alg.initialize()
39        args = {}
40        kwargs = {}
41        _set_properties(alg, *args, **kwargs)
42        alg.execute()
43
44
45AlgorithmFactory.subscribe(ChildAlg)
46
47class ParentAlg(DataProcessorAlgorithm):
48
49    def PyInit(self):
50        self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '', Direction.Output),
51                             doc="Name to give the output workspace.")
52
53    def PyExec(self):
54        ws_name = self.getPropertyValue("OutputWorkspace")
55        alg = self.createChildAlgorithm('ChildAlg')
56        alg.initialize()
57        args = {}
58        kwargs = {}
59        _set_properties(alg, *args, **kwargs)
60        alg.execute()
61
62        ws = CreateWorkspace([0, 1, 2], [0, 1, 2], OutputWorkspace=ws_name)
63        self.setProperty('OutputWorkspace', ws)
64
65AlgorithmFactory.subscribe(ParentAlg)
66
67#############################################################
68
69ws_name = '__tmp_test_algorithm_history'
70
71alg = AlgorithmManager.createUnmanaged('ParentAlg')
72alg.initialize()
73alg.setProperty("OutputWorkspace", ws_name)
74alg.execute()
75history = mtd[ws_name].getHistory()
76
77alg_hists = history.getAlgorithmHistories()
78assert history.size() == 1
79assert len(alg_hists) == 1
80
81alg_history = history.getAlgorithmHistory(0)
82assert alg_history.name() == "ParentAlg"
83assert alg_history.childHistorySize() == 2
84
85child_history = alg_history.getChildAlgorithmHistory(0)
86assert child_history.name() == "ChildAlg"
87assert child_history.childHistorySize() == 1
88
89create_workspace_history = alg_history.getChildAlgorithmHistory(1)
90assert create_workspace_history.name() == "CreateWorkspace"
91assert len(create_workspace_history.getProperties()) == 12
92assert create_workspace_history.childHistorySize() == 0
93
94basic_history = child_history.getChildAlgorithmHistory(0)
95assert basic_history.name() == "BasicAlg"
96assert basic_history.childHistorySize() == 0