| 1 | import re |
| 2 | from enum import Enum |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from skrf.vi import validators |
| 7 | from skrf.vi.validators import ValidationError |
| 8 | |
| 9 | |
| 10 | @pytest.mark.parametrize( |
| 11 | "bounds,test_in,expected", |
| 12 | [ |
| 13 | ((None, None), 0, 0), |
| 14 | ((None, None), 0.0, 0), |
| 15 | ((None, None), "0", 0), |
| 16 | ((None, None), "-1", -1), |
| 17 | ((None, None), "1", 1), |
| 18 | ((None, None), "100000", 100000), |
| 19 | ((-1, None), "-1", -1), |
| 20 | ((None, 1), "0", 0), |
| 21 | ((-1, 1), 1, 1), |
| 22 | ((0, 100), "1", 1), |
| 23 | ], |
| 24 | ) |
| 25 | def test_int_validator(bounds, test_in, expected): |
| 26 | v = validators.IntValidator(*bounds) |
| 27 | assert v.validate_input(test_in) == expected |
| 28 | |
| 29 | |
| 30 | @pytest.mark.parametrize( |
| 31 | "bounds,test_in", |
| 32 | [ |
| 33 | ((-1, 1), 2), |
| 34 | ((-1, 1), -2.0), |
| 35 | ((-1, 1), "-2"), |
| 36 | ((-1, None), -2), |
| 37 | ((None, 1), 2), |
| 38 | ((None, 1), 2), |
| 39 | ], |
| 40 | ) |
| 41 | def test_int_validator_out_of_bounds(bounds, test_in): |
| 42 | v = validators.IntValidator(*bounds) |
| 43 | with pytest.raises(ValidationError): |
| 44 | v.validate_input(test_in) |
| 45 | |
| 46 | |
| 47 | @pytest.mark.parametrize( |
| 48 | "bounds,test_in,expected", |
| 49 | [ |
| 50 | ((None, None), 0.0, 0.0), |
| 51 | ((None, None), 0, 0.0), |
| 52 | ((None, None), "0", 0.0), |
| 53 | ((None, None), "0.", 0.0), |
| 54 | ((None, None), "-1", -1.0), |
| 55 | ((None, None), "1.0", 1.0), |
| 56 | ((None, None), "2.5", 2.5), |
| 57 | ((None, None), "-2.5", -2.5), |
| 58 | ((-1, None), "0", 0), |
| 59 | ((None, 1), "0", 0), |
| 60 | ((-0.5, 0.5), "0.25", 0.25), |
| 61 | ], |
| 62 | ) |
| 63 | def test_float_validator(bounds, test_in, expected): |
| 64 | v = validators.FloatValidator(*bounds) |
| 65 | assert v.validate_input(test_in) == expected |
| 66 | |
| 67 | |
| 68 | @pytest.mark.parametrize( |
| 69 | "bounds,test_in", |
| 70 | [ |
| 71 | ((-1, 1), 2), |
| 72 | ((-1, 1), -2), |
| 73 | ((-1, 1), "-2.0"), |
| 74 | ((-1, None), -2), |
| 75 | ((None, 1), 2), |
| 76 | ], |
| 77 | ) |
| 78 | def test_float_validator_out_of_bounds(bounds, test_in): |
| 79 | v = validators.FloatValidator(*bounds) |
| 80 | with pytest.raises(ValidationError): |
| 81 | v.validate_input(test_in) |
| 82 | |
| 83 | |
| 84 | @pytest.mark.parametrize( |
| 85 | "test_in,expected", |
| 86 | [ |
| 87 | ("100", 100), |
| 88 | ("1hz", 1), |
| 89 | ("1hZ", 1), |
| 90 | ("1Hz", 1), |
| 91 | ("1HZ", 1), |
| 92 | ("1 Hz", 1), |
| 93 | ("1 kHz", 1000), |
| 94 | ("1 MHz", 1_000_000), |
| 95 | ("1 GHz", 1_000_000_000), |
| 96 | ("1.5 GHz", 1_500_000_000), |
| 97 | ("1 kHz", 1000), |
| 98 | ], |
| 99 | ) |
| 100 | def test_freq_validator(test_in, expected): |
| 101 | v = validators.FreqValidator() |
| 102 | assert v.validate_input(test_in) == expected |
| 103 | |
| 104 | |
| 105 | def test_enum_validator(): |
| 106 | class Foo(Enum): |
| 107 | A = "A" |
| 108 | B = "B" |
| 109 | |
| 110 | v = validators.EnumValidator(Foo) |
| 111 | assert v.validate_input("A") == "A" |
| 112 | assert v.validate_input("B") == "B" |
| 113 | assert v.validate_input(Foo.A) == "A" |
| 114 | assert v.validate_input(Foo.B) == "B" |
| 115 | |
| 116 | with pytest.raises(ValidationError): |
| 117 | v.validate_input("C") |
| 118 | |
| 119 | assert v.validate_output('A') == Foo.A |
| 120 | assert v.validate_output('B') == Foo.B |
| 121 | with pytest.raises(ValidationError): |
| 122 | v.validate_output("C") |
| 123 | |
| 124 | |
| 125 | def test_set_validator(): |
| 126 | v = validators.SetValidator([1, 2]) |
| 127 | assert v.validate_input(1) == 1 |
| 128 | assert v.validate_input(2) == 2 |
| 129 | with pytest.raises(ValidationError): |
| 130 | v.validate_input(3) |
| 131 | |
| 132 | with pytest.raises(ValueError): |
| 133 | validators.SetValidator([1, '2']) |
| 134 | |
| 135 | def test_dict_validator(): |
| 136 | arg_str = "{a},{b}" |
| 137 | resp_pat = r"(?P<a>\d),(?P<b>\d)" |
| 138 | v = validators.DictValidator(arg_str, resp_pat) |
| 139 | |
| 140 | assert v.validate_input({"a":1, "b":2}) == "1,2" |
| 141 | assert v.validate_output('1,2') == {"a":'1', "b":'2'} |
| 142 | |
| 143 | with pytest.raises(ValidationError): |
| 144 | v.validate_input({"a":1}) |
| 145 | |
| 146 | with pytest.raises(ValidationError): |
| 147 | v.validate_output('1,2,3') |
| 148 | |
| 149 | v = validators.DictValidator(arg_str, re.compile(r"(?P<a>\d),(?P<b>\d)")) |
| 150 | assert v.validate_input({"a":1, "b":2}) == "1,2" |