|
13 | 13 | from mantid.dataobjects import TableWorkspaceNotEmptyValidator
|
14 | 14 |
|
15 | 15 | 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 |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class WorkspaceValidatorsTest(unittest.TestCase):
|
20 | 21 |
|
21 | 22 | def test_TableWorkspaceNotEmptyValidator_construction(self):
|
22 | 23 | """
|
23 | 24 | Test that the TableWorkspaceNotEmptyValidator can be constructed
|
24 |
| - and detect an empty TableWorkspace |
25 | 25 | """
|
26 | 26 |
|
| 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 |
27 | 50 | tableWS = CreateEmptyTableWorkspace()
|
28 | 51 |
|
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) |
31 | 54 |
|
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) |
34 | 58 |
|
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) |
40 | 62 |
|
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) |
42 | 67 |
|
43 | 68 |
|
44 | 69 | if __name__ == "__main__":
|
|
0 commit comments