Skip to content

Commit

Permalink
Refs #9369 Add unit tests for HistoryItem and HistoryView
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Jackson committed May 15, 2014
1 parent 9a8f330 commit 3e0c8c8
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Code/Mantid/Framework/API/test/HistoryItemTest.h
@@ -0,0 +1,30 @@
#ifndef HISTORYITEMTEST_H_
#define HISTORYITEMTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/HistoryView.h"

using namespace Mantid::API;
using namespace Mantid::Kernel;

class HistoryItemTest : public CxxTest::TestSuite
{

public:

void test_Minimum()
{
//not really much to test
AlgorithmHistory algHist("AnAlg", 1);
HistoryItem item(boost::make_shared<AlgorithmHistory>(algHist));
item.unrolled(true);

TS_ASSERT_EQUALS( *(item.getAlgorithmHistory()), algHist )
TS_ASSERT( item.isUnrolled() )
TS_ASSERT_EQUALS( item.numberOfChildren(), 0)
}

};

#endif
222 changes: 222 additions & 0 deletions Code/Mantid/Framework/API/test/HistoryViewTest.h
@@ -0,0 +1,222 @@
#ifndef HISTORYVIEWTEST_H_
#define HISTORYVIEWTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/HistoryView.h"

using namespace Mantid::API;
using namespace Mantid::Kernel;


// 'Empty' algorithm class for tests
class testalg : public Algorithm
{
public:
testalg() : Algorithm() {}
virtual ~testalg() {}
const std::string name() const { return "testalg";} ///< Algorithm's name for identification
int version() const { return 1;} ///< Algorithm's version for identification
const std::string category() const { return "Cat";} ///< Algorithm's category for identification

void init()
{
declareProperty("name","",Direction::Input);
}
void exec() {}
};

class HistoryViewTest : public CxxTest::TestSuite
{
private:
AlgorithmHistory_sptr createFromTestAlg(const std::string& name )
{
testalg alg;
alg.initialize();
alg.setPropertyValue("name", name);
alg.execute();

Mantid::Kernel::DateAndTime execTime = Mantid::Kernel::DateAndTime::defaultTime();

AlgorithmHistory history(&alg, execTime, 14.0, m_execCount++);
return boost::make_shared<AlgorithmHistory>(history);
}

public:
HistoryViewTest() : m_wsHist(), m_execCount(0)
{
//create dummy history structure
auto alg1 = createFromTestAlg("alg1");
auto child1 = createFromTestAlg("child1");
alg1->addChildHistory(child1);

auto alg2 = createFromTestAlg("alg2");
auto child2 = createFromTestAlg("child2");

auto subChild21 = createFromTestAlg("subChild21");
auto subChild22 = createFromTestAlg("subChild22");

child2->addChildHistory(subChild21);
child2->addChildHistory(subChild22);

alg2->addChildHistory(child2);

auto alg3 = createFromTestAlg("alg3");

m_wsHist.addHistory(alg1);
m_wsHist.addHistory(alg2);
m_wsHist.addHistory(alg3);
}

void test_Empty()
{
WorkspaceHistory wsHist;
HistoryView view(wsHist);
TS_ASSERT_EQUALS ( view.size(), 0 );
TS_ASSERT_THROWS_ANYTHING ( view.unroll(0) );
TS_ASSERT_THROWS_ANYTHING ( view.roll(0) );
}

void test_Build()
{
HistoryView view(m_wsHist);
TS_ASSERT_EQUALS( view.size(), 3);

int i = 0;
auto items = view.getAlgorithmsList();
for (auto it = items.begin(); it != items.end(); ++it, ++i)
{
auto history = it->getAlgorithmHistory();
auto props = history->getProperties();
TS_ASSERT_EQUALS(props[0].value(), "alg" + boost::lexical_cast<std::string>(i+1) );
}

}

void test_Unroll_History()
{
HistoryView view(m_wsHist);
//unroll alg 2
TS_ASSERT_THROWS_NOTHING( view.unroll(0) );
TS_ASSERT_EQUALS(view.size(), 4);

auto items = view.getAlgorithmsList();
TS_ASSERT_EQUALS(items.size(), 4);

std::string propName = items[0].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg1")
propName = items[1].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "child1")
propName = items[2].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg2")
propName = items[3].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg3")
}

void test_Simple_Roll_History()
{
//tests the case where we have a single layer of history unrolled
HistoryView view(m_wsHist);

//unroll alg 2
TS_ASSERT_THROWS_NOTHING( view.unroll(0) );

TS_ASSERT_EQUALS(view.size(), 4);
auto items = view.getAlgorithmsList();
TS_ASSERT_EQUALS(items.size(), 4);

//check it unrolled properly
std::string propName = items[0].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg1")
propName = items[1].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "child1")
propName = items[2].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg2")
propName = items[3].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg3")

//roll it back up
TS_ASSERT_THROWS_NOTHING( view.roll(0) );

TS_ASSERT_EQUALS(view.size(), 3);
items = view.getAlgorithmsList();
TS_ASSERT_EQUALS(items.size(), 3);

//check it rolled back up properly
propName = items[0].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg1")
propName = items[1].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg2")
propName = items[2].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg3")
}

void test_Complex_Roll_History()
{
//tests the case where we have multiple layers of history unrolled
HistoryView view(m_wsHist);

//unroll alg2
TS_ASSERT_THROWS_NOTHING( view.unroll(1) );

TS_ASSERT_EQUALS(view.size(), 4);
auto items = view.getAlgorithmsList();
TS_ASSERT_EQUALS(items.size(), 4);

std::string propName = items[0].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg1")
propName = items[1].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg2")
propName = items[2].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "child2")
propName = items[3].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg3")

//unroll another level of history
TS_ASSERT_THROWS_NOTHING( view.unroll(2) )

TS_ASSERT_EQUALS(view.size(), 6);
items = view.getAlgorithmsList();
TS_ASSERT_EQUALS(items.size(), 6);
propName = items[0].getAlgorithmHistory()->getProperties()[0].value();

TS_ASSERT_EQUALS(propName, "alg1")
propName = items[1].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg2")
propName = items[2].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "child2")
propName = items[3].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "subChild21")
propName = items[4].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "subChild22")
propName = items[5].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg3")

//now roll everything back up to the top level
TS_ASSERT_THROWS_NOTHING( view.roll(1) )

TS_ASSERT_EQUALS(view.size(), 3);
items = view.getAlgorithmsList();
TS_ASSERT_EQUALS(items.size(), 3);

propName = items[0].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg1")
propName = items[1].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg2")
propName = items[2].getAlgorithmHistory()->getProperties()[0].value();
TS_ASSERT_EQUALS(propName, "alg3")
}

void test_Index_To_Large()
{
HistoryView view(m_wsHist);
TS_ASSERT_THROWS_ANYTHING ( view.unroll(3) );
TS_ASSERT_THROWS_ANYTHING ( view.roll(3) );
}

WorkspaceHistory m_wsHist;
size_t m_execCount;

};

#endif

0 comments on commit 3e0c8c8

Please sign in to comment.