From 6d43b7c247d6c26205b611eb70aa3d0c9b3847f0 Mon Sep 17 00:00:00 2001 From: Samuel Jackson Date: Mon, 28 Apr 2014 11:42:31 +0100 Subject: [PATCH] Refs #8913 Add unit tests for nested history in DataProcessorAlgorithm --- .../API/test/DataProcessorAlgorithmTest.h | 186 +++++++++++++++++- 1 file changed, 183 insertions(+), 3 deletions(-) diff --git a/Code/Mantid/Framework/API/test/DataProcessorAlgorithmTest.h b/Code/Mantid/Framework/API/test/DataProcessorAlgorithmTest.h index 5fe93e4352dc..a624fb3d36b5 100644 --- a/Code/Mantid/Framework/API/test/DataProcessorAlgorithmTest.h +++ b/Code/Mantid/Framework/API/test/DataProcessorAlgorithmTest.h @@ -6,13 +6,126 @@ #include "MantidKernel/System.h" #include #include - #include "MantidAPI/DataProcessorAlgorithm.h" +#include "MantidTestHelpers/FakeObjects.h" using namespace Mantid; using namespace Mantid::API; using namespace Mantid::API; +class SubAlgorithm : public Algorithm +{ +public: + SubAlgorithm() : Algorithm() {} + virtual ~SubAlgorithm() {} + const std::string name() const { return "SubAlgorithm";} + int version() const { return 1;} + const std::string category() const { return "Cat;Leopard;Mink";} + const std::string workspaceMethodName() const { return "methodname"; } + const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; } + const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; } + + void init() + { + declareProperty("PropertyA", "Hello"); + declareProperty("PropertyB", "World"); + } + void exec() + { + //nothing to do! + } +}; +DECLARE_ALGORITHM(SubAlgorithm); + +// basic algorithm. This acts as a child called for other DataProcessorAlgorithms +class BasicAlgorithm : public Algorithm +{ +public: + BasicAlgorithm() : Algorithm() {} + virtual ~BasicAlgorithm() {} + const std::string name() const { return "BasicAlgorithm";} + int version() const { return 1;} + const std::string category() const { return "Cat;Leopard;Mink";} + const std::string workspaceMethodName() const { return "methodname"; } + const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; } + const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; } + + void init() + { + declareProperty("PropertyA", "Hello"); + declareProperty("PropertyB", "World"); + } + void exec() + { + // the history from this should never be stored + auto alg = createChildAlgorithm("SubAlgorithm"); + alg->initialize(); + alg->setProperty("PropertyA", "I Don't exist!"); + alg->execute(); + } +}; +DECLARE_ALGORITHM(BasicAlgorithm) + +//middle layer algorithm executed by a top level algorithm +class NestedAlgorithm : public DataProcessorAlgorithm +{ +public: + NestedAlgorithm() : DataProcessorAlgorithm() {} + virtual ~NestedAlgorithm() {} + const std::string name() const { return "NestedAlgorithm";} + int version() const { return 1;} + const std::string category() const { return "Cat;Leopard;Mink";} + const std::string workspaceMethodName() const { return "methodname"; } + const std::string workspaceMethodOnTypes() const { return "MatrixWorkspace;ITableWorkspace"; } + const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; } + + void init() + { + declareProperty("PropertyA", 13); + declareProperty("PropertyB", 42); + } + + void exec() + { + auto alg = createChildAlgorithm("BasicAlgorithm"); + alg->initialize(); + alg->setProperty("PropertyA", "Same!"); + alg->execute(); + } +}; +DECLARE_ALGORITHM(NestedAlgorithm) + +//top level algorithm which executes -> NestedAlgorithm which executes -> BasicAlgorithm +class TopLevelAlgorithm : public DataProcessorAlgorithm +{ +public: + TopLevelAlgorithm() : DataProcessorAlgorithm() {} + virtual ~TopLevelAlgorithm() {} + const std::string name() const { return "TopLevelAlgorithm";} + int version() const { return 1;} + const std::string category() const { return "Cat;Leopard;Mink";} + const std::string workspaceMethodName() const { return "methodname"; } + const std::string workspaceMethodOnTypes() const { return "Workspace;MatrixWorkspace;ITableWorkspace"; } + const std::string workspaceMethodInputProperty() const { return "InputWorkspace"; } + + void init() + { + declareProperty(new WorkspaceProperty("InputWorkspace", "", Direction::Input)); + declareProperty(new WorkspaceProperty("OutputWorkspace","", Direction::Output)); + } + void exec() + { + auto alg = createChildAlgorithm("NestedAlgorithm"); + alg->initialize(); + alg->execute(); + + boost::shared_ptr output(new WorkspaceTester()); + setProperty("OutputWorkspace", output); + } +}; +DECLARE_ALGORITHM(TopLevelAlgorithm) + + class DataProcessorAlgorithmTest : public CxxTest::TestSuite { public: @@ -22,12 +135,79 @@ class DataProcessorAlgorithmTest : public CxxTest::TestSuite static void destroySuite( DataProcessorAlgorithmTest *suite ) { delete suite; } - void test_Something() + void test_Nested_History() { + boost::shared_ptr input(new WorkspaceTester()); + AnalysisDataService::Instance().addOrReplace("test_input_workspace", input); + + TopLevelAlgorithm alg; + alg.initialize(); + alg.setRethrows(true); + alg.setProperty("InputWorkspace", input); + alg.setPropertyValue("OutputWorkspace", "test_output_workspace"); + + TS_ASSERT_THROWS_NOTHING( alg.execute() ); + TS_ASSERT( alg.isExecuted() ); + + // check workspace history + auto ws = AnalysisDataService::Instance().retrieveWS("test_output_workspace"); + auto wsHist = ws->getHistory(); + TS_ASSERT_EQUALS( wsHist.size(), 1); + + // check top level algorithm history + auto algHist = wsHist.getAlgorithmHistory(0); + + TS_ASSERT_EQUALS(algHist.name(), "TopLevelAlgorithm"); + TS_ASSERT_EQUALS(algHist.childHistorySize(), 1); + + // check nested algorithm history + auto childHist = algHist.getChildAlgorithmHistory(0); + + TS_ASSERT_EQUALS(childHist.name(), "NestedAlgorithm"); + TS_ASSERT_EQUALS(childHist.childHistorySize(), 1); + + // check basic algorithm history + childHist = childHist.getChildAlgorithmHistory(0); + TS_ASSERT_EQUALS(childHist.name(), "BasicAlgorithm"); + + //even though BasicAlgorithm calls another algorithm, + //it should not store the history. + TS_ASSERT_EQUALS(childHist.childHistorySize(), 0); + + AnalysisDataService::Instance().remove("test_output_workspace"); + AnalysisDataService::Instance().remove("test_input_workspace"); } + void test_Dont_Record_Nested_History() + { + boost::shared_ptr input(new WorkspaceTester()); + AnalysisDataService::Instance().addOrReplace("test_input_workspace", input); + + TopLevelAlgorithm alg; + alg.initialize(); + alg.setRethrows(true); + alg.enableHistoryRecordingForChild(false); + alg.setProperty("InputWorkspace", input); + alg.setPropertyValue("OutputWorkspace", "test_output_workspace"); + + TS_ASSERT_THROWS_NOTHING( alg.execute() ); + TS_ASSERT( alg.isExecuted() ); + + // check workspace history + auto ws = AnalysisDataService::Instance().retrieveWS("test_output_workspace"); + auto wsHist = ws->getHistory(); + TS_ASSERT_EQUALS( wsHist.size(), 1 ); + + auto algHist = wsHist.getAlgorithmHistory(0); + TS_ASSERT_EQUALS( algHist.name(), "TopLevelAlgorithm"); + //algorithm should have no child histories. + TS_ASSERT_EQUALS( algHist.childHistorySize(), 0 ); + + AnalysisDataService::Instance().remove("test_output_workspace"); + AnalysisDataService::Instance().remove("test_input_workspace"); + } }; -#endif /* MANTID_API_DATAPROCESSORALGORITHMTEST_H_ */ \ No newline at end of file +#endif /* MANTID_API_DATAPROCESSORALGORITHMTEST_H_ */