From a43f69a4fbef2d362cb38e75be7b0ba985f86c9f Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Mon, 31 Mar 2014 16:03:53 +0100 Subject: [PATCH] Basic functionality works with tests. Refs #9227 --- .../Algorithms/src/AddTimeSeriesLog.cpp | 29 ++++++++-- .../Algorithms/test/AddTimeSeriesLogTest.h | 58 +++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/AddTimeSeriesLog.cpp b/Code/Mantid/Framework/Algorithms/src/AddTimeSeriesLog.cpp index 5643c304d580..e7db7ba311f5 100644 --- a/Code/Mantid/Framework/Algorithms/src/AddTimeSeriesLog.cpp +++ b/Code/Mantid/Framework/Algorithms/src/AddTimeSeriesLog.cpp @@ -15,12 +15,16 @@ ISO8601 format, e.g. 2010-09-14T04:20:12." *WIKI_USAGE*/ #include "MantidAlgorithms/AddTimeSeriesLog.h" +#include "MantidKernel/DateTimeValidator.h" +#include "MantidKernel/TimeSeriesProperty.h" #include "MantidKernel/MandatoryValidator.h" namespace Mantid { namespace Algorithms { + using namespace API; + using namespace Kernel; // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(AddTimeSeriesLog) @@ -49,16 +53,13 @@ namespace Mantid */ void AddTimeSeriesLog::init() { - using namespace API; - using namespace Kernel; declareProperty(new WorkspaceProperty("Workspace", "", Direction::InOut), "In/out workspace that will store the new log information"); - auto nonEmptyString = boost::make_shared>(); - declareProperty("Name", "", nonEmptyString, + declareProperty("Name", "", boost::make_shared>(), "A string name for either a new time series log to be created " "or an existing name to update", Direction::Input); - declareProperty("Time", "", nonEmptyString, + declareProperty("Time", "", boost::make_shared(), "An ISO formatted date/time string specifying the timestamp for " "the given log value, e.g 2010-09-14T04:20:12", Direction::Input); @@ -73,6 +74,24 @@ namespace Mantid */ void AddTimeSeriesLog::exec() { + MatrixWorkspace_sptr logWS = getProperty("Workspace"); + std::string name = getProperty("Name"); + std::string time = getProperty("Time"); + double value = getProperty("Value"); + + auto & run = logWS->mutableRun(); + TimeSeriesProperty *timeSeries(NULL); + if(run.hasProperty(name)) + { + timeSeries = dynamic_cast*>(run.getLogData(name)); + if(!timeSeries) throw std::invalid_argument("Log '" + name + "' already exists but is not a time series."); + } + else + { + timeSeries = new TimeSeriesProperty(name); + run.addProperty(timeSeries); + } + timeSeries->addValue(time, value); } diff --git a/Code/Mantid/Framework/Algorithms/test/AddTimeSeriesLogTest.h b/Code/Mantid/Framework/Algorithms/test/AddTimeSeriesLogTest.h index 8ff4ff6b9134..4e6d7031cf6b 100644 --- a/Code/Mantid/Framework/Algorithms/test/AddTimeSeriesLogTest.h +++ b/Code/Mantid/Framework/Algorithms/test/AddTimeSeriesLogTest.h @@ -3,6 +3,7 @@ #include #include "MantidAlgorithms/AddTimeSeriesLog.h" +#include "MantidKernel/TimeSeriesProperty.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h" class AddTimeSeriesLogTest : public CxxTest::TestSuite @@ -13,6 +14,25 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite static AddTimeSeriesLogTest *createSuite() { return new AddTimeSeriesLogTest(); } static void destroySuite( AddTimeSeriesLogTest *suite ) { delete suite; } + void test_Workspace2D() + { + auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10); + TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:12", 20.0)); + checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:12", 20.0, 0); + TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:19", 40.0)); + checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:19", 40.0, 1); + + } + + void test_EventWorkspace() + { + auto ws = WorkspaceCreationHelper::CreateEventWorkspace(10,10); + TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:12", 20.0)); + checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:12", 20.0, 0); + TS_ASSERT_THROWS_NOTHING(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:19", 40.0)); + checkLogWithEntryExists(ws, "Test Name", "2010-09-14T04:20:19", 40.0, 1); + } + //-------------------------- Failure cases ------------------------------------ void test_empty_log_name_not_allowed() { @@ -38,6 +58,24 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite TS_ASSERT_THROWS(alg.setPropertyValue("Value", ""), std::invalid_argument); } + void test_time_as_non_iso_formatted_string_throws_invalid_argument() + { + Mantid::Algorithms::AddTimeSeriesLog alg; + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + + TS_ASSERT_THROWS(alg.setPropertyValue("Time", "NotATime"), std::invalid_argument); + TS_ASSERT_THROWS(alg.setPropertyValue("Time", "2014 03 31 09 30"), std::invalid_argument); + TS_ASSERT_THROWS(alg.setPropertyValue("Time", "09:30:00"), std::invalid_argument); + } + + void test_algorithm_fails_if_log_exists_but_is_not_a_time_series() + { + auto ws = WorkspaceCreationHelper::Create2DWorkspace(10,10); + auto & run = ws->mutableRun(); + run.addProperty("Test Name", 1.0); + TS_ASSERT_THROWS(executeAlgorithm(ws, "Test Name", "2010-09-14T04:20:12", 20.0), std::invalid_argument); + } + private: void executeAlgorithm(Mantid::API::MatrixWorkspace_sptr testWS, const std::string & logName, const std::string & logTime, @@ -45,6 +83,7 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite { //execute algorithm Mantid::Algorithms::AddTimeSeriesLog alg; + alg.setChild(true); // not in ADS TS_ASSERT_THROWS_NOTHING(alg.initialize()); TS_ASSERT( alg.isInitialized() ) @@ -56,7 +95,26 @@ class AddTimeSeriesLogTest : public CxxTest::TestSuite alg.execute(); } + void checkLogWithEntryExists(Mantid::API::MatrixWorkspace_sptr testWS, const std::string & logName, const std::string & logTime, + const double logValue, const size_t position) + { + using Mantid::Kernel::DateAndTime; + using Mantid::Kernel::TimeSeriesProperty; + + const auto & run = testWS->run(); + TSM_ASSERT("Run does not contain the expected log entry", run.hasProperty(logName)); + auto *prop = run.getLogData(logName); + auto *timeSeries = dynamic_cast*>(prop); + TSM_ASSERT("A log entry with the given name exists but it is not a time series", timeSeries); + auto times = timeSeries->timesAsVector(); + TS_ASSERT(times.size() >= position + 1); + auto values = timeSeries->valuesAsVector(); + TS_ASSERT_EQUALS(DateAndTime(logTime), times[position]); + + TS_ASSERT(values.size() >= position + 1); + TS_ASSERT_EQUALS(logValue, values[position]); + } };