-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Python helper class for testing alg deprecation. Refs #6677
- Loading branch information
1 parent
28519e9
commit a55922e
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/DeprecatedAlgorithmChecker.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...Mantid/Framework/PythonInterface/test/python/mantid/api/DeprecatedAlgorithmCheckerTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |