Skip to content
Snippets Groups Projects
Commit 99bca82f authored by Gigg, Martyn Anthony's avatar Gigg, Martyn Anthony
Browse files

Remove requirement for test to write to disk

This should help with eliminating random failures when running the tests
in parallel.
Refs #11306
parent 067b949f
No related branches found
No related tags found
No related merge requests found
...@@ -4,37 +4,6 @@ from mantid.api import (AlgorithmFactory, AlgorithmProxy, IAlgorithm, IEventWork ...@@ -4,37 +4,6 @@ from mantid.api import (AlgorithmFactory, AlgorithmProxy, IAlgorithm, IEventWork
import mantid.simpleapi as simpleapi import mantid.simpleapi as simpleapi
import numpy import numpy
import os
import subprocess
import sys
#======================================================================================================================
# Helper class for test
class TemporaryPythonAlgorithm(object):
"""
Dumps the given code to a file in the Python algorithm directory
an removes the file in the del method
"""
def __init__(self, name, code):
from mantid import config
plugin_dirs = config['python.plugins.directories'].split(";")
if len(plugin_dirs) == 0:
raise RuntimeError("No Python algorithm directories defined")
self._pyfile = os.path.join(plugin_dirs[0], name + ".py")
alg_file = open(self._pyfile, "w")
alg_file.write(code)
alg_file.close()
def __del__(self):
try:
os.remove(self._pyfile)
pycfile = self._pyfile.replace(".py",".pyc")
os.remove(pycfile)
except OSError:
pass
#====================================================================================================================== #======================================================================================================================
class SimpleAPITest(unittest.TestCase): class SimpleAPITest(unittest.TestCase):
...@@ -219,41 +188,34 @@ FullBinsOnly(Input) *boolean* Omit the final bin if it's width is smaller ...@@ -219,41 +188,34 @@ FullBinsOnly(Input) *boolean* Omit the final bin if it's width is smaller
self.assertTrue('raw' in mtd) self.assertTrue('raw' in mtd)
def test_python_alg_can_use_other_python_alg_through_simple_api(self): def test_python_alg_can_use_other_python_alg_through_simple_api(self):
""" class SimpleAPIPythonAlgorithm1(PythonAlgorithm):
Runs a test in a separate process as it requires a reload of the def PyInit(self):
whole mantid module pass
""" def PyExec(self):
src = """ from mantid.simpleapi import SimpleAPIPythonAlgorithm2
from mantid.api import PythonAlgorithm, AlgorithmFactory SimpleAPIPythonAlgorithm2()
import mantid.simpleapi as api class SimpleAPIPythonAlgorithm2(PythonAlgorithm):
from mantid.simpleapi import * def PyInit(self):
pass
class %(name)s(PythonAlgorithm): def PyExec(self):
pass
def PyInit(self): AlgorithmFactory.subscribe(SimpleAPIPythonAlgorithm1)
pass AlgorithmFactory.subscribe(SimpleAPIPythonAlgorithm2)
def PyExec(self): # ---------------------------------------------------------
%(execline1)s alg1 = SimpleAPIPythonAlgorithm1()
%(execline2)s alg1.initialize()
# Puts function in simpleapi globals
AlgorithmFactory.subscribe(%(name)s) simpleapi_alg1_func = simpleapi._create_algorithm_function("SimpleAPIPythonAlgorithm1", 1, alg1)
""" alg2 = SimpleAPIPythonAlgorithm1()
name1 = "SimpleAPIPythonAlgorithm1" alg2.initialize()
name2 = "SimpleAPIPythonAlgorithm2" # Puts function in simpleapi globals
src1 = src % {"name":name1,"execline1":name2+"()","execline2":"api."+name2+"()"} simpleapi._create_algorithm_function("SimpleAPIPythonAlgorithm2", 1, alg2)
src2 = src % {"name":name2,"execline1":"pass","execline2":"pass"}
a = TemporaryPythonAlgorithm(name1,src1)
b = TemporaryPythonAlgorithm(name2,src2)
# Try to use algorithm 1 to run algorithm 2
cmd = sys.executable + ' -c "from mantid.simpleapi import %(name)s;%(name)s()"' % {'name':name1}
try: try:
subprocess.check_call(cmd,shell=True) simpleapi_alg1_func()
except subprocess.CalledProcessError, exc: except RuntimeError, exc:
self.fail("Error occurred running one Python algorithm from another: %s" % str(exc)) self.fail("Running algorithm 2 from 1 failed: " + str(exc))
# Ensure the files are removed promptly
del a,b
def test_optional_workspaces_are_ignored_if_not_present_in_output_even_if_given_as_input(self): def test_optional_workspaces_are_ignored_if_not_present_in_output_even_if_given_as_input(self):
# Test algorithm # Test algorithm
from mantid.api import AlgorithmManager,PropertyMode,PythonAlgorithm,MatrixWorkspaceProperty,WorkspaceFactory from mantid.api import AlgorithmManager,PropertyMode,PythonAlgorithm,MatrixWorkspaceProperty,WorkspaceFactory
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment