From 2d45d123976c7596d47532baa51c24f311de5cfd Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Fri, 11 Apr 2014 10:07:15 +0100 Subject: [PATCH] Refs #8962 Unit test for SaveReflTBL Unit test has been written and tests data with and without commas, failing on a group too large, failing when the tableworkspace isn't wide enough, and also that the files can be Loaded with LoadReflTBL. --- .../DataHandling/test/SaveReflTBLTest.h | 234 +++++++++++++++++- 1 file changed, 231 insertions(+), 3 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/test/SaveReflTBLTest.h b/Code/Mantid/Framework/DataHandling/test/SaveReflTBLTest.h index e195d04ffca5..adfccf5c2fc8 100644 --- a/Code/Mantid/Framework/DataHandling/test/SaveReflTBLTest.h +++ b/Code/Mantid/Framework/DataHandling/test/SaveReflTBLTest.h @@ -5,6 +5,8 @@ #include "MantidDataHandling/SaveReflTBL.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidAPI/FrameworkManager.h" +#include "MantidAPI/AlgorithmManager.h" +#include "MantidAPI/TableRow.h" #include #include @@ -20,17 +22,243 @@ class SaveReflTBLTest : public CxxTest::TestSuite static SaveReflTBLTest *createSuite() { return new SaveReflTBLTest(); } static void destroySuite(SaveReflTBLTest *suite) { delete suite; } - SaveReflTBLTest() + SaveReflTBLTest(): m_name("SaveReflTBLTestWS"), m_filename("SaveReflTBLTest.tbl"), m_abspath() { } + ~SaveReflTBLTest() { } - void testExec() + void testNoQuotes() { + ITableWorkspace_sptr ws = CreateWorkspace(); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL"); + alg->setRethrows(true); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + m_abspath = alg->getPropertyValue("Filename"); //Get absolute path + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveReflTBL"); + } + + TS_ASSERT( Poco::File(m_abspath).exists() ); + std::ifstream file(m_abspath); + std::string line = ""; + getline(file,line); + TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,13470,2.3,13463,0.035,0.3,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,13462,2.3,13463,0.035,0.3,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,,,,,,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,,,,,,,,,,,0.04,2"); + + file.close(); + cleanupafterwards(); + } + + void testQuotes() + { + ITableWorkspace_sptr ws = CreateWorkspace(); + + TableRow row = ws->appendRow(); + row << "13460" << "0.7" << "13463,13464" << "0.01" << "0.06" << "0.04" << "2" << 4; + + row = ws->appendRow(); + row << "13470" << "2.3" << "13463,13464" << "0.035" << "0.3" << "0.04" << "2" << 5; + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL"); + alg->setRethrows(true); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + m_abspath = alg->getPropertyValue("Filename"); //Get absolute path + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveReflTBL"); + } + + TS_ASSERT( Poco::File(m_abspath).exists() ); + std::ifstream file(m_abspath); + std::string line = ""; + getline(file,line); + TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,13470,2.3,13463,0.035,0.3,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,13462,2.3,13463,0.035,0.3,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,13460,0.7,\"13463,13464\",0.01,0.06,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13470,2.3,\"13463,13464\",0.035,0.3,,,,,,,,,,,0.04,2"); + getline(file,line); + TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,,,,,,,,,,,0.04,2"); + + file.close(); + cleanupafterwards(); + } + + void testFourGroupFail() + { + ITableWorkspace_sptr ws = CreateWorkspace(); + + TableRow row = ws->appendRow(); + row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 1; + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL"); + alg->setRethrows(true); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + m_abspath = alg->getPropertyValue("Filename"); //Get absolute path + TS_ASSERT_THROWS_ANYTHING(alg->execute()); + + // the algorithm shouldn't have written a file to disk + TS_ASSERT( !Poco::File(m_abspath).exists() ); + TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name)); } -}; + void testNotEnoughColumns() + { + ITableWorkspace_sptr ws = WorkspaceFactory::Instance().createTable(); + AnalysisDataService::Instance().addOrReplace(m_name, ws); + auto colRuns = ws->addColumn("str","Run(s)"); + auto colTheta = ws->addColumn("str","ThetaIn"); + auto colTrans = ws->addColumn("str","TransRun(s)"); + auto colQmin = ws->addColumn("str","Qmin"); + auto colQmax = ws->addColumn("str","Qmax"); + auto colDqq = ws->addColumn("str","dq/q"); + auto colScale = ws->addColumn("str","Scale"); + + colRuns->setPlotType(0); + colTheta->setPlotType(0); + colTrans->setPlotType(0); + colQmin->setPlotType(0); + colQmax->setPlotType(0); + colDqq->setPlotType(0); + colScale->setPlotType(0); + + TableRow row = ws->appendRow(); + row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2"; + + row = ws->appendRow(); + row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2"; + row = ws->appendRow(); + row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2"; + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL"); + alg->setRethrows(true); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + m_abspath = alg->getPropertyValue("Filename"); //Get absolute path + TS_ASSERT_THROWS_ANYTHING(alg->execute()); + + // the algorithm shouldn't have written a file to disk + TS_ASSERT( !Poco::File(m_abspath).exists() ); + TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name)); + } + + void testLoadWithLoadReflTBL() + { + ITableWorkspace_sptr ws = CreateWorkspace(); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL"); + alg->setRethrows(true); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + m_abspath = alg->getPropertyValue("Filename"); //Get absolute path + TS_ASSERT_THROWS_NOTHING(alg->execute()); + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveReflTBL"); + } + + TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name)); + + Mantid::API::IAlgorithm_sptr algLoad = Mantid::API::AlgorithmManager::Instance().create("LoadReflTBL"); + algLoad->setRethrows(true); + algLoad->setPropertyValue("OutputWorkspace", m_name); + algLoad->setPropertyValue("Filename", m_abspath); + TS_ASSERT_THROWS_NOTHING(algLoad->execute()); + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run LoadReflTBL"); + } + + cleanupafterwards(); + } + +private: + + void cleanupafterwards() + { + TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name)); + TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove()); + } + + ITableWorkspace_sptr CreateWorkspace(bool colFail = false) + { + ITableWorkspace_sptr ws = WorkspaceFactory::Instance().createTable(); + AnalysisDataService::Instance().addOrReplace(m_name, ws); + auto colRuns = ws->addColumn("str","Run(s)"); + auto colTheta = ws->addColumn("str","ThetaIn"); + auto colTrans = ws->addColumn("str","TransRun(s)"); + auto colQmin = ws->addColumn("str","Qmin"); + auto colQmax = ws->addColumn("str","Qmax"); + auto colDqq = ws->addColumn("str","dq/q"); + auto colScale = ws->addColumn("str","Scale"); + auto colStitch = ws->addColumn("int","StitchGroup"); + + colRuns->setPlotType(0); + colTheta->setPlotType(0); + colTrans->setPlotType(0); + colQmin->setPlotType(0); + colQmax->setPlotType(0); + colDqq->setPlotType(0); + colScale->setPlotType(0); + colStitch->setPlotType(0); + + TableRow row = ws->appendRow(); + row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 1; + + row = ws->appendRow(); + row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 1; + + row = ws->appendRow(); + row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 1; + + row = ws->appendRow(); + row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 2; + + row = ws->appendRow(); + row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 2; + + row = ws->appendRow(); + row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 3; + + row = ws->appendRow(); + row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 0; + + //this row's last two cells will show in the tableworkspace, but the first row in stich group 3's will take priority when saving + row = ws->appendRow(); + row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.4" << "3" << 3; + + row = ws->appendRow(); + row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 4; + + return ws; + } + + std::string m_name; + std::string m_filename; + std::string m_abspath; +}; #endif /*SAVEREFLTBLTEST_H_*/