Skip to content

Commit

Permalink
Updating WorkspaceGroup unit tests.
Browse files Browse the repository at this point in the history
Also updated RenameWorkspace documentation with know issue.

Refs #7807
  • Loading branch information
Samuel Jackson committed Sep 9, 2013
1 parent 59c0aee commit e376da7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
86 changes: 79 additions & 7 deletions Code/Mantid/Framework/API/test/WorkspaceGroupTest.h
Expand Up @@ -65,19 +65,19 @@ class WorkspaceGroupTest : public CxxTest::TestSuite
public:

/// Make a simple group
WorkspaceGroup_sptr makeGroup()
WorkspaceGroup_sptr makeGroup(const std::string& groupName = "group",const std::string& prefix = "ws")
{
WorkspaceGroup_sptr group(new WorkspaceGroup());
AnalysisDataService::Instance().addOrReplace(groupName, group);

for (size_t i=0; i<3; i++)
{
boost::shared_ptr<WorkspaceTester> ws(new WorkspaceTester());
ws->initialize(2,3,4);
AnalysisDataService::Instance().addOrReplace("ws" + Strings::toString(i), ws);
AnalysisDataService::Instance().addOrReplace(prefix + Strings::toString(i), ws);
group->add(prefix + Strings::toString(i));
}
WorkspaceGroup_sptr group(new WorkspaceGroup());
AnalysisDataService::Instance().addOrReplace("group", group);
group->add("ws0");
group->add("ws1");
group->add("ws2");

return group;
}

Expand Down Expand Up @@ -334,6 +334,78 @@ class WorkspaceGroupTest : public CxxTest::TestSuite
TS_ASSERT_THROWS( group->isInGroup( *b ), std::runtime_error );
}

void test_renameWorkspaceToWorkspaceGroupChild()
{
WorkspaceGroup_sptr group = makeGroup();

//create new top level workspace
boost::shared_ptr<WorkspaceTester> ws(new WorkspaceTester());
ws->initialize(5,6,7);
AnalysisDataService::Instance().addOrReplace("NewWorkspace", ws);

//rename workspace should replace child in group
TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("NewWorkspace", "ws1") );

TS_ASSERT( group->getNames().size() == 3 );
TS_ASSERT_THROWS_NOTHING( group->getItem("ws1") );
auto ws1 = boost::dynamic_pointer_cast<WorkspaceTester>(AnalysisDataService::Instance().retrieve("ws1"));
TS_ASSERT( ws->blocksize() == 7 );
}

void test_renameWorkspaceChildToTopLevelWorkspace()
{
WorkspaceGroup_sptr group = makeGroup();

//create new top level workspace
boost::shared_ptr<WorkspaceTester> ws(new WorkspaceTester());
ws->initialize(5,6,7);
AnalysisDataService::Instance().addOrReplace("NewWorkspace", ws);

//rename child should replace top level workspace
TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("ws1", "NewWorkspace") );

TS_ASSERT_EQUALS( group->getNames().size(), 3 );
TS_ASSERT_THROWS( group->getItem("ws1"), std::out_of_range );
TS_ASSERT_THROWS_NOTHING( group->getItem("NewWorkspace") );
auto ws1 = boost::dynamic_pointer_cast<WorkspaceTester>(group->getItem("NewWorkspace"));
TS_ASSERT( ws1->blocksize() == 4 );
}

void test_renameChildFromOneGroupToAnother()
{
WorkspaceGroup_sptr groupA = makeGroup("wsA", "wsA");
WorkspaceGroup_sptr groupB = makeGroup("wsB", "wsB");

TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("wsA1", "wsB1") );

TS_ASSERT_EQUALS( groupA->getNames().size(), 3 );
TS_ASSERT_EQUALS( groupB->getNames().size(), 3 );

TS_ASSERT_THROWS( groupA->getItem("wsA1"), std::out_of_range );
TS_ASSERT_THROWS_NOTHING( groupB->getItem("wsB1") );

auto ws = boost::dynamic_pointer_cast<WorkspaceTester>(groupB->getItem("wsB1"));
TS_ASSERT( ws->blocksize() == 4 );
}

void test_renameOneGroupToAnother()
{
WorkspaceGroup_sptr groupA = makeGroup("wsA", "wsA");
WorkspaceGroup_sptr groupB = makeGroup("wsB", "wsB");

TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().rename("wsB", "wsA") );

TS_ASSERT_THROWS_NOTHING( groupA = boost::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("wsA")) );
TS_ASSERT_THROWS( AnalysisDataService::Instance().retrieve("wsB"), Mantid::Kernel::Exception::NotFoundError );

TS_ASSERT_THROWS_NOTHING( groupA->getItem("wsB1") );
TS_ASSERT_THROWS( groupA->getItem("wsA1"), std::out_of_range );

//A's workspaces should still exist outside the renamed group
TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().retrieve("wsA1") );
TS_ASSERT_THROWS_NOTHING( AnalysisDataService::Instance().retrieve("wsA2") );
}

};


Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/Algorithms/src/RenameWorkspace.cpp
@@ -1,10 +1,11 @@
/*WIKI*
Renames a workspace to a different name in the data service. If the same name is provided for input and output then the algorithm will fail with an error. The Renaming is implemented as a removal of the original workspace from the data service and re-addition under the new name.
Renames a workspace to a different name in the data service. If the same name is provided for input and output then the algorithm will fail with an error.
If run on a group workspace, the members of the group will be renamed if their names follow the pattern groupName_1, groupName_2, etc. (they will be renamed to newName_1, newname_2, etc.). Otherwise, only the group itself will be renamed - the members will keep their previous names.
There is a known issue that if a member of a workspace group is renamed to be the same as the member of a different workspace group, two pointers to the same object workspace are created.
*WIKI*/
//----------------------------------------------------------------------
// Includes
Expand Down

0 comments on commit e376da7

Please sign in to comment.