Skip to content

Commit

Permalink
Added Python helper class for testing alg deprecation. Refs #6677
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Mar 14, 2013
1 parent 28519e9 commit a55922e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set ( MODULE_TEMPLATE src/api.cpp.in )
set ( EXPORT_FILES
src/Exports/IAlgorithm.cpp
src/Exports/Algorithm.cpp
src/Exports/DeprecatedAlgorithmChecker.cpp
src/Exports/PythonAlgorithmExport.cpp
src/Exports/AlgorithmFactory.cpp
src/Exports/AlgorithmManager.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "MantidAPI/DeprecatedAlgorithm.h"
#include "MantidAPI/AlgorithmManager.h"
#include <boost/python/class.hpp>

using Mantid::API::AlgorithmManager;
using Mantid::API::DeprecatedAlgorithm;
using Mantid::API::IAlgorithm_sptr;

using namespace boost::python;

namespace
{
/**
* It is not going to be possible to directly test if an algorithm is deprecated from Python. This
* is because we only export up to API and the deprecation happens at the concrete layer meaning
* Python cannot work out that the concrete class inherits from DeprecatedAlgorithm.
*
* To work around this we create a small DeprecatedAlgorithmTester class to handle to querying the C++
* inheritance structure
*/
class DeprecatedAlgorithmChecker
{
public:
/**
* Constructor. Throws if the algorithm does not exist
* @param algName The name of the algorithm
* @param version The algorithm version
*/
DeprecatedAlgorithmChecker(const std::string & algName, int version)
{
m_alg = AlgorithmManager::Instance().createUnmanaged(algName, version);
}

/// Check if the algorithm is deprecated
/// @returns A string containing a deprecation message if the algorithm is deprecated, empty string otherwise
const std::string isDeprecated() const
{
std::string deprecMessage="";
DeprecatedAlgorithm * depr = dynamic_cast<DeprecatedAlgorithm *>(m_alg.get());
if (depr)
deprecMessage = depr->deprecationMsg(m_alg.get());
return deprecMessage;
}

private:
/// Private default constructor
DeprecatedAlgorithmChecker();
/// Pointer to unmanaged algorithm
IAlgorithm_sptr m_alg;
};
}

void export_DeprecatedAlgorithmChecker()
{
class_<DeprecatedAlgorithmChecker>("DeprecatedAlgorithmChecker", no_init)
.def(init<const std::string&,int>((arg("algName"),arg("version")),"Constructs a DeprecatedAlgorithmChecker for the given algorithm & version. (-1 indicates latest version)"))
.def("isDeprecated",&DeprecatedAlgorithmChecker::isDeprecated, "A string containing a deprecation message if the algorithm is deprecated, empty string otherwise")
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set ( TEST_PY_FILES
AlgorithmManagerTest.py
AnalysisDataServiceTest.py
AxisTest.py
DeprecatedAlgorithmCheckerTest.py
ExperimentInfoTest.py
FilePropertyTest.py
FileFinderTest.py
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from mantid.api import DeprecatedAlgorithmChecker

class DeprecatedAlgorithmCheckerTest(unittest.TestCase):

def test_constructor_throws_for_non_existant_algorithm(self):
self.assertRaises(RuntimeError, DeprecatedAlgorithmChecker, "A_Very_Silly_Alg_Name",-1)

def test_non_deprecated_algorithm_returns_empty_string_from_isDeprecated(self):
deprecation_check = DeprecatedAlgorithmChecker("LoadRaw",-1)
msg = deprecation_check.isDeprecated()
self.assertTrue(len(msg) == 0)

def test_deprecated_algorithm_returns_non_empty_string_from_isDeprecated(self):
deprecation_check = DeprecatedAlgorithmChecker("GetMaskedDetectors",-1)
msg = deprecation_check.isDeprecated()
self.assertTrue(len(msg) > 0)


if __name__ == '__main__':
unittest.main()

0 comments on commit a55922e

Please sign in to comment.