Skip to content

Commit 33744bf

Browse files
committed
Improve Unit Test
1 parent 3cc0d09 commit 33744bf

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

Framework/PythonInterface/test/python/mantid/dataobjects/WorkspaceValidatorsTest.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,57 @@
1313
from mantid.dataobjects import TableWorkspaceNotEmptyValidator
1414

1515
from mantid.simpleapi import CreateEmptyTableWorkspace
16-
from mantid.api import ITableWorkspace
16+
from mantid.api import ITableWorkspace, PythonAlgorithm, WorkspaceProperty
17+
from mantid.kernel import Direction
1718

1819

1920
class WorkspaceValidatorsTest(unittest.TestCase):
2021

2122
def test_TableWorkspaceNotEmptyValidator_construction(self):
2223
"""
2324
Test that the TableWorkspaceNotEmptyValidator can be constructed
24-
and detect an empty TableWorkspace
2525
"""
2626

27+
# test the validator can be constructed without error
28+
testhelpers.assertRaisesNothing(self, TableWorkspaceNotEmptyValidator)
29+
30+
def test_TableWorkspaceNotEmptyValidator_usage(self):
31+
32+
class TestAlgorithm(PythonAlgorithm):
33+
"""Mock an extended algorithm that uses TableWorkspaceNotEmptyValidator"""
34+
35+
def PyInit(self):
36+
self.declareProperty(
37+
WorkspaceProperty(
38+
name="InputWorkspace", defaultValue="", direction=Direction.Input, validator=TableWorkspaceNotEmptyValidator()
39+
)
40+
)
41+
42+
def PyExec(self):
43+
pass
44+
45+
# setup the algorithm
46+
alg = TestAlgorithm()
47+
alg.PyInit()
48+
49+
# setup the table workspace
2750
tableWS = CreateEmptyTableWorkspace()
2851

29-
# test the validator can be constructed
30-
testhelpers.assertRaisesNothing(self, TableWorkspaceNotEmptyValidator)
52+
# test the validator rejects an 0-column 0-row TableWorkspace
53+
self.assertRaises(ValueError, alg.setProperty, "InputWorkspace", tableWS)
3154

32-
# test the validator rejects an empty TableWorkspace
33-
self.assertRaises(Exception, TableWorkspaceNotEmptyValidator, tableWS)
55+
# test the validator rejects a n-column, 0-row TableWorkspace
56+
tableWS.addColumn("int", "values0")
57+
self.assertRaises(ValueError, alg.setProperty, "InputWorkspace", tableWS)
3458

35-
# thest the validator accepts a filled TableWorkspace
36-
tableWS.addColumn("int", "values")
37-
tableWS.addRow([1])
38-
tableWS.addRow([2])
39-
tableWS.addRow([3])
59+
tableWS.addColumn("int", "values1")
60+
tableWS.addColumn("int", "values2")
61+
self.assertRaises(ValueError, alg.setProperty, "InputWorkspace", tableWS)
4062

41-
testhelpers.assertRaisesNothing(self, TableWorkspaceNotEmptyValidator)
63+
# test the validator accepts a filled TableWorkspace
64+
tableWS.addRow([1, 2, 3])
65+
tableWS.addRow([4, 5, 6])
66+
testhelpers.assertRaisesNothing(self, alg.setProperty, "InputWorkspace", tableWS)
4267

4368

4469
if __name__ == "__main__":

0 commit comments

Comments
 (0)