ArrayLengthValidatorTest.py 4.72 KB
Newer Older
1
2
3
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
# SPDX - License - Identifier: GPL - 3.0 +
7
8
9
import unittest
import testhelpers

10
11
from mantid.kernel import FloatArrayLengthValidator, FloatArrayProperty
from mantid.api import PythonAlgorithm
12

13

14
15
class ArrayLengthValidatorTest(unittest.TestCase):

16
17
18
19
20
    def test_empty_constructor_has_no_lengths_set(self):
        validator = FloatArrayLengthValidator()
        self.assertFalse(validator.hasLength())
        self.assertFalse(validator.hasMinLength())
        self.assertFalse(validator.hasMaxLength())
21

22
23
24
25
    def test_fixed_length_constructor_return_hasLength(self):
        fixedlength = 9
        validator = FloatArrayLengthValidator(fixedlength)
        self.assertTrue(validator.hasLength())
26
        self.assertEqual(validator.getLength(), fixedlength)
27
28
29
30
31
32
33

    def test_range_length_constructor_returns_hasMinMax(self):
        lower = 4
        upper = 7
        validator = FloatArrayLengthValidator(lower, upper)
        self.assertTrue(validator.hasMinLength())
        self.assertTrue(validator.hasMaxLength())
34
35
        self.assertEqual(validator.getMinLength(), lower)
        self.assertEqual(validator.getMaxLength(), upper)
36

37
38
39
40
41
42
    def test_setFixedLength_alters_accepted_lenth(self):
        validator = FloatArrayLengthValidator()
        self.assertFalse(validator.hasLength())
        fixed = 5
        validator.setLength(fixed)
        self.assertTrue(validator.hasLength())
43
        self.assertEqual(validator.getLength(), fixed)
44
45
46
47
48
49
50
51
52
53
54
55
        validator.clearLength()
        self.assertFalse(validator.hasLength())

    def test_setMinMaxLength_alters_accepted_range(self):
        validator = FloatArrayLengthValidator()
        self.assertFalse(validator.hasMinLength())
        self.assertFalse(validator.hasMaxLength())
        lower = 4
        upper = 7
        validator.setLengthMin(lower)
        self.assertTrue(validator.hasMinLength())
        self.assertFalse(validator.hasMaxLength())
56
        self.assertEqual(validator.getMinLength(), lower)
57
58
59
        validator.setLengthMax(upper)
        self.assertTrue(validator.hasMinLength())
        self.assertTrue(validator.hasMaxLength())
60
        self.assertEqual(validator.getMaxLength(), upper)
61

62
63
64
65
66
67
68
        validator.clearLengthMin()
        self.assertFalse(validator.hasMinLength())
        self.assertTrue(validator.hasMaxLength())
        validator.clearLengthMax()
        self.assertFalse(validator.hasMinLength())
        self.assertFalse(validator.hasMaxLength())

69
70
71
72
73
    def test_Validator_on_ArrayProperty_accepts_array_of_specified_length(self):
        fixedlength = 6
        alg = self._create_alg_with_fixedlength_validator(fixedlength)
        input_vals = [1.,2.4,5.6,8.0,4.6,6.]
        testhelpers.assertRaisesNothing(self, alg.setProperty, "Input", input_vals)
74

75
76
77
78
79
    def test_Validator_on_ArrayProperty_rejects_array_of_without_correct_length(self):
        fixedlength = 6
        alg = self._create_alg_with_fixedlength_validator(fixedlength)
        input_vals = [1.,2.4,5.6]
        self.assertRaises(ValueError, alg.setProperty, "Input", input_vals)
80

81
82
83
84
85
86
87
88
89
    def test_Validator_on_ArrayProperty_accepts_array_with_length_in_range(self):
        alg = self._create_alg_with_range_validator(3,5)
        input_vals = []
        for i in range(1,7):
            input_vals.append(float(1))
            if i < 3 or i > 5:
                self.assertRaises(ValueError, alg.setProperty, "Input", input_vals)
            else:
                testhelpers.assertRaisesNothing(self, alg.setProperty, "Input", input_vals)
90

91
92
93
94
95
    def _create_alg_with_fixedlength_validator(self, fixedlength):
        """
            Creates a test algorithm with a fixed length validator
        """
        class TestAlgorithm(PythonAlgorithm):
96

97
98
99
100
101
102
            def PyInit(self):
                validator = FloatArrayLengthValidator(fixedlength)
                self.declareProperty(FloatArrayProperty("Input", validator))

            def PyExec(self):
                pass
103

104
105
106
107
108
109
110
111
112
        alg = TestAlgorithm()
        alg.initialize()
        return alg

    def _create_alg_with_range_validator(self, minlength, maxlength):
        """
            Creates a test algorithm with a range length validator
        """
        class TestAlgorithm(PythonAlgorithm):
113

114
115
116
117
118
119
            def PyInit(self):
                validator = FloatArrayLengthValidator(minlength, maxlength)
                self.declareProperty(FloatArrayProperty("Input", validator))

            def PyExec(self):
                pass
120

121
122
123
124
125
126
127
        alg = TestAlgorithm()
        alg.initialize()
        return alg


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